-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_billing.py
More file actions
125 lines (116 loc) · 4.59 KB
/
github_billing.py
File metadata and controls
125 lines (116 loc) · 4.59 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import argparse
import requests
import json
import os
from dotenv import load_dotenv
def github_org_actions_billing(organization:str):
""" pull github org billing details"""
org_billing_endpoint = f'https://api.github.com/orgs/{organization}/settings/billing/actions'
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
"X-GitHub-Api-Version": "2022-11-28"
}
# API call here
response = requests.get(url=org_billing_endpoint, headers=headers)
response_json = response.json()
if response.status_code == 200:
print(f"API request successful on {org_billing_endpoint}")
# print(response_json)
else:
print(f"API request failed with status code {response.status_code}:")
# print(response_json)
json_data = json.dumps(response_json, indent=4)
# Write JSON string to a file
with open("org_billing_details.json", "w") as file:
file.write(json_data)
def github_org_packages_billing(organization: str):
"""
pull github org billing details
:param organization:
:return:
"""
org_package_billing_endpoint = f'https://api.github.com/orgs/{organization}/settings/billing/packages'
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
"X-GitHub-Api-Version": "2022-11-28"
}
# API call here
response = requests.get(url=org_package_billing_endpoint, headers=headers)
response_json = response.json()
if response.status_code == 200:
print(f"API request successful on {org_package_billing_endpoint}")
# print(response_json)
else:
print(f"API request failed with status code {response.status_code}:")
# print(response_json)
json_data = json.dumps(response_json, indent=4)
# Write JSON string to a file
with open("org_package_billing_details.json", "w") as file:
file.write(json_data)
def github_user_actions_billing(user: str):
"""
pull github org billing details
:param user:
:return:
"""
user_actions_billing_endpoint = f'https://api.github.com/users/{user}/settings/billing/actions'
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {os.getenv('GH_TOKEN_CLASSIC')}",
"X-GitHub-Api-Version": "2022-11-28"
}
# API call here
response = requests.get(url=user_actions_billing_endpoint, headers=headers)
response_json = response.json()
if response.status_code == 200:
print(f"API request successful on {user_actions_billing_endpoint}")
# print(response_json)
else:
print(f"API request failed with status code {response.status_code}:")
# print(response_json)
json_data = json.dumps(response_json, indent=4)
# Write JSON string to a file
with open("user_actions_billing_details.json", "w") as file:
file.write(json_data)
def github_user_packages_billing(user: str):
"""
pull github org billing details
:param user:
:return:
"""
user_actions_billing_endpoint = f'https://api.github.com/users/{user}/settings/billing/packages'
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {os.getenv('GH_TOKEN_CLASSIC')}",
"X-GitHub-Api-Version": "2022-11-28"
}
# API call here
response = requests.get(url=user_actions_billing_endpoint, headers=headers)
response_json = response.json()
if response.status_code == 200:
print(f"API request successful on {user_actions_billing_endpoint}")
# print(response_json)
else:
print(f"API request failed with status code {response.status_code}:")
# print(response_json)
json_data = json.dumps(response_json, indent=4)
# Write JSON string to a file
with open("user_packages_billing_details.json", "w") as file:
file.write(json_data)
def main():
""" main function to test"""
load_dotenv()
parser = argparse.ArgumentParser(description="Github organization billing details")
parser.add_argument("--organization", required=True, type=str, help="Github organization name")
parser.add_argument("--account_name",required=True, type=str, help="Github user name")
args = parser.parse_args()
organization = args.organization
account_name = args.account_name
github_org_actions_billing(organization=organization)
github_org_packages_billing(organization=organization)
github_user_actions_billing(user=account_name)
github_user_packages_billing(user=account_name)
if __name__ == '__main__':
main()