Skip to content

Instantly share code, notes, and snippets.

@renier
Created March 24, 2012 13:23
Show Gist options
  • Save renier/2182749 to your computer and use it in GitHub Desktop.
Save renier/2182749 to your computer and use it in GitHub Desktop.
Gerrit hook for auto-submission when enough reviews and verifications are detected.
#!/usr/bin/python
import json, sys
from subprocess import Popen, PIPE
change = sys.argv[2]
for x in range(len(sys.argv)):
if sys.argv[x] == "--change":
change = sys.argv[x+1]
break
username = "foo.bar"
server = "myhost.com"
results = Popen([
"ssh", "-p", "29418", username+"@"+server, "gerrit", "query",
"--format", "JSON", "--all-approvals", change], stdout=PIPE).communicate()[0]
parsed = json.loads(results.split("\n")[0])
verified_min = 1
reviewed_min = 2
verified_count = 0
reviewed_count = 0
commit = parsed["patchSets"][-1]["revision"]
if not parsed["patchSets"][-1].has_key("approvals"):
sys.exit(0)
for approval in parsed["patchSets"][-1]["approvals"]:
if approval["type"] == "VRIF":
verified_count += int(approval["value"])
elif approval["type"] == "CRVW":
reviewed_count += int(approval["value"])
if verified_count >= verified_min and reviewed_count >= reviewed_min:
results = Popen([
"ssh", "-p", "29418", username+"@"+server,
"gerrit", "review", "--submit", commit], stdout=PIPE).communicate()[0]:
@yakir-Yang
Copy link

Hi Renier, how can I integrate this script with Gerrit system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment