@@ -12,16 +12,43 @@ def add_repository_to_github_app(organization:str,matching_repos: list[dict], ap
12
12
:return:
13
13
https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#add-a-repository-to-an-app-installation
14
14
"""
15
- # Assuming that the Github app name doesnot contain any special cgharacters and github app name is same as app_slug.
16
- print (f'Matching repos are: { matching_repos } ' )
17
- print (f'GitHub App details are: { app_short_list } ' )
18
- add_repo_to_github_app_endpoint = f'https://api.github.com/user/installations/{ id } / repositories / REPOSITORY_ID
19
- '
20
- headers = {
21
- "Accept" : "application/vnd.github+json" ,
22
- "Authorization" : f"Bearer { os .getenv ('GH_TOKEN' )} " ,
23
- "X-GitHub-Api-Version" : "2022-11-28"
24
- }
15
+ # Assuming that the Github app name doesnot contain any special characters and github app name is same as app_slug.
16
+ # print(f'Matching repos are: {matching_repos}')
17
+ # print(f'GitHub App details are: {app_short_list}')
18
+
19
+ # Getting Github app installation id
20
+ github_app_installation_id = app_short_list [0 ]['id' ]
21
+ github_app_name = app_short_list [0 ]['app_slug' ]
22
+ print (f'Github App installation Id is : { github_app_installation_id } ' )
23
+ # Iterating through matching repos list
24
+ for repo in matching_repos :
25
+ repo_id = repo ['id' ]
26
+ repo_name = repo ['name' ]
27
+ print (f'repository id of { repo_name } is : { repo_id } ' )
28
+ add_repo_to_github_app_endpoint = f'https://api.github.com/user/installations/{ github_app_installation_id } /repositories/{ repo_id } '
29
+ headers = {
30
+ "Accept" : "application/vnd.github+json" ,
31
+ "Authorization" : f"Bearer { os .getenv ('GH_TOKEN_CLASSIC' )} " ,
32
+ "X-GitHub-Api-Version" : "2022-11-28"
33
+ }
34
+
35
+ #Status code Description
36
+ # 204 - No Content
37
+ # 304 - Not Modified
38
+ # 403 - Forbidden
39
+ # 404 - Resource not found
40
+ response = requests .put (url = add_repo_to_github_app_endpoint , headers = headers )
41
+ response_code = response .status_code
42
+
43
+ if response_code == 204 :
44
+ print (f'Repository { repo_name } added to { github_app_name } ' )
45
+ elif response_code == 304 :
46
+ print (f'Repository { repo_name } Not added to { github_app_name } ' )
47
+ elif response_code == 403 :
48
+ print (f'This action is forbidden. No permission!' )
49
+ else :
50
+ print (f'Resource { repo_name } or { github_app_name } not found. { response .text } ' )
51
+ break
25
52
26
53
27
54
def main ():
@@ -30,15 +57,15 @@ def main():
30
57
organization = os .getenv ('ORGANIZATION' )
31
58
parser = argparse .ArgumentParser (description = "Add specific repos matching a string in repo names to a github teams" )
32
59
parser .add_argument ("--search_string" , required = True , type = str , help = "github repo name search string" )
33
- parser .add_argument ("--github_app_name" , required = True , type = str , help = "github repo name search string " )
60
+ parser .add_argument ("--github_app_name" , required = True , type = str , help = "github app name" )
34
61
args = parser .parse_args ()
35
62
search_string = args .search_string
36
63
github_app_name = args .github_app_name
37
64
# Function call for repo list
38
65
matching_repos = list_repos_in_github_org (organization , search_string )
39
66
# Function call for github app details
40
67
app_short_list = list_github_apps_in_organization_matching_name (organization , github_app_name )
41
- # Fucntion call for adding repo to github app
68
+ # Function call for adding repo to github app
42
69
add_repository_to_github_app (organization , matching_repos , app_short_list )
43
70
44
71
0 commit comments