Skip to content

Commit 8d05b8c

Browse files
DEVOPS-46 initial commit
1 parent 5e5b48a commit 8d05b8c

File tree

4 files changed

+290
-1
lines changed

4 files changed

+290
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ rad.json
1919
#License-file
2020
*.flf
2121
#Test results file
22-
TestResults.xml
22+
TestResults.xml
23+
.idea/*

Pipfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
requests = "==2.31.0"
8+
9+
[requires]
10+
python_version = "3"

Pipfile.lock

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

list_repos_and_add_to_teams.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import argparse
2+
import requests
3+
import os
4+
5+
6+
def list_repos_and_add_to_github_teams(org_name: str, github_team_name: str, search_string: str, permission: str):
7+
"""
8+
function to add specific repos matching a string in repo names to a github teams
9+
:return:
10+
"""
11+
# GitHub endpoint for listing repos under an organization
12+
repo_url = f"https://api.github.com/orgs/{org_name}/repos"
13+
print(f"github api endpoint url {repo_url}")
14+
15+
headers = {
16+
"Accept": "application/vnd.github+json",
17+
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
18+
"X-GitHub-Api-Version": "2022-11-28"
19+
}
20+
21+
# Define pagination parameters
22+
per_page = 100 # Number of records per page
23+
page = 1 # Initial page number
24+
list_of_repo_names = []
25+
26+
while True:
27+
# Add pagination parameters to the URL
28+
params = {'per_page': per_page, 'page': page}
29+
response = requests.get(repo_url, headers=headers, params=params)
30+
response_json = response.json() ## Github repo details
31+
32+
# Checking the API status code
33+
if response.status_code == 200:
34+
print(f"API request successful on {repo_url}")
35+
# print(response_json)
36+
else:
37+
print(f"API request failed with status code {response.status_code}:")
38+
# print(response_json)
39+
break
40+
41+
# Get the repo names from the list of dictionaries and add to another list
42+
for repo in response_json:
43+
list_of_repo_names.append(repo['full_name'])
44+
45+
page += 1 # Move to the next page
46+
47+
# Break the loop if no more pages
48+
if len(response_json) < per_page:
49+
break
50+
51+
for repo in list_of_repo_names:
52+
print(f" repo name: {repo} ")
53+
54+
print(f"Total Number of repos in {org_name} Org is {len(list_of_repo_names)}")
55+
56+
# Finding repos starting with search string
57+
matching_repos = [repo for repo in list_of_repo_names if repo.startswith(f'{org_name}/{search_string}')]
58+
print(f"Matching repos {search_string} are: {matching_repos}")
59+
60+
### Adding repos to teams ###
61+
# GitHub endpoint for listing teamws under an organization
62+
team_url = f"https://api.github.com/orgs/{org_name}/teams?per_page={per_page}"
63+
print(f"github api endpoint url {team_url}")
64+
65+
headers = {
66+
"Accept": "application/vnd.github+json",
67+
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
68+
"X-GitHub-Api-Version": "2022-11-28"
69+
}
70+
page = 1 # Initial page number
71+
while True:
72+
params = {'per_page': per_page, 'page': page}
73+
response = requests.get(team_url, headers=headers, params=params)
74+
response_json = response.json() ## Github team details
75+
print("wait")
76+
77+
team_slug = "" # defining empty team slug
78+
for team in response_json:
79+
if team['name'] == github_team_name:
80+
team_slug = team['slug']
81+
print(f"GitHub team slug is : {team_slug}")
82+
83+
for repo in matching_repos:
84+
print(f"Adding {repo} to {github_team_name} team")
85+
86+
# GitHub endpoint for adding repos to a team in an organization
87+
repo_addition_url = f"https://api.github.com/orgs/{org_name}/teams/{team_slug}/repos/{repo}"
88+
print(f"github api endpoint url {repo_addition_url}")
89+
90+
headers = {
91+
"Accept": "application/vnd.github+json",
92+
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
93+
"X-GitHub-Api-Version": "2022-11-28"
94+
}
95+
data = {
96+
"permission": permission
97+
}
98+
99+
response = requests.put(repo_addition_url, headers=headers, params=params, json=data)
100+
print(f"Repository {repo} successfully added to {github_team_name} with permission {permission}")
101+
# Increment page number
102+
page += 1
103+
# Break the loop if no more pages
104+
if len(response_json) < per_page:
105+
break
106+
107+
108+
def main():
109+
"""main function to test the code"""
110+
parser = argparse.ArgumentParser(description="Add specific repos matching a string in repo names to a github teams")
111+
parser.add_argument("--org_name",required=True, type=str, help="Github org name")
112+
parser.add_argument("--github_team_name",required=True, type=str, help="Your GitHub team name")
113+
parser.add_argument("--search_string", required=True, type=str, help="github repo name search string")
114+
parser.add_argument("--permission", required=True,choices=['admin', 'push', 'pull', 'triage', 'maintain'], help="Permissions for Github teams across repository" )
115+
args = parser.parse_args()
116+
os.environ["GH_TOKEN"] = "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"
117+
org_name = args.org_name
118+
github_team_name = args.github_team_name
119+
search_string = args.search_string
120+
permission = args.permission
121+
122+
123+
# function call
124+
list_repos_and_add_to_github_teams(org_name, github_team_name, search_string, permission)
125+
126+
127+
if __name__ == "__main__":
128+
main()

0 commit comments

Comments
 (0)