Skip to content

Commit 3017050

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

File tree

3 files changed

+182
-140
lines changed

3 files changed

+182
-140
lines changed
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
repo = git.Repo(".")
39+
commits = repo.iter_commits(f"{target_branch}..{source_branch}")
40+
loglines_to_check = 13
41+
for commit in commits:
42+
print(f"{commit.hexsha} {commit.message.splitlines()[0]}")
43+
44+
commit_sha = commit.hexsha
45+
46+
git_cmd = "git show " + commit_sha
47+
gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
48+
49+
loglines = gitlog_out.splitlines()
50+
lines_counted = 0
51+
local_diffdiff_sha = commit_sha
52+
upstream_diffdiff_sha = ""
53+
upstream_diff = False
54+
55+
for logline in loglines:
56+
# print(f"Processing logline {commit_sha}")
57+
lines_counted += 1
58+
if lines_counted == 1:
59+
file.write("Merge Request sha: " + local_diffdiff_sha)
60+
file.write("\n")
61+
if lines_counted == 2: # email address
62+
if "ciq.com" not in logline.lower():
63+
# Bad Author
64+
s = f"error:\nBad {logline}\n"
65+
print(s)
66+
file.write(s)
67+
file.close()
68+
return retcode
69+
if lines_counted > 1:
70+
if "jira" in logline.lower():
71+
file.write("\t" + logline + "\n")
72+
73+
if "upstream-diff" in logline.lower():
74+
upstream_diff = True
75+
76+
if "commit" in logline.lower():
77+
local_commit_sha = re.search(r'[0-9a-f]{40}', logline)
78+
upstream_diffdiff_sha = str(local_commit_sha.group(0)) if local_commit_sha else ""
79+
if upstream_diffdiff_sha:
80+
print(f"Upstream : " + upstream_diffdiff_sha)
81+
file.write("\tUpstream sha: " + upstream_diffdiff_sha)
82+
file.write("\n")
83+
84+
if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines
85+
# print(f"Breaking after {loglines_to_check} lines of commit checking")
86+
break
87+
88+
if local_diffdiff_sha and upstream_diffdiff_sha:
89+
diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha
90+
# print("diffdiff: " + diff_cmd)
91+
process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True)
92+
diff_out = process.stdout
93+
diff_err = process.stderr
94+
diff_status = process.returncode
95+
96+
if diff_status != 0 and not upstream_diff:
97+
print(f"diffdiff out: " + diff_out)
98+
print(f"diffdiff err: " + diff_err)
99+
retcode = 1
100+
file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n")
101+
102+
return retcode
103+
104+
first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv
105+
106+
if len(argv_in) < 5:
107+
print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor")
108+
sys.exit()
109+
110+
fname = str(first_arg)
111+
fname = "tmp-" + fname
112+
# print("filename is " + fname)
113+
target_branch = str(argv_in[0])
114+
# print("target branch is " + target_branch)
115+
source_branch = str(argv_in[1])
116+
# print("source branch is " + source_branch)
117+
prj_dir = str(argv_in[2])
118+
# print("project dir is " + prj_dir)
119+
pullreq = str(argv_in[3])
120+
# print("pull request is " + pullreq)
121+
requestor = str(argv_in[4])
122+
123+
retcode = process_git_request(fname, target_branch, source_branch, prj_dir)
124+
125+
if retcode != 0:
126+
with open(fname, 'r') as fd:
127+
contents = fd.read()
128+
print(contents)
129+
sys.exit(1)
130+
else:
131+
print("Done")
132+
133+
sys.exit(0)
134+

.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.ref }} ${{ github.workspace }} ${{ github.head_ref }} ${{ github.actor }}

0 commit comments

Comments
 (0)