-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh_pr_update_description.py
executable file
·119 lines (94 loc) · 2.99 KB
/
gh_pr_update_description.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
#!/usr/bin/env python3
import re
import sys
from dataclasses import dataclass
try:
import click
import git
import github
except ImportError:
print("pip install click gitpython pygithub", file=sys.stderr)
@dataclass
class GitInfo:
title: str
message: str
apitoken: str
branch: str
repos: list
def get_commit_info(commitish):
repo = git.Repo("./")
# get commit title + message
commit = repo.commit(commitish)
title = commit.summary
message = github.GithubObject.NotSet
commitlines = commit.message.splitlines()
if len(commitlines) > 1:
message = commitlines[1:]
# strip leading and trailing blank lines
message = "\n".join(commitlines[1:]).strip("\n").rstrip("\n")
# get github api token
with repo.config_reader() as reader:
apitoken = reader.get_value("github", "apitoken", default="")
assert (
apitoken
), "Error: set a github apitoken like `git config github.apitoken <your token>`"
# get org + repo names from remote urls
repos = []
for remote in repo.remotes:
url = remote.url
m = re.match(r".*github\.com[:/](.*?)\/(.*?)(\.git)?$", url) # yolo...
assert m, "Error couldn't parse org+reponame from {}".format(url)
org, reponame, _ = m.groups()
repos.append("{}/{}".format(org, reponame))
return GitInfo(
title=title,
message=message,
apitoken=apitoken,
branch=repo.active_branch,
repos=repos,
)
def get_pr_for_branch(gitinfo):
g = github.Github(gitinfo.apitoken)
pulls = []
for reponame in gitinfo.repos:
repo = g.get_repo(reponame)
pulls.append(repo.get_pulls())
pr = None
for pull_list in pulls:
for pull in pull_list:
if pull.head.ref == str(gitinfo.branch):
pr = pull
assert pr, "Can't find matching pr for {}".format(gitinfo.branch)
return pr
def update_description(pr, title, message):
pr.edit(title=title, body=message)
@click.version_option()
@click.command()
@click.option("--yes", help="Skip prompt, just apply the change", is_flag=True)
@click.option(
"--commitish",
help="Specify commit-ish to use for description message",
default="HEAD",
show_default=True,
)
def main(yes, commitish):
"""Update GitHub PR description from current repo branch"""
# get git info
gitinfo = get_commit_info(commitish)
# try to find pr for this branch
pr = get_pr_for_branch(gitinfo)
# update description on pr
try:
if yes or click.confirm(
"About to update description on {}, ready?".format(
click.style(pr.html_url, fg="blue", bold=True)
),
default="Y",
show_default=True,
):
update_description(pr, gitinfo.title, gitinfo.message)
click.echo((click.style("PR successfully updated 🎉 !", fg="green")))
except click.exceptions.Abort:
pass
if __name__ == "__main__":
main()