-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
1169 lines (945 loc) · 35.9 KB
/
Copy path__init__.py
File metadata and controls
1169 lines (945 loc) · 35.9 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2016-2018 Scality
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import time
from functools import lru_cache
from collections import defaultdict, namedtuple
from itertools import groupby
from jwt import JWT, jwk_from_pem
from requests import HTTPError
from urllib.parse import quote_plus as quote
from bert_e.lib.lru_cache import LRUCache
from . import schema
from .. import base, cache, factory
LOG = logging.getLogger(__name__)
class Error(base.Error):
pass
CacheEntry = namedtuple('CacheEntry', ['obj', 'etag', 'date'])
@factory.api_client('github')
class Client(base.AbstractClient):
def __init__(self, login: str, password: str, email: str,
app_id: int | None = None, installation_id: int | None = None,
private_key: str | None = None, org=None,
base_url='https://api.github.com',
accept_header="application/vnd.github.v3+json"):
rlog = logging.getLogger('requests.packages.urllib3.connectionpool')
rlog.setLevel(logging.CRITICAL)
self.session = base.BertESession()
self.login = login
self.password = password
self.app_id = app_id
self.installation_id = installation_id
self.private_key = jwk_from_pem(private_key.encode('utf-8')) \
if private_key else None
self.email = email
self.org = org
self.base_url = base_url.rstrip('/')
self.query_cache = defaultdict(LRUCache)
self.accept_header = accept_header
self.session.headers.update(self.headers)
def _get_jwt(self):
"""Get a JWT for the installation."""
payload = {
# Issued at time
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
# GitHub App's identifier
'iss': self.app_id
}
jwt_instance = JWT()
return jwt_instance.encode(payload, self.private_key, alg='RS256')
@lru_cache()
def _get_installation_token(self, ttl_cache=None):
"""Get an installation token for the client's installation."""
# ttl_cache is a parameter used by lru_cache to set the time to live
# of the cache. It is not used in this method.
del ttl_cache
url = (
f'{self.base_url}/app/installations/'
f'{self.installation_id}/access_tokens'
)
headers = {
'Authorization': f'Bearer {self._get_jwt()}',
'Accept': self.accept_header,
}
print(headers)
response = self.session.post(url, headers=headers)
response.raise_for_status()
return response.json()['token']
@property
def is_app(self):
if self.app_id and self.installation_id and self.private_key:
return True
return False
@property
def headers(self):
headers = {
'Accept': self.accept_header,
'User-Agent': 'Bert-E',
'Content-Type': 'application/json',
'From': self.email,
}
if self.is_app:
token = self._get_installation_token(
ttl_cache=round(time.time() / 600))
headers['Authorization'] = f'Bearer {token}'
else:
headers['Authorization'] = f'token {self.password}'
return headers
def _patch_url(self, url):
"""Patch URLs if it is relative to the API root.
Returns: an absolute url corresponding to client.base_url / url
"""
if not url.startswith('http'):
url = '/'.join((self.base_url, url.lstrip('/')))
return url
@staticmethod
def _mk_key(url, params):
return (url,) + tuple(sorted(params.items()))
def _cache_value(self, method, url, params, res):
"""Put a request result in the query cache.
If the response's headers contain an ETag or a Last-Modified field,
the response can be used in subsequent calls to avoid hitting github's
rate limit.
Args:
- method (str): request method (e.g. GET)
- url (str): request url
- params (dict): request parameter dict as per requests library's
params argument
- res (requests.Response): the request's response
Returns:
The response that was put in cache.
"""
key = self._mk_key(url, params)
headers = res.headers
etag = headers.get('ETag', None)
date = headers.get('Last-Modified', None)
if etag or date:
self.query_cache[method].set(key, CacheEntry(res, etag, date))
return res
def _get_cached_value(self, method, url, params):
"""Get a value from the cache if any.
This method is intended to be called before performing an HTTP request,
in order to define the special headers used by GitHub's rate-limit
system.
If the request that follows returns a HTTP 304 code, this means that:
- the cached value returned by this method can be returned as a
valid result
- the request wasn't decremented from GitHub's rate limit counter
Args:
- method (str): request method (e.g. GET)
- url (str): request url
- params (dict): request parameter dict as per requests library's
params argument
Returns:
A (response, headers) tuple.
- response is the last response we've received for this request
(possibly None).
- headers is a dictionary defining 'If-None-Match' and
'If-Modified-Since' headers to add to the request.
See: the _get() method to understand how it is used.
"""
key = self._mk_key(url, params)
entry = self.query_cache[method].get(key, None)
headers = {
'If-None-Match': None,
'If-Modified-Since': None
}
if entry is None:
return None, headers
if entry.etag:
headers['If-None-Match'] = entry.etag
elif entry.date:
headers['If-Modified-Since'] = entry.date
return entry.obj, headers
def _get(self, url, **kwargs):
"""Perform a GET request using the rate-limit cache system.
This method is not supposed to be called by other objects. Instead, it
is wrapped by the get() and iter_get() methods.
Returns:
A requests.Response object
"""
params = kwargs.get('params', {})
url = self._patch_url(url)
res, headers = self._get_cached_value('GET', url, params)
if headers:
kwargs.setdefault('headers', {}).update(headers)
response = self.session.get(url, **kwargs)
if response.status_code == 304:
LOG.debug('Not Modified. Returning cached result')
return res
response.raise_for_status()
return self._cache_value('GET', url, params, response)
def get(self, url, **kwargs):
"""Perform an HTTP GET request to the github API.
This method handles cache verfication and uses conditional requests
+ a local cache to avoid consuming API calls as counted by Github's
rate limit system.
Args: same as requests.get()
Returns: a deserialized json structure
Raises: requests.HTTPError
"""
return json.loads(self._get(url, **kwargs).text)
def post(self, url, data, **kwargs):
"""Perform a POST request to the github API.
Args: same as requests.post()
Returns: a deserialized json structure
Raises:
requests.HTTPError
"""
url = self._patch_url(url)
response = self.session.post(url, data=data, **kwargs)
response.raise_for_status()
return json.loads(response.text)
def patch(self, url, data, **kwargs):
"""Perform a PATCH request to the github API.
Args: same as requests.patch()
Returns: a deserialized json structure
Raises:
requests.HTTPError
"""
url = self._patch_url(url)
response = self.session.post(url, data=data, **kwargs)
response.raise_for_status()
return json.loads(response.text)
def delete(self, url, **kwargs):
"""Perform a DELETE request on the github API.
Args: same as requests.delete()
Raises: requests.HTTPError
"""
url = self._patch_url(url)
response = self.session.delete(url, **kwargs)
response.raise_for_status()
def put(self, url, **kwargs):
"""Perform a PUT request to the Github API.
Args: same as requests.put()
Raises: requests.HTTPError
"""
url = self._patch_url(url)
response = self.session.put(url, **kwargs)
response.raise_for_status()
def iter_get(self, url, per_page=100, **kwargs):
"""Perform a paginated GET request to the Github API.
This method handles cache verfication and uses conditional requests
+ a local cache to avoid consuming API calls as counted by Github's
rate limit system.
Args:
- per_page: number of objects to get per page (max & default: 100)
- same as requests.get()
Yields: deserialized json structures
Raises: requests.HTTPError
"""
params = kwargs.setdefault('params', {})
params.setdefault('per_page', per_page)
next_page = url
while next_page:
response = self._get(next_page, **kwargs)
yield from json.loads(response.text)
next_page = None
if 'link' not in response.headers:
break
for link_rel in response.headers['link'].split(','):
link, rel = link_rel.split(';')
if 'rel="next"' in rel.strip():
next_page = link.strip(' <>')
break
# Params are already contained in the next page's url
params.clear()
def get_repository(self, slug: str, owner=None) -> base.AbstractRepository:
"""See AbstractClient.get_repository()"""
if owner is None:
owner = self.org or self.login
try:
return Repository.get(self, owner=owner, repo=slug)
except HTTPError as err:
if err.response.status_code == 404:
raise base.NoSuchRepository(
'{}/{}'.format(owner, slug)) from err
raise
def get_user_id(self) -> int:
return User.get(self).data['id']
def create_repository(self, slug: str, owner=None, **kwargs):
"""See AbstractClient.create_repository()"""
url = Repository.CREATE_URL
owner = owner or self.login
if owner != self.login:
url = Repository.CREATE_ORG_URL
kwargs['name'] = slug
try:
return Repository.create(self, kwargs, url=url, owner=owner)
except HTTPError as err:
if err.response.status_code == 422:
raise base.RepositoryExists(slug) from err
raise
def delete_repository(self, slug: str, owner=None):
"""See AbstractClient.delete_repository()"""
if owner is None:
owner = self.login
try:
Repository.delete(self, owner=owner, repo=slug)
except HTTPError as err:
if err.response.status_code == 404:
raise base.NoSuchRepository(
'{}/{}'.format(owner, slug)) from err
raise
class Repository(base.AbstractGitHostObject, base.AbstractRepository):
GET_URL = '/repos/{owner}/{repo}'
DELETE_URL = GET_URL
CREATE_URL = '/user/repos'
CREATE_ORG_URL = '/orgs/{owner}/repos'
SCHEMA = schema.Repo
GET_SCHEMA = schema.Repo
CREATE_SCHEMA = schema.CreateRepo
@property
def full_name(self) -> str:
return self.data['full_name']
@property
def owner(self) -> str:
return self.data['owner']['login']
@property
def slug(self) -> str:
return self.data['name']
@property
def git_url(self) -> str:
return 'https://{}:{}@github.com/{}/{}.git'.format(
quote(self.client.login),
quote(self.client.password),
self.owner,
self.slug)
def get_commit_url(self, revision):
return 'https://github.com/{}/{}/commit/{}'.format(self.owner,
self.slug,
revision)
def get_commit_status(self, ref):
try:
combined = AggregatedStatus.get(self.client,
owner=self.owner,
repo=self.slug, ref=ref)
actions = AggregatedWorkflowRuns.get(
client=self.client,
owner=self.owner,
repo=self.slug,
params={
'head_sha': ref
})
combined.status[actions.key] = actions
except HTTPError as err:
if err.response.status_code == 404:
return None
raise
for key, status in combined.status.items():
cache.BUILD_STATUS_CACHE[key].set(combined.commit, status)
return combined
def get_build_status(self, revision: str, key: str) -> str:
status = cache.BUILD_STATUS_CACHE[key].get(revision, None)
if status and status.state == 'SUCCESSFUL':
return status.state
try:
return self.get_commit_status(revision).status.get(key, None).state
except AttributeError as e:
LOG.error(e)
return 'NOTSTARTED'
def get_build_url(self, revision: str, key: str) -> str:
# Only look inside the cache. There is no need for an API call for this
# build's URL.
status = cache.BUILD_STATUS_CACHE[key].get(revision, None)
if status:
return status.url
def get_build_description(self, revision: str, key: str) -> str:
status = cache.BUILD_STATUS_CACHE[key].get(revision, None)
if status:
return status.description
def set_build_status(self, revision: str, key: str, state: str,
url='', description=''):
trans = {
'INPROGRESS': 'pending',
'SUCCESSFUL': 'success',
'FAILED': 'failure',
'STOPPED': 'error',
'NOTSTARTED': 'pending'
}
data = {
'state': trans[state],
'target_url': url,
'description': description,
'context': key
}
return Status.create(
self.client, data=data, owner=self.owner, repo=self.slug,
sha=revision
)
def get_pull_requests(self, author=None, src_branch=None, status='OPEN'):
if author is None:
author = self.owner
query_state = {
'OPEN': 'open',
'MERGED': 'closed',
'DECLINED': 'closed'
}[status]
if isinstance(src_branch, str):
src_branch = [src_branch]
if not src_branch:
args = [{'state': query_state}]
else:
args = [{'head': '{}:{}'.format(author, b),
'state': query_state} for b in src_branch]
for arg in args:
for pull_request in PullRequest.list(self.client, params=arg,
owner=self.owner,
repo=self.slug):
if pull_request.status == status:
yield pull_request
def get_pull_request(self, pull_request_id):
return PullRequest.get(self.client, owner=self.owner, repo=self.slug,
number=pull_request_id)
def create_pull_request(self, title, src_branch, dst_branch, description,
**kwargs):
kwargs.update({
'title': title,
'head': src_branch,
'base': dst_branch,
'body': description
})
return PullRequest.create(self.client, data=kwargs, owner=self.owner,
repo=self.slug)
class AggregatedStatus(base.AbstractGitHostObject):
GET_URL = '/repos/{owner}/{repo}/commits/{ref}/status'
SCHEMA = schema.AggregatedStatus
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._status = {}
for status_data in self.data.get('statuses', []):
status = Status(**status_data, _validate=False)
self._status[status_data['context']] = status
@property
def commit(self) -> str:
return self.data['sha']
@property
def status(self):
return self._status
class Status(base.AbstractGitHostObject, base.AbstractBuildStatus):
CREATE_URL = '/repos/{owner}/{repo}/statuses/{sha}'
SCHEMA = schema.Status
CREATE_SCHEMA = schema.Status
@property
def state(self) -> str:
trans = {
'pending': 'INPROGRESS',
'success': 'SUCCESSFUL',
'error': 'FAILED',
'failure': 'FAILED',
None: 'NOTSTARTED'
}
return trans[self.data['state']]
@property
def url(self) -> str:
return self.data['target_url']
@property
def description(self) -> str:
return self.data['description']
@property
def key(self) -> str:
return self.data['context']
def __str__(self) -> str:
return self.state
class WorkflowRun(base.AbstractGitHostObject):
"""
Endpoint to have access about workflows runs
"""
GET_URL = "/repos/{owner}/{repo}/actions/runs/{id}"
CREATE_URL = "/repos/{owner}/{repo}/actions/runs"
SCHEMA = schema.WorkflowRun
class AggregatedWorkflowRuns(base.AbstractGitHostObject):
GET_URL = "/repos/{owner}/{repo}/actions/runs"
SCHEMA = schema.AggregateWorkflowRuns
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._workflow_runs = [elem for elem in self.data['workflow_runs']]
@property
def url(self):
if len(self._workflow_runs) == 0:
return None
return f"https://github.com/{self.full_repo}/actions?query=branch%3A{self.branch}" # noqa
@property
def commit(self) -> str | None:
if len(self._workflow_runs) == 0:
return None
return self._workflow_runs[0]['head_sha']
@property
def full_repo(self) -> str | None:
if len(self._workflow_runs) == 0:
return None
return self._workflow_runs[0]['repository']['full_name']
def is_pending(self, workflow_runs=None):
if workflow_runs is None:
workflow_runs = self._workflow_runs
return len([
elem for elem in workflow_runs if elem['status'] == 'pending'
]) > 0
def is_queued(self, workflow_runs=None):
if workflow_runs is None:
workflow_runs = self._workflow_runs
return len([
elem for elem in workflow_runs if elem['status'] == 'queued'
]) > 0
@property
def owner(self) -> str | None:
if self._workflow_runs.__len__() > 0:
return self._workflow_runs[0]['repository']['owner']['login']
return None
@property
def repo(self) -> str | None:
if self._workflow_runs.__len__() > 0:
return self._workflow_runs[0]['repository']['name']
return None
@property
def branch(self) -> str | None:
if self._workflow_runs.__len__() > 0:
return self._workflow_runs[0]['head_branch']
return None
def remove_unwanted_workflows(self):
"""
Remove two things:
- check-suites not triggerd by github-actions
- check-suites workflow triggerd by a `workflow_dispatch` event
- Same workflow with different result
"""
if self._workflow_runs.__len__() == 0:
return
self._workflow_runs = list(filter(
lambda elem: elem['event'] != 'workflow_dispatch',
self._workflow_runs
))
# When two of the same workflow ran on the same branch,
# we only keep the best one.
conclusion_ranking = {
'success': 4, 'skipped': 4, None: 3, 'failure': 2, 'cancelled': 1
}
best_runs = {}
for run in self._workflow_runs:
workflow_id = run['workflow_id']
conclusion = run['conclusion']
if (workflow_id not in best_runs or
conclusion_ranking[conclusion] >
conclusion_ranking[best_runs[workflow_id]['conclusion']]):
best_runs[workflow_id] = run
self._workflow_runs = list(best_runs.values())
def branch_state(self, branch_workflow_runs):
all_complete = all(
elem['conclusion'] is not None for elem in branch_workflow_runs
)
all_success = all(
elem['conclusion'] in ('success', 'skipped')
for elem in branch_workflow_runs
)
LOG.info(f'State on {self.branch}: '
f'complete: {all_complete} '
f'success: {all_success} '
f'pending: {self.is_pending(branch_workflow_runs)} '
f'queued: {self.is_queued(branch_workflow_runs)}')
LOG.info(f'branch check suites {branch_workflow_runs}')
if branch_workflow_runs.__len__() == 0:
return 'NOTSTARTED'
elif (self.is_pending(branch_workflow_runs) or
self.is_queued(branch_workflow_runs) or not all_complete):
return 'INPROGRESS'
elif all_complete and all_success:
return 'SUCCESSFUL'
else:
return 'FAILED'
@property
def state(self):
self.remove_unwanted_workflows()
res = [list(v) for i, v in groupby(
self._workflow_runs,
lambda elem: elem['head_branch']
)]
status = [
self.branch_state(branch_check_suite)
for branch_check_suite in res
]
if 'SUCCESSFUL' in status:
return 'SUCCESSFUL'
elif 'INPROGRESS' in status:
return 'INPROGRESS'
elif 'FAILED' in status:
return 'FAILED'
else:
return 'NOTSTARTED'
def state_for_branch(self, branch_name: str) -> str:
"""Return aggregated state for runs on a specific branch only.
Filters to branch_name before deduplicating, so a sibling branch
that shares the same SHA cannot cause a false SUCCESSFUL result.
Unlike .state, this never returns SUCCESSFUL for a different branch.
"""
conclusion_ranking = {
'success': 4, 'skipped': 4, None: 3, 'failure': 2, 'cancelled': 1
}
runs = [
r for r in self._workflow_runs
if r['head_branch'] == branch_name and
r.get('event') != 'workflow_dispatch'
]
best: dict = {}
for run in runs:
wid = run['workflow_id']
conclusion = run['conclusion']
if (wid not in best or
conclusion_ranking[conclusion] >
conclusion_ranking[best[wid]['conclusion']]):
best[wid] = run
branch_runs = list(best.values())
LOG.info(
"state_for_branch: branch=%s runs=%s",
branch_name,
[{'id': r['id'], 'status': r['status'],
'conclusion': r['conclusion']}
for r in branch_runs],
)
return self.branch_state(branch_runs)
@property
def description(self) -> str:
return 'github actions CI'
@property
def key(self) -> str:
return 'github_actions'
@property
def total_count(self):
return self.data['total_count']
@property
def workflow_runs(self):
return self._workflow_runs
def __str__(self) -> str:
return self.state
class PullRequest(base.AbstractGitHostObject, base.AbstractPullRequest):
LIST_URL = '/repos/{owner}/{repo}/pulls'
GET_URL = '/repos/{owner}/{repo}/pulls/{number}'
CREATE_URL = LIST_URL
SCHEMA = schema.PullRequest
CREATE_SCHEMA = schema.CreatePullRequest
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._reviews = None
@property
def id(self) -> int:
return int(self.data['number'])
@property
def title(self) -> str:
return self.data['title']
@property
def author(self) -> str:
return self.data['user']['login'].lower()
@property
def author_display_name(self) -> str:
return self.data['user'].get('name', self.author)
@property
def description(self) -> str:
return self.data.get('body', '')
@property
def src_branch(self) -> str:
return self.data['head']['ref']
@property
def dst_branch(self) -> str:
return self.data['base']['ref']
@property
def src_commit(self) -> str:
return self.data['head']['sha']
@src_commit.setter
def src_commit(self, sha1):
self.data['head']['sha'] = sha1
@property
def status(self) -> str:
state = self.data['state']
if state == 'open':
return 'OPEN'
elif self.data['merged_at']:
return 'MERGED'
else:
return 'DECLINED'
@property
def url(self) -> str:
return self.data['html_url']
def add_comment(self, msg: str):
url = self.data['comments_url']
return Comment.create(self.client, {'body': msg}, url=url)
def set_bot_status(self, status: str | None, title: str, summary: str):
if self.client and self.client.is_app is False:
LOG.error("Cannot set bot status without a GitHub App")
return
conclusion: str | None = None
if status == "success":
conclusion = "success"
status = "completed"
elif status == "failure":
conclusion = "failure"
status = "completed"
else:
conclusion = None
self._add_checkrun(
name='bert-e', status=status, conclusion=conclusion,
title=title, summary=summary
)
def _add_checkrun(
self, name: str, status: str, conclusion: str | None,
title: str, summary: str):
data = {
'name': name,
'head_sha': self.src_commit,
'status': status,
'output': {
'title': title,
'summary': summary,
},
}
if conclusion is not None:
data['conclusion'] = conclusion
LOG.debug(data)
return CheckRun.create(
client=self.client,
data=data,
owner=self.repo.owner, repo=self.repo.slug
)
def get_comments(self):
return Comment.list(self.client, url=self.data['comments_url'])
@property
def comments(self):
return list(self.get_comments())
@property
def repo(self):
return Repository(**self.data['base']['repo'], client=self.client)
def get_reviews(self):
repo = self.repo
self._reviews = list(Review.list(
self.client,
# Special header needed to use Reviews API
headers={
'Accept': 'application/vnd.github.black-cat-preview+json'
},
owner=repo.owner, repo=repo.slug, number=self.id)
)
return self._reviews
def get_participants(self):
reviews = self._reviews or self.get_reviews()
return (r.author.lower() for r in reviews)
def get_summarized_reviews(self):
"""
Github API provides three statuses relevant for gating:
APPROVED, DISMISSED, CHANGES_REQUESTED.
Any dismissed review means that either a change_request was dismissed
(but no approval since) or that the latest approval was dismissed. As
such, only the last "relevant" review item shall be accounted for each
reviewer, as any remaining approval from that author (in the API) may
not be the latest status.
"""
reviews = self._reviews or self.get_reviews()
# Filter reviews (remove COMMENTED entries)
filtered = list(filter(lambda r: not r.commented, reviews))
# Order the reviews by id (so the order matches the PR's timeline)
filtered.sort(key=lambda r: r.id)
# ID-based ordering ensure that we can simply select the last for any
# author, to get the "current" status.
summary = {}
for author in self.get_participants():
author_reviews = list(
filter(lambda r: r.author == author, filtered))
if len(author_reviews):
summary[author] = author_reviews[-1]
return summary
def get_change_requests(self):
summary = self.get_summarized_reviews()
return (r.author.lower()
for r in summary.values() if r.changes_requested)
def get_approvals(self):
summary = self.get_summarized_reviews()
return (r.author.lower() for r in summary.values() if r.approved)
def comment_review(self):
rev = Review.create(
client=self.client,
data={'body': 'not wanting to block', 'event': 'COMMENT'},
headers={
'Accept': 'application/vnd.github.black-cat-preview+json'
},
owner=self.repo.owner, repo=self.repo.slug, number=self.id
)
return rev
def request_changes(self):
rev = Review.create(
client=self.client,
data={'body': 'NOPE', 'event': 'REQUEST_CHANGES'},
headers={
'Accept': 'application/vnd.github.black-cat-preview+json'
},
owner=self.repo.owner, repo=self.repo.slug, number=self.id
)
return rev
def approve(self):
rev = Review.create(
client=self.client,
data={'body': 'LGTM', 'event': 'APPROVE'},
headers={
'Accept': 'application/vnd.github.black-cat-preview+json'
},
owner=self.repo.owner, repo=self.repo.slug, number=self.id
)
return rev
def decline(self):
self.update(client=self.client, owner=self.repo.owner,
repo=self.repo.slug, number=self.id,
data={'state': 'closed'})
def dismiss(self, review):
self.client.put(
url=Review.DISMISS_URL.format(
owner=self.repo.owner, repo=self.repo.slug,
number=self.id, id=review.id),
headers={
'Accept': 'application/vnd.github.black-cat-preview+json',
},
data=json.dumps({
"message": "no longer relevant.",
})
)
class Comment(base.AbstractGitHostObject, base.AbstractComment):
GET_URL = '/repos/{owner}/{repo}/issues/{number}/comments/{id}'
LIST_URL = '/repos/{owner}/{repo}/issues/{number}/comments'
CREATE_URL = LIST_URL
SCHEMA = schema.Comment
CREATE_SCHEMA = schema.CreateComment