def delete_hooks(orgOrUser, repoOrRepos, hook_type=None): hooks = list_hooks(orgOrUser, repoOrRepos, hook_type) url_templ = 'https://api.github.com/repos/{owner}/{repo}/hooks/{hook}' auth = HTTPBasicAuth(*get_auth()) for hook in hooks: url = url_templ.format(owner=orgOrUser, **hook) requests.delete(url, auth=auth)
def trigger_push_action(proj, repo, branch, sha=None): if not sha: url_templ = 'https://api.github.com/repos/{repo}/git/refs/heads/{branch}' url = url_templ.format(repo=repo, branch=branch) auth = HTTPBasicAuth(*get_auth()) r = requests.get(url, auth=auth) if r.status_code / 100 != 2: raise Exception('Cannot get the branch', r.text) ref = r.json() sha = ref['object']['sha'] url_templ = 'http://api.gitbot.io/hooks/projects/{project}/trigger' event = 'push' data = dict( project=proj, repo=repo, branch=branch, sha=sha, event=event ) headers = { "Content-type": "application/json", "Accept": "text/plain" } url = url_templ.format(project=urllib.quote_plus(proj)) response = requests.post(url, data=json.dumps(data), headers=headers) if response.status_code == 200: print 'Build triggerred successfully.' else: print 'Cannot trigger build' print response.text
def add_status(orgOrUser, repo, sha, data): url_templ = 'https://api.github.com/repos/{owner}/{repo}/statuses/{sha}' auth = HTTPBasicAuth(*get_auth()) url = url_templ.format(owner=orgOrUser, repo=repo, sha=sha) r = requests.post(url, data=json.dumps(data), auth=auth) if r.status_code == 201: return list_status(orgOrUser, repo, sha) else: raise Exception(r.text)
def get_token(scopes, note): auth = HTTPBasicAuth(*get_auth()) url = 'https://api.github.com/authorizations' data = dict(scopes=scopes, note=note) r = requests.post(url, data=json.dumps(data), auth=auth) if r.status_code == 201: res = r.json() return res['token'] else: raise Exception(r.text)
def list_status(orgOrUser, repo, sha): url_templ = 'https://api.github.com/repos/{owner}/{repo}/statuses/{sha}' auth = HTTPBasicAuth(*get_auth()) url = url_templ.format(owner=orgOrUser, repo=repo, sha=sha) r = requests.get(url, auth=auth) res = r.json() statuses = [] for status in res: statuses.append(dict( id=status['id'], state=status['state'], target_url=status['target_url'], description=status['description'] )) return statuses
def list_hooks(orgOrUser, repoOrRepos, hook_type=None): url_templ = 'https://api.github.com/repos/{owner}/{repo}/hooks' if not isinstance(repoOrRepos, list): repoOrRepos = [repoOrRepos] hooks = [] auth = HTTPBasicAuth(*get_auth()) for repo in repoOrRepos: url = url_templ.format(owner=orgOrUser, repo=repo) r = requests.get(url, auth=auth) res = r.json() for hook in res: if not hook_type or hook['name'] == hook_type: hooks.append(dict( events=hook['events'], repo=repo, hook=hook['id'] )) return hooks
def trigger_pull_action(proj, repo, number): # Fetch pull request from github url_templ = 'https://api.github.com/repos/{repo}/pulls/{number}' api_url = url_templ.format(repo=repo, number=number) auth = HTTPBasicAuth(*get_auth()) r = requests.get(api_url, auth=auth) if r.status_code / 100 != 2: raise Exception('Cannot get the pull request', r.text) pull_request = r.json() # Call trigger action base = pull_request['base'] head = pull_request['head'] data = dict( project=proj, repo=repo, branch=base['ref'].replace('refs/heads/',''), sha=base['sha'], ref=base['ref'], event='pull_request', praction='synchronize', number=pull_request['number'], source=dict( repo=head['repo']['full_name'], branch=head['ref'].replace('refs/heads/',''), sha=head['sha'], ref=head['ref'] ) ) headers = { "Content-type": "application/json", "Accept": "text/plain" } url_templ = 'http://api.gitbot.io/hooks/projects/{project}/trigger' url = url_templ.format(project=urllib.quote_plus(proj)) response = requests.post(url, data=json.dumps(data), headers=headers) if response.status_code == 200: print 'Build triggerred successfully.' else: print 'Cannot trigger build' print response.text