Skip to content

Commit dd031f3

Browse files
authored
Merge pull request #222 from unthreaded/feature/GH-127-check-pr-labels
2 parents af9a341 + ac26c11 commit dd031f3

File tree

6 files changed

+95
-39
lines changed

6 files changed

+95
-39
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: "Setup Python"
2+
description: "Setup the correct version of python"
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
- name: Set up Python
8+
uses: actions/setup-python@v5
9+
with:
10+
python-version: 3.13

.github/workflows/build.yml

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ on:
1010

1111
jobs:
1212
build:
13-
1413
runs-on: ${{ matrix.os }}
1514
strategy:
1615
fail-fast: false
@@ -20,44 +19,38 @@ jobs:
2019

2120
steps:
2221
- uses: actions/checkout@v5
23-
- name: Set up Python
24-
uses: actions/setup-python@v5
25-
with:
26-
python-version: 3.13
22+
- uses: ./.github/actions/setup-python
23+
2724
- name: Install dependencies
28-
run: |
29-
python scripts/os_specific_requirements.py
30-
python -m pip install --upgrade pip
31-
pip install -r requirements.txt
32-
shell: bash
33-
- name: Prepare Scripts
34-
run: |
35-
chmod +x scripts/*
25+
run: pip install -r requirements.txt
26+
3627
- name: Lint
37-
run: |
38-
python scripts/lint.py
39-
shell: bash
28+
run: python scripts/lint.py
29+
4030
- name: Test
41-
run: |
42-
pytest --doctest-modules --cov=src/main/ --cov-report=xml src/test
31+
run: pytest --doctest-modules --cov=src/main/ --cov-report=xml src/test
32+
4333
- name: Package
44-
run: |
45-
python scripts/build_executable.py
34+
run: python scripts/build_executable.py
35+
4636
- uses: actions/upload-artifact@master
4737
with:
4838
name: ${{ runner.os }}
4939
path: ./dist/*
40+
5041
- name: Integration
51-
run: |
52-
pytest src/integration
42+
run: pytest src/integration
43+
5344
- name: Upload code coverage to codecov
5445
if: runner.os == 'Linux'
5546
env:
5647
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5748
run: bash <(curl -s https://codecov.io/bash) -t "$CODECOV_TOKEN" -f coverage.xml
49+
5850
- name: Zip binaries for release
5951
if: github.event_name == 'release'
6052
run: python scripts/zip_release.py
53+
6154
- name: Upload binaries to release
6255
if: github.event_name == 'release'
6356
uses: svenstaro/upload-release-action@v2
@@ -66,6 +59,7 @@ jobs:
6659
file_glob: true
6760
file: '*.zip'
6861
tag: ${{ github.ref }}
62+
6963
- uses: actions/setup-ruby@v1
7064
- name: Send Webhook Notification
7165
if: failure()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: PR Label Check
2+
3+
on:
4+
pull_request:
5+
types: [opened, labeled, unlabeled]
6+
7+
jobs:
8+
validate-labels:
9+
name: Validate release label
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: ./.github/actions/setup-python
14+
- name: Check PR has exactly one release label
15+
run: |
16+
python scripts/pr_label_check.py "$GITHUB_EVENT_PATH"
17+
shell: bash

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ Lastly, edit this config file to your liking:
6565
* Check your [core.hooksPath](https://git-scm.com/docs/githooks) - this setting can be `--global` or by repository. Check this first.
6666
* #### The hook is having a runtime error
6767
* Please submit an issue with the terminal output you received [here](https://github.com/unthreaded/git-hooks/issues) with as much detail as possible.
68-
69-
## Development details
70-
For Mac, you must install some dependencies via brew. Simply run [`os_specific_requirements.py`](scripts/os_specific_requirements.py) and you'll be good to go!
7168

7269
## Contributing
7370
[How to contribute](./CONTRIBUTING.md)

scripts/os_specific_requirements.py

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

scripts/pr_label_check.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Ensure a pull request has exactly one version label
3+
"""
4+
import os
5+
import sys
6+
import json
7+
8+
VALID_LABELS = {'Patch', 'Minor', 'Major', "no-release"}
9+
10+
def extract_labels_from_event(event_file: str) -> set[str]:
11+
"""Extract label names from a GitHub event JSON file.
12+
13+
Args:
14+
event_file: Path to the JSON event file produced by GitHub Actions.
15+
16+
Returns:
17+
A set of label names (strings) found on the pull request.
18+
"""
19+
with open(event_file, 'r', encoding='utf-8') as f:
20+
payload = json.load(f)
21+
labels = payload.get('pull_request', {}).get('labels', [])
22+
return {str(lbl.get('name', '')).strip() for lbl in labels}
23+
24+
25+
def are_labels_valid(labels_on_pr: set[str]) -> bool:
26+
"""Check whether the PR has exactly one allowed version label.
27+
28+
Args:
29+
labels_on_pr: Set of label names present on the pull request.
30+
31+
Returns:
32+
True if exactly one label from VALID_LABELS is present; otherwise False.
33+
"""
34+
print(f"Found labels on pull request: {labels_on_pr}")
35+
overlap = VALID_LABELS.intersection(labels_on_pr)
36+
return len(overlap) == 1
37+
38+
39+
if __name__ == "__main__":
40+
print("Checking pull request labels")
41+
event_path = sys.argv[1]
42+
success: bool = False
43+
if event_path and os.path.isfile(event_path):
44+
all_labels = extract_labels_from_event(event_path)
45+
success = are_labels_valid(all_labels)
46+
else:
47+
print("No labels found")
48+
49+
if not success:
50+
print(f"Did not find exactly one of the following labels: {VALID_LABELS}")
51+
52+
sys.exit(0 if success else 1)

0 commit comments

Comments
 (0)