Skip to content

Commit

Permalink
Run patchcheck in GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored and AA-Turner committed Sep 16, 2023
1 parent 8ea4ad4 commit 8af0f2c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,41 @@ jobs:
with:
python-version: "3.x"
- uses: pre-commit/action@v3.0.0

patchcheck:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- uses: actions/checkout@v4
with:
# Check out the original HEAD of the PR branch.
# Note that this value is cached by GitHub, so further commits to the
# branch will not be reflected in this checkout.
ref: ${{ github.event.pull_request.head.sha }}
show-progress: false
# Adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721
- name: Fetch merge-base
run: >
git fetch
origin
+${{ github.event.pull_request.head.sha }}:origin/${{ github.event.pull_request.head.ref }}
--depth=$(( ${{ github.event.pull_request.commits }} + 1 ))
--no-tags
--prune
--no-recurse-submodules
- name: Fetch base branch
run: >
git fetch
origin
+${{ github.event.pull_request.base.sha }}:origin/${{ github.event.pull_request.base.ref }}
--depth=5
--no-tags
--prune
--no-recurse-submodules
- uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: "Run patchcheck.py"
run: python ./Tools/patchcheck/patchcheck.py --ci true
45 changes: 42 additions & 3 deletions Tools/patchcheck/patchcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,44 @@ def call_fxn(*args, **kwargs):
return decorated_fxn


def get_patchlevel_version_info():
patchlevel_h = os.path.join(SRCDIR, 'Include', 'patchlevel.h')

try:
with open(patchlevel_h, encoding="utf-8") as f:
patchlevel = f.read()
except FileNotFoundError:
return sys.version_info[:4]

d = {}
skip = True
for line in patchlevel.splitlines():
if skip:
if line == "/*--start constants--*/":
skip = False
continue

for name in ("MAJOR_VERSION", "MINOR_VERSION", "MICRO_VERSION",
"RELEASE_LEVEL"):
if line.startswith(define := f"#define PY_{name}"):
d[name] = line.removeprefix(define).lstrip(" ")
break

if len(d) == 4:
break

major = int(d['MAJOR_VERSION'])
minor = int(d['MINOR_VERSION'])
micro = int(d['MICRO_VERSION'])
level = {
'PY_RELEASE_LEVEL_ALPHA': 'alpha',
'PY_RELEASE_LEVEL_BETA': 'beta',
'PY_RELEASE_LEVEL_GAMMA': 'rc',
'PY_RELEASE_LEVEL_FINAL': 'final',
}[d['RELEASE_LEVEL']]
return major, minor, micro, level


def get_git_branch():
"""Get the symbolic name for the current git branch"""
cmd = "git rev-parse --abbrev-ref HEAD".split()
Expand Down Expand Up @@ -102,11 +140,11 @@ def get_base_branch():
# Not a git checkout, so there's no base branch
return None
upstream_remote = get_git_upstream_remote()
version = sys.version_info
if version.releaselevel == 'alpha':
major, minor, micro, level = get_patchlevel_version_info()
if level == 'alpha':
base_branch = get_git_remote_default_branch(upstream_remote)
else:
base_branch = "{0.major}.{0.minor}".format(version)
base_branch = f"{major}.{minor}"
this_branch = get_git_branch()
if this_branch is None or this_branch == base_branch:
# Not on a git PR branch, so there's no base branch
Expand Down Expand Up @@ -320,6 +358,7 @@ def main():
help='Perform pass/fail checks')
args = parser.parse_args()
if args.ci:
SRCDIR = os.getcwd()
ci(args.ci)
else:
main()

0 comments on commit 8af0f2c

Please sign in to comment.