Skip to content

Commit d7bc233

Browse files
DN6sayakpaul
andauthored
[CI] Add PR/Issue Auto Labeler (#13380)
* update * update * update * update * update * update * update * update * Apply suggestion from @sayakpaul Co-authored-by: Sayak Paul <spsayakpaul@gmail.com> --------- Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
1 parent 9884ed2 commit d7bc233

File tree

5 files changed

+405
-0
lines changed

5 files changed

+405
-0
lines changed

.github/labeler.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# https://github.com/actions/labeler
2+
pipelines:
3+
- changed-files:
4+
- any-glob-to-any-file:
5+
- src/diffusers/pipelines/**
6+
7+
models:
8+
- changed-files:
9+
- any-glob-to-any-file:
10+
- src/diffusers/models/**
11+
12+
schedulers:
13+
- changed-files:
14+
- any-glob-to-any-file:
15+
- src/diffusers/schedulers/**
16+
17+
single-file:
18+
- changed-files:
19+
- any-glob-to-any-file:
20+
- src/diffusers/loaders/single_file.py
21+
- src/diffusers/loaders/single_file_model.py
22+
- src/diffusers/loaders/single_file_utils.py
23+
24+
ip-adapter:
25+
- changed-files:
26+
- any-glob-to-any-file:
27+
- src/diffusers/loaders/ip_adapter.py
28+
29+
lora:
30+
- changed-files:
31+
- any-glob-to-any-file:
32+
- src/diffusers/loaders/lora_base.py
33+
- src/diffusers/loaders/lora_conversion_utils.py
34+
- src/diffusers/loaders/lora_pipeline.py
35+
- src/diffusers/loaders/peft.py
36+
37+
loaders:
38+
- changed-files:
39+
- any-glob-to-any-file:
40+
- src/diffusers/loaders/textual_inversion.py
41+
- src/diffusers/loaders/transformer_flux.py
42+
- src/diffusers/loaders/transformer_sd3.py
43+
- src/diffusers/loaders/unet.py
44+
- src/diffusers/loaders/unet_loader_utils.py
45+
- src/diffusers/loaders/utils.py
46+
- src/diffusers/loaders/__init__.py
47+
48+
quantization:
49+
- changed-files:
50+
- any-glob-to-any-file:
51+
- src/diffusers/quantizers/**
52+
53+
hooks:
54+
- changed-files:
55+
- any-glob-to-any-file:
56+
- src/diffusers/hooks/**
57+
58+
guiders:
59+
- changed-files:
60+
- any-glob-to-any-file:
61+
- src/diffusers/guiders/**
62+
63+
modular-pipelines:
64+
- changed-files:
65+
- any-glob-to-any-file:
66+
- src/diffusers/modular_pipelines/**
67+
68+
experimental:
69+
- changed-files:
70+
- any-glob-to-any-file:
71+
- src/diffusers/experimental/**
72+
73+
documentation:
74+
- changed-files:
75+
- any-glob-to-any-file:
76+
- docs/**
77+
78+
tests:
79+
- changed-files:
80+
- any-glob-to-any-file:
81+
- tests/**
82+
83+
examples:
84+
- changed-files:
85+
- any-glob-to-any-file:
86+
- examples/**
87+
88+
CI:
89+
- changed-files:
90+
- any-glob-to-any-file:
91+
- .github/**
92+
93+
utils:
94+
- changed-files:
95+
- any-glob-to-any-file:
96+
- src/diffusers/utils/**
97+
- src/diffusers/commands/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Issue Labeler
2+
3+
on:
4+
issues:
5+
types: [opened]
6+
7+
permissions:
8+
contents: read
9+
issues: write
10+
11+
jobs:
12+
label:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
16+
- name: Install dependencies
17+
run: pip install huggingface_hub
18+
- name: Get labels from LLM
19+
id: get-labels
20+
env:
21+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
22+
ISSUE_TITLE: ${{ github.event.issue.title }}
23+
ISSUE_BODY: ${{ github.event.issue.body }}
24+
run: |
25+
LABELS=$(python utils/label_issues.py)
26+
echo "labels=$LABELS" >> "$GITHUB_OUTPUT"
27+
- name: Apply labels
28+
if: steps.get-labels.outputs.labels != ''
29+
env:
30+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
ISSUE_NUMBER: ${{ github.event.issue.number }}
32+
LABELS: ${{ steps.get-labels.outputs.labels }}
33+
run: |
34+
for label in $(echo "$LABELS" | python -c "import json,sys; print('\n'.join(json.load(sys.stdin)))"); do
35+
gh issue edit "$ISSUE_NUMBER" --add-label "$label"
36+
done

.github/workflows/pr_labeler.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: PR Labeler
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
contents: read
9+
pull-requests: write
10+
11+
jobs:
12+
label:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # v5
16+
with:
17+
sync-labels: true
18+
19+
missing-tests:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
23+
- name: Check for missing tests
24+
id: check
25+
env:
26+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
PR_NUMBER: ${{ github.event.pull_request.number }}
28+
REPO: ${{ github.repository }}
29+
run: |
30+
gh api --paginate "repos/${REPO}/pulls/${PR_NUMBER}/files" \
31+
| python utils/check_test_missing.py
32+
- name: Add or remove missing-tests label
33+
if: always()
34+
env:
35+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
PR_NUMBER: ${{ github.event.pull_request.number }}
37+
run: |
38+
if [ "${{ steps.check.outcome }}" = "failure" ]; then
39+
gh pr edit "$PR_NUMBER" --add-label "missing-tests"
40+
else
41+
gh pr edit "$PR_NUMBER" --remove-label "missing-tests" 2>/dev/null || true
42+
fi
43+
44+
size-label:
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Label PR by diff size
48+
env:
49+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
PR_NUMBER: ${{ github.event.pull_request.number }}
51+
REPO: ${{ github.repository }}
52+
run: |
53+
DIFF_SIZE=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" --jq '.additions + .deletions')
54+
for label in size/S size/M size/L; do
55+
gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "$label" 2>/dev/null || true
56+
done
57+
if [ "$DIFF_SIZE" -lt 50 ]; then
58+
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "size/S"
59+
elif [ "$DIFF_SIZE" -lt 200 ]; then
60+
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "size/M"
61+
else
62+
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "size/L"
63+
fi

utils/check_test_missing.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import ast
2+
import json
3+
import sys
4+
5+
6+
SRC_DIRS = ["src/diffusers/pipelines/", "src/diffusers/models/", "src/diffusers/schedulers/"]
7+
MIXIN_BASES = {"ModelMixin", "SchedulerMixin", "DiffusionPipeline"}
8+
9+
10+
def extract_classes_from_file(filepath: str) -> list[str]:
11+
with open(filepath) as f:
12+
tree = ast.parse(f.read())
13+
14+
classes = []
15+
for node in ast.walk(tree):
16+
if not isinstance(node, ast.ClassDef):
17+
continue
18+
base_names = set()
19+
for base in node.bases:
20+
if isinstance(base, ast.Name):
21+
base_names.add(base.id)
22+
elif isinstance(base, ast.Attribute):
23+
base_names.add(base.attr)
24+
if base_names & MIXIN_BASES:
25+
classes.append(node.name)
26+
27+
return classes
28+
29+
30+
def extract_imports_from_file(filepath: str) -> set[str]:
31+
with open(filepath) as f:
32+
tree = ast.parse(f.read())
33+
34+
names = set()
35+
for node in ast.walk(tree):
36+
if isinstance(node, ast.ImportFrom):
37+
for alias in node.names:
38+
names.add(alias.name)
39+
elif isinstance(node, ast.Import):
40+
for alias in node.names:
41+
names.add(alias.name.split(".")[-1])
42+
43+
return names
44+
45+
46+
def main():
47+
pr_files = json.load(sys.stdin)
48+
49+
new_classes = []
50+
for f in pr_files:
51+
if f["status"] != "added" or not f["filename"].endswith(".py"):
52+
continue
53+
if not any(f["filename"].startswith(d) for d in SRC_DIRS):
54+
continue
55+
try:
56+
new_classes.extend(extract_classes_from_file(f["filename"]))
57+
except (FileNotFoundError, SyntaxError):
58+
continue
59+
60+
if not new_classes:
61+
sys.exit(0)
62+
63+
new_test_files = [
64+
f["filename"]
65+
for f in pr_files
66+
if f["status"] == "added" and f["filename"].startswith("tests/") and f["filename"].endswith(".py")
67+
]
68+
69+
imported_names = set()
70+
for filepath in new_test_files:
71+
try:
72+
imported_names |= extract_imports_from_file(filepath)
73+
except (FileNotFoundError, SyntaxError):
74+
continue
75+
76+
untested = [cls for cls in new_classes if cls not in imported_names]
77+
78+
if untested:
79+
print(f"missing-tests: {', '.join(untested)}")
80+
sys.exit(1)
81+
else:
82+
sys.exit(0)
83+
84+
85+
if __name__ == "__main__":
86+
main()

0 commit comments

Comments
 (0)