Skip to content

Commit d61ffe6

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

File tree

3 files changed

+192
-156
lines changed

3 files changed

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

.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)