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