-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnormalize_repos.py
executable file
·154 lines (133 loc) · 4.46 KB
/
normalize_repos.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
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
"""
Please see README.md.
"""
import os
from github import Github
from labels import REQUIRED_LABELS
BRANCH_PROTECTION_EXEMPT_REPOSITORIES = [
# non-engineering repo
'australian-chapter',
# non-engineering repo
'cc-cert-core',
# non-engineering repo
'cc-cert-edu',
# non-engineering repo
'cc-cert-gov',
# non-engineering repo
'cc-cert-lib',
# exempted to allow community maintainer to self-merge PRs
'ccsearch-browser-extension',
# exempted for bot pushes to master
'creativecommons.github.io-source',
# exempted for bot pushes to master
'creativecommons.github.io',
# non-engineering repo
'global-network-strategy',
# non-engineering repo
'network-platforms',
# non-engineering repo
'sre-wiki-js',
# non-engineering repo
'tech-support',
# exempted to allow community maintainer to self-merge PRs
'vocabulary',
# exempted to allow community maintainer to self-merge PRs
'vue-vocabulary',
]
BRANCH_PROTECTION_REQUIRED_STATUS_CHECK_MAP = {
'cccatalog-api': [
'continuous-integration/travis-ci'
],
'cccatalog-frontend': [
'ci/circleci: lint',
'ci/circleci: unit'
],
'creativecommons.github.io-source': [
'continuous-integration/travis-ci'
],
'vocabulary': [
'ci/circleci: lint',
'ci/circleci: unit',
'netlify/cc-vocabulary/deploy-preview'
],
'vue-vocabulary': [
'ci/circleci: lint',
'ci/circleci: unit',
'netlify/cc-vue-vocabulary/deploy-preview'
],
}
def set_up_github_client():
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
github = Github(GITHUB_TOKEN)
return github
def get_cc_repos(github):
cc = github.get_organization('creativecommons')
return cc.get_repos()
def create_existing_label(labels):
existing_labels = {}
for label in labels:
existing_labels[label.name] = {
'color': label.color,
'description': label.description,
'label_object': label
}
return existing_labels
def edit_label(label_object, name, color, description):
print(f'Updating color and description for label "{name}"')
label_object.edit(name=name, color=color, description=description)
def create_label(repo, name, color, description):
print(f'Creating label "{name}"')
repo.create_label(name=name, color=color, description=description)
def update_labels(repo):
if repo.archived:
return
print(f'\n{repo.name}\n-----')
labels = repo.get_labels()
existing_labels = create_existing_label(labels)
for label_name in REQUIRED_LABELS.keys():
required_label = REQUIRED_LABELS[label_name]
if label_name in existing_labels.keys():
existing_label = existing_labels[label_name]
if (required_label['color'] != existing_label['color']) or \
(required_label['description']
!= existing_label['description']):
edit_label(
label_object=existing_label['label_object'],
name=label_name,
color=required_label['color'],
description=required_label['description']
)
else:
create_label(
repo=repo,
name=label_name,
color=required_label['color'],
description=required_label['description']
)
def update_branch_protection(repo):
master = repo.get_branch('master')
if repo.name not in BRANCH_PROTECTION_EXEMPT_REPOSITORIES:
if repo.name in BRANCH_PROTECTION_REQUIRED_STATUS_CHECK_MAP:
master.edit_protection(
required_approving_review_count=1,
user_push_restrictions=[],
strict=True,
contexts=BRANCH_PROTECTION_REQUIRED_STATUS_CHECK_MAP[repo.name]
)
else:
master.edit_protection(
required_approving_review_count=1,
user_push_restrictions=[]
)
print(f'Updating branch protection for: "{repo.name}"')
else:
print(f'Skipping branch protection for exempt repo: "{repo.name}"')
if __name__ == '__main__':
github = set_up_github_client()
repos = get_cc_repos(github)
for repo in repos:
# TODO: Set up automatic deletion of merged branches
update_labels(repo)
update_branch_protection(repo)