Skip to content

Commit 46d03c3

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

File tree

3 files changed

+198
-156
lines changed

3 files changed

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

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