4
4
import re
5
5
import requests
6
6
import time
7
+ from gitconsensus .repository import Repository
7
8
from gitconsensusservice import app
9
+ import github3
8
10
9
11
10
12
def get_jwt ():
@@ -40,14 +42,11 @@ def request(url, method='GET'):
40
42
response = requestfunc ('https://api.github.com/%s' % (url ,), headers = headers )
41
43
response .raise_for_status ()
42
44
retobj = response .json ()
43
- if 'Link' in response .headers :
44
- regex = r"\<https://api.github.com/([^>]*)\>; rel=\"([a-z]*)\""
45
- groups = re .findall (regex , response .headers ['Link' ])
46
- for group in groups :
47
- if group [1 ] == 'next' :
48
- nextresults = request (group [1 ])
49
- retobj += nextresults
50
- break
45
+
46
+ nextpage = get_link_from_response (response )
47
+ if nextpage :
48
+ nextresults = request (nextpage )
49
+ retobj += nextresults
51
50
return retobj
52
51
53
52
@@ -73,9 +72,55 @@ def get_installation_token(installation_id):
73
72
expiration = tokens [installation_id ]['expires_at' ]
74
73
testtime = datetime .now () - timedelta (minutes = 3 )
75
74
exptime = datetime .strptime (expiration , "%Y-%m-%dT%H:%M:%SZ" )
76
- if exptime < testtime :
75
+ if exptime > testtime :
77
76
return tokens [installation_id ]['token' ]
78
77
79
78
url = "installations/%s/access_tokens" % (installation_id ,)
80
79
tokens [installation_id ] = request (url , 'POST' )
81
80
return tokens [installation_id ]['token' ]
81
+
82
+
83
+ def get_installation_repositories (installation_id , url = False ):
84
+ if not url :
85
+ url = 'https://api.github.com/installation/repositories'
86
+ res = installation_request (installation_id , url )
87
+ repodata = res .json ()
88
+ repos = [repo ['full_name' ] for repo in repodata ['repositories' ]]
89
+ nextpage = get_link_from_response (res )
90
+ if nextpage :
91
+ repos += get_installation_repositories (installation_id , nextpage )
92
+ return repos
93
+
94
+
95
+ def installation_request (installation_id , url ):
96
+ client = get_github3_client (installation_id )
97
+ headers = {'Accept' : 'application/vnd.github.machine-man-preview+json' }
98
+ res = client ._get (url , headers = headers )
99
+ res .raise_for_status ()
100
+ return res
101
+
102
+
103
+ def list_prs (installation_id , user , repo ):
104
+ repository = get_repository (installation_id , user , repo ).repository
105
+ prs = repository .iter_pulls (state = "open" )
106
+ return [pr .number for pr in prs ]
107
+
108
+
109
+ def get_repository (install_id , username , repository_name ):
110
+ client = get_github3_client (install_id )
111
+ return Repository (username , repository_name , client )
112
+
113
+
114
+ def get_github3_client (installation_id ):
115
+ token = get_installation_token (installation_id )
116
+ return github3 .login (token = token )
117
+
118
+
119
+ def get_link_from_response (response ):
120
+ if 'Link' in response .headers :
121
+ regex = r"\<https://api.github.com/([^>]*)\>; rel=\"([a-z]*)\""
122
+ groups = re .findall (regex , response .headers ['Link' ])
123
+ for group in groups :
124
+ if group [1 ] == 'next' :
125
+ return group [1 ]
126
+ return False
0 commit comments