|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | + |
| 5 | +requestors = {"gvrose8192": ""} |
| 6 | + |
| 7 | +def file_prepend(file, str): |
| 8 | + with open(file, 'r') as fd: |
| 9 | + contents = fd.read() |
| 10 | + new_contents = str + contents |
| 11 | + |
| 12 | + # Overwrite file but now with prepended string on it |
| 13 | + with open(file, 'w') as fd: |
| 14 | + fd.write(new_contents) |
| 15 | + |
| 16 | +def process_git_request(fname, target_branch, source_branch, prj_dir): |
| 17 | + retcode = 200 # presume success |
| 18 | + # print(f"Opening file {fname}") |
| 19 | + file = open(fname, "w") |
| 20 | + working_dir = prj_dir |
| 21 | + # print(f"Working Dir : {working_dir}") |
| 22 | + os.chdir(working_dir) |
| 23 | + # print(f"pwd : {os.getcwd()}") |
| 24 | + git_cmd = f"git log --oneline --no-abbrev-commit origin/{target_branch}..origin/{source_branch}" |
| 25 | + # print(git_cmd) |
| 26 | + try: |
| 27 | + out, err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, |
| 28 | + stderr=subprocess.PIPE, text=True).communicate() |
| 29 | + if err: |
| 30 | + print(f"Command error output is {err}") |
| 31 | + file.write(f"Command error output is {err}") |
| 32 | + file.close() |
| 33 | + retcode = 201 |
| 34 | + return retcode |
| 35 | + |
| 36 | + output_lines = out.split() |
| 37 | + # we just want the commit sha IDs |
| 38 | + for x in output_lines: |
| 39 | + print(f"This is output_lines {x}") |
| 40 | + |
| 41 | + file.close() |
| 42 | + return retcode |
| 43 | + except Exception as e: |
| 44 | + print(f"Error executing git command: {str(e)}") |
| 45 | + file.close() |
| 46 | + return 201 |
| 47 | + |
| 48 | +first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv |
| 49 | + |
| 50 | +if len(argv_in) < 5: |
| 51 | + print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor") |
| 52 | + sys.exit() |
| 53 | + |
| 54 | +fname = str(first_arg) |
| 55 | +fname = "tmp-" + fname |
| 56 | +# print("filename is " + fname) |
| 57 | +target_branch = str(argv_in[0]) |
| 58 | +# print("target branch is " + target_branch) |
| 59 | +source_branch = str(argv_in[1]) |
| 60 | +# print("source branch is " + source_branch) |
| 61 | +prj_dir = str(argv_in[2]) |
| 62 | +# print("project dir is " + prj_dir) |
| 63 | +pullreq = str(argv_in[3]) |
| 64 | +# print("pull request is " + pullreq) |
| 65 | +requestor = str(argv_in[4]) |
| 66 | + |
| 67 | +retcode = process_git_request(fname, target_branch, source_branch, prj_dir) |
| 68 | + |
| 69 | +if retcode != 200: |
| 70 | + with open(fname, 'r') as fd: |
| 71 | + contents = fd.read() |
| 72 | + print(contents) |
| 73 | + sys.exit(1) |
| 74 | +else: |
| 75 | + print("Done") |
| 76 | + |
| 77 | +sys.exit(0) |
| 78 | + |
0 commit comments