-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (57 loc) · 2.37 KB
/
main.py
File metadata and controls
73 lines (57 loc) · 2.37 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
71
72
73
import os
import requests
import re
def check_issue_title(title):
if re.match(r'^Y[0-9]{2}-[0-9]{3,4}', title):
return False
return True
def get_org_variable(org, var_name, token):
url = f"https://api.github.com/orgs/{org}/actions/variables/{var_name}"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()["value"]
def update_org_variable(org, var_name, new_value, token):
url = f"https://api.github.com/orgs/{org}/actions/variables/{var_name}"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28"
}
data = {
"name": var_name,
"value": str(new_value)
}
response = requests.patch(url, headers=headers, json=data)
response.raise_for_status()
def update_issue_title(repo, issue_prefix, issue_number, token, next_number, issue_title):
issue_number_formatted = f"{issue_prefix}-{next_number:03d}"
new_title = f"{issue_number_formatted} - {issue_title}"
url = f"https://api.github.com/repos/{repo}/issues/{issue_number}"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
data = {"title": new_title}
response = requests.patch(url, headers=headers, json=data)
if response.status_code != 200:
print(f"Failed to update issue title: {response.json()}")
if __name__ == "__main__":
github_token = os.getenv('GITHUB_TOKEN')
repo = os.getenv('GITHUB_REPOSITORY')
issue_prefix = os.getenv('ISSUE_PREFIX')
issue_counter_var = os.getenv('ISSUE_COUNTER_VAR')
issue_title = os.getenv('GITHUB_EVENT_ISSUE_TITLE')
issue_number = os.getenv('GITHUB_EVENT_ISSUE_NUMBER')
org_name = os.getenv('ORG_NAME')
if not check_issue_title(issue_title):
print(f"Invalid issue title: {issue_title}")
exit(0)
issue_counter = get_org_variable(org_name, issue_counter_var, github_token)
next_number = int(issue_counter) + 1
update_org_variable(org_name, issue_counter_var, next_number, github_token)
update_issue_title(repo, issue_prefix, issue_number, github_token, next_number, issue_title)