summaryrefslogtreecommitdiff
path: root/gerrit/auto-submit/gerrit-autosubmit
blob: e2024fa69d307b279b62617a224c513f4c99a141 (plain)
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
#!/usr/bin/python3
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#

import json
import os
import subprocess
import time


def get_config(key):
    sock = subprocess.Popen(["git", "config", "-f", ".gitreview", key], stdout=subprocess.PIPE)
    ret = sock.stdout.readline().strip()
    sock.stdout.close()
    return ret.decode("utf-8")

server = get_config("gerrit.host")
port = get_config("gerrit.port")
project = get_config("gerrit.project")
branch = get_config("gerrit.defaultbranch")

sshcommand = ["ssh"]
if port != None and port != "":
    sshcommand.extend(["-p", port])

while True:
    cmd = sshcommand + ["-n", "-o", "ServerAliveInterval=5", server, "gerrit",
            "stream-events", "-s", "comment-added"]
    sock = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)

    try:
        for line in iter(sock.stdout.readline, b''):
            event = json.loads(line)

            if event['type'] != "comment-added":
                continue # should never happen
            if 'username' not in event['change']['owner'].keys():
                continue
            if event['change']['owner']['username'] != os.environ['USER']:
                continue
            if event['change']['project'] != project:
                continue
            if event['change']['branch'] != branch:
                continue
            if 'approvals' not in event.keys():
                continue
            if len([approval for approval in event['approvals'] if approval['type'] == "Verified" and approval['value'] == "1"]) != 1:
                continue

            rev = event['patchSet']['revision']
            cmd = sshcommand + [server, "gerrit", "review", "-s", rev]
            print(' '.join(cmd))
            subprocess.call(cmd)
    except KeyboardInterrupt:
        print("Interrupted.")
        break

    print("Socket closed, reconnecting.")
    time.sleep(1)

sock.communicate()

# vim:set shiftwidth=4 softtabstop=4 expandtab: