|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import os |
| 6 | +import re |
| 7 | + |
| 8 | +def file_prepend(file, str): |
| 9 | + with open(file, 'r') as fd: |
| 10 | + contents = fd.read() |
| 11 | + new_contents = str + contents |
| 12 | + |
| 13 | + # Overwrite file but now with prepended string on it |
| 14 | + with open(file, 'w') as fd: |
| 15 | + fd.write(new_contents) |
| 16 | + |
| 17 | +def process_git_request(fname, target_branch, source_branch, prj_dir): |
| 18 | + retcode = 0 # presume success |
| 19 | + file = open(fname, "w") |
| 20 | + working_dir = prj_dir |
| 21 | + os.chdir(working_dir) |
| 22 | + |
| 23 | + git_cmd = f"git log -1 --format=%H origin/{target_branch}" |
| 24 | + gitbr_out, gitbr_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 25 | + if gitbr_err: |
| 26 | + print(f"git log -1 returned error {gitbr_err}") |
| 27 | + else: |
| 28 | + gitbr_lines = gitbr_out.splitlines() |
| 29 | + for x in gitbr_lines: |
| 30 | + print(f"git log -1 output line {x}") |
| 31 | + local_target_branch = x |
| 32 | + |
| 33 | + git_cmd = f"git log -1 --format=%H origin/{source_branch}" |
| 34 | + gitbr_out, gitbr_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 35 | + if gitbr_err: |
| 36 | + print(f"git log -1 returned error {gitbr_err}") |
| 37 | + else: |
| 38 | + gitbr_lines = gitbr_out.splitlines() |
| 39 | + for x in gitbr_lines: |
| 40 | + print(f"git log -1 output line {x}") |
| 41 | + local_source_branch = x |
| 42 | + |
| 43 | + git_cmd = f"git log --oneline --no-abbrev-commit " + local_target_branch + ".." + local_source_branch + "-- ." |
| 44 | + print(f"git command is {git_cmd}") |
| 45 | + loglines_to_check = 13 |
| 46 | + try: |
| 47 | + out = subprocess.run(git_cmd, shell=True, capture_output=True, text=True, encoding='latin-1') |
| 48 | + if out.returncode: |
| 49 | + print(f"Command error output is {out}") |
| 50 | + file.write(f"Command error output is {out}") |
| 51 | + file.close() |
| 52 | + return 1 |
| 53 | + else: |
| 54 | + print(f"Git log line executed") |
| 55 | + |
| 56 | + line_count = len(str(out.stdout).splitlines()) |
| 57 | + print(f"Got {line_count} lines of git log output") |
| 58 | + if line_count > 1000: |
| 59 | + print(f"Huge Line count {line_count}") |
| 60 | + return 0 |
| 61 | + |
| 62 | + output_lines = out.stdout |
| 63 | + print(f"{output_lines}") |
| 64 | + return 0 |
| 65 | + commit_sha = "" |
| 66 | + # we just want the commit sha IDs |
| 67 | + for x in output_lines: |
| 68 | +# print(f"This is output_lines {x}") |
| 69 | + if not bool(re.search(r'[^\x30-\x39a-fA-F]', x)): # equivalent to Ruby's !x[/\H/] |
| 70 | + continue |
| 71 | + else: |
| 72 | + y = x.split() |
| 73 | + commit_sha = str(y[0]) |
| 74 | + print(f"Found a commit in line ", commit_sha) |
| 75 | + |
| 76 | + git_cmd = "git show " + commit_sha |
| 77 | + gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 78 | + |
| 79 | + loglines = gitlog_out.splitlines() |
| 80 | + lines_counted = 0 |
| 81 | + local_diffdiff_sha = commit_sha |
| 82 | + upstream_diffdiff_sha = "" |
| 83 | + upstream_diff = False |
| 84 | + |
| 85 | + for logline in loglines: |
| 86 | +# print(f"Processing logline {commit_sha}") |
| 87 | + lines_counted += 1 |
| 88 | + if lines_counted == 1: |
| 89 | + file.write("Merge Request sha: " + local_diffdiff_sha) |
| 90 | + file.write("\n") |
| 91 | + if lines_counted == 2: # email address |
| 92 | + if "ciq.com" not in logline.lower(): |
| 93 | + # Bad Author |
| 94 | + s = f"error:\nBad {logline}\n" |
| 95 | + print(s) |
| 96 | + file.write(s) |
| 97 | + file.close() |
| 98 | + return retcode |
| 99 | + if lines_counted > 1: |
| 100 | + if "jira" in logline.lower(): |
| 101 | + file.write("\t" + logline + "\n") |
| 102 | + |
| 103 | + if "upstream-diff" in logline.lower(): |
| 104 | + upstream_diff = True |
| 105 | + |
| 106 | + if "commit" in logline.lower(): |
| 107 | + commit_sha = re.search(r'[0-9a-f]{40}', logline) |
| 108 | + upstream_diffdiff_sha = str(commit_sha.group(0)) if commit_sha else "" |
| 109 | + print(f"Upstream : " + upstream_diffdiff_sha) |
| 110 | + if upstream_diffdiff_sha: |
| 111 | + file.write("\tUpstream sha: " + upstream_diffdiff_sha) |
| 112 | + file.write("\n") |
| 113 | + |
| 114 | + if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines |
| 115 | + # print(f"Breaking after {loglines_to_check} lines of commit checking") |
| 116 | + break |
| 117 | + |
| 118 | + if local_diffdiff_sha and upstream_diffdiff_sha: |
| 119 | + diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha |
| 120 | + # print("diffdiff: " + diff_cmd) |
| 121 | + process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True) |
| 122 | + diff_out = process.stdout |
| 123 | + diff_err = process.stderr |
| 124 | + diff_status = process.returncode |
| 125 | + |
| 126 | + if diff_status != 0 and not upstream_diff: |
| 127 | + print(f"diffdiff out: " + diff_out) |
| 128 | + print(f"diffdiff err: " + diff_err) |
| 129 | + retcode = 1 |
| 130 | + file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n") |
| 131 | + except Exception as error: |
| 132 | + print(f"Exception in git log command error {error}") |
| 133 | + |
| 134 | + finally: |
| 135 | + file.close() |
| 136 | + |
| 137 | + return retcode |
| 138 | + |
| 139 | +first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv |
| 140 | + |
| 141 | +if len(argv_in) < 5: |
| 142 | + print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor") |
| 143 | + sys.exit() |
| 144 | + |
| 145 | +fname = str(first_arg) |
| 146 | +fname = "tmp-" + fname |
| 147 | +# print("filename is " + fname) |
| 148 | +target_branch = str(argv_in[0]) |
| 149 | +# print("target branch is " + target_branch) |
| 150 | +source_branch = str(argv_in[1]) |
| 151 | +# print("source branch is " + source_branch) |
| 152 | +prj_dir = str(argv_in[2]) |
| 153 | +# print("project dir is " + prj_dir) |
| 154 | +pullreq = str(argv_in[3]) |
| 155 | +# print("pull request is " + pullreq) |
| 156 | +requestor = str(argv_in[4]) |
| 157 | + |
| 158 | +retcode = process_git_request(fname, target_branch, pullreq, prj_dir) |
| 159 | + |
| 160 | +if retcode != 0: |
| 161 | + with open(fname, 'r') as fd: |
| 162 | + contents = fd.read() |
| 163 | + print(contents) |
| 164 | + sys.exit(1) |
| 165 | +else: |
| 166 | + print("Done") |
| 167 | + |
| 168 | +sys.exit(0) |
| 169 | + |
0 commit comments