Skip to content

Commit 97351e3

Browse files
DEVOPS-50 list github app py files
1 parent fc7e099 commit 97351e3

File tree

5 files changed

+259
-1
lines changed

5 files changed

+259
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,5 @@ cython_debug/
157157
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
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.
160-
#.idea/
160+
.idea/
161+
list_of_github_installations.json

Pipfile

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

Pipfile.lock

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

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# automation-to-add-repos-to-a-github-app
22
create an automation to add repos to a github app
3+
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)

list_all_github_app_in_org.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# reference: https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-app-installations-for-an-organization
2+
import requests
3+
import json
4+
import os
5+
from dotenv import load_dotenv
6+
7+
def list_github_apps_in_organization(organization: str):
8+
"""
9+
list GitHub apps in the orgaization
10+
:param organization:
11+
:return:
12+
reference: https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-app-installations-for-an-organization
13+
"""
14+
15+
list_github_app_endpoint = f'https://api.github.com/orgs/{organization}/installations'
16+
headers = {
17+
"Accept": "application/vnd.github+json",
18+
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
19+
"X-GitHub-Api-Version": "2022-11-28"
20+
}
21+
22+
response = requests.get(list_github_app_endpoint, headers=headers)
23+
response_json = response.json()
24+
response_code = response.status_code
25+
26+
if response_code == 200:
27+
print(f'Listing GitHub Apps in {organization} organization successful')
28+
else:
29+
print(f'Listing GitHub Apps in {organization} failed')
30+
31+
total_num_of_github_app_installations = response_json['total_count']
32+
print(f'Found {total_num_of_github_app_installations} GitHub App in {organization} organization')
33+
34+
# Extract the list of dictionaries
35+
list_of_installations = response_json["installations"]
36+
37+
# Convert list of dictionaries to JSON string
38+
json_data = json.dumps(list_of_installations, indent=4)
39+
40+
# Write JSON string to a file
41+
with open("list_of_github_installations.json", "w") as file:
42+
file.write(json_data)
43+
44+
app_short_list = []
45+
for app in list_of_installations:
46+
app_dict = {}
47+
app_dict['id'] = app['id']
48+
app_dict['app_id'] = app['app_id']
49+
app_dict['app_slug'] = app['app_slug']
50+
app_short_list.append(app_dict)
51+
52+
return app_short_list
53+
54+
55+
def main():
56+
""" To test the code """
57+
load_dotenv()
58+
GH_TOKEN = os.getenv('gh_token')
59+
# organization = os.getenv('ORGANIZATION')
60+
organization = 'devwithkrishna'
61+
62+
app_short_list = list_github_apps_in_organization(organization)
63+
64+
if __name__ == '__main__':
65+
main()
66+
67+

0 commit comments

Comments
 (0)