-
Notifications
You must be signed in to change notification settings - Fork 133
/
githubhandler.py
1368 lines (1181 loc) · 51.5 KB
/
githubhandler.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
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
"""Wrappers for Github Web-API Bindings"""
import abc
import base64
import datetime
import logging
import os
import time
from copy import copy
from enum import Enum
from typing import Any, AsyncIterator, Dict, List, Optional, Tuple, Union
import aiohttp
import backoff
import cachetools
import gidgethub
import gidgethub.aiohttp
import gidgethub.sansio
import jwt
import uritemplate
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
#: State for Github Issues
IssueState = Enum("IssueState", "open closed all") # pylint: disable=invalid-name
#: State of Github Check Run
CheckRunStatus = Enum("CheckRunState", "queued in_progress completed")
#: Conclusion of Github Check Run
CheckRunConclusion = Enum("CheckRunConclusion",
"success failure neutral cancelled timed_out action_required")
#: Merge method
MergeMethod = Enum("MergeMethod", "merge squash rebase")
#: Pull request review state
ReviewState = Enum("ReviewState", "APPROVED CHANGES_REQUESTED COMMENTED DISMISSED PENDING")
#: ContentType for Project Cards
CardContentType = Enum("CardContentType", "Issue PullRequest")
def iso_now() -> str:
"""Creates ISO 8601 timestamp in format
``YYYY-MM-DDTHH:MM:SSZ`` as required by Github
"""
return datetime.datetime.utcnow().replace(microsecond=0).isoformat()+'Z'
class GitHubHandler:
"""Handles interaction with GitHub
Arguments:
token: OAUTH token granting permissions to GH
dry_run: Don't actually modify things if set
to_user: Target User/Org for PRs
to_repo: Target repository within **to_user**
"""
USER = "/user"
USER_APPS = "/user/installations"
USER_ORGS = "/user/orgs"
USER_TEAMS = "/user/teams"
PULLS = "/repos/{user}/{repo}/pulls{/number}{?head,base,state}"
PULL_FILES = "/repos/{user}/{repo}/pulls/{number}/files"
PULL_COMMITS = "/repos/{user}/{repo}/pulls/{number}/commits"
PULL_MERGE = "/repos/{user}/{repo}/pulls/{number}/merge"
PULL_REVIEWS = "/repos/{user}/{repo}/pulls/{number}/reviews{/review_id}"
PULL_UPDATE = "/repos/{user}/{repo}/pulls/{number}/update-branch"
BRANCH_PROTECTION = "/repos/{user}/{repo}/branches/{branch}/protection"
ISSUES = "/repos/{user}/{repo}/issues{/number}"
ISSUE_COMMENTS = "/repos/{user}/{repo}/issues/{number}/comments"
COMMENTS = "/repos/{user}/{repo}/issues/comments{/comment_id}"
CHECK_RUN = "/repos/{user}/{repo}/check-runs{/id}"
GET_CHECK_RUNS = "/repos/{user}/{repo}/commits/{commit}/check-runs"
GET_STATUSES = "/repos/{user}/{repo}/commits/{commit}/statuses"
CONTENTS = "/repos/{user}/{repo}/contents/{path}{?ref}"
GIT_REFERENCE = "/repos/{user}/{repo}/git/refs/{ref}"
ORG_MEMBERS = "/orgs/{user}/members{/username}"
ORG = "/orgs/{user}"
ORG_TEAMS = "/orgs/{user}/teams{/team_slug}"
PROJECTS_BY_REPO = "/repos/{user}/{repo}/projects"
PROJECT_COL_CARDS = "/projects/columns/{column_id}/cards"
PROJECT_CARDS = "/projects/columns/cards/{card_id}"
TEAMS_MEMBERSHIP = "/teams/{team_id}/memberships/{username}"
SEARCH_ISSUES = "/search/issues?q="
STATE = IssueState
def __init__(self, token: str = None,
dry_run: bool = False,
to_user: str = "bioconda",
to_repo: str = "bioconda-recipes",
installation: int = None) -> None:
#: API Bearer Token
self.token = token
#: If set, no actual modifying actions are taken
self.dry_run = dry_run
#: The installation ID if this instance is connected to an App
self.installation = installation
#: Owner of the Repo
self.user = to_user
#: Name of the Repo
self.repo = to_repo
#: Default variables for API calls
self.var_default = {'user': to_user,
'repo': to_repo}
# filled in by login():
#: Gidgethub API object
self.api: gidgethub.abc.GitHubAPI = None
#: Login username
self.username: str = None
#: User avatar URL
self.avatar_url: str = None
def __str__(self):
return f"{self.user}/{self.repo}"
def __repr__(self):
return f"{self.__class__.__name__}({self.user}/{self.repo})"
def for_json(self):
"""Return JSON repesentation of object"""
return {
'__module__': self.__module__,
'__type__': self.__class__.__qualname__,
'dry_run': self.dry_run,
'to_user': self.user,
'to_repo': self.repo,
'installation': self.installation
}
def __to_json__(self):
return self.for_json()
@property
def rate_limit(self) -> gidgethub.sansio.RateLimit:
"""Last recorded rate limit data"""
return self.api.rate_limit
def set_oauth_token(self, token: str) -> None:
"""Update oauth token for the wrapped GitHubAPI object"""
self.token = token
self.api.oauth_token = token
@abc.abstractmethod
def create_api_object(self, *args, **kwargs):
"""Create API object"""
def get_file_relurl(self, path: str, branch_name: str = "master") -> str:
"""Format domain relative url for **path** on **branch_name**"""
return "/{user}/{repo}/tree/{branch_name}/{path}".format(
branch_name=branch_name, path=path, **self.var_default)
async def login(self, *args, **kwargs) -> bool:
"""Log into API (fills `username`)"""
if self.api is None:
self.create_api_object(*args, **kwargs)
self.username = "UNKNOWN [no token]"
self.avatar_url = None
if self.token:
user = await self.get_user()
if user:
self.username = user["login"]
self.avatar_url = user["avatar_url"]
return True
return False
async def get_user(self) -> Dict[str, Any]:
"""Fetches the user's info
Returns:
Empty dict if the request failed
"""
try:
return await self.api.getitem(self.USER)
except gidgethub.GitHubException:
return {}
async def get_user_orgs(self) -> List[str]:
"""Fetches the user's orgs
Returns:
Empty list if the request failed
"""
try:
return [org['login'] for org in await self.api.getitem(self.USER_ORGS)]
except gidgethub.GitHubException:
return []
async def iter_teams(self) -> AsyncIterator[Dict[str, Any]]:
"""List organization teams
Returns:
Async iterator over dicts, containing **id**,
**name**, **slug**, **description**, etc.
"""
async for team in self.api.getiter(self.ORG_TEAMS, self.var_default):
yield team
async def get_team_id(self, team_slug: str = None,
team_name: str = None) -> Optional[int]:
"""Get the Team ID from the Team slug
If both are set, **team_slug** is tried first.
Args:
team_slug: urlized team name, e.g. "justice-league" for "Justice League"
team_name: alternative, use normal name (requires extra API call internally)
Returns:
Team ID if found, otherwise `None`
"""
if team_slug:
var_data = copy(self.var_default)
var_data['team_slug'] = team_slug
try:
team = await self.api.getitem(self.ORG_TEAMS, var_data)
if team and 'id' in team:
return team['id']
except gidgethub.BadRequest as exc:
if exc.status_code != 404:
raise
if team_name:
async for team in self.iter_teams():
if team.get('name') == team_name:
return team.get('id')
async def is_team_member(self, username: str, team: Union[str, int]) -> bool:
"""Check if user is a member of given team
Args:
username: Name of user to check
team: ID, Slug or Name of team to check
Returns:
True if the user is a member of the team
"""
if isinstance(team, int):
team_id = team
else:
team_id = await self.get_team_id(team, team)
if not team:
logger.error("Could not find team for name '%s'", team)
return False
var_data = {
'username': username,
'team_id': team_id,
}
accept = "application/vnd.github.hellcat-preview+json"
try:
data = await self.api.getitem(self.TEAMS_MEMBERSHIP, var_data, accept=accept)
if data['state'] == "active":
return True
except gidgethub.BadRequest as exc:
if exc.status_code != 404:
raise
return False
async def is_member(self, username: str, team: Optional[Union[str, int]] = None) -> bool:
"""Check user membership
Args:
username: Name of user for whom to check membership
team: Name, Slug or ID of team to check
Returns:
True if the user is member of the organization, and, if **team**
is provided, if user is member of that team.
"""
if not username:
return False
var_data = copy(self.var_default)
var_data['username'] = username
try:
await self.api.getitem(self.ORG_MEMBERS, var_data)
except gidgethub.BadRequest:
logger.debug("User %s is not a member of %s", username, var_data['user'])
return False
logger.debug("User %s IS a member of %s", username, var_data['user'])
if team:
return await self.is_team_member(username, team)
return True
async def search_issues(self, author=None, pr=False, issue=False, sha=None,
closed=None):
"""Search issues/PRs on our repos
Arguments:
author: login name of user to search
sha: SHA of commit to search for
pr: whether to consider only PRs
issue: whether to consider only non-PR issues
closed: search only closed if true, only open if false
"""
query = ["org:" + self.user]
if pr and not issue:
query += ["is:pr"]
elif issue and not pr:
query += ["is:issue"]
if closed is not None:
if closed:
query += ["is:closed"]
else:
query += ["is:open"]
if author:
query += ["author:" + author]
if sha:
query += ["sha:" + sha]
return await self.api.getitem(self.SEARCH_ISSUES + '+'.join(query))
async def get_pr_count(self, user) -> int:
"""Get the number of PRs opened by user
Arguments:
user: login of user to query
Returns:
Number of PRs that **user** has opened so far
"""
result = await self.search_issues(pr=True, author=user)
return result.get('total_count', 0)
async def is_org(self) -> bool:
"""Check if we are operating on an organization"""
try:
await self.api.getitem(self.ORG, self.var_default)
except gidgethub.BadRequest:
return False
return True
async def get_prs_from_sha(self, head_sha: str, only_open=False) -> List[int]:
"""Searches for PRs matching **head_sha**
Args:
head_sha: The head checksum to search for
only_open: If true, return only open PRs
Result:
List of PR numbers.
"""
pr_numbers = []
result = await self.search_issues(pr=True, sha=head_sha,
closed=False if only_open else None)
for pull in result.get('items', []):
pr_number = int(pull['number'])
logger.error("checking %s", pr_number)
full_pr = await self.get_prs(number=pr_number)
if full_pr['head']['sha'].startswith(head_sha):
pr_numbers.append(pr_number)
return pr_numbers
# pylint: disable=too-many-arguments
@backoff.on_exception(backoff.fibo, gidgethub.BadRequest, max_tries=10,
giveup=lambda ex: ex.status_code not in [429, 502, 503, 504])
async def get_prs(self,
from_branch: Optional[str] = None,
from_user: Optional[str] = None,
to_branch: Optional[str] = None,
number: Optional[int] = None,
state: Optional[IssueState] = None) -> List[Dict[Any, Any]]:
"""Retrieve list of PRs matching parameters
Arguments:
from_branch: Name of branch from which PR asks to pull
from_user: Name of user/org in from which to pull
(default: from auth)
to_branch: Name of branch into which to pull (default: master)
number: PR number
"""
var_data = copy(self.var_default)
if not from_user:
from_user = self.username
if from_branch:
if from_user:
var_data['head'] = f"{from_user}:{from_branch}"
else:
var_data['head'] = from_branch
if to_branch:
var_data['base'] = to_branch
if number:
var_data['number'] = str(number)
if state:
var_data['state'] = state.name.lower()
accept = "application/vnd.github.shadow-cat-preview" # for draft
try:
return await self.api.getitem(self.PULLS, var_data, accept=accept)
except gidgethub.BadRequest as exc:
if exc.status_code == 404:
if number:
return {}
return []
raise
async def get_issue(self, number: int) -> Dict[str, Any]:
"""Retrieve a single PR or Issue by its number
Arguments:
number: PR/Issue number
Returns:
The dict will contain a 'pull_request' key (containing dict)
if the Issue is a PR.
"""
var_data = copy(self.var_default)
var_data['number'] = str(number)
return await self.api.getitem(self.ISSUES, var_data)
async def iter_pr_commits(self, number: int):
"""Create iterator over commits in a PR"""
var_data = copy(self.var_default)
var_data['number'] = str(number)
try:
async for item in self.api.getiter(self.PULL_COMMITS, var_data):
yield item
except gidgethub.GitHubException:
return
# pylint: disable=too-many-arguments
async def create_pr(self, title: str,
from_branch: str,
from_user: Optional[str] = None,
to_branch: Optional[str] = "master",
body: Optional[str] = None,
maintainer_can_modify: bool = True,
draft: bool = False) -> Dict[Any, Any]:
"""Create new PR
Arguments:
title: Title of new PR
from_branch: Name of branch from which PR asks to pull (aka head)
from_user: Name of user/org in from which to pull
to_branch: Name of branch into which to pull (aka base, default: master)
body: Body text of PR
maintainer_can_modify: Whether to allow maintainer to modify from_branch
draft: whether PR is draft
"""
var_data = copy(self.var_default)
if not from_user:
from_user = self.username
data: Dict[str, Any] = {
'title': title,
'base' : to_branch,
'body': body or '',
'maintainer_can_modify': maintainer_can_modify,
'draft': draft,
}
if from_user and from_user != self.username:
data['head'] = f"{from_user}:{from_branch}"
else:
data['head'] = from_branch
logger.debug("PR data %s", data)
if self.dry_run:
logger.info("Would create PR '%s'", title)
if title:
logger.info(" title: %s", title)
if body:
logger.info(" body:\n%s\n", body)
return {'number': -1}
logger.info("Creating PR '%s'", title)
accept = "application/vnd.github.shadow-cat-preview" # for draft
return await self.api.post(self.PULLS, var_data, data=data, accept=accept)
async def merge_pr(self, number: int, title: str = None, message: str = None, sha: str = None,
method: MergeMethod = MergeMethod.squash) -> Tuple[bool, str]:
"""Merge a PR
Arguments:
number: PR Number
title: Title for the commit message
message: Message to append to automatic message
sha: SHA PR head must match
merge_method: `MergeMethod` to use. Defaults to squash.
Returns:
Tuple: True if successful and message
"""
var_data = copy(self.var_default)
var_data['number'] = str(number)
data = {}
if title:
data['commit_title'] = title
if message:
data['commit_message'] = message
if sha:
data['sha'] = sha
if method:
data['merge_method'] = method.name
try:
res = await self.api.put(self.PULL_MERGE, var_data, data=data)
return True, res['message']
except gidgethub.BadRequest as exc:
if exc.status_code in (405, 409):
# not allowed / conflict
return False, exc.args[0]
raise
async def pr_is_merged(self, number) -> bool:
"""Checks whether a PR has been merged
Arguments:
number: PR Number
Returns:
True if the PR has been merged.
"""
var_data = copy(self.var_default)
var_data['number'] = str(number)
try:
await self.api.getitem(self.PULL_MERGE, var_data)
return True
except gidgethub.BadRequest as exc:
if exc.status_code == 404:
return False
raise
async def pr_update_branch(self, number) -> bool:
"""Updates PR branch
Merges changes to "base" into "head"
"""
var_data = copy(self.var_default)
var_data['number'] = str(number)
accept = "application/vnd.github.lydian-preview+json"
try:
await self.api.put(self.PULL_UPDATE, var_data, accept=accept, data={})
return True
except gidgethub.HTTPException as exc:
if exc.status_code == 202:
# Usually, we will get our "true" result via this exception.
# GitHub sends 202 "Accepted" for this API call. Gidgethub raises
# on anything but 200, 201, 204 (see sansio.py:decipher_response).
# So we catch and check the status code.
return True
logger.exception("pr_update_branch_failed (2). status_code=%s, args=%s",
exc.status_code, exc.args)
return False
async def modify_issue(self, number: int,
labels: Optional[List[str]] = None,
title: Optional[str] = None,
body: Optional[str] = None) -> Dict[Any, Any]:
"""Modify existing issue (PRs are issues)
Arguments:
labels: list of labels to assign to issue
title: new title
body: new body
"""
var_data = copy(self.var_default)
var_data["number"] = str(number)
data: Dict[str, Any] = {}
if labels:
data['labels'] = labels
if title:
data['title'] = title
if body:
data['body'] = body
if self.dry_run:
logger.info("Would modify PR %s", number)
if title:
logger.info("New title: %s", title)
if labels:
logger.info("New labels: %s", labels)
if body:
logger.info("New Body:\n%s\n", body)
return {'number': number}
logger.info("Modifying PR %s", number)
return await self.api.patch(self.ISSUES, var_data, data=data)
async def create_comment(self, number: int, body: str) -> int:
"""Create issue comment
Arguments:
number: Issue number
body: Comment content
"""
var_data = copy(self.var_default)
var_data["number"] = str(number)
data = {
'body': body
}
if self.dry_run:
logger.info("Would create comment on issue #%i", number)
return -1
logger.info("Creating comment on issue #%i", number)
res = await self.api.post(self.ISSUE_COMMENTS, var_data, data=data)
return res['id']
async def iter_comments(self, number: int) -> List[Dict[str, Any]]:
"""List comments for issue"""
var_data = copy(self.var_default)
var_data["number"] = str(number)
return self.api.getiter(self.ISSUE_COMMENTS, var_data)
async def update_comment(self, number: int, body: str) -> int:
"""Update issue comment
Arguments:
number: Comment number (NOT PR NUMBER!)
body: Comment content
"""
var_data = copy(self.var_default)
var_data["comment_id"] = str(number)
data = {
'body': body
}
if self.dry_run:
logger.info("Would update comment %i", number)
return -1
logger.info("Updating comment %i", number)
res = await self.api.patch(self.COMMENTS, var_data, data=data)
return res['id']
async def get_pr_modified_files(self, number: int) -> List[Dict[str, Any]]:
"""Retrieve the list of files modified by the PR
Arguments:
number: PR issue number
"""
var_data = copy(self.var_default)
var_data["number"] = str(number)
return await self.api.getitem(self.PULL_FILES, var_data)
async def create_check_run(self, name: str, head_sha: str,
details_url: str = None, external_id: str = None) -> int:
"""Create a check run
Arguments:
name: The name of the check, e.g. ``bioconda-test``
head_sha: The sha of the commit to check
details_url: URL for "View more details on <App name>" link
external_id: ID for us
Returns:
The ID of the check run.
"""
var_data = copy(self.var_default)
data = {
'name': name,
'head_sha': head_sha,
}
if details_url:
data['details_url'] = details_url
if external_id:
data['external_id'] = external_id
accept = "application/vnd.github.antiope-preview+json"
result = await self.api.post(self.CHECK_RUN, var_data, data=data, accept=accept)
return int(result.get('id'))
async def modify_check_run(self, number: int,
status: CheckRunStatus = None,
conclusion: CheckRunConclusion = None,
output_title: str = None,
output_summary: str = None,
output_text: str = None,
output_annotations: List[Dict] = None,
actions: List[Dict] = None) -> Dict['str', Any]:
"""Modify a check runs
Arguments:
number: id number of check run
status: current status
conclusion: result of check run, needed if status is completed
output_title: title string for result window
output_summary: subtitle/summary string for result window (reqired if title given)
output_text: Markdown text for result window
annotations: List of annotated code pieces, each has to have ``path``,
``start_line`` and ``end_line``, ``annotation_level`` (``notice``,
``warning``, ``failure``), and a ``message``. May also have
may have ``start_column`` and ``end_column`` (if only one line),
``title`` and ``raw_details``.
actions: List of up to three "actions" as dict of ``label``, ``description``
and ``identifier``
Returns:
Check run "object" as dict.
"""
logger.info("Modifying check run %i: status=%s conclusion=%s title=%s",
number, status.name, conclusion.name if conclusion else "N/A", output_title)
var_data = copy(self.var_default)
var_data['id'] = str(number)
data = {}
if status is not None:
data['status'] = status.name.lower()
if status == CheckRunStatus.in_progress:
data['started_at'] = iso_now()
if conclusion is not None:
data['conclusion'] = conclusion.name.lower()
data['completed_at'] = iso_now()
if output_title:
data['output'] = {
'title': output_title,
'summary': output_summary or "",
'text': output_text or "",
}
if output_annotations:
data['output']['annotations'] = output_annotations
if actions:
data['actions'] = actions
accept = "application/vnd.github.antiope-preview+json"
return await self.api.patch(self.CHECK_RUN, var_data, data=data, accept=accept)
async def get_check_runs(self, sha: str) -> List[Dict[str, Any]]:
"""List check runs for **sha**
Arguments:
sha: The commit SHA for which to search for check runs
Returns:
List of check run "objects"
"""
var_data = copy(self.var_default)
var_data['commit'] = sha
accept = "application/vnd.github.antiope-preview+json"
res = await self.api.getitem(self.GET_CHECK_RUNS, var_data, accept=accept)
return res['check_runs']
async def get_statuses(self, sha: str) -> List[Dict[str, Any]]:
"""List status checks for **sha**
Arguments:
sha: The commit SHA for which to find statuses
Returns:
List of status "objects"
"""
var_data = copy(self.var_default)
var_data['commit'] = sha
return await self.api.getitem(self.GET_STATUSES, var_data)
async def get_pr_reviews(self, pr_number: int) -> List[Dict[str, Any]]:
"""Get reviews filed for a PR
Arguments:
pr_number: Number of PR
Returns:
List of dictionaries each having ``body`` (`str`), ``state`` (`ReviewState`),
and ``commit_id`` (SHA, `str`) as well as a ``user`` `dict`.
"""
var_data = copy(self.var_default)
var_data['number'] = str(pr_number)
return await self.api.getitem(self.PULL_REVIEWS, var_data)
async def get_branch_protection(self, branch: str = "master") -> Dict[str, Any]:
"""Retrieve protection settings for branch
Arguments:
branch: Branch for which to get protection settings
Returns:
Deep dict as example below. Protections not in place will not be present
in dict.
.. code-block:: yaml
required_status_checks: # require status checks to pass
strict: False # require PR branch to be up to date with base
contexts: # list of status checks required
- bioconda-test
enforce_admins: # admins, too, must follow rules
- enabled: True
required_pull_request_reviews: # require approving review
required_approving_review_count: 1 # 1 - 6 valid
dismiss_stale_reviews: False # auto dismiss approval after push
require_code_owner_reviews: False
dismissal_restrictions: # specify who may dismiss reviews
users:
- login: bla
teams:
- id: 1
- name: Bl Ub
- slug: bl-ub
restrictions: # specify who may push
users:
- login: bla
teams:
- id: 1
enforce_admins:
enabled: True # apply to admins also
"""
var_data = copy(self.var_default)
var_data["branch"] = branch
accept = "application/vnd.github.luke-cage-preview+json"
res = await self.api.getitem(self.BRANCH_PROTECTION, var_data, accept=accept)
return res
async def check_protections(self, pr_number: int,
head_sha: str = None) -> Tuple[Optional[bool], str]:
"""Check whether PR meets protection requirements
Arguments:
pr_number: issue number of PR
head_sha: if given, check that this is still the latest sha
"""
pr = await self.get_prs(number=pr_number)
logger.info("Checking protections for PR #%s : %s", pr_number, head_sha)
# check that no new commits were made
if head_sha and head_sha != pr['head']['sha']:
return False, "Most recent SHA in PR differs"
head_sha = pr['head']['sha']
# check whether it's already merged
if pr['merged']:
return False, "PR has already been merged"
# check whether it's in draft state
if pr.get('draft'):
return False, "PR is marked as draft"
# check whether it's mergeable
if pr['mergeable'] is None:
return None, "PR mergability unknown. Retry again later."
# get required checks for target branch
protections = await self.get_branch_protection(pr['base']['ref'])
# Verify required_checks
required_checks = set(protections.get('required_status_checks', {}).get('contexts', []))
if required_checks:
logger.info("Verifying %s required checks", len(required_checks))
for status in await self.get_statuses(head_sha):
if status['state'] == 'success':
required_checks.discard(status['context'])
for check in await self.get_check_runs(head_sha):
if check.get('conclusion') == "success":
required_checks.discard(check['name'])
if required_checks:
return False, "Not all required checks have passed"
logger.info("All status checks passed for #%s", pr_number)
# Verify required reviews
required_reviews = protections.get('required_pull_request_reviews', {})
if required_reviews:
required_count = required_reviews.get('required_approving_review_count', 1)
logger.info("Checking for %s approvng reviews and no change requests",
required_count)
approving_count = 0
for review in await self.get_pr_reviews(pr_number):
user = review['user']['login']
if review['state'] == ReviewState.CHANGES_REQUESTED.name:
return False, f"Changes have been requested by `@{user}`"
if review['state'] == ReviewState.APPROVED.name:
logger.info("PR #%s was approved by @%s", pr_number, user)
approving_count += 1
if approving_count < required_count:
return False, (f"Insufficient number of approving reviews"
f"({approving_count}/{required_count})")
logger.info("PR #%s is passing configured checks", pr_number)
return True, "LGTM"
async def get_contents(self, path: str, ref: str = None) -> str:
"""Get contents of a file in repo
Arguments:
path: file path
ref: git reference (branch, commit, tag)
Returns:
The contents of the file
Raises:
RuntimeError if the content encoding is not understood.
(Should always be base64)
"""
var_data = copy(self.var_default)
var_data['path'] = path
if ref:
var_data['ref'] = ref
else:
ref = 'master'
result = await self.api.getitem(self.CONTENTS, var_data)
if result['encoding'] != 'base64':
raise RuntimeError(f"Got unknown encoding for {self}/{path}:{ref}")
content_bytes = base64.b64decode(result['content'])
content = content_bytes.decode('utf-8')
return content
async def delete_branch(self, ref: str) -> None:
"""Delete a branch (ref)
Arguments:
ref: Name of reference/branch
"""
var_data = copy(self.var_default)
var_data['ref'] = ref
await self.api.delete(self.GIT_REFERENCE, var_data)
def _deparse_card_pr_number(self, card: Dict[str, Any]) -> Dict[str, Any]:
"""Extracts the card's issue's number from the content_url
This is a hack. The card data returned from github does not contain
content_id or anything referencing the PR/issue except for the
content_url. We deparse this here manually.
Arguments:
card: Card dict as returned from github
Results:
Card dict with ``issue_number`` field added if card is not a note
"""
if 'content_url' not in card: # not content_url to parse
return card
if 'issue_number' in card: # target value already taken
return card
issue_url = gidgethub.sansio.format_url(self.ISSUES, self.var_default)
content_url = card['content_url']
if content_url.startswith(issue_url):
try:
card['issue_number'] = int(content_url.lstrip(issue_url))
except ValueError:
pass
if 'issue_number' not in card:
logger.error("Failed to deparse content url to issue number.\n"
"content_url=%s\nissue_url=%s\n",
content_url, issue_url)
return card
async def list_project_cards(self, column_id: int) -> List[Dict[str, Any]]:
"""List cards in a project column
Arguments:
column_id: ID number of project column
"""
var_data = {'column_id': str(column_id)}
accept = "application/vnd.github.inertia-preview+json"
res = await self.api.getitem(self.PROJECT_COL_CARDS, var_data, accept=accept)
return [self._deparse_card_pr_number(card) for card in res]
async def get_project_card(self, card_id: int) -> Dict[str, Any]:
"""Get a project card
Arguments:
card_id: ID number of project card
Returns:
Empty dict if the project card was not found
"""
var_data = {'card_id': str(card_id)}
accept = "application/vnd.github.inertia-preview+json"
try:
res = await self.api.getitem(self.PROJECT_CARDS, var_data, accept=accept)
except gidgethub.BadRequest as exc:
if exc.status_code == 404:
return {}
return self._deparse_card_pr_number(res)
async def create_project_card(self, column_id: int,
note: str = None,
content_id: int = None,
content_type: CardContentType = None,
number: int = None) -> Dict[str, Any]:
"""Create a new project card
In addition to **column_id**, you must provide *either*:
- The **note** parameter for a free text note card
- The **content_type** specifying whether the card references a PR or an Issue,
*and* the **content_id** with the **id** field from the PR or Issue. Note that
PRs have two IDs, once as their issue baseclass and once as PR. You must use
the latter for PRs.
- The **number** giving either PR or Issue number. Will trigger one or two extra
API calls to fill in the **content_type** and **content_id** fields.
Arguments:
column_id: The ID number of the project column
note: Content of a note card.
content_id: ID number of PR or Issue
content_type: Must match content_id type
number: PR or Issue number
Returns:
Dict describing newly created card. Empty if no card.
"""
var_data = copy(self.var_default)
var_data['column_id'] = str(column_id)
accept = "application/vnd.github.inertia-preview+json"
data = {}
if note:
if content_id or content_type or number:
raise ValueError("Project Cards can only be a note or a Issue/PR")
data['note'] = note
elif content_id:
if number: