Skip to content

Commit bb459c8

Browse files
committed
Update CI files
1 parent 9898e22 commit bb459c8

File tree

2 files changed

+162
-2
lines changed

2 files changed

+162
-2
lines changed

.ci/scripts/skip_tests.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/env python3
2+
"""
3+
skip_tests.py - Check if only documentation files were changed in a git branch
4+
5+
Usage:
6+
./skip_tests.py <git_root> <reference_branch>
7+
8+
Arguments:
9+
git_root: The root directory of the git project
10+
reference_branch: The branch to compare against
11+
12+
Returns:
13+
0: Skip
14+
1: NoSkip
15+
*: Error
16+
"""
17+
18+
import sys
19+
import os
20+
import re
21+
import git
22+
import textwrap
23+
import argparse
24+
25+
DOC_PATTERNS = [
26+
r"^docs/",
27+
r"\.md$",
28+
r"\.txt$",
29+
r"LICENSE.*",
30+
r"CHANGELOG.*",
31+
r"CHANGES.*",
32+
r"CONTRIBUTING.*",
33+
]
34+
35+
# Exit codes
36+
CODE_SKIP = 0
37+
CODE_NO_SKIP = 1
38+
CODE_ERROR = 2
39+
40+
41+
def main() -> int:
42+
git_root, reference_branch = get_args()
43+
changed_files = get_changed_files(git_root, reference_branch)
44+
if not changed_files:
45+
return CODE_SKIP
46+
doc_files = [f for f in changed_files if is_doc_file(f)]
47+
not_doc_files = set(changed_files) - set(doc_files)
48+
print_changes(doc_files, not_doc_files)
49+
if not_doc_files:
50+
return CODE_NO_SKIP
51+
else:
52+
return CODE_SKIP
53+
54+
55+
# Utils
56+
57+
58+
def get_changed_files(git_root: str, reference_branch: str) -> list[str]:
59+
"""Get list of files changed between current branch and reference branch."""
60+
repo = git.Repo(git_root)
61+
diff_index = repo.git.diff("--name-only", reference_branch).strip()
62+
if not diff_index:
63+
return []
64+
return [f.strip() for f in diff_index.split("\n") if f.strip()]
65+
66+
67+
def is_doc_file(file_path: str) -> bool:
68+
"""Check if a file is a documentation file."""
69+
for pattern in DOC_PATTERNS:
70+
if re.search(pattern, file_path):
71+
return True
72+
return False
73+
74+
75+
def print_changes(doc_files: list[str], not_doc_files: list[str]) -> None:
76+
display_doc = " \n".join(doc_files)
77+
print(f"doc_files({len(doc_files)})")
78+
if doc_files:
79+
display_doc = "\n".join(doc_files)
80+
print(textwrap.indent(display_doc, " "))
81+
82+
print(f"non_doc_files({len(not_doc_files)})")
83+
if not_doc_files:
84+
display_non_doc = " \n".join(not_doc_files)
85+
print(textwrap.indent(display_non_doc, " "))
86+
87+
88+
def get_args() -> tuple[str, str]:
89+
"""Parse command line arguments and validate them."""
90+
parser = argparse.ArgumentParser(description="Check if CI can skip tests for a git branch")
91+
parser.add_argument("git_root", help="The root directory of the git project")
92+
parser.add_argument("reference_branch", help="The branch to compare against")
93+
args = parser.parse_args()
94+
git_root = os.path.abspath(args.git_root)
95+
ref_branch = args.reference_branch
96+
97+
if not os.path.exists(git_root):
98+
raise ValueError(f"Git root directory does not exist: {git_root}")
99+
if not os.path.isdir(git_root):
100+
raise ValueError(f"Git root is not a directory: {git_root}")
101+
try:
102+
git.Repo(git_root)
103+
except git.InvalidGitRepositoryError:
104+
raise ValueError(f"Directory is not a git repository: {git_root}")
105+
return git_root, ref_branch
106+
107+
108+
if __name__ == "__main__":
109+
try:
110+
sys.exit(main())
111+
except Exception as e:
112+
print(e)
113+
sys.exit(CODE_ERROR)

.github/workflows/ci.yml

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,49 @@ jobs:
4343
run: |
4444
.github/workflows/scripts/check_commit.sh
4545
46+
check-changes:
47+
runs-on: ubuntu-latest
48+
outputs:
49+
run_tests: ${{ steps.check.outputs.run_tests }}
50+
steps:
51+
- uses: "actions/checkout@v4"
52+
with:
53+
fetch-depth: 0
54+
path: "pulp_python"
55+
56+
- uses: "actions/setup-python@v5"
57+
with:
58+
python-version: "3.12"
59+
60+
- name: "Install python dependencies"
61+
run: |
62+
echo ::group::PYDEPS
63+
pip install gitpython
64+
echo ::endgroup::
65+
66+
- name: Analyze changed files
67+
shell: bash
68+
id: check
69+
run: |
70+
set +e
71+
BASE_REF=${{ github.event.pull_request.base.sha }}
72+
echo "Checking against:"
73+
git name-rev $BASE_REF
74+
python3 .ci/scripts/skip_tests.py . $BASE_REF
75+
exit_code=$?
76+
if [ $exit_code -ne 0 ] && [ $exit_code -ne 1 ]; then
77+
echo "Error: skip_tests.py returned unexpected exit code $exit_code"
78+
exit $exit_code
79+
fi
80+
echo "run_tests=$exit_code" >> $GITHUB_OUTPUT
81+
4682
docs:
4783
uses: "./.github/workflows/docs.yml"
4884

4985
lint:
86+
needs:
87+
- "check-changes"
88+
if: needs.check-changes.outputs.run_tests == '1'
5089
uses: "./.github/workflows/lint.yml"
5190

5291
build:
@@ -84,6 +123,7 @@ jobs:
84123
# This is a dummy dependent task to have a single entry for the branch protection rules.
85124
runs-on: "ubuntu-latest"
86125
needs:
126+
- "check-changes"
87127
- "check-commits"
88128
- "lint"
89129
- "test"
@@ -93,6 +133,13 @@ jobs:
93133
- name: "Collect needed jobs results"
94134
working-directory: "."
95135
run: |
96-
echo '${{toJson(needs)}}' | jq -r 'to_entries[]|select(.value.result!="success")|.key + ": " + .value.result'
97-
echo '${{toJson(needs)}}' | jq -e 'to_entries|map(select(.value.result!="success"))|length == 0'
136+
if [ ${{ needs.check-changes.outputs.run_tests }} == "1" ]; then
137+
# Full test run - check all jobs
138+
echo '${{toJson(needs)}}' | jq -r 'to_entries[]|select(.value.result!="success")|.key + ": " + .value.result'
139+
echo '${{toJson(needs)}}' | jq -e 'to_entries|map(select(.value.result!="success"))|length == 0'
140+
else
141+
# Docs-only run - check only required jobs (exclude lint and test)
142+
echo '${{toJson(needs)}}' | jq -r 'to_entries[]|select(.key != "lint" and .key != "test")|select(.value.result!="success")|.key + ": " + .value.result'
143+
echo '${{toJson(needs)}}' | jq -e 'to_entries|map(select(.key != "lint" and .key != "test"))|map(select(.value.result!="success"))|length == 0'
144+
fi
98145
echo "CI says: Looks good!"

0 commit comments

Comments
 (0)