-
Notifications
You must be signed in to change notification settings - Fork 14
/
github_helpers.py
69 lines (57 loc) · 3.13 KB
/
github_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import keyring
import json
import re
import requests
def delete_excess_branches(repository, username='SirArthurTheSubmitter', organization='camelot-project'):
S = requests.Session()
S.headers['User-Agent']= 'camelot-project '+S.headers['User-Agent']
kwargs = {'username': username, 'organization':organization, 'repository':repository}
r = S.get('https://api.github.com/repos/{organization}/{repository}/pulls'.format(**kwargs),
params={'state':'closed'})
r.raise_for_status()
results = json.loads(r.content)
git_user = username
password = keyring.get_password('github', git_user)
for result in results:
pullnumber = result['id']
if result['state'] == 'closed':
ref = result['head']['ref']
print("Pruning pull {0} with title {1} and refid {2}".format(pullnumber, result['title'], ref))
r = S.delete('https://api.github.com/repos/{organization}/{repository}/git/refs/heads/{ref}'.format(ref=ref, **kwargs),
auth=(git_user, password))
if r.status_code == 204:
# success
continue
d = json.loads(r.content)
if d['message'] == 'Reference does not exist':
print("Branch {0} seems to be already deleted.".format(ref))
continue
r.raise_for_status()
else:
print("Leaving unchanged pull {0} with title {1}".format(pullnumber, result['title']))
raise Exception("This state should not be reachable if the API worked right ang gave us only closed PRs")
def close_pull_request(repository, pr_id, username='SirArthurTheSubmitter',
organization='camelot-project', delete_branch=True):
S = requests.Session()
S.headers['User-Agent']= 'camelot-project '+S.headers['User-Agent']
kwargs = {'username': username, 'organization':organization,
'repository':repository}
git_user = username
password = keyring.get_password('github', git_user)
r = S.post('https://api.github.com/repos/camelot-project/{repository}/pulls/{0}'.format(pr_id, repository=repository),
data=json.dumps({'state':'closed'}), auth=(git_user, password))
#print("PR closing: {0} resulted in {1}".format(pr_id, r.status_code))
r.raise_for_status()
response = S.get('https://api.github.com/repos/camelot-project/{repository}/pulls/{0}'.format(pr_id,
repository=repository),
auth=(git_user, password))
result = json.loads(response.content)
if 'head' in result:
ref = result['head']['ref']
print("Pruning pull {0} with title {1} and refid {2}".format(pr_id, result['title'], ref))
r = S.delete('https://api.github.com/repos/{organization}/{repository}/git/refs/heads/{ref}'.format(ref=ref, **kwargs),
auth=(git_user, password))
print("Status code: ",r.status_code,". 204 means 'success', 422 means 'probably already deleted'")
else:
print("PR #{0} not found".format(pr_id))
return r.status_code