Skip to content

Commit f4f300e

Browse files
Merge branch 'master' into v0.12.0-dev
2 parents 6a5219f + 8d2f293 commit f4f300e

File tree

2 files changed

+83
-70
lines changed

2 files changed

+83
-70
lines changed

dev_tools/auto_merge.py

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import datetime
2-
import traceback
3-
from typing import Callable, Optional, List, Any, Dict, Set, Union
4-
52
import json
63
import os
7-
import time
84
import sys
5+
import time
6+
import traceback
7+
from typing import Callable, Optional, List, Any, Dict, Set, Union
98

109
from google.cloud import secretmanager_v1beta1
11-
import requests
1210

1311
from dev_tools.github_repository import GithubRepository
1412

@@ -47,11 +45,11 @@ def from_github(repo: GithubRepository, pull_id: int) -> 'PullRequestDetails':
4745
References:
4846
https://developer.github.com/v3/pulls/#get-a-single-pull-request
4947
"""
50-
url = "https://api.github.com/repos/{}/{}/pulls/{}?access_token={}".format(
51-
repo.organization, repo.name, pull_id, repo.access_token
48+
url = "https://api.github.com/repos/{}/{}/pulls/{}".format(
49+
repo.organization, repo.name, pull_id
5250
)
5351

54-
response = requests.get(url)
52+
response = repo.get(url)
5553

5654
if response.status_code != 200:
5755
raise RuntimeError(
@@ -126,12 +124,11 @@ def check_collaborator_has_write(
126124
References:
127125
https://developer.github.com/v3/issues/events/#list-events-for-an-issue
128126
"""
129-
url = (
130-
"https://api.github.com/repos/{}/{}/collaborators/{}/permission"
131-
"?access_token={}".format(repo.organization, repo.name, username, repo.access_token)
127+
url = "https://api.github.com/repos/{}/{}/collaborators/{}/permission" "".format(
128+
repo.organization, repo.name, username
132129
)
133130

134-
response = requests.get(url)
131+
response = repo.get(url)
135132

136133
if response.status_code != 200:
137134
raise RuntimeError(
@@ -148,13 +145,13 @@ def check_collaborator_has_write(
148145

149146

150147
# pylint: enable=docstring-first-line-empty
151-
def get_all(url_func: Callable[[int], str]) -> List[Any]:
148+
def get_all(repo: GithubRepository, url_func: Callable[[int], str]) -> List[Any]:
152149
results: List[Any] = []
153150
page = 0
154151
has_next = True
155152
while has_next:
156153
url = url_func(page)
157-
response = requests.get(url)
154+
response = repo.get(url)
158155

159156
if response.status_code != 200:
160157
raise RuntimeError(
@@ -179,12 +176,11 @@ def check_auto_merge_labeler(
179176
https://developer.github.com/v3/issues/events/#list-events-for-an-issue
180177
"""
181178
events = get_all(
179+
repo,
182180
lambda page: (
183181
"https://api.github.com/repos/{}/{}/issues/{}/events"
184-
"?access_token={}&per_page=100&page={}".format(
185-
repo.organization, repo.name, pull_id, repo.access_token, page
186-
)
187-
)
182+
"?per_page=100&page={}".format(repo.organization, repo.name, pull_id, page)
183+
),
188184
)
189185

190186
relevant = [
@@ -204,11 +200,11 @@ def add_comment(repo: GithubRepository, pull_id: int, text: str) -> None:
204200
References:
205201
https://developer.github.com/v3/issues/comments/#create-a-comment
206202
"""
207-
url = "https://api.github.com/repos/{}/{}/issues/{}/comments?access_token={}".format(
208-
repo.organization, repo.name, pull_id, repo.access_token
203+
url = "https://api.github.com/repos/{}/{}/issues/{}/comments".format(
204+
repo.organization, repo.name, pull_id
209205
)
210206
data = {'body': text}
211-
response = requests.post(url, json=data)
207+
response = repo.post(url, json=data)
212208

213209
if response.status_code != 201:
214210
raise RuntimeError(
@@ -224,11 +220,11 @@ def edit_comment(repo: GithubRepository, text: str, comment_id: int) -> None:
224220
References:
225221
https://developer.github.com/v3/issues/comments/#edit-a-comment
226222
"""
227-
url = "https://api.github.com/repos/{}/{}/issues/comments/{}?access_token={}".format(
228-
repo.organization, repo.name, comment_id, repo.access_token
223+
url = "https://api.github.com/repos/{}/{}/issues/comments/{}".format(
224+
repo.organization, repo.name, comment_id
229225
)
230226
data = {'body': text}
231-
response = requests.patch(url, json=data)
227+
response = repo.patch(url, json=data)
232228

233229
if response.status_code != 200:
234230
raise RuntimeError(
@@ -244,10 +240,10 @@ def get_branch_details(repo: GithubRepository, branch: str) -> Any:
244240
References:
245241
https://developer.github.com/v3/repos/branches/#get-branch
246242
"""
247-
url = "https://api.github.com/repos/{}/{}/branches/{}?access_token={}".format(
248-
repo.organization, repo.name, branch, repo.access_token
243+
url = "https://api.github.com/repos/{}/{}/branches/{}".format(
244+
repo.organization, repo.name, branch
249245
)
250-
response = requests.get(url)
246+
response = repo.get(url)
251247

252248
if response.status_code != 200:
253249
raise RuntimeError(
@@ -266,10 +262,10 @@ def get_pr_statuses(pr: PullRequestDetails) -> List[Dict[str, Any]]:
266262
https://developer.github.com/v3/repos/statuses/#list-statuses-for-a-specific-ref
267263
"""
268264

269-
url = "https://api.github.com/repos/{}/{}/commits/{}/statuses?access_token={}".format(
270-
pr.repo.organization, pr.repo.name, pr.branch_sha, pr.repo.access_token
265+
url = "https://api.github.com/repos/{}/{}/commits/{}/statuses".format(
266+
pr.repo.organization, pr.repo.name, pr.branch_sha
271267
)
272-
response = requests.get(url)
268+
response = pr.repo.get(url)
273269

274270
if response.status_code != 200:
275271
raise RuntimeError(
@@ -288,10 +284,10 @@ def get_pr_check_status(pr: PullRequestDetails) -> Any:
288284
https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref
289285
"""
290286

291-
url = "https://api.github.com/repos/{}/{}/commits/{}/status?access_token={}".format(
292-
pr.repo.organization, pr.repo.name, pr.branch_sha, pr.repo.access_token
287+
url = "https://api.github.com/repos/{}/{}/commits/{}/status".format(
288+
pr.repo.organization, pr.repo.name, pr.branch_sha
293289
)
294-
response = requests.get(url)
290+
response = pr.repo.get(url)
295291

296292
if response.status_code != 200:
297293
raise RuntimeError(
@@ -356,9 +352,9 @@ def get_pr_review_status(pr: PullRequestDetails, per_page: int = 100) -> Any:
356352
url = (
357353
f"https://api.github.com/repos/{pr.repo.organization}/{pr.repo.name}"
358354
f"/pulls/{pr.pull_id}/reviews"
359-
f"?per_page={per_page}&access_token={pr.repo.access_token}"
355+
f"?per_page={per_page}"
360356
)
361-
response = requests.get(url)
357+
response = pr.repo.get(url)
362358

363359
if response.status_code != 200:
364360
raise RuntimeError(
@@ -378,9 +374,9 @@ def get_pr_checks(pr: PullRequestDetails) -> Dict[str, Any]:
378374
"""
379375
url = (
380376
f"https://api.github.com/repos/{pr.repo.organization}/{pr.repo.name}"
381-
f"/commits/{pr.branch_sha}/check-runs?per_page=100&access_token={pr.repo.access_token}"
377+
f"/commits/{pr.branch_sha}/check-runs?per_page=100"
382378
)
383-
response = requests.get(url, headers={'Accept': 'application/vnd.github.antiope-preview+json'})
379+
response = pr.repo.get(url, headers={'Accept': 'application/vnd.github.antiope-preview+json'})
384380

385381
if response.status_code != 200:
386382
raise RuntimeError(
@@ -439,10 +435,8 @@ def get_repo_ref(repo: GithubRepository, ref: str) -> Dict[str, Any]:
439435
https://developer.github.com/v3/git/refs/#get-a-reference
440436
"""
441437

442-
url = "https://api.github.com/repos/{}/{}/git/refs/{}?access_token={}".format(
443-
repo.organization, repo.name, ref, repo.access_token
444-
)
445-
response = requests.get(url)
438+
url = f"https://api.github.com/repos/{repo.organization}/{repo.name}/git/refs/{ref}"
439+
response = repo.get(url)
446440
if response.status_code != 200:
447441
raise RuntimeError(
448442
'Refs get failed. Code: {}. Content: {!r}.'.format(
@@ -466,10 +460,10 @@ def list_pr_comments(repo: GithubRepository, pull_id: int) -> List[Dict[str, Any
466460
References:
467461
https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue
468462
"""
469-
url = "https://api.github.com/repos/{}/{}/issues/{}/comments?access_token={}".format(
470-
repo.organization, repo.name, pull_id, repo.access_token
463+
url = "https://api.github.com/repos/{}/{}/issues/{}/comments".format(
464+
repo.organization, repo.name, pull_id
471465
)
472-
response = requests.get(url)
466+
response = repo.get(url)
473467
if response.status_code != 200:
474468
raise RuntimeError(
475469
'Comments get failed. Code: {}. Content: {!r}.'.format(
@@ -486,10 +480,10 @@ def delete_comment(repo: GithubRepository, comment_id: int) -> None:
486480
References:
487481
https://developer.github.com/v3/issues/comments/#delete-a-comment
488482
"""
489-
url = "https://api.github.com/repos/{}/{}/issues/comments/{}?access_token={}".format(
490-
repo.organization, repo.name, comment_id, repo.access_token
483+
url = "https://api.github.com/repos/{}/{}/issues/comments/{}".format(
484+
repo.organization, repo.name, comment_id
491485
)
492-
response = requests.delete(url)
486+
response = repo.delete(url)
493487
if response.status_code != 204:
494488
raise RuntimeError(
495489
'Comment delete failed. Code: {}. Content: {!r}.'.format(
@@ -512,12 +506,11 @@ def update_branch(pr: PullRequestDetails) -> Union[bool, CannotAutomergeError]:
512506
url = (
513507
f"https://api.github.com/repos/{pr.repo.organization}/{pr.repo.name}"
514508
f"/pulls/{pr.pull_id}/update-branch"
515-
f"?access_token={pr.repo.access_token}"
516509
)
517510
data = {
518511
'expected_head_sha': pr.branch_sha,
519512
}
520-
response = requests.put(
513+
response = pr.repo.put(
521514
url,
522515
json=data,
523516
# Opt into BETA feature.
@@ -546,15 +539,13 @@ def attempt_sync_with_master(pr: PullRequestDetails) -> Union[bool, CannotAutome
546539
"""
547540
master_sha = get_master_sha(pr.repo)
548541
remote = pr.remote_repo
549-
url = "https://api.github.com/repos/{}/{}/merges?access_token={}".format(
550-
remote.organization, remote.name, remote.access_token
551-
)
542+
url = f"https://api.github.com/repos/{remote.organization}/{remote.name}/merges"
552543
data = {
553544
'base': pr.branch_name,
554545
'head': master_sha,
555546
'commit_message': 'Update branch (automerge)',
556547
}
557-
response = requests.post(url, json=data)
548+
response = pr.remote_repo.post(url, json=data)
558549

559550
if response.status_code == 201:
560551
# Merge succeeded.
@@ -589,16 +580,16 @@ def attempt_squash_merge(pr: PullRequestDetails) -> Union[bool, CannotAutomergeE
589580
References:
590581
https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
591582
"""
592-
url = "https://api.github.com/repos/{}/{}/pulls/{}/merge?access_token={}".format(
593-
pr.repo.organization, pr.repo.name, pr.pull_id, pr.repo.access_token
583+
url = "https://api.github.com/repos/{}/{}/pulls/{}/merge".format(
584+
pr.repo.organization, pr.repo.name, pr.pull_id
594585
)
595586
data = {
596587
'commit_title': f'{pr.title} (#{pr.pull_id})',
597588
'commit_message': pr.body,
598589
'sha': pr.branch_sha,
599590
'merge_method': 'squash',
600591
}
601-
response = requests.put(url, json=data)
592+
response = pr.repo.put(url, json=data)
602593

603594
if response.status_code == 200:
604595
# Merge succeeded.
@@ -638,10 +629,10 @@ def auto_delete_pr_branch(pr: PullRequestDetails) -> bool:
638629
)
639630
return False
640631

641-
url = "https://api.github.com/repos/{}/{}/git/refs/heads/{}?access_token={}".format(
642-
remote.organization, remote.name, pr.branch_name, remote.access_token
632+
url = "https://api.github.com/repos/{}/{}/git/refs/heads/{}".format(
633+
remote.organization, remote.name, pr.branch_name
643634
)
644-
response = requests.delete(url)
635+
response = pr.repo.delete(url)
645636

646637
if response.status_code == 204:
647638
# Delete succeeded.
@@ -662,17 +653,15 @@ def branch_data_modified_recently(payload: Any) -> bool:
662653

663654
# TODO(#3388) Add summary line to docstring.
664655
# pylint: disable=docstring-first-line-empty
665-
def add_labels_to_pr(
666-
repo: GithubRepository, pull_id: int, *labels: str, override_token: str = None
667-
) -> None:
656+
def add_labels_to_pr(repo: GithubRepository, pull_id: int, *labels: str) -> None:
668657
"""
669658
References:
670659
https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue
671660
"""
672-
url = "https://api.github.com/repos/{}/{}/issues/{}/labels?access_token={}".format(
673-
repo.organization, repo.name, pull_id, override_token or repo.access_token
661+
url = "https://api.github.com/repos/{}/{}/issues/{}/labels".format(
662+
repo.organization, repo.name, pull_id
674663
)
675-
response = requests.post(url, json=list(labels))
664+
response = repo.post(url, json=list(labels))
676665

677666
if response.status_code != 200:
678667
raise RuntimeError(
@@ -688,10 +677,10 @@ def remove_label_from_pr(repo: GithubRepository, pull_id: int, label: str) -> bo
688677
References:
689678
https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
690679
"""
691-
url = "https://api.github.com/repos/{}/{}/issues/{}/labels/{}?access_token={}".format(
692-
repo.organization, repo.name, pull_id, label, repo.access_token
680+
url = "https://api.github.com/repos/{}/{}/issues/{}/labels/{}".format(
681+
repo.organization, repo.name, pull_id, label
693682
)
694-
response = requests.delete(url)
683+
response = repo.delete(url)
695684

696685
if response.status_code == 404:
697686
payload = json.JSONDecoder().decode(response.content.decode())
@@ -715,14 +704,14 @@ def list_open_pull_requests(
715704
) -> List[PullRequestDetails]:
716705
url = (
717706
f"https://api.github.com/repos/{repo.organization}/{repo.name}/pulls"
718-
f"?per_page={per_page};access_token={repo.access_token}"
707+
f"?per_page={per_page}"
719708
)
720709
data = {
721710
'state': 'open',
722711
}
723712
if base_branch is not None:
724713
data['base'] = base_branch
725-
response = requests.get(url, json=data)
714+
response = repo.get(url, json=data)
726715

727716
if response.status_code != 200:
728717
raise RuntimeError(

dev_tools/github_repository.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from typing import Optional
1616

17+
import requests
18+
1719

1820
class GithubRepository:
1921
"""Details how to access a repository on github."""
@@ -36,3 +38,25 @@ def __init__(self, organization: str, name: str, access_token: Optional[str]) ->
3638
def as_remote(self) -> str:
3739
"""Returns a string identifying the location of this repository."""
3840
return f'[email protected]:{self.organization}/{self.name}.git'
41+
42+
def delete(self, url, **kwargs):
43+
return requests.delete(url, **self._auth(kwargs))
44+
45+
def get(self, url, **kwargs):
46+
return requests.get(url, **self._auth(kwargs))
47+
48+
def put(self, url, **kwargs):
49+
return requests.put(url, **self._auth(kwargs))
50+
51+
def post(self, url, **kwargs):
52+
return requests.post(url, **self._auth(kwargs))
53+
54+
def patch(self, url, **kwargs):
55+
return requests.patch(url, **self._auth(kwargs))
56+
57+
def _auth(self, kwargs):
58+
new_kwargs = kwargs.copy()
59+
headers = kwargs.get('headers', {})
60+
headers.update({"Authorization": f"token {self.access_token}"})
61+
new_kwargs.update(headers=headers)
62+
return new_kwargs

0 commit comments

Comments
 (0)