Skip to content

Commit 8af0f2c

Browse files
sobolevnAA-Turner
authored andcommitted
Run patchcheck in GitHub Actions
1 parent 8ea4ad4 commit 8af0f2c

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

.github/workflows/lint.yml

+38
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,41 @@ jobs:
2424
with:
2525
python-version: "3.x"
2626
- uses: pre-commit/[email protected]
27+
28+
patchcheck:
29+
if: github.event_name == 'pull_request'
30+
runs-on: ubuntu-latest
31+
timeout-minutes: 10
32+
33+
steps:
34+
- uses: actions/checkout@v4
35+
with:
36+
# Check out the original HEAD of the PR branch.
37+
# Note that this value is cached by GitHub, so further commits to the
38+
# branch will not be reflected in this checkout.
39+
ref: ${{ github.event.pull_request.head.sha }}
40+
show-progress: false
41+
# Adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721
42+
- name: Fetch merge-base
43+
run: >
44+
git fetch
45+
origin
46+
+${{ github.event.pull_request.head.sha }}:origin/${{ github.event.pull_request.head.ref }}
47+
--depth=$(( ${{ github.event.pull_request.commits }} + 1 ))
48+
--no-tags
49+
--prune
50+
--no-recurse-submodules
51+
- name: Fetch base branch
52+
run: >
53+
git fetch
54+
origin
55+
+${{ github.event.pull_request.base.sha }}:origin/${{ github.event.pull_request.base.ref }}
56+
--depth=5
57+
--no-tags
58+
--prune
59+
--no-recurse-submodules
60+
- uses: actions/setup-python@v4
61+
with:
62+
python-version: "3.x"
63+
- name: "Run patchcheck.py"
64+
run: python ./Tools/patchcheck/patchcheck.py --ci true

Tools/patchcheck/patchcheck.py

+42-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ def call_fxn(*args, **kwargs):
4444
return decorated_fxn
4545

4646

47+
def get_patchlevel_version_info():
48+
patchlevel_h = os.path.join(SRCDIR, 'Include', 'patchlevel.h')
49+
50+
try:
51+
with open(patchlevel_h, encoding="utf-8") as f:
52+
patchlevel = f.read()
53+
except FileNotFoundError:
54+
return sys.version_info[:4]
55+
56+
d = {}
57+
skip = True
58+
for line in patchlevel.splitlines():
59+
if skip:
60+
if line == "/*--start constants--*/":
61+
skip = False
62+
continue
63+
64+
for name in ("MAJOR_VERSION", "MINOR_VERSION", "MICRO_VERSION",
65+
"RELEASE_LEVEL"):
66+
if line.startswith(define := f"#define PY_{name}"):
67+
d[name] = line.removeprefix(define).lstrip(" ")
68+
break
69+
70+
if len(d) == 4:
71+
break
72+
73+
major = int(d['MAJOR_VERSION'])
74+
minor = int(d['MINOR_VERSION'])
75+
micro = int(d['MICRO_VERSION'])
76+
level = {
77+
'PY_RELEASE_LEVEL_ALPHA': 'alpha',
78+
'PY_RELEASE_LEVEL_BETA': 'beta',
79+
'PY_RELEASE_LEVEL_GAMMA': 'rc',
80+
'PY_RELEASE_LEVEL_FINAL': 'final',
81+
}[d['RELEASE_LEVEL']]
82+
return major, minor, micro, level
83+
84+
4785
def get_git_branch():
4886
"""Get the symbolic name for the current git branch"""
4987
cmd = "git rev-parse --abbrev-ref HEAD".split()
@@ -102,11 +140,11 @@ def get_base_branch():
102140
# Not a git checkout, so there's no base branch
103141
return None
104142
upstream_remote = get_git_upstream_remote()
105-
version = sys.version_info
106-
if version.releaselevel == 'alpha':
143+
major, minor, micro, level = get_patchlevel_version_info()
144+
if level == 'alpha':
107145
base_branch = get_git_remote_default_branch(upstream_remote)
108146
else:
109-
base_branch = "{0.major}.{0.minor}".format(version)
147+
base_branch = f"{major}.{minor}"
110148
this_branch = get_git_branch()
111149
if this_branch is None or this_branch == base_branch:
112150
# Not on a git PR branch, so there's no base branch
@@ -320,6 +358,7 @@ def main():
320358
help='Perform pass/fail checks')
321359
args = parser.parse_args()
322360
if args.ci:
361+
SRCDIR = os.getcwd()
323362
ci(args.ci)
324363
else:
325364
main()

0 commit comments

Comments
 (0)