Skip to content

Commit 0e3f83e

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

File tree

3 files changed

+166
-156
lines changed

3 files changed

+166
-156
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import subprocess
6+
import os
7+
import re
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+
# print(f"Opening file {fname}")
21+
file = open(fname, "w")
22+
working_dir = prj_dir
23+
# print(f"Working Dir : {working_dir}")
24+
os.chdir(working_dir)
25+
os.environ['PYTHONIOENCODING'] = 'utf-8'
26+
# print(f"pwd : {os.getcwd()}")
27+
# git_cmd = f"git checkout {target_branch}"
28+
# gitbr_out, gitbr_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
29+
# if gitbr_err:
30+
# print(f"Could not check out target branch {target_branch} {gitbr_err}")
31+
# return 1
32+
git_cmd = f"git checkout -b {source_branch} origin/{source_branch}"
33+
gitbr_out, gitbr_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, encoding='utf-8').communicate()
34+
# if gitbr_err:
35+
# print(f"Could not check out source branch {source_branch} {gitbr_err}")
36+
# return 1
37+
git_cmd = f"git log --oneline --no-abbrev-commit " + target_branch + ".." + source_branch
38+
print(f"git command is {git_cmd}")
39+
loglines_to_check = 13
40+
try:
41+
out, err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE,
42+
stderr=subprocess.PIPE, universal_newlines=True, encoding='utf-8').communicate()
43+
if err:
44+
print(f"Command error output is {err}")
45+
file.write(f"Command error output is {err}")
46+
file.close()
47+
return 1
48+
else:
49+
print(f"Git log line executed")
50+
51+
output_lines = out.splitlines()
52+
commit_sha = ""
53+
# we just want the commit sha IDs
54+
for x in output_lines:
55+
# print(f"This is output_lines {x}")
56+
if not bool(re.search(r'[^\x30-\x39a-fA-F]', x)): # equivalent to Ruby's !x[/\H/]
57+
continue
58+
else:
59+
y = x.split()
60+
print(f"This is y {y}")
61+
commit_sha = str(y[0])
62+
# print(f"Found a commit in line ", commit_sha)
63+
64+
git_cmd = "git show " + commit_sha
65+
gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()
66+
67+
loglines = gitlog_out.splitlines()
68+
lines_counted = 0
69+
local_diffdiff_sha = commit_sha
70+
upstream_diffdiff_sha = ""
71+
upstream_diff = False
72+
73+
for logline in loglines:
74+
print(f"Processing logline {commit_sha}")
75+
lines_counted += 1
76+
if lines_counted == 1:
77+
file.write("Merge Request sha: " + local_diffdiff_sha)
78+
file.write("\n")
79+
if lines_counted == 2: # email address
80+
if "ciq.com" not in logline.lower():
81+
# Bad Author
82+
s = f"error:\nBad {logline}\n"
83+
print(s)
84+
file.write(s)
85+
file.close()
86+
return retcode
87+
if lines_counted > 1:
88+
if "jira" in logline.lower():
89+
file.write("\t" + logline + "\n")
90+
91+
if "upstream-diff" in logline.lower():
92+
upstream_diff = True
93+
94+
if "commit" in logline.lower():
95+
commit_sha = re.search(r'[0-9a-f]{40}', logline)
96+
upstream_diffdiff_sha = str(commit_sha.group(0)) if commit_sha else ""
97+
print(f"Upstream : " + upstream_diffdiff_sha)
98+
if upstream_diffdiff_sha:
99+
file.write("\tUpstream sha: " + upstream_diffdiff_sha)
100+
file.write("\n")
101+
102+
if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines
103+
# print(f"Breaking after {loglines_to_check} lines of commit checking")
104+
break
105+
106+
if local_diffdiff_sha and upstream_diffdiff_sha:
107+
diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha
108+
# print("diffdiff: " + diff_cmd)
109+
process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True)
110+
diff_out = process.stdout
111+
diff_err = process.stderr
112+
diff_status = process.returncode
113+
114+
if diff_status != 0 and not upstream_diff:
115+
print(f"diffdiff out: " + diff_out)
116+
print(f"diffdiff err: " + diff_err)
117+
retcode = 1
118+
file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n")
119+
except Exception as error:
120+
print(f"Exception in git log command error {error}")
121+
122+
finally:
123+
file.close()
124+
125+
return retcode
126+
127+
first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv
128+
129+
if len(argv_in) < 5:
130+
print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor")
131+
sys.exit()
132+
133+
fname = str(first_arg)
134+
fname = "tmp-" + fname
135+
# print("filename is " + fname)
136+
target_branch = str(argv_in[0])
137+
# print("target branch is " + target_branch)
138+
source_branch = str(argv_in[1])
139+
# print("source branch is " + source_branch)
140+
prj_dir = str(argv_in[2])
141+
# print("project dir is " + prj_dir)
142+
pullreq = str(argv_in[3])
143+
# print("pull request is " + pullreq)
144+
requestor = str(argv_in[4])
145+
146+
retcode = process_git_request(fname, target_branch, pullreq, prj_dir)
147+
148+
if retcode != 0:
149+
with open(fname, 'r') as fd:
150+
contents = fd.read()
151+
print(contents)
152+
sys.exit(1)
153+
else:
154+
print("Done")
155+
156+
sys.exit(0)
157+

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

Lines changed: 0 additions & 140 deletions
This file was deleted.

.github/workflows/process-pull-request.yml

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@ 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
26+
- name: Set Locale
27+
run: |
28+
sudo locale-gen en_US.UTF-8
29+
sudo update-locale LANG=en_US.UTF-8
3730
- name: Run tests
3831
run: |
3932
/usr/bin/pip3 install gitPython
@@ -42,15 +35,15 @@ jobs:
4235
if ! git fetch origin ${{ github.head_ref }}; then
4336
echo "Unable to checkout ${{ github.head_ref }}"
4437
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:"
38+
# git remote add linux https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
39+
# git fetch --shallow-since="3 years ago" linux
40+
echo "Will run process-git-request.py with:"
4841
echo "fname = ${{ github.run_id }}"
4942
echo "target_branch = ${{ github.base_ref }}"
5043
echo "source_branch = ${{ github.head_ref }}"
5144
echo "prj_dir = ${{ github.workspace }}"
5245
echo "pull_request = ${{ github.ref }}"
5346
echo "requestor = ${{ github.actor }}"
5447
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 }}
48+
/usr/bin/python3 -X utf-8 .github/workflows/process-git-request.py ${{ github.run_id }} ${{ github.base_ref }} \
49+
${{ github.ref }} ${{ github.workspace }} ${{ github.head_ref }} ${{ github.actor }}

0 commit comments

Comments
 (0)