-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_backport_labels.py
More file actions
executable file
·60 lines (49 loc) · 1.87 KB
/
update_backport_labels.py
File metadata and controls
executable file
·60 lines (49 loc) · 1.87 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
# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by it. Please use
# './plugin-template --github pulp_rust' to update this file.
#
# For more info visit https://github.com/pulp/plugin_template
import os
import random
import requests
import yaml
def random_color():
"""Generates a random 24-bit number in hex"""
color = random.randrange(0, 2**24)
return format(color, "06x")
session = requests.Session()
token = os.getenv("GITHUB_TOKEN")
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
session.headers.update(headers)
# get all labels from the repository's current state
response = session.get("https://api.github.com/repos/pulp/pulp_rust/labels", headers=headers)
assert response.status_code == 200
old_labels = set([x["name"] for x in response.json() if x["name"].startswith("backport-")])
# get list of branches from template_config.yml
with open("./template_config.yml", "r") as f:
plugin_template = yaml.safe_load(f)
branches = set(plugin_template["supported_release_branches"])
latest_release_branch = plugin_template["latest_release_branch"]
if latest_release_branch is not None:
branches.add(latest_release_branch)
new_labels = {"backport-" + x for x in branches}
# delete old labels that are not in new labels
for label in old_labels.difference(new_labels):
response = session.delete(
f"https://api.github.com/repos/pulp/pulp_rust/labels/{label}", headers=headers
)
assert response.status_code == 204
# create new labels that are not in old labels
for label in new_labels.difference(old_labels):
color = random_color()
response = session.post(
"https://api.github.com/repos/pulp/pulp_rust/labels",
headers=headers,
json={"name": label, "color": color},
)
assert response.status_code == 201