skia2/experimental/tools/gerrit-change-id-to-number
Hal Canary b5680ca0d8 experimental/tools: minor code cleanup
Change-Id: I4d84dfed284aea9746808bf8a22f45e5bd9c519f
Reviewed-on: https://skia-review.googlesource.com/118597
Reviewed-by: Hal Canary <halcanary@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
2018-04-05 14:16:02 +00:00

43 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python2
# Copyright 2017 Google Inc.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import httplib
import json
import re
import subprocess
import sys
def retrieve_changeid(commit_or_branch):
b = subprocess.check_output(['git', 'log', '-1', '--format=%B', commit_or_branch])
r = re.compile(r'^Change-Id: (.*)$')
for l in b.split('\n'):
m = r.match(l)
if m:
return m.group(1)
return None
def gerrit_change_id_to_number(cid):
conn = httplib.HTTPSConnection('skia-review.googlesource.com')
conn.request('GET', '/changes/?q=change:%s' % cid)
r = conn.getresponse()
assert(r.status == 200)
x = r.read()
i = 0
while i < len(x) and x[i] != '[':
i += 1
return json.loads(x[i:])[0]['_number']
if __name__ == '__main__':
try:
if len(sys.argv) == 2 and len(sys.argv[1]) == 41 and sys.argv[1][0] == 'I':
gerrit_change_id_to_number(sys.argv[1])
else:
changeid = retrieve_changeid(sys.argv[1] if len(sys.argv) == 2 else 'HEAD')
if changeid is None:
exit(2)
sys.stdout.write('%d\n' % gerrit_change_id_to_number(changeid))
except:
exit(1)