|
| 1 | +require 'open3' |
| 2 | + |
| 3 | +requestors = { "gvrose8192" => "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" } |
| 4 | + |
| 5 | +def file_prepend(file, str) |
| 6 | + new_contents = "" |
| 7 | + File.open(file, 'r') do |fd| |
| 8 | + contents = fd.read |
| 9 | + new_contents = str << contents |
| 10 | + end |
| 11 | + # Overwrite file but now with prepended string on it |
| 12 | + File.open(file, 'w') do |fd| |
| 13 | + fd.write(new_contents) |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +def process_git_request(fname, target_branch, source_branch, prj_dir) |
| 18 | + retcode = 200 #presume success |
| 19 | +# puts "Opening file " + fname |
| 20 | + file = File.new(fname, "w") |
| 21 | + working_dir = prj_dir |
| 22 | +# puts "Working Dir : " + working_dir |
| 23 | + Dir.chdir working_dir |
| 24 | +# puts "pwd : " + Dir.pwd |
| 25 | + git_cmd = "git log --oneline --no-abbrev-commit origin/" + target_branch + ".." + "origin/" + source_branch |
| 26 | +# puts git_cmd |
| 27 | + out, err, status = Open3.capture3(git_cmd) |
| 28 | + if status.exitstatus != 0 |
| 29 | + puts "Command error output is " + err |
| 30 | + file.write("Command error output is " + err) |
| 31 | + file.close |
| 32 | + retcode = 201 |
| 33 | + return retcode |
| 34 | + end |
| 35 | + output_lines = out.split(' ') |
| 36 | +# we just want the commit sha IDs |
| 37 | + output_lines.each { |x| |
| 38 | +# puts "This is output_lines " + x |
| 39 | + upstream_diff = false |
| 40 | + if !x[/\H/] |
| 41 | + if x.length < 40 |
| 42 | + next |
| 43 | + end |
| 44 | + git_cmd = "git show " + x |
| 45 | + gitlog_out, gitlog_err, gitlog_status = Open3.capture3(git_cmd) |
| 46 | + if gitlog_status.exitstatus != 0 |
| 47 | + file.write("git show command error output is " + gitlog_err) |
| 48 | + retcode = 201 |
| 49 | + end |
| 50 | + loglines = gitlog_out.lines.map(&:chomp) |
| 51 | + lines_counted = 0 |
| 52 | + local_diffdiff_sha = "" |
| 53 | + upstream_diffdiff_sha = "" |
| 54 | + loglines.each { |logline| |
| 55 | + lines_counted = lines_counted + 1 |
| 56 | + if lines_counted == 1 |
| 57 | + local_commit_sha = logline.match("[0-9a-f]\{40\}") |
| 58 | + local_diffdiff_sha = local_commit_sha.to_s |
| 59 | +# puts "Local : " + local_diffdiff_sha |
| 60 | + file.write("Merge Request sha: " + local_diffdiff_sha) |
| 61 | + file.write("\n") |
| 62 | + end |
| 63 | + if lines_counted == 2 #email address |
| 64 | + if !logline.downcase.include? "ciq.com" |
| 65 | + # Bad Author |
| 66 | + s = "error:\nBad " + logline + "\n" |
| 67 | + puts s |
| 68 | + file.write(s) |
| 69 | + retcode = 201 |
| 70 | + else |
| 71 | + file.write("\t" + logline + "\n") |
| 72 | + end |
| 73 | + end |
| 74 | + if lines_counted > 1 |
| 75 | + if logline.downcase.include? "jira" |
| 76 | + file.write("\t" + logline + "\n") |
| 77 | + end |
| 78 | + if logline.downcase.include? "upstream-diff" |
| 79 | + upstream_diff = true |
| 80 | + end |
| 81 | + if logline.downcase.include? "commit" |
| 82 | + commit_sha = logline.match("[0-9a-f]\{40\}") |
| 83 | + upstream_diffdiff_sha = commit_sha.to_s |
| 84 | +# puts "Upstream : " + upstream_diffdiff_sha |
| 85 | + if (!upstream_diffdiff_sha.empty?) |
| 86 | + file.write("\tUpstream sha: " + upstream_diffdiff_sha) |
| 87 | + file.write("\n") |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + if lines_counted > 8 #Everything we need should be in the first 8 lines |
| 92 | + break |
| 93 | + end |
| 94 | + } |
| 95 | + if !local_diffdiff_sha.empty? && !upstream_diffdiff_sha.empty? |
| 96 | + diff_cmd = Dir.pwd + "/.github/workflows/diffdiff.py --colour --commit " + local_diffdiff_sha |
| 97 | + puts "diffdiff: " + diff_cmd |
| 98 | + diff_out, diff_err, diff_status = Open3.capture3(diff_cmd) |
| 99 | + if diff_status.exitstatus != 0 && !upstream_diff |
| 100 | + puts "diffdiff out: " + diff_out |
| 101 | + puts "diffdiff err: " + diff_err |
| 102 | + retcode = 201 |
| 103 | + file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n") |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + } |
| 108 | + file.close |
| 109 | + return retcode |
| 110 | +end |
| 111 | + |
| 112 | +first_arg, *argv_in = ARGV |
| 113 | +if argv_in.length < 5 |
| 114 | + puts "Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor" |
| 115 | + exit |
| 116 | +end |
| 117 | +fname = first_arg.to_s |
| 118 | +fname = "tmp-" + fname |
| 119 | +# puts "filename is " + fname |
| 120 | +target_branch = argv_in[0].to_s |
| 121 | +# puts "target branch is " + target_branch |
| 122 | +source_branch = argv_in[1].to_s |
| 123 | +# puts "source branch is " + source_branch |
| 124 | +prj_dir = argv_in[2].to_s |
| 125 | +# puts "project dir is " + prj_dir |
| 126 | +pullreq = argv_in[3].to_s |
| 127 | +# puts "pull request is " + pullreq |
| 128 | +requestor = argv_in[4].to_s |
| 129 | +retcode = process_git_request(fname, target_branch, source_branch, prj_dir) |
| 130 | +if retcode != 200 |
| 131 | + File.open(fname, 'r') do |fd| |
| 132 | + contents = fd.read |
| 133 | + puts contents |
| 134 | + end |
| 135 | + exit(1) |
| 136 | +else |
| 137 | + puts "Done" |
| 138 | +end |
| 139 | +exit(0) |
| 140 | + |
0 commit comments