-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgerrit2github.py
executable file
·73 lines (64 loc) · 2.78 KB
/
gerrit2github.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/env python
# Gerrit is assumed to be accessible via the charmgit ssh alias and be set as the "origin" remote
# Github is assumed to be set as the "github" remote
# Should be run somewhere inside the project folder (so the git commands work)
import sys
import os
import subprocess
import json
import re
import unidecode
def run_command_status(*argv, **kwargs):
if len(argv) == 1:
# for python2 compatibility with shlex
if sys.version_info < (3,) and isinstance(argv[0], unicode):
argv = shlex.split(argv[0].encode('utf-8'))
else:
argv = shlex.split(str(argv[0]))
stdin = kwargs.pop('stdin', None)
newenv = os.environ.copy()
newenv['LANG'] = 'C'
newenv['LANGUAGE'] = 'C'
newenv.update(kwargs)
p = subprocess.Popen(argv,
stdin=subprocess.PIPE if stdin else None,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=newenv, universal_newlines=True)
(out, nothing) = p.communicate(stdin)
return (p.returncode, out.strip())
def parseGerritChangesJSON(data):
changes = []
for line in data.split("\n"):
if line[0] == "{":
try:
data = json.loads(line)
if "type" not in data:
changes.append(data)
except Exception:
print "Error parsing Gerrit change requests", line
sys.exit(-1)
return changes
if len(sys.argv) < 2:
print "Must specify project name"
sys.exit(1)
project_name = sys.argv[1]
query = "project:%s status:open" % project_name
(returnValue, output) = run_command_status("ssh", "-x", "charmgit",
"gerrit", "query",
"--format=JSON %s" % query)
changes = parseGerritChangesJSON(output)
print project_name, len(changes), "changes"
# Fetch the change requests from Gerrit and push them to Github
for change in changes:
author = re.sub('\W+', '_', unidecode.unidecode(change['owner']['name'])).lower()
branchName = "review/%s/%s" % (author, change['number'])
(returnValue, output) = run_command_status("ssh", "-x", "charmgit",
"gerrit", "query",
"--format=JSON", "--current-patch-set",
"change:%s" % change['number'])
changeInfo = parseGerritChangesJSON(output)
print branchName, changeInfo[0]["currentPatchSet"]["ref"]
run_command_status("git", "fetch", "origin", changeInfo[0]["currentPatchSet"]["ref"])
run_command_status("git", "checkout", "-b", branchName, "FETCH_HEAD")
run_command_status("git", "push", "github", branchName)