1+ import os
2+ import pytz
3+ import requests
4+ import argparse
5+ from datetime import datetime
6+
7+ def current_ist_time ():
8+ """code to return time in IST"""
9+ # Get the current time in IST
10+ ist = pytz .timezone ('Asia/Kolkata' )
11+ ist_now = datetime .now (ist )
12+
13+ # Format and print the current time in IST
14+ ist_now_formatted = ist_now .strftime ('%Y-%m-%d %H:%M:%S %Z%z' )
15+ return ist_now_formatted
16+ def get_repository_public_key (organization :str ,repository_name : str ):
17+ """
18+ Get the ORG name and repo name retrieve the public key of the repository
19+ :param organization:
20+ :param repository_name:
21+ :return:
22+ """
23+ repository_public_key_url = f'https://api.github.com/repos/{ organization } /{ repository_name } /actions/secrets/public-key'
24+ headers = {
25+ "Accept" : "application/vnd.github+json" ,
26+ "Authorization" : f"Bearer { os .getenv ('GH_TOKEN' )} " ,
27+ "X-GitHub-Api-Version" : "2022-11-28"
28+ }
29+ response = requests .get (repository_public_key_url , headers = headers )
30+ response_json = response .json ()
31+ repository_public_key = response_json ['key' ]
32+ # print(f'repo public key is {repository_public_key}')
33+ response_code = response .status_code
34+
35+ if response_code == 200 :
36+ print (f'Public key for { repository_name } retrieved from { organization } Org Successfully at { current_ist_time ()} ' )
37+ else :
38+ print (f'Failed to retrieve public key for { repository_name } retrieved from { organization } Org at { current_ist_time ()} ' )
39+
40+ return repository_public_key
41+
42+
43+ def get_repository_public_key_id (organization :str ,repository_name : str ):
44+ """
45+ Get the ORG name and repo name retrieve the public key id of the repository
46+ :param organization:
47+ :param repository_name:
48+ :return:
49+ """
50+ repository_public_key_id_url = f'https://api.github.com/repos/{ organization } /{ repository_name } /actions/secrets/public-key'
51+ headers = {
52+ "Accept" : "application/vnd.github+json" ,
53+ "Authorization" : f"Bearer { os .getenv ('GH_TOKEN' )} " ,
54+ "X-GitHub-Api-Version" : "2022-11-28"
55+ }
56+ response = requests .get (repository_public_key_id_url , headers = headers )
57+ response_json = response .json ()
58+ repository_public_key_id = response_json ['key_id' ]
59+ # print(f'repo public key is {repository_public_key_id}')
60+ response_code = response .status_code
61+
62+ if response_code == 200 :
63+ print (f'Public key id for { repository_name } retrieved from { organization } Org Successfully at { current_ist_time ()} ' )
64+ else :
65+ print (f'Failed to retrieve public key id for { repository_name } retrieved from { organization } Org at { current_ist_time ()} ' )
66+
67+ return repository_public_key_id
68+
69+
70+ def main ():
71+ """ To test the python code """
72+ # GH_TOKEN = os.environ.get("GH_TOKEN")
73+ gh_token = "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"
74+ os .environ ['GH_TOKEN' ] = gh_token
75+ parser = argparse .ArgumentParser (description = "Get the public key of the repository in GitHub" )
76+ parser .add_argument ("--organization" , required = True , type = str , help = "GitHub organization name" )
77+ parser .add_argument ("--repository_name" , help = "GitHub repository name" , type = str , required = True )
78+
79+ args = parser .parse_args ()
80+ organization = args .organization
81+ repository_name = args .repository_name
82+
83+ try :
84+ repo_public_key = get_repository_public_key (organization , repository_name )
85+ os .system (f'echo "REPOSITORY_PUBLIC_KEY={ repo_public_key } " >> $GITHUB_ENV' )
86+ print (f"Public key added as a environment variable" )
87+ repo_public_key_id = get_repository_public_key_id (organization , repository_name )
88+ os .system (f'echo "REPOSITORY_PUBLIC_KEY_ID={ repo_public_key } " >> $GITHUB_ENV' )
89+ print (f"Public key id added as a environment variable" )
90+
91+
92+ # return encrypted_secret
93+ except Exception as e :
94+ print (f"Error encrypting secret: { e } " )
95+ exit (1 )
96+
97+ if __name__ == "__main__" :
98+ main ()
0 commit comments