-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepository_outside_collaborators.py
executable file
·50 lines (44 loc) · 1.81 KB
/
repository_outside_collaborators.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
#!/usr/bin/env python3
import argparse
import os
from utils import simplify, get_collaborators_for_repository
from utils import get_repositories
from utils import ForbiddenAccessError
def main():
repos = get_repositories()
for repo in repos:
repo_name = repo['name']
if repo.get('isArchived'):
# Skip the repo if it's archived as GitHub API will fail
continue
collaborators = get_collaborators_for_repository(repo_name, affiliation="OUTSIDE")
for collaborator in collaborators:
username = collaborator['node']['login']
permission = collaborator['permission']
output = f"""
resource "github_repository_collaborator" "{simplify(repo_name)}-{simplify(username)}" {{
repository = github_repository.{simplify(repo_name)}.name
username = "{username}"
permission = "{permission.lower()}"
}}
"""
print(output)
def gen_import():
repos = get_repositories()
for repo in repos:
repo_name = repo['name']
if repo.get('isArchived'):
# Skip the repo if it's archived as GitHub API will fail
continue
collaborators = get_collaborators_for_repository(repo_name, affiliation="OUTSIDE")
for collaborator in collaborators:
username = collaborator['node']['login']
print(f"terraform import github_repository_collaborator.{simplify(repo_name)}-{simplify(username)} {repo_name}:{username}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate Outside Collaborators for repos')
parser.add_argument('--gen-imports', default=False, action='store_true')
args = parser.parse_args()
if args.gen_imports:
gen_import()
else:
main()