Skip to content

Commit bd231ef

Browse files
committed
GH-127: Add PR label check
1 parent 85bc9cf commit bd231ef

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
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: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ jobs:
2020

2121
steps:
2222
- uses: actions/checkout@v5
23-
- name: Set up Python
24-
uses: actions/setup-python@v5
25-
with:
26-
python-version: 3.13
23+
- uses: ./.github/actions/setup-python
2724
- name: Install dependencies
2825
run: |
2926
python scripts/os_specific_requirements.py
3027
python -m pip install --upgrade pip
3128
pip install -r requirements.txt
3229
shell: bash
33-
- name: Prepare Scripts
34-
run: |
35-
chmod +x scripts/*
3630
- name: Lint
3731
run: |
3832
python scripts/lint.py
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

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)