Skip to content

Commit a9eeb8d

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

File tree

3 files changed

+168
-140
lines changed

3 files changed

+168
-140
lines changed
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import subprocess
5+
import os
6+
import re
7+
import git
8+
9+
def file_prepend(file, str):
10+
with open(file, 'r') as fd:
11+
contents = fd.read()
12+
new_contents = str + contents
13+
14+
# Overwrite file but now with prepended string on it
15+
with open(file, 'w') as fd:
16+
fd.write(new_contents)
17+
18+
def process_git_request(fname, target_branch, source_branch, prj_dir):
19+
retcode = 0 # presume success
20+
file = open(fname, "w")
21+
working_dir = prj_dir
22+
os.chdir(working_dir)
23+
24+
repo = git.Repo(".")
25+
commits = repo.iter_commits(f"{target_branch}..{source_branch}")
26+
loglines_to_check = 13
27+
for commit in commits:
28+
print(f"{commit.hexsha} {commit.message.splitlines()[0]}")
29+
30+
commit_sha = commit.hexsha
31+
32+
git_cmd = "git show " + commit_sha
33+
gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
34+
35+
loglines = gitlog_out.splitlines()
36+
lines_counted = 0
37+
local_diffdiff_sha = commit_sha
38+
upstream_diffdiff_sha = ""
39+
upstream_diff = False
40+
41+
for logline in loglines:
42+
# print(f"Processing logline {commit_sha}")
43+
lines_counted += 1
44+
if lines_counted == 1:
45+
file.write("Merge Request sha: " + local_diffdiff_sha)
46+
file.write("\n")
47+
if lines_counted == 2: # email address
48+
if "ciq.com" not in logline.lower():
49+
# Bad Author
50+
s = f"error:\nBad {logline}\n"
51+
print(s)
52+
file.write(s)
53+
file.close()
54+
return retcode
55+
if lines_counted > 1:
56+
if "jira" in logline.lower():
57+
file.write("\t" + logline + "\n")
58+
59+
if "upstream-diff" in logline.lower():
60+
upstream_diff = True
61+
62+
if "commit" in logline.lower():
63+
local_commit_sha = re.search(r'[0-9a-f]{40}', logline)
64+
upstream_diffdiff_sha = str(local_commit_sha.group(0)) if local_commit_sha else ""
65+
if upstream_diffdiff_sha:
66+
print(f"Upstream : " + upstream_diffdiff_sha)
67+
file.write("\tUpstream sha: " + upstream_diffdiff_sha)
68+
file.write("\n")
69+
70+
if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines
71+
# print(f"Breaking after {loglines_to_check} lines of commit checking")
72+
break
73+
74+
if local_diffdiff_sha and upstream_diffdiff_sha:
75+
diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha
76+
# print("diffdiff: " + diff_cmd)
77+
process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True)
78+
diff_out = process.stdout
79+
diff_err = process.stderr
80+
diff_status = process.returncode
81+
82+
if diff_status != 0 and not upstream_diff:
83+
print(f"diffdiff out: " + diff_out)
84+
print(f"diffdiff err: " + diff_err)
85+
retcode = 1
86+
file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n")
87+
88+
return retcode
89+
90+
first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv
91+
92+
if len(argv_in) < 5:
93+
print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor")
94+
sys.exit()
95+
96+
fname = str(first_arg)
97+
fname = "tmp-" + fname
98+
# print("filename is " + fname)
99+
target_branch = str(argv_in[0])
100+
# print("target branch is " + target_branch)
101+
source_branch = str(argv_in[1])
102+
# print("source branch is " + source_branch)
103+
prj_dir = str(argv_in[2])
104+
# print("project dir is " + prj_dir)
105+
pullreq = str(argv_in[3])
106+
# print("pull request is " + pullreq)
107+
requestor = str(argv_in[4])
108+
109+
retcode = process_git_request(fname, target_branch, source_branch, prj_dir)
110+
111+
if retcode != 0:
112+
with open(fname, 'r') as fd:
113+
contents = fd.read()
114+
print(contents)
115+
sys.exit(1)
116+
else:
117+
print("Done")
118+
119+
sys.exit(0)
120+

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

-140
This file was deleted.
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
6+
name: Pull Request Checker
7+
8+
on:
9+
pull_request:
10+
branches:
11+
- '**'
12+
- '!mainline'
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
test:
19+
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
- name: Set up Python
25+
uses: actions/setup-python@v5
26+
- name: Run tests
27+
run: |
28+
/usr/bin/pip3 install gitPython
29+
python -c "import sys; import git; print(sys.version)"
30+
rm -rf /home/runner/work/kernel-src-tree/kernel-src-tree
31+
cd /home/runner/work/kernel-src-tree
32+
git clone https://github.com/ctrliq/kernel-src-tree
33+
cd kernel-src-tree
34+
git fetch --all
35+
git checkout -b ${{ github.head_ref }} origin/${{ github.head_ref }}
36+
git checkout ${{ github.base_ref }}
37+
git remote add linux https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
38+
git fetch --shallow-since="2 years ago" linux
39+
echo "Will run process-git-request.py with:"
40+
echo "fname = ${{ github.run_id }}"
41+
echo "target_branch = ${{ github.base_ref }}"
42+
echo "source_branch = ${{ github.head_ref }}"
43+
echo "prj_dir = ${{ github.workspace }}"
44+
echo "pull_request = ${{ github.ref }}"
45+
echo "requestor = ${{ github.actor }}"
46+
cd ${{ github.workspace }}
47+
/usr/bin/python3 .github/workflows/process-git-request.py ${{ github.run_id }} ${{ github.base_ref }} \
48+
${{ github.head_ref }} ${{ github.workspace }} ${{ github.head_ref }} ${{ github.actor }}

0 commit comments

Comments
 (0)