Skip to content

Commit d43a575

Browse files
DEVOPS-50 fixed the auth issue while addding repo togh app
1 parent ac5081d commit d43a575

File tree

5 files changed

+118
-15
lines changed

5 files changed

+118
-15
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: add-repositories-to-github-app-automation
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
organization:
6+
description: 'GitHub organization Name'
7+
required: true
8+
type: string
9+
default: 'devwithkrishna'
10+
search-string:
11+
description: 'github repo name search string'
12+
required: true
13+
type: string
14+
github-app-name:
15+
description: 'github app name'
16+
required: true
17+
type: string
18+
19+
jobs:
20+
add-repositories-to-github-app-automation:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: git checkout
24+
uses: actions/checkout@v4
25+
- name: set up python 3.11
26+
uses: actions/setup-python@v4
27+
with:
28+
python-version: '3.11'
29+
- name: package installations
30+
run: |
31+
pip install pipenv
32+
pipenv install
33+
- name: execute python program
34+
env:
35+
ORGANIZATION: ${{ inputs.organization }}
36+
GH_TOKEN: ${{ secrets.DEVWITHKRISHNA_PERSONAL_ACCESS_TOKEN }}
37+
GH_TOKEN_CLASSIC: ${{ secrets.GH_TOKEN_CLASSIC }}
38+
run: |
39+
pipenv run python3 add_a_repo_to_github_app.py --search_string ${{ inputs.search-string }} --github_app_name ${{ inputs.github-app-name }}
40+
- name: Completed
41+
run: |
42+
echo "program completed successfully"

.github/workflows/github_app_installation_details.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
type: string
99
default: 'devwithkrishna'
1010
schedule:
11-
- cron: '0 0 1 * *' # Monthly once 12:00 AM
11+
- cron: '0 0 * * *' # Everyday # '0 0 1 * *' # Monthly once 12:00 AM
1212

1313
jobs:
1414
github-app-installation-details:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,4 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
.idea/
161-
list_of_github_installations.json
161+
*.json

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
# automation-to-add-repos-to-a-github-app
22
create an automation to add repos to a github app
33

4-
* Reference : [list-app-installations-for-an-organization](https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-app-installations-for-an-organization)
4+
* Reference : [list-app-installations-for-an-organization](https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-app-installations-for-an-organization)
5+
6+
# Credentials used for authorization
7+
8+
| credential | purpose | check |
9+
|------------|---------|--------------------|
10+
| fine grained personal access token | this is used for all purpose like listing repos, github app detailsetc | :heavy_check_mark: |
11+
| personal access token classic | this is used for api call to add repos to github app | :heavy_check_mark: |
12+
13+
**personal access token classic - provide `all repo access`**
14+
![Screenshot_3-5-2024_234236_github.com.jpeg](..%2F..%2F..%2F..%2FDownloads%2FScreenshot_3-5-2024_234236_github.com.jpeg)
15+
16+
# parameters
17+
18+
| input name | type | description |
19+
|------------|------|-------------|
20+
| organization | string | Github organizarion name. Default - `devwithkrishna` |
21+
| search-string | string | name of github repo starts with |
22+
| github-app-name | string | name of github app to which repositories will be added |
23+
24+
25+
# How code works
26+
27+
* Based on the search string, the code will use the end point `https://api.github.com/orgs/{orgaization}/repos` to list all
28+
repositories and get the ones matching. matching is done in a way that the seach string is considered as begining word of
29+
Github repository name. - _list_repos_in_org.py_ & _list_github_app_matching_name.py_
30+
31+
* Once its listed, later script pulls down all available github apps installed across the organization using the
32+
endpoint `https://api.github.com/orgs/{organization}/installations` - _list_all_github_app_in_org.py_
33+
34+
* Based on the Github app name provided as input it gathers the installation id. This is later used.
35+
36+
* using the matching repos based on search string and github app name provded, the script will use these details to the
37+
endpoint `https://api.github.com/user/installations/{github_app_installation_id}/repositories/{repo_id}`. This uses
38+
the personal access token classic to add repositories to github app - _add_a_repo_to_github_app.py_

add_a_repo_to_github_app.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,43 @@ def add_repository_to_github_app(organization:str,matching_repos: list[dict], ap
1212
:return:
1313
https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#add-a-repository-to-an-app-installation
1414
"""
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
2552

2653

2754
def main():
@@ -30,15 +57,15 @@ def main():
3057
organization = os.getenv('ORGANIZATION')
3158
parser = argparse.ArgumentParser(description="Add specific repos matching a string in repo names to a github teams")
3259
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")
3461
args = parser.parse_args()
3562
search_string = args.search_string
3663
github_app_name = args.github_app_name
3764
# Function call for repo list
3865
matching_repos = list_repos_in_github_org(organization, search_string)
3966
# Function call for github app details
4067
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
4269
add_repository_to_github_app(organization, matching_repos, app_short_list)
4370

4471

0 commit comments

Comments
 (0)