-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcodeowners.py
More file actions
40 lines (36 loc) · 1.33 KB
/
codeowners.py
File metadata and controls
40 lines (36 loc) · 1.33 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
"""Importing GitHub Module."""
from github import Github
def add(orgname, pat, reponame, branchname):
"""Add or Update CODEOWNERS."""
g = Github(pat)
repo = g.get_organization(orgname).get_repo(reponame)
all_files = []
contents = repo.get_contents("")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file)
.replace('ContentFile(path="', '')
.replace('")', ''))
with open('./CODEOWNERS', 'r') as file:
content = file.read()
# Upload to github
git_prefix = '.github/'
git_file = git_prefix + 'CODEOWNERS'
if git_file in all_files:
contents = repo.get_contents(git_file)
repo.update_file(contents.path,
"updating CODEOWNERS",
content,
contents.sha,
branch=branchname)
print(git_file + ' updated for: ' + reponame)
else:
repo.create_file(git_file,
"adding CODEOWNERS",
content,
branch=branchname)
print(git_file + ' created for: ' + reponame)