Skip to content

Commit 407e72c

Browse files
authored
Merge pull request PowerDNS#17453 from rgacogne/changelog-from-pr-improvements
changelog-from-pr: Reuse HTTP connections, fix PR/issue confusion for tickets
2 parents 2dbefe1 + 64dd632 commit 407e72c

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

build-scripts/changelog-from-pr.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
import re
77
import getpass
88

9+
10+
def isIssue(session, number):
11+
try:
12+
res = session.get(f"https://api.github.com/repos/PowerDNS/pdns/issues/{number}")
13+
issue_info = res.json()
14+
# GitHub's REST API considers every pull request an issue, but not every issue is a pull request. For this reason, "Issues" endpoints may return both issues and pull requests in the response. You can identify pull requests by the pull_request key.
15+
return not "pull_request" in issue_info
16+
except (requests.exceptions.HTTPError, ValueError):
17+
return False
18+
19+
920
argp = argparse.ArgumentParser()
1021
argp.add_argument("--oneline", action="store_true", help="Make one-lined changelog entries (for 4.0 and older)")
1122
argp.add_argument(
@@ -20,25 +31,22 @@
2031

2132
out = ""
2233
httpAuth = None
34+
session = requests.Session()
2335
if arguments.username:
2436
password = getpass.getpass("GitHub password for '" + arguments.username + "': ")
25-
httpAuth = requests.auth.HTTPBasicAuth(arguments.username, password)
37+
session.auth(arguments.username, password)
2638

2739
# https://github.com/settings/tokens
2840
# A token with `repo` and `user` access will definitely work.
2941
access_token = arguments.access_token
42+
if access_token:
43+
session.headers.update({"Authorization": "token " + access_token})
3044

3145
for pr in sorted(arguments.pullrequest):
3246
if pr[0] == "#":
3347
pr = pr[1:]
3448
try:
35-
if access_token:
36-
res = requests.get(
37-
"https://api.github.com/repos/PowerDNS/pdns/pulls/{}".format(pr),
38-
headers={"Authorization": "token " + access_token},
39-
)
40-
else:
41-
res = requests.get("https://api.github.com/repos/PowerDNS/pdns/pulls/{}".format(pr), auth=httpAuth)
49+
res = session.get("https://api.github.com/repos/PowerDNS/pdns/pulls/{}".format(pr))
4250
pr_info = res.json()
4351
except (requests.exceptions.HTTPError, ValueError) as e:
4452
print(e)
@@ -54,7 +62,12 @@
5462
print("{}".format(pr_info["message"]))
5563
sys.exit(1)
5664
elif body:
57-
tickets = re.findall(ticket_regex, body)
65+
tickets = []
66+
candidateTicketNumbers = re.findall(ticket_regex, body)
67+
for ticket in candidateTicketNumbers:
68+
if isIssue(session, ticket):
69+
tickets.append(ticket)
70+
5871
if len(tickets):
5972
out += " :tickets: {}\n".format(", ".join(tickets))
6073
out += "\n {}".format(pr_info["title"][0].capitalize() + pr_info["title"][1:])
@@ -69,12 +82,7 @@
6982
"pieterlexis",
7083
]:
7184
try:
72-
if access_token:
73-
user_info = requests.get(
74-
pr_info["user"]["url"], headers={"Authorization": "token " + access_token}
75-
).json()
76-
else:
77-
user_info = requests.get(pr_info["user"]["url"], auth=httpAuth).json()
85+
user_info = session.get(pr_info["user"]["url"]).json()
7886
except (requests.exceptions.HTTPError, ValueError) as e:
7987
print(e)
8088
sys.exit(1)

0 commit comments

Comments
 (0)