-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-linear-labels.py
145 lines (120 loc) · 3.93 KB
/
update-linear-labels.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
import sys
import re
linear_token = sys.argv[3]
url = 'https://api.linear.app/graphql'
headers = {'Authorization': linear_token,
'Content-Type': 'application/json'}
def get_issue(team_key, issue_number):
query = """
query Issues($teamKey: String!, $issueNumber: Float) {
issues(filter: {team: {key: {eq: $teamKey}}, number: {eq: $issueNumber}}) {
nodes {
id,
branchName,
parent {
id
},
team {
id
},
labels {
nodes {
id
}
}
}
}
}
"""
variables = {'teamKey': team_key, 'issueNumber': issue_number}
payload = {'query': query, 'variables': variables}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
matched_issues = response.json()['data']['issues']['nodes']
if len(matched_issues) == 0:
return None
else:
return matched_issues[0]
def get_label_id(team_id, label_name):
query = """
query LabelsByTeam ($teamId: ID) {
issueLabels(filter: {team: {id: {eq: $teamId}}}) {
nodes {
id,
name
}
}
}
"""
variables = {'teamId': team_id}
payload = {'query': query, 'variables': variables}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
labels = response.json()['data']['issueLabels']['nodes']
matched_label = list(filter(lambda x: x['name'] == label_name, labels))
if len(matched_label) == 0:
return None
else:
return matched_label[0]['id']
def set_labels(issue_id, label_ids):
query = """
mutation IssueUpdate ($issueId: String!, $labelIds: [String!]) {
issueUpdate(
id: $issueId,
input: {
labelIds: $labelIds
}
) {
success
issue {
id
}
}
}
"""
variables = {'labelIds': label_ids, 'issueId': issue_id}
payload = {'query': query, 'variables': variables}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return None
def label_issue(branch_name, label_to_add, error_exit_code=1):
match = re.search("^(\w+)-(\d+)-.*$", branch_name)
if match is None:
print('Unable to infer issue code from branch name', flush=True)
sys.exit(error_exit_code)
team_key = match.group(1).upper()
issue_number = int(match.group(2))
print("Inferred issue code='{}-{}' from branch='{}'".format(team_key,
issue_number, branch_name), flush=True)
issue = get_issue(team_key, issue_number)
if issue is None:
print('No matching issues found!', flush=True)
sys.exit(error_exit_code)
issue_id = issue['id']
if issue['parent']:
parent_id = issue['parent']['id']
else:
parent_id = None
team_id = issue['team']['id']
label_ids = list(map(
lambda x: x['id'], issue['labels']['nodes']))
new_label_id = get_label_id(team_id, label_to_add)
if new_label_id is None:
print('No matching label found!')
sys.exit(error_exit_code)
label_ids.append(new_label_id)
label_ids = list(set(label_ids))
set_labels(issue_id, label_ids)
# We support only one level of nested issues for now.
# Anything more would require writing a recursive query which is not worth it
# unless we have use-cases for it.
if parent_id:
set_labels(parent_id, label_ids)
if __name__ == '__main__':
try:
label_issue(sys.argv[2], sys.argv[1], error_exit_code=int(sys.argv[4]))
except requests.HTTPError as e:
print("API response: {}".format(e.response.text), flush=True)
raise
print("All done!")