blob: bb018f9bcfcba11b5a051e297ac803afc12edfca [file] [log] [blame]
David Ostrovsky2b5fe092021-03-03 11:52:30 +01001#!/usr/bin/env python3
Chirayu Desai4c5ee482013-05-13 13:48:43 +05302
3from __future__ import print_function
4
5import subprocess
Brad Larsonc575f032010-06-08 17:24:37 -05006import getopt
7import sys
8
9
10SSH_USER = 'bot'
11SSH_HOST = 'localhost'
12SSH_PORT = 29418
Chad Horohoe69142322018-05-17 10:19:22 -070013SSH_COMMAND = 'ssh %s@%s -p %d gerrit approve ' % (SSH_USER,
14 SSH_HOST,
15 SSH_PORT)
Brad Larsonc575f032010-06-08 17:24:37 -050016FAILURE_SCORE = '--code-review=-2'
17FAILURE_MESSAGE = 'This commit message does not match the standard.' \
18 + ' Please correct the commit message and upload a replacement patch.'
19PASS_SCORE = '--code-review=0'
20PASS_MESSAGE = ''
21
Chad Horohoedd224702018-05-16 22:33:06 -040022
Brad Larsonc575f032010-06-08 17:24:37 -050023def main():
24 change = None
25 project = None
26 branch = None
27 commit = None
28 patchset = None
29
30 try:
Chad Horohoe69142322018-05-17 10:19:22 -070031 opts, _args = getopt.getopt(sys.argv[1:], '',
32 ['change=', 'project=', 'branch=',
33 'commit=', 'patchset='])
Chirayu Desai4c5ee482013-05-13 13:48:43 +053034 except getopt.GetoptError as err:
35 print('Error: %s' % (err))
Brad Larsonc575f032010-06-08 17:24:37 -050036 usage()
37 sys.exit(-1)
38
39 for arg, value in opts:
40 if arg == '--change':
41 change = value
42 elif arg == '--project':
43 project = value
44 elif arg == '--branch':
45 branch = value
46 elif arg == '--commit':
47 commit = value
48 elif arg == '--patchset':
49 patchset = value
50 else:
Chirayu Desai4c5ee482013-05-13 13:48:43 +053051 print('Error: option %s not recognized' % (arg))
Brad Larsonc575f032010-06-08 17:24:37 -050052 usage()
53 sys.exit(-1)
54
Chad Horohoe69142322018-05-17 10:19:22 -070055 if any(p is None for p in [change, project, branch, commit, patchset]):
Brad Larsonc575f032010-06-08 17:24:37 -050056 usage()
57 sys.exit(-1)
58
59 command = 'git cat-file commit %s' % (commit)
Chirayu Desai4c5ee482013-05-13 13:48:43 +053060 status, output = subprocess.getstatusoutput(command)
Brad Larsonc575f032010-06-08 17:24:37 -050061
62 if status != 0:
Chad Horohoe69142322018-05-17 10:19:22 -070063 print('Error running \'%s\'. status: %s, output:\n\n%s' %
Chad Horohoedd224702018-05-16 22:33:06 -040064 (command, status, output))
Brad Larsonc575f032010-06-08 17:24:37 -050065 sys.exit(-1)
66
67 commitMessage = output[(output.find('\n\n')+2):]
68 commitLines = commitMessage.split('\n')
69
70 if len(commitLines) > 1 and len(commitLines[1]) != 0:
Chad Horohoe69142322018-05-17 10:19:22 -070071 fail(commit, 'Invalid commit summary. The summary must be '
Chad Horohoedd224702018-05-16 22:33:06 -040072 + 'one line followed by a blank line.')
Brad Larsonc575f032010-06-08 17:24:37 -050073
74 i = 0
75 for line in commitLines:
76 i = i + 1
77 if len(line) > 80:
78 fail(commit, 'Line %d is over 80 characters.' % i)
79
80 passes(commit)
81
Chad Horohoedd224702018-05-16 22:33:06 -040082
Brad Larsonc575f032010-06-08 17:24:37 -050083def usage():
Chirayu Desai4c5ee482013-05-13 13:48:43 +053084 print('Usage:\n')
Chad Horohoe69142322018-05-17 10:19:22 -070085 print(sys.argv[0] + ' --change <change id> --project <project name> '
Chad Horohoedd224702018-05-16 22:33:06 -040086 + '--branch <branch> --commit <sha1> --patchset <patchset id>')
Brad Larsonc575f032010-06-08 17:24:37 -050087
Chad Horohoedd224702018-05-16 22:33:06 -040088
89def fail(commit, message):
Brad Larsonc575f032010-06-08 17:24:37 -050090 command = SSH_COMMAND + FAILURE_SCORE + ' -m \\\"' \
Chad Horohoedd224702018-05-16 22:33:06 -040091 + _shell_escape(FAILURE_MESSAGE + '\n\n' + message) \
Brad Larsonc575f032010-06-08 17:24:37 -050092 + '\\\" ' + commit
Chirayu Desai4c5ee482013-05-13 13:48:43 +053093 subprocess.getstatusoutput(command)
Brad Larsonc575f032010-06-08 17:24:37 -050094 sys.exit(1)
95
Chad Horohoedd224702018-05-16 22:33:06 -040096
97def passes(commit):
Brad Larsonc575f032010-06-08 17:24:37 -050098 command = SSH_COMMAND + PASS_SCORE + ' -m \\\"' \
99 + _shell_escape(PASS_MESSAGE) + ' \\\" ' + commit
Chirayu Desai4c5ee482013-05-13 13:48:43 +0530100 subprocess.getstatusoutput(command)
Brad Larsonc575f032010-06-08 17:24:37 -0500101
Chad Horohoedd224702018-05-16 22:33:06 -0400102
Brad Larsonc575f032010-06-08 17:24:37 -0500103def _shell_escape(x):
104 s = ''
105 for c in x:
106 if c in '\n':
107 s = s + '\\\"$\'\\n\'\\\"'
108 else:
109 s = s + c
110 return s
111
Chad Horohoedd224702018-05-16 22:33:06 -0400112
Brad Larsonc575f032010-06-08 17:24:37 -0500113if __name__ == '__main__':
114 main()