Page MenuHomePhabricator
Paste P8845

Draft of swat-summary script
ActivePublic

Authored by awight on Aug 1 2019, 2:01 PM.
Subscribers
None
Tokens
"Pterodactyl" token, awarded by brennen.
#!/usr/bin/env python3
"""
Given a Gerrit URL, build its scap summary in the SWAT deployment format.
Usage:
swat-summary <gerrit-url>
swat-summary -h | --help
Examples:
$ swat-summary https://gerrit.wikimedia.org/r/445240
SWAT: [[gerrit:445240|Update to use editRevId (T58849)]]
"""
import docopt
import json
import re
import requests
GERRIT_CHANGE_API = 'https://gerrit.wikimedia.org/r/changes/{gerrit_number}?o=CURRENT_REVISION&o=CURRENT_COMMIT'
GERRIT_RE = 'https://gerrit.wikimedia.org/r/(?:(?:#/)?c/[-+_/a-zA-Z0-9]+/)?(?P<gerrit_number>[0-9]+)/?'
PHAB_HEADER_RE = '(?m)^Bug: (?P<phab_task>T[0-9]+)$'
SUMMARY_MSG = 'SWAT: [[gerrit:{gerrit_number}|{commit_message} ({phabricator_task})]]'
def build_summary(gerrit_url):
matches = re.match(GERRIT_RE, gerrit_url)
if matches:
gerrit_number = matches.group('gerrit_number')
else:
raise RuntimeError("Couldn't extract gerrit change number from string " + gerrit_url)
change = get_gerrit(GERRIT_CHANGE_API, gerrit_number=gerrit_number)
revision_id = change['current_revision']
commit = change['revisions'][revision_id]['commit']
bugs = re.findall(PHAB_HEADER_RE, commit['message'])
return SUMMARY_MSG.format(
gerrit_number=gerrit_number,
commit_message=commit['subject'],
phabricator_task=', '.join(bugs))
def get_gerrit(api_format, **params):
url = api_format.format(**params)
r = requests.get(url)
if r.status_code != 200:
raise RuntimeError("Bad HTTP response from " + url)
return json.loads(
r.text.lstrip(")]}'"))
if __name__ == '__main__':
arguments = docopt.docopt(__doc__)
print(build_summary(arguments['<gerrit-url>']))

Event Timeline

awight created this paste.Aug 1 2019, 2:01 PM
awight edited the content of this paste. (Show Details)Aug 2 2019, 10:27 AM