Skip to content

github actions: Use python PR check script - LE-2214 #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions .github/workflows/process-git-request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python3

import sys
import subprocess
import os
import re
import git

def file_prepend(file, str):
with open(file, 'r') as fd:
contents = fd.read()
new_contents = str + contents

# Overwrite file but now with prepended string on it
with open(file, 'w') as fd:
fd.write(new_contents)

def process_git_request(fname, target_branch, source_branch, prj_dir):
retcode = 0 # presume success
file = open(fname, "w")
working_dir = prj_dir
os.chdir(working_dir)

repo = git.Repo(".")
commits = repo.iter_commits(f"{target_branch}..{source_branch}")
loglines_to_check = 13
for commit in commits:
print(f"{commit.hexsha} {commit.message.splitlines()[0]}")

commit_sha = commit.hexsha

git_cmd = "git show " + commit_sha
gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate()

loglines = gitlog_out.splitlines()
lines_counted = 0
local_diffdiff_sha = commit_sha
upstream_diffdiff_sha = ""
upstream_diff = False

for logline in loglines:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't touched this is a while but I'm going to start looking at it again for the curent tiger team I'm on,
What are your thoughts about leveraging this and putting this process-git-request.py into kernel-src-tree-tools and check that repo out along side kernel-src-tree
https://github.com/ctrliq/kernel-tools/blob/master/ciq_helpers.py#L12-L47

# print(f"Processing logline {commit_sha}")
lines_counted += 1
if lines_counted == 1:
file.write("Merge Request sha: " + local_diffdiff_sha)
file.write("\n")
if lines_counted == 2: # email address
if "ciq.com" not in logline.lower():
# Bad Author
s = f"error:\nBad {logline}\n"
print(s)
file.write(s)
file.close()
return retcode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think retcode is still 0 here (which is success I think), but in the ruby code a non-ciq email address was a failure.

BUT, we are going to have non-ciq authors now, right? so maybe we don't want this check at all?

Copy link
Author

@gvrose8192 gvrose8192 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that will need fixing. It was an assumption that is no longer true.
Edited to state clearly that yes, I'm just returning a pass code on a non-ciq email address.

if lines_counted > 1:
if "jira" in logline.lower():
file.write("\t" + logline + "\n")

if "upstream-diff" in logline.lower():
upstream_diff = True

if "commit" in logline.lower():
local_commit_sha = re.search(r'[0-9a-f]{40}', logline)
upstream_diffdiff_sha = str(local_commit_sha.group(0)) if local_commit_sha else ""
if upstream_diffdiff_sha:
print(f"Upstream : " + upstream_diffdiff_sha)
file.write("\tUpstream sha: " + upstream_diffdiff_sha)
file.write("\n")

if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines
# print(f"Breaking after {loglines_to_check} lines of commit checking")
break

if local_diffdiff_sha and upstream_diffdiff_sha:
diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha
# print("diffdiff: " + diff_cmd)
process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True)
diff_out = process.stdout
diff_err = process.stderr
diff_status = process.returncode

if diff_status != 0 and not upstream_diff:
print(f"diffdiff out: " + diff_out)
print(f"diffdiff err: " + diff_err)
retcode = 1
file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to close the file here before returning we should do the same for consistency ... even though the file is closed when the scope of the object is released
a9eeb8d#diff-675bae9c01428bfeaef67c1d6366e76b5eba0bb631b387036dd57c6ba35b97ddR53

return retcode

first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv

if len(argv_in) < 5:
print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor")
sys.exit()

fname = str(first_arg)
fname = "tmp-" + fname
# print("filename is " + fname)
target_branch = str(argv_in[0])
# print("target branch is " + target_branch)
source_branch = str(argv_in[1])
# print("source branch is " + source_branch)
prj_dir = str(argv_in[2])
# print("project dir is " + prj_dir)
pullreq = str(argv_in[3])
# print("pull request is " + pullreq)
requestor = str(argv_in[4])
Comment on lines +90 to +107
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very bash we could use argparse like so to make sure the switches have what they need.
https://github.com/ctrliq/kernel-src-tree-tools/blob/mainline/ciq-cherry-pick.py#L13-L21

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll get no argument from me. 😁 OK, let me fix that up.


retcode = process_git_request(fname, target_branch, source_branch, prj_dir)

if retcode != 0:
with open(fname, 'r') as fd:
contents = fd.read()
print(contents)
sys.exit(1)
else:
print("Done")

sys.exit(0)

140 changes: 0 additions & 140 deletions .github/workflows/process-git-request.rb

This file was deleted.

48 changes: 48 additions & 0 deletions .github/workflows/process-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Pull Request Checker

on:
pull_request:
branches:
- '**'
- '!mainline'

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
- name: Run tests
run: |
/usr/bin/pip3 install gitPython
python -c "import sys; import git; print(sys.version)"
rm -rf /home/runner/work/kernel-src-tree/kernel-src-tree
cd /home/runner/work/kernel-src-tree
git clone https://github.com/ctrliq/kernel-src-tree
cd kernel-src-tree
git fetch --all
git checkout -b ${{ github.head_ref }} origin/${{ github.head_ref }}
git checkout ${{ github.base_ref }}
git remote add linux https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
git fetch --shallow-since="2 years ago" linux
echo "Will run process-git-request.py with:"
echo "fname = ${{ github.run_id }}"
echo "target_branch = ${{ github.base_ref }}"
echo "source_branch = ${{ github.head_ref }}"
echo "prj_dir = ${{ github.workspace }}"
echo "pull_request = ${{ github.ref }}"
echo "requestor = ${{ github.actor }}"
cd ${{ github.workspace }}
/usr/bin/python3 .github/workflows/process-git-request.py ${{ github.run_id }} ${{ github.base_ref }} \
${{ github.head_ref }} ${{ github.workspace }} ${{ github.head_ref }} ${{ github.actor }}
Loading