Skip to content

Commit d19b8df

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

File tree

3 files changed

+194
-156
lines changed

3 files changed

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

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

-140
This file was deleted.

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

+8-16
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)