-
Notifications
You must be signed in to change notification settings - Fork 7
70 lines (62 loc) · 2.46 KB
/
issue_label_bot.yaml
File metadata and controls
70 lines (62 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
name: Issue Label Bot
on:
issues:
types: [opened, edited]
permissions:
issues: write
contents: read
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Apply labels based on keywords
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
ISSUE_TITLE: ${{ github.event.issue.title }}
ISSUE_BODY: ${{ github.event.issue.body }}
GH_REPOSITORY: ${{ github.repository }}
run: |
python3 << 'EOF'
import os, sys, yaml, json
from urllib.request import Request, urlopen
from urllib.error import HTTPError
token = os.environ['GITHUB_TOKEN']
issue_number = os.environ['ISSUE_NUMBER']
repo = os.environ['GH_REPOSITORY']
title = (os.environ.get('ISSUE_TITLE') or '').lower()
body = (os.environ.get('ISSUE_BODY') or '').lower()
text = title + ' ' + body
config_path = '.github/issue_label_bot.yaml'
try:
with open(config_path) as f:
config = yaml.safe_load(f)
except FileNotFoundError:
print(f'Error: config file not found at {config_path}', file=sys.stderr)
sys.exit(1)
except yaml.YAMLError as e:
print(f'Error: failed to parse {config_path}: {e}', file=sys.stderr)
sys.exit(1)
labels_to_add = []
for matcher in config['matchers']:
if any(kw.lower() in text for kw in matcher['keywords']):
labels_to_add.append(matcher['label'])
if labels_to_add:
url = f'https://api.github.com/repos/{repo}/issues/{issue_number}/labels'
data = json.dumps({'labels': labels_to_add}).encode()
req = Request(url, data=data, headers={
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json',
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
})
try:
with urlopen(req) as resp:
print(f'Added labels: {labels_to_add}')
except HTTPError as e:
print(f'Error: GitHub API returned {e.code}: {e.read().decode()}', file=sys.stderr)
sys.exit(1)
else:
print('No matching labels found.')
EOF