]> git.ipfire.org Git - thirdparty/pdns.git/blame - build-scripts/changelog-from-pr.py
Do not use github deprecated API for changelog script
[thirdparty/pdns.git] / build-scripts / changelog-from-pr.py
CommitLineData
d98a21e6
PL
1#!/usr/bin/env python3
2
3import requests
4import sys
5import argparse
6import re
99bd1729 7import getpass
d98a21e6
PL
8
9argp = argparse.ArgumentParser()
10argp.add_argument('--oneline', action='store_true',
11 help='Make one-lined changelog entries (for 4.0 and older)')
99bd1729 12argp.add_argument('--username',
eb6af1ad 13 help='Use the specified username for Basic Authentication to the GitHub API, allowing a higher rate limit')
14argp.add_argument('--access_token',
15 help='Use API access token instead of username & password combination')
d98a21e6
PL
16argp.add_argument('pullrequest', metavar='PULL_REQUEST', nargs='+',
17 help='Make changelogs for these Pull Request #\'s')
18arguments = argp.parse_args()
19
20ticket_regex = re.compile(r'(?:[Cc]loses|[Ff]ixes)? #(\d+)')
21
22out = ''
99bd1729
RG
23httpAuth = None
24if arguments.username:
25 password = getpass.getpass("GitHub password for '" + arguments.username + "': ")
26 httpAuth = requests.auth.HTTPBasicAuth(arguments.username, password)
27
eb6af1ad 28# https://github.com/settings/tokens
29# A token with `repo` and `user` access will definitely work.
30access_token = arguments.access_token
31
d98a21e6
PL
32for pr in arguments.pullrequest:
33 if pr[0] == '#':
34 pr = pr[1:]
35 try:
eb6af1ad 36 if access_token:
37 res = requests.get('https://api.github.com/repos/PowerDNS/pdns/pulls/'
7789db70
OM
38 '{}'.format(pr),
39 headers={'Authorization': 'token ' + access_token})
eb6af1ad 40 else:
41 res = requests.get('https://api.github.com/repos/PowerDNS/pdns/pulls/'
42 '{}'.format(pr), auth=httpAuth)
d98a21e6
PL
43 pr_info = res.json()
44 except (requests.exceptions.HTTPError, ValueError) as e:
45 print(e)
46 sys.exit(1)
47
48 if arguments.oneline:
49 out += '- `#{pr} <{url}>`__: {title}'.format(
50 pr=pr, url=pr_info['html_url'], title=pr_info['title']
51 )
52 else:
53 out += ' .. change::\n' + \
8e7a1365
PL
54 ' :tags: XXXXXX\n' + \
55 ' :pullreq: {}\n'.format(pr)
eb6af1ad 56 body = pr_info.get('body', None)
57 if pr_info.get('message', None) and not body:
58 # A bit blunt but better than we had.
59 print('{}'.format(pr_info['message']))
60 sys.exit(1)
61 elif body:
62 tickets = re.findall(ticket_regex, body)
63 if len(tickets):
64 out += ' :tickets: {}\n'.format(', '.join(tickets))
11dbb976 65 out += '\n {}'.format(pr_info['title'][0].capitalize() + pr_info['title'][1:])
d98a21e6
PL
66
67 if pr_info['user']['login'].lower() not in ['ahupowerdns', 'habbie',
68 'pieterlexis', 'rgacogne',
1dcf6da7
RG
69 'aerique', 'chbruyand',
70 'omoerbeek']:
d98a21e6 71 try:
eb6af1ad 72 if access_token:
7789db70
OM
73 user_info = requests.get(pr_info['user']['url'],
74 headers={'Authorization': 'token ' + access_token}).json()
eb6af1ad 75 else:
76 user_info = requests.get(pr_info['user']['url'], auth=httpAuth).json()
d98a21e6
PL
77 except (requests.exceptions.HTTPError, ValueError) as e:
78 print(e)
79 sys.exit(1)
ef2ea4bf 80 if 'name' in user_info:
5bc65c08
RG
81 out += ' ({})'.format(user_info['name'])
82 else:
83 out += ' (@{})'.format(user_info['login'])
d98a21e6
PL
84 out += '\n'
85
86 if not arguments.oneline:
87 out += '\n'
88
89print(out)