2
2
import time
3
3
import argparse
4
4
import os
5
-
5
+ import requests
6
+ from dotenv import load_dotenv
6
7
7
8
def create_jwt (private_key , app_id ):
8
9
"""
@@ -12,7 +13,7 @@ def create_jwt(private_key, app_id):
12
13
:return:
13
14
"""
14
15
# Open PEM
15
- # with open(pem_path , 'rb') as pem_file:
16
+ # with open(private_key , 'rb') as pem_file:
16
17
# signing_key = jwk_from_pem(pem_file.read())
17
18
signing_key = jwk_from_pem (private_key .encode ('utf-8' ))
18
19
@@ -30,27 +31,82 @@ def create_jwt(private_key, app_id):
30
31
encoded_jwt = jwt_instance .encode (payload , signing_key , alg = 'RS256' )
31
32
32
33
# Set JWT as environment variable
33
- os .environ ["GITHUB_JWT" ] = encoded_jwt
34
+ # os.environ["GITHUB_JWT"] = encoded_jwt
34
35
35
- print (f"JWT set as environment variable: JWT= { encoded_jwt } " )
36
+ # print(f"JWT token created successfully ")
36
37
return encoded_jwt
37
38
39
+ def get_app_installation_id (jwt :str , github_account_type :str ):
40
+ """
41
+ returns github app installation id on user and org accounts
42
+ :param jwt:
43
+ :return:
44
+ """
45
+ GITHUB_REPOSITORY = os .getenv ('GITHUB_REPOSITORY' )
46
+ GITHUB_REPOSITORY_OWNER = os .getenv ('GITHUB_REPOSITORY_OWNER' )
47
+ org_url = f'https://api.github.com/repos/{ GITHUB_REPOSITORY } /installation'
48
+ user_url = f'https://api.github.com/users/{ GITHUB_REPOSITORY_OWNER } /installation'
49
+ if github_account_type == 'user' :
50
+ url = user_url
51
+ else :
52
+ url = org_url
53
+ headers = {
54
+ "Accept" : "application/vnd.github+json" ,
55
+ "Authorization" : f"Bearer { jwt } " ,
56
+ "X-GitHub-Api-Version" : "2022-11-28"
57
+ }
58
+ response = requests .get (url = url , headers = headers )
59
+
60
+ if response .status_code == 200 :
61
+ print (f'Okay. Received proper response.Got installation id' )
62
+ response_json = response .json ()
63
+ elif response .status_code == 301 :
64
+ print (f'Moved permanently. Cant get a response' )
65
+ else :
66
+ print (f'Resource Not Found!' )
67
+
68
+ # Installation id of github app
69
+ installation_id = response_json ['id' ]
70
+ return installation_id
71
+
72
+ def generate_token_by_post_call (installation_id :int , jwt :str ):
73
+ """
74
+ create a app installation token by doing a rest api post call with permissions for application
75
+ :return:
76
+ """
77
+ url = f'https://api.github.com/app/installations/{ installation_id } /access_tokens'
78
+ headers = {
79
+ "Accept" : "application/vnd.github+json" ,
80
+ "Authorization" : f"Bearer { jwt } " ,
81
+ "X-GitHub-Api-Version" : "2022-11-28"
82
+ }
83
+ response = requests .post (url = url , headers = headers )
84
+ response_json = response .json ()
85
+ if response .status_code == 201 :
86
+ print (f'Github app installation token generate succcessfully, expires at { response_json ["expires_at" ]} ' )
87
+ os .environ ['GH_TOKEN' ] = response_json ['token' ]
38
88
39
89
def main ():
40
90
"""
41
91
to test the code
42
92
:return:
43
93
"""
94
+ load_dotenv ()
44
95
parser = argparse .ArgumentParser (description = "Create JWT for GitHub App authentication" )
45
96
parser .add_argument ("--github_app_private_key" ,required = True , type = str , help = "Github App Private key" )
97
+ parser .add_argument ("--github_account_type" ,required = True , choices = ['user' ,'organization' ], help = "Github account whether user account ot github org" )
46
98
parser .add_argument ("--github_app_id" ,required = True , type = str , help = "Your GitHub App ID" )
47
99
args = parser .parse_args ()
48
100
49
101
private_key = args .github_app_private_key
50
102
app_id = args .github_app_id
103
+ github_account_type = args .github_account_type
51
104
52
105
# function call
53
- create_jwt (private_key , app_id )
106
+ jwt = create_jwt (private_key = private_key , app_id = app_id )
107
+ installation_id = get_app_installation_id (jwt = jwt , github_account_type = github_account_type )
108
+ generate_token_by_post_call (installation_id = installation_id , jwt = jwt )
109
+
54
110
55
111
if __name__ == "__main__" :
56
112
main ()
0 commit comments