Skip to content

Commit a53aa61

Browse files
committed
github actions: Use python PR check script
jira LE-2214 Obsoletes the old ruby PR check script
1 parent c053ab5 commit a53aa61

File tree

3 files changed

+169
-156
lines changed

3 files changed

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

.github/workflows/process-git-request.rb

Lines changed: 0 additions & 140 deletions
This file was deleted.

.github/workflows/process-pull-request.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,9 @@ jobs:
1818
test:
1919

2020
runs-on: ubuntu-latest
21-
strategy:
22-
matrix:
23-
ruby-version: ['3.0']
2421

2522
steps:
2623
- uses: actions/checkout@v4
27-
- name: Set up Ruby
28-
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
29-
# change this to (see https://github.com/ruby/setup-ruby#versioning):
30-
uses: ruby/setup-ruby@v1
31-
# uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
32-
with:
33-
ruby-version: ${{ matrix.ruby-version }}
34-
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
3524
- name: Set up Python
3625
uses: actions/setup-python@v5
3726
- name: Run tests
@@ -42,15 +31,15 @@ jobs:
4231
if ! git fetch origin ${{ github.head_ref }}; then
4332
echo "Unable to checkout ${{ github.head_ref }}"
4433
fi
45-
git remote add linux https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
46-
git fetch --shallow-since="3 years ago" linux
47-
echo "Will run process-git-request.rb with:"
34+
# git remote add linux https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
35+
# git fetch --shallow-since="3 years ago" linux
36+
echo "Will run process-git-request.py with:"
4837
echo "fname = ${{ github.run_id }}"
4938
echo "target_branch = ${{ github.base_ref }}"
5039
echo "source_branch = ${{ github.head_ref }}"
5140
echo "prj_dir = ${{ github.workspace }}"
5241
echo "pull_request = ${{ github.ref }}"
5342
echo "requestor = ${{ github.actor }}"
5443
cd ${{ github.workspace }}
55-
/usr/bin/ruby .github/workflows/process-git-request.rb ${{ github.run_id }} ${{ github.base_ref }} \
56-
${{ github.head_ref }} ${{ github.workspace }} ${{ github.ref }} ${{ github.actor }}
44+
/usr/bin/python3 .github/workflows/process-git-request.py ${{ github.run_id }} ${{ github.base_ref }} \
45+
${{ github.ref }} ${{ github.workspace }} ${{ github.head_ref }} ${{ github.actor }}

0 commit comments

Comments
 (0)