-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathget-pr-base-sha
More file actions
executable file
·74 lines (65 loc) · 2.12 KB
/
get-pr-base-sha
File metadata and controls
executable file
·74 lines (65 loc) · 2.12 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
#!/usr/bin/env python
"""
Saves the commit sha of the requested merge-into branch of a PR
"""
import os
import sys
import argparse
from socket import setdefaulttimeout
from github import Github
from Mu2eCI import config
from Mu2eCI.logger import log
from Mu2eCI.common import api_rate_limits
setdefaulttimeout(120)
parser = argparse.ArgumentParser(
description="Process a pull request given a repository and PR number."
)
parser.add_argument(
"-r",
"--repo",
type=str,
help="The GitHub repository. Must be in the format e.g. Mu2e/Offline",
choices=config.main["supported_repos"],
)
parser.add_argument("-p", "--pr-id", type=int, help="The Pull Request ID.")
parser.add_argument(
"-f", "--filename", type=str, help="File to write the base commit sha to"
)
parser.add_argument(
"-j",
"--just-ref",
type=bool,
required=False,
help="If this arg is included at all, save the base ref name instead of base commit sha",
)
args = parser.parse_args()
if __name__ == "__main__":
gh = Github(login_or_token=os.environ["GITHUBTOKEN"], retry=3)
try:
api_rate_limits(gh)
repository = gh.get_repo(args.repo)
issue = repository.get_issue(args.pr_id)
if not issue.pull_request:
log.warning("Failure: Not a PR")
raise RuntimeError("not a PR")
pr = repository.get_pull(args.pr_id)
prBaseRef = pr.base.ref
baseSha = repository.get_branch(prBaseRef).commit.sha
log.info(
"{0} PR {1} base ref is {2}; the commit sha is {3}".format(
args.repo, args.pr_id, prBaseRef, baseSha
)
)
with open(args.filename, "w") as out_file:
if args.just_ref:
out_file.write(prBaseRef)
else:
out_file.write(baseSha)
except Exception as e:
exStr = None
if args.just_ref:
exStr = "Failed to get PR base branch name for {0} PR {1}: {2}"
else:
exStr = "Failed to get PR master commit sha for {0} PR {1}: {2}"
log.exception(exStr.format(args.repo, args.pr_id, str(e)))
sys.exit(1)