Skip to content

Commit c928767

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

File tree

3 files changed

+162
-140
lines changed

3 files changed

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

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

Lines changed: 0 additions & 140 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
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 --fname ${{ github.run_id }} \
48+
--target-branch ${{ github.base_ref }} --source-branch ${{ github.head_ref }} --prj-dir ${{ github.workspace }}

0 commit comments

Comments
 (0)