David Ostrovsky | 2b5fe09 | 2021-03-03 11:52:30 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 2 | |
| 3 | from __future__ import print_function |
| 4 | |
| 5 | import subprocess |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 6 | import getopt |
| 7 | import sys |
| 8 | |
| 9 | |
| 10 | SSH_USER = 'bot' |
| 11 | SSH_HOST = 'localhost' |
| 12 | SSH_PORT = 29418 |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 13 | SSH_COMMAND = 'ssh %s@%s -p %d gerrit approve ' % (SSH_USER, |
| 14 | SSH_HOST, |
| 15 | SSH_PORT) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 16 | FAILURE_SCORE = '--code-review=-2' |
| 17 | FAILURE_MESSAGE = 'This commit message does not match the standard.' \ |
| 18 | + ' Please correct the commit message and upload a replacement patch.' |
| 19 | PASS_SCORE = '--code-review=0' |
| 20 | PASS_MESSAGE = '' |
| 21 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 22 | |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 23 | def main(): |
| 24 | change = None |
| 25 | project = None |
| 26 | branch = None |
| 27 | commit = None |
| 28 | patchset = None |
| 29 | |
| 30 | try: |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 31 | opts, _args = getopt.getopt(sys.argv[1:], '', |
| 32 | ['change=', 'project=', 'branch=', |
| 33 | 'commit=', 'patchset=']) |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 34 | except getopt.GetoptError as err: |
| 35 | print('Error: %s' % (err)) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 36 | 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 Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 51 | print('Error: option %s not recognized' % (arg)) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 52 | usage() |
| 53 | sys.exit(-1) |
| 54 | |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 55 | if any(p is None for p in [change, project, branch, commit, patchset]): |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 56 | usage() |
| 57 | sys.exit(-1) |
| 58 | |
| 59 | command = 'git cat-file commit %s' % (commit) |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 60 | status, output = subprocess.getstatusoutput(command) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 61 | |
| 62 | if status != 0: |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 63 | print('Error running \'%s\'. status: %s, output:\n\n%s' % |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 64 | (command, status, output)) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 65 | 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 Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 71 | fail(commit, 'Invalid commit summary. The summary must be ' |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 72 | + 'one line followed by a blank line.') |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 73 | |
| 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 Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 82 | |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 83 | def usage(): |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 84 | print('Usage:\n') |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 85 | print(sys.argv[0] + ' --change <change id> --project <project name> ' |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 86 | + '--branch <branch> --commit <sha1> --patchset <patchset id>') |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 87 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 88 | |
| 89 | def fail(commit, message): |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 90 | command = SSH_COMMAND + FAILURE_SCORE + ' -m \\\"' \ |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 91 | + _shell_escape(FAILURE_MESSAGE + '\n\n' + message) \ |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 92 | + '\\\" ' + commit |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 93 | subprocess.getstatusoutput(command) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 94 | sys.exit(1) |
| 95 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 96 | |
| 97 | def passes(commit): |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 98 | command = SSH_COMMAND + PASS_SCORE + ' -m \\\"' \ |
| 99 | + _shell_escape(PASS_MESSAGE) + ' \\\" ' + commit |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 100 | subprocess.getstatusoutput(command) |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 101 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 102 | |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 103 | def _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 Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 112 | |
Brad Larson | c575f03 | 2010-06-08 17:24:37 -0500 | [diff] [blame] | 113 | if __name__ == '__main__': |
| 114 | main() |