Skip to content

Commit 1a2311b

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

File tree

3 files changed

+184
-156
lines changed

3 files changed

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

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

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

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

Lines changed: 8 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
@@ -41,16 +30,19 @@ jobs:
4130
git fetch origin ${{ github.base_ref }}
4231
if ! git fetch origin ${{ github.head_ref }}; then
4332
echo "Unable to checkout ${{ github.head_ref }}"
33+
else
34+
git checkout -b ${{ github.head_ref }} origin/${{ github.head_ref }}
35+
git checkout ${{ github.base_ref }}
4436
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:"
37+
# git remote add linux https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
38+
# git fetch --shallow-since="3 years ago" linux
39+
echo "Will run process-git-request.py with:"
4840
echo "fname = ${{ github.run_id }}"
4941
echo "target_branch = ${{ github.base_ref }}"
5042
echo "source_branch = ${{ github.head_ref }}"
5143
echo "prj_dir = ${{ github.workspace }}"
5244
echo "pull_request = ${{ github.ref }}"
5345
echo "requestor = ${{ github.actor }}"
5446
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 }}
47+
/usr/bin/python3 .github/workflows/process-git-request.py ${{ github.run_id }} ${{ github.base_ref }} \
48+
${{ github.ref }} ${{ github.workspace }} ${{ github.head_ref }} ${{ github.actor }}

0 commit comments

Comments
 (0)