|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import os |
| 6 | +import re |
| 7 | +import git |
| 8 | + |
| 9 | +def file_prepend(file, str): |
| 10 | + with open(file, 'r') as fd: |
| 11 | + contents = fd.read() |
| 12 | + new_contents = str + contents |
| 13 | + |
| 14 | + # Overwrite file but now with prepended string on it |
| 15 | + with open(file, 'w') as fd: |
| 16 | + fd.write(new_contents) |
| 17 | + |
| 18 | +def process_git_request(fname, target_branch, source_branch, prj_dir): |
| 19 | + retcode = 0 # presume success |
| 20 | + file = open(fname, "w") |
| 21 | + working_dir = prj_dir |
| 22 | + os.chdir(working_dir) |
| 23 | + |
| 24 | + repo = git.Repo(".") |
| 25 | + commits = repo.iter_commits(f"{target_branch}..{source_branch}") |
| 26 | + loglines_to_check = 13 |
| 27 | + for commit in commits: |
| 28 | + print(f"{commit.hexsha} {commit.message.splitlines()[0]}") |
| 29 | + |
| 30 | + commit_sha = commit.hexsha |
| 31 | + |
| 32 | + git_cmd = "git show " + commit_sha |
| 33 | + gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 34 | + |
| 35 | + loglines = gitlog_out.splitlines() |
| 36 | + lines_counted = 0 |
| 37 | + local_diffdiff_sha = commit_sha |
| 38 | + upstream_diffdiff_sha = "" |
| 39 | + upstream_diff = False |
| 40 | + |
| 41 | + for logline in loglines: |
| 42 | +# print(f"Processing logline {commit_sha}") |
| 43 | + lines_counted += 1 |
| 44 | + if lines_counted == 1: |
| 45 | + file.write("Merge Request sha: " + local_diffdiff_sha) |
| 46 | + file.write("\n") |
| 47 | + if lines_counted == 2: # email address |
| 48 | + if "ciq.com" not in logline.lower(): |
| 49 | + # Bad Author |
| 50 | + s = f"error:\nBad {logline}\n" |
| 51 | + print(s) |
| 52 | + file.write(s) |
| 53 | + file.close() |
| 54 | + return retcode |
| 55 | + if lines_counted > 1: |
| 56 | + if "jira" in logline.lower(): |
| 57 | + file.write("\t" + logline + "\n") |
| 58 | + |
| 59 | + if "upstream-diff" in logline.lower(): |
| 60 | + upstream_diff = True |
| 61 | + |
| 62 | + if "commit" in logline.lower(): |
| 63 | + local_commit_sha = re.search(r'[0-9a-f]{40}', logline) |
| 64 | + upstream_diffdiff_sha = str(local_commit_sha.group(0)) if local_commit_sha else "" |
| 65 | + if upstream_diffdiff_sha: |
| 66 | + print(f"Upstream : " + upstream_diffdiff_sha) |
| 67 | + file.write("\tUpstream sha: " + upstream_diffdiff_sha) |
| 68 | + file.write("\n") |
| 69 | + |
| 70 | + if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines |
| 71 | +# print(f"Breaking after {loglines_to_check} lines of commit checking") |
| 72 | + break |
| 73 | + |
| 74 | + if local_diffdiff_sha and upstream_diffdiff_sha: |
| 75 | + diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha |
| 76 | +# print("diffdiff: " + diff_cmd) |
| 77 | + process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True) |
| 78 | + diff_out = process.stdout |
| 79 | + diff_err = process.stderr |
| 80 | + diff_status = process.returncode |
| 81 | + |
| 82 | + if diff_status != 0 and not upstream_diff: |
| 83 | + print(f"diffdiff out: " + diff_out) |
| 84 | + print(f"diffdiff err: " + diff_err) |
| 85 | + retcode = 1 |
| 86 | + file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n") |
| 87 | + |
| 88 | + return retcode |
| 89 | + |
| 90 | +first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv |
| 91 | + |
| 92 | +if len(argv_in) < 5: |
| 93 | + print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor") |
| 94 | + sys.exit() |
| 95 | + |
| 96 | +fname = str(first_arg) |
| 97 | +fname = "tmp-" + fname |
| 98 | +# print("filename is " + fname) |
| 99 | +target_branch = str(argv_in[0]) |
| 100 | +# print("target branch is " + target_branch) |
| 101 | +source_branch = str(argv_in[1]) |
| 102 | +# print("source branch is " + source_branch) |
| 103 | +prj_dir = str(argv_in[2]) |
| 104 | +# print("project dir is " + prj_dir) |
| 105 | +pullreq = str(argv_in[3]) |
| 106 | +# print("pull request is " + pullreq) |
| 107 | +requestor = str(argv_in[4]) |
| 108 | + |
| 109 | +retcode = process_git_request(fname, target_branch, source_branch, prj_dir) |
| 110 | + |
| 111 | +if retcode != 0: |
| 112 | + with open(fname, 'r') as fd: |
| 113 | + contents = fd.read() |
| 114 | + print(contents) |
| 115 | + sys.exit(1) |
| 116 | +else: |
| 117 | + print("Done") |
| 118 | + |
| 119 | +sys.exit(0) |
| 120 | + |
0 commit comments