def get_repository_stat(repo_owner, repo_name, repo_created_at): git = Connection(TOKEN) now = [datetime.now().year, datetime.now().month, datetime.now().day] lifetime_days = (int(now[0]) - int(repo_created_at[0])) * 365 + (int(now[1]) - int(repo_created_at[1])) * 30 + int(now[2]) - int(repo_created_at[2]) repository = {} # getting number of contributors try: uri = '/repos/%s/%s/contributors' % (str(repo_owner), str(repo_name)) params = {} contributors = git.send('GET', uri, params=None) repo_number_of_contributors = 0 for contributor in contributors: repo_number_of_contributors += 1 # calculating contibutors per day repo_contributors_per_day = float(repo_number_of_contributors) / lifetime_days # getting number of commits uri = '/repos/%s/%s/commits' % (str(repo_owner), str(repo_name)) params = {'anon': True} commits = git.send('GET', uri, params=params) repo_number_of_commits = 0 for commit in commits: repo_number_of_commits += 1 # getting commits per day repo_commits_per_day = float(repo_number_of_commits) / lifetime_days repository = { "name": repo_name, "contributors": repo_number_of_contributors, "contributors_per_day": repo_contributors_per_day, "commits": repo_number_of_commits, "commits_per_day": repo_commits_per_day } except: print "Error: ", sys.exc_info()[0] repository = { "name": repo_name, "contributors": 0, "contributors_per_day": 0, "commits": 0, "commits_per_day": 0 } return repository
class Repository: def __init__(self, token, repository, api_url=None): self.token = token self.repository = repository self.api_url = api_url self._connect() def _connect(self): self.connection = Connection(self.token) if self.api_url: self.connection.endpoint = self.api_url def fetch_releases(self): uri = '/repos/{}/releases'.format(self.repository) paged_releases = [r.parsed for r in Pager(self.connection, uri, None)] return list(itertools.chain.from_iterable(paged_releases)) def create_release(self, data): uri = '/repos/{}/releases'.format(self.repository) response = self.connection.send('POST', uri, data=json.dumps(data)) return response def edit_release(self, release_id, data): uri = '/repos/{}/releases/{}'.format(self.repository, release_id) response = self.connection.send('PATCH', uri, data=json.dumps(data)) return response def delete_release(self, release_id): uri = '/repos/{}/releases/{}'.format(self.repository, release_id) response = self.connection.send('DELETE', uri) assert response.status_code == 204 return response def download_asset(self, uri): headers = dict() headers["Authorization"] = "token " + self.token headers["Accept"] = "application/octet-stream" r = requests.get(uri, stream=True, headers=headers) assert r.status_code == 200 asset_binary = StringIO() for chunk in r.iter_content(1024): asset_binary.write(chunk) return asset_binary def upload_asset(self, uri, filename, content_type, data): headers = dict() headers["Authorization"] = "token " + self.token headers["Content-Type"] = content_type r = requests.post(uri, params={'name': filename}, headers=headers, data=data, timeout=1) assert r.status_code == 201 return r
def main(): try: opts, args = getopt.gnu_getopt(sys.argv[1:], 'h', ['help', 'input=', 'max-pages=']) except getopt.GetoptError as e: usage(e) data = None max_pages = None for opt, val in opts: if opt in ('-h', '--help'): usage() elif opt == '--input': if val == '-': data = sys.stdin else: with open(val, 'r') as fob: data = json.load(fob) elif opt == '--max-pages': max_pages = int(val) if len(args) == 0: usage() if len(args) < 2: usage('incorrect number of arguments') method = args[0] uri = args[1] if max_pages is not None and method != 'GET': fatal('--max-pages is only supported with method GET') params = {} for arg in args[2:]: key, val = arg.split('=') params[key] = val token = os.environ.get('OCTOHUB_TOKEN', None) conn = Connection(token) try: if max_pages is None: response = conn.send(method, uri, params, data) print(json.dumps(response.parsed, indent=1)) else: parsed = [] pager = Pager(conn, uri, params, max_pages=max_pages) for response in pager: parsed.extend(response.parsed) print(json.dumps(parsed, indent=1), end=' ') except ResponseError as e: fatal(e)
def github_add_or_update_issue(title, msg, gist_files=None): token = os.environ.get('KNESSET_DATA_GITHUB_TOKEN', None) if token: from octohub.connection import Connection as OctohubConnection github = OctohubConnection(token) if gist_files: res = github.send('POST', '/gists', data=json.dumps({ "description": "automatically created gist from knesset-data", "files": gist_files })) gist_id = res.parsed['id'] gist_url = "https://gist.github.com/OriHoch/%s"%gist_id msg += "\n\nfor more details see: "+gist_url search_query = 'is:issue is:open label:"auto-created" "%s"'%title res = github.send('GET', '/search/issues', params={'q': search_query}) if len(res.parsed['items']) > 0: issue_number = res.parsed['items'][0]['number'] github.send('POST', '/repos/hasadna/knesset-data/issues/%s/comments'%issue_number, data=json.dumps({ 'body': "_**Encountered the error again**_\n\n%s"%msg, })) else: github.send('POST', '/repos/hasadna/knesset-data/issues', data=json.dumps({ 'title': title, 'body': "_**This issue was automatically created, please do not modify the title or remove any labels**_\n\n%s"%msg, 'labels': ["auto-created","Knesset bug"], }))
def github_add_or_update_issue(title, msg, gist_files=None): token = os.environ.get('KNESSET_DATA_GITHUB_TOKEN', None) if token: github = OctohubConnection(token) if gist_files: res = github.send('POST', '/gists', data=json.dumps({ "description": "automatically created gist from knesset-data", "files": gist_files })) gist_id = res.parsed['id'] gist_url = "https://gist.github.com/OriHoch/%s"%gist_id msg += "\n\nfor more details see: "+gist_url search_query = 'is:issue is:open label:"auto-created" "%s"'%title res = github.send('GET', '/search/issues', params={'q': search_query}) if len(res.parsed['items']) > 0: issue_number = res.parsed['items'][0]['number'] github.send('POST', '/repos/hasadna/knesset-data/issues/%s/comments'%issue_number, data=json.dumps({ 'body': "_**Encountered the error again**_\n\n%s"%msg, })) else: github.send('POST', '/repos/hasadna/knesset-data/issues', data=json.dumps({ 'title': title, 'body': "_**This issue was automatically created, please do not modify the title or remove any labels**_\n\n%s"%msg, 'labels': ["auto-created","Knesset bug"], }))
def search_repositories(number, created_after, sort_by): git = Connection(TOKEN) created_after = created_after.split("/") repositories = [] page = 1 while number: try: uri = '/search/repositories?page=%s&per_page=100' % page params = {'q': 'created:>%s-%s-01' % (created_after[1], created_after[0]), 'sort': sort_by} repos = git.send('GET', uri, params=params) repos = repos.parsed for repo in repos['items'][:number]: repo_created_at = repo['created_at'] repo_updated_at = repo['updated_at'] # calculating lifetime days repo_created_at = repo_created_at.split('T')[0].split('-') repo_created_at = [int(repo_created_at[0]), int(repo_created_at[1]), int(repo_created_at[2])] now = [int(datetime.now().year), int(datetime.now().month), int(datetime.now().day)] lifetime_days = (now[0] - repo_created_at[0]) * 365 + (now[1] - repo_created_at[1]) * 30 + now[2] - repo_created_at[2] repo_updated_at = repo_updated_at.split('T')[0].split('-') repo_updated_at = [int(repo_updated_at[0]), int(repo_updated_at[1]), int(repo_updated_at[2])] # calculating stars per day repo_stars_per_day = float(repo["stargazers_count"]) / lifetime_days repository = { "name": repo["name"], "owner": repo["owner"]["login"], "url": repo["html_url"], "created": repo_created_at, "updated": repo_updated_at, "stars": repo["stargazers_count"], "stars_per_day": repo_stars_per_day, } repositories.append(repository) if number > 100: number -= 100 page += 1 else: number = False except: print "Error: ", sys.exc_info()[0] break return repositories
def main(): try: opts, args = getopt.gnu_getopt(sys.argv[1:], 'hn', ['help', 'noauth']) except getopt.GetoptError as e: usage(e) auth = True repos = [] for opt, val in opts: if opt in ('-h', '--help'): usage() elif opt in ('-n', '--noauth'): auth = False token = os.environ.get('OCTOHUB_TOKEN', None) if not token and auth: fatal('OCTOHUB_TOKEN is required, override with --noauth') if not auth: token = None if not len(args) == 1: usage() conn = Connection(token) if '/' in args[0]: owner = args[0].split('/')[0] repos.append(args[0].split('/')[1]) else: owner = args[0] try: repos = get_repos(conn, '/orgs/%s/repos' % owner) except ResponseError as e: repos = get_repos(conn, '/users/%s/repos' % owner) for repo in repos: response = conn.send('GET', '/repos/%s/%s/pulls' % (owner, repo)) if response.parsed: print('%s/%s\n' % (owner, repo)) for pull in response.parsed: print(' [%s] %s' % (pull.head.user.login, pull.title)) print(' %s' % pull.html_url) print()
def create_gist(token, uri, paths, public=False, description=None): data = {} data['files'] = {} for path in paths: name = os.path.basename(path) content = file(path, 'r').read() data['files'][name] = {'content': content} if description: data['description'] = description data['public'] = public conn = Connection(token) response = conn.send('POST', uri, params={}, data=json.dumps(data)) return response.parsed
def create_gist(token, uri, paths, public=False, description=None): data = {} data["files"] = {} for path in paths: name = os.path.basename(path) content = file(path, "r").read() data["files"][name] = {"content": content} if description: data["description"] = description data["public"] = public conn = Connection(token) response = conn.send("POST", uri, params={}, data=json.dumps(data)) return response.parsed
method = args[0] uri = args[1] if max_pages is not None and method != 'GET': fatal('--max-pages is only supported with method GET') params = {} for arg in args[2:]: key, val = arg.split('=') params[key] = val token = os.environ.get('OCTOHUB_TOKEN', None) conn = Connection(token) try: if max_pages is None: response = conn.send(method, uri, params, data) print json.dumps(response.parsed, indent=1) else: parsed = [] pager = Pager(conn, uri, params, max_pages=max_pages) for response in pager: parsed.extend(response.parsed) print json.dumps(parsed, indent=1), except ResponseError, e: fatal(e) if __name__ == '__main__': main()
fatal('--max-pages is only supported with method GET') params = {} for arg in args[2:]: key, val = arg.split('=') params[key] = val token = os.environ.get('OCTOHUB_TOKEN', None) headers = {} if preview_key: headers[ 'Accept'] = 'application/vnd.github.%s-preview+json' % preview_key conn = Connection(token, headers=headers) try: if max_pages is None: response = conn.send(method, uri, params, data) print json.dumps(response.parsed, indent=1) else: parsed = [] pager = Pager(conn, uri, params, max_pages=max_pages) for response in pager: parsed.extend(response.parsed) print json.dumps(parsed, indent=1), except ResponseError, e: fatal(e) if __name__ == '__main__': main()
from octohub.connection import Connection conn = Connection() uri = '/repos/Kreogist/Mu/git/refs/tags' resp = conn.send('GET', uri) jsobj = resp.parsed print(jsobj[-1]['ref'].split('/')[-1])
if not auth: token = None if not len(args) == 1: usage() conn = Connection(token) if '/' in args[0]: owner = args[0].split('/')[0] repos.append(args[0].split('/')[1]) else: owner = args[0] try: repos = get_repos(conn, '/orgs/{0!s}/repos'.format(owner)) except ResponseError, e: repos = get_repos(conn, '/users/{0!s}/repos'.format(owner)) for repo in repos: response = conn.send('GET', '/repos/{0!s}/{1!s}/pulls'.format(owner, repo)) if response.parsed: print '{0!s}/{1!s}\n'.format(owner, repo) for pull in response.parsed: print ' [{0!s}] {1!s}'.format(pull.head.user.login, pull.title) print ' {0!s}'.format(pull.html_url) print if __name__ == '__main__': main()
if not auth: token = None if not len(args) == 1: usage() conn = Connection(token) if '/' in args[0]: owner = args[0].split('/')[0] repos.append(args[0].split('/')[1]) else: owner = args[0] try: repos = get_repos(conn, '/orgs/%s/repos' % owner) except ResponseError, e: repos = get_repos(conn, '/users/%s/repos' % owner) for repo in repos: response = conn.send('GET', '/repos/%s/%s/pulls' % (owner, repo)) if response.parsed: print '%s/%s\n' % (owner, repo) for pull in response.parsed: print ' [%s] %s' % (pull.head.user.login, pull.title) print ' %s' % pull.issue_url print if __name__ == '__main__': main()
if not auth: token = None if not len(args) == 1: usage() conn = Connection(token) if '/' in args[0]: owner = args[0].split('/')[0] repos.append(args[0].split('/')[1]) else: owner = args[0] try: repos = get_repos(conn, '/orgs/%s/repos' % owner) except ResponseError, e: repos = get_repos(conn, '/users/%s/repos' % owner) for repo in repos: response = conn.send('GET', '/repos/%s/%s/pulls' % (owner, repo)) if response.parsed: print '%s/%s\n' % (owner, repo) for pull in response.parsed: print ' [%s] %s' % (pull.head.user.login, pull.title) print ' %s' % pull.html_url print if __name__ == '__main__': main()