Page MenuHomePhabricator
Paste P8845

backport-summary script

Authored by awight on Aug 1 2019, 2:01 PM.
#!/usr/bin/env python3
Given a Gerrit URL, build its scap summary in the backport deployment format.
backport-summary <gerrit-url>
backport-summary -h | --help
$ backport-summary
Backport: [[gerrit:603482|Wrap WAN-cached PropertyInfoLookup with an APCu cache (T254536)]]
$ backport-summary
Config: [[gerrit:561355|Modify $wgArticleCount to 'any' for ta.wiktionary (T241684)]]
'Config: [[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_RE = '[-+_/a-zA-Z0-9]+/)?(?P<gerrit_number>[0-9]+)/?'
PHAB_HEADER_RE = '(?m)^Bug: (?P<phab_task>T[0-9]+)$'
SUMMARY_MSG = '{kind}: [[gerrit:{gerrit_number}|{commit_message} ({phabricator_task})]]'
def build_summary(gerrit_url):
matches = re.match(GERRIT_RE, gerrit_url)
if matches:
gerrit_number ='gerrit_number')
raise RuntimeError("Couldn't extract gerrit change number from string " + gerrit_url)
change = get_gerrit(GERRIT_CHANGE_API, gerrit_number=gerrit_number)
kind = 'Config' if change['project'] == 'operations/mediawiki-config' else 'Backport'
revision_id = change['current_revision']
commit = change['revisions'][revision_id]['commit']
bugs = re.findall(PHAB_HEADER_RE, commit['message'])
return SUMMARY_MSG.format(
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(
if __name__ == '__main__':
arguments = docopt.docopt(__doc__)
summary = build_summary(arguments['<gerrit-url>'])
quoted = shlex.quote(summary)
if quoted != summary and quoted != "'{}'".format(summary):
# correctly quoting this is not trivial, do it for the user

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)
Lucas_Werkmeister_WMDE changed the title of this paste from swat-summary script to backport-summary script.Mon, Jun 8, 5:25 PM
Lucas_Werkmeister_WMDE edited the content of this paste. (Show Details)