forked from osism/github-manager
-
Notifications
You must be signed in to change notification settings - Fork 3
/
manage.py
115 lines (93 loc) · 3.78 KB
/
manage.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
import os
import github
import logging
import yaml
from argparse import ArgumentParser
logging.basicConfig(
format="%(asctime)s - %(message)s", level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S"
)
API_TOKEN = os.environ.get("API_TOKEN")
ORGANIZATION = os.environ.get("ORGANIZATION", "sovereigncloudstack")
gh = github.Github(login_or_token=API_TOKEN)
parser = ArgumentParser()
parser.add_argument(
"-d", "--dry", default=False, help="Dry run - if true does not change github items"
)
parser.add_argument(
"-kl",
"--keep_labels",
default=False,
help="If true does not remove manually added, non-defined labels",
)
args = parser.parse_args()
with open("config.yaml") as fp:
CONFIG = yaml.load(fp, Loader=yaml.SafeLoader)
for gh_repository in gh.get_organization(ORGANIZATION).get_repos(type="public"):
if gh_repository.archived:
continue
logging.info(f"Checking {gh_repository.name}")
# handle labels
gh_labels = gh_repository.get_labels()
labels = {}
for gh_label in gh_labels:
labels[gh_label.name] = gh_label
for label in CONFIG["labels"]:
if label["name"] not in labels.keys():
logging.info(f"{gh_repository.name} - label {label['name']} does not exist")
if args.dry is False:
try:
gh_repository.create_label(
name=label["name"],
description=label["description"],
color=label["color"],
)
except:
logging.info(
f"{gh_repository.name} - label {label['name']} - Failed to create label"
)
else:
gh_label = labels[label["name"]]
if (
gh_label.description != label["description"]
or gh_label.color != label["color"]
):
logging.info(f"{gh_repository.name} - label {label['name']} changed")
if args.dry is False:
gh_label.edit(
name=label["name"],
description=label["description"],
color=label["color"],
)
del labels[label["name"]]
# remove undefined labels
for label in labels.keys():
logging.info(f"{gh_repository.name} - {label} should not exist")
if args.dry is False and args.keep_labels is False:
gh_label = labels[label]
gh_label.delete()
logging.info(f"{gh_repository.name} - {label} removed")
# handle milestones
gh_milestones = gh_repository.get_milestones(state="open")
for gh_milestone in gh_milestones:
if gh_milestone.title not in CONFIG["milestones"]:
logging.info(
f"{gh_repository.name} - {gh_milestone.title} should be in state 'closed'"
)
if args.dry is False:
try:
gh_milestone.edit(title=gh_milestone.title, state="closed")
except:
logging.info(f"{gh_repository.name} - milestone {milestone} - Failed to close milestone")
gh_milestone_titles = []
for gh_milestone in gh_milestones:
gh_milestone_titles.append(gh_milestone.title)
for milestone in CONFIG["milestones"]:
if milestone in gh_milestone_titles:
logging.info(f"{gh_repository.name} - milestone {milestone} does exist")
else:
logging.info(f"{gh_repository.name} - milestone {milestone} does not exist")
if args.dry is False:
try:
gh_repository.create_milestone(title=milestone, state="open")
except:
logging.info(f"{gh_repository.name} - milestone {milestone} - Failed to create milestone")