|
6 | 6 | import re |
7 | 7 | import getpass |
8 | 8 |
|
| 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 | + |
9 | 20 | argp = argparse.ArgumentParser() |
10 | 21 | argp.add_argument("--oneline", action="store_true", help="Make one-lined changelog entries (for 4.0 and older)") |
11 | 22 | argp.add_argument( |
|
20 | 31 |
|
21 | 32 | out = "" |
22 | 33 | httpAuth = None |
| 34 | +session = requests.Session() |
23 | 35 | if arguments.username: |
24 | 36 | password = getpass.getpass("GitHub password for '" + arguments.username + "': ") |
25 | | - httpAuth = requests.auth.HTTPBasicAuth(arguments.username, password) |
| 37 | + session.auth(arguments.username, password) |
26 | 38 |
|
27 | 39 | # https://github.com/settings/tokens |
28 | 40 | # A token with `repo` and `user` access will definitely work. |
29 | 41 | access_token = arguments.access_token |
| 42 | +if access_token: |
| 43 | + session.headers.update({"Authorization": "token " + access_token}) |
30 | 44 |
|
31 | 45 | for pr in sorted(arguments.pullrequest): |
32 | 46 | if pr[0] == "#": |
33 | 47 | pr = pr[1:] |
34 | 48 | 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)) |
42 | 50 | pr_info = res.json() |
43 | 51 | except (requests.exceptions.HTTPError, ValueError) as e: |
44 | 52 | print(e) |
|
54 | 62 | print("{}".format(pr_info["message"])) |
55 | 63 | sys.exit(1) |
56 | 64 | 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 | + |
58 | 71 | if len(tickets): |
59 | 72 | out += " :tickets: {}\n".format(", ".join(tickets)) |
60 | 73 | out += "\n {}".format(pr_info["title"][0].capitalize() + pr_info["title"][1:]) |
|
69 | 82 | "pieterlexis", |
70 | 83 | ]: |
71 | 84 | 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() |
78 | 86 | except (requests.exceptions.HTTPError, ValueError) as e: |
79 | 87 | print(e) |
80 | 88 | sys.exit(1) |
|
0 commit comments