Page MenuHomePhabricator
Paste P8845

swat-summary script
ActivePublic

Authored by awight on Aug 1 2019, 2:01 PM.
#!/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)]]
$ swat-summary https://gerrit.wikimedia.org/r/c/operations/mediawiki-config/+/561355
SWAT: [[gerrit:561355|Modify $wgArticleCount to 'any' for ta.wiktionary (T241684)]]
'SWAT: [[gerrit:561355|Modify $wgArticleCount to '"'"'any'"'"' for ta.wiktionary (T241684)]]'
If the summary contains shell special characters, it will be printed again,
suitably quoted this time (as in the second example).
Various formats of Gerrit URLs are accepted.
"""
import docopt
import json
import re
import requests
import shlex
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__)
summary = build_summary(arguments['<gerrit-url>'])
print(summary)
quoted = shlex.quote(summary)
if quoted != summary and quoted != "'{}'".format(summary):
# correctly quoting this is not trivial, do it for the user
print(quoted)

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
Lucas_Werkmeister_WMDE changed the title of this paste from Draft of swat-summary script to swat-summary script.Jan 7 2020, 2:22 PM
Lucas_Werkmeister_WMDE edited the content of this paste. (Show Details)