1
- import requests
2
1
import os
3
2
from datetime import datetime
3
+
4
4
import pytz
5
+ import requests
6
+
5
7
6
8
def current_ist_time ():
7
- """code to return time in IST"""
8
- # Get the current time in IST
9
- ist = pytz .timezone ('Asia/Kolkata' )
10
- ist_now = datetime .now (ist )
9
+ """code to return time in IST"""
10
+ # Get the current time in IST
11
+ ist = pytz .timezone ('Asia/Kolkata' )
12
+ ist_now = datetime .now (ist )
11
13
12
- # Format and print the current time in IST
13
- ist_now_formatted = ist_now .strftime ('%Y-%m-%d %H:%M:%S %Z%z' )
14
- return ist_now_formatted
14
+ # Format and print the current time in IST
15
+ ist_now_formatted = ist_now .strftime ('%Y-%m-%d %H:%M:%S %Z%z' )
16
+ return ist_now_formatted
15
17
16
18
17
19
def create_or_update_repository_secret_github (repo_name : str , secret_name : str , secret_value : str , public_key_id : int ):
18
- """
19
- Create or update org level secret in GitHub
20
- Ref https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret
21
-
22
- The token must have the following permission set: organization_secrets:write
23
- """
24
- encrypted_secret = secret_value
25
- organization = os .getenv ('GITHUB_REPOSITORY_OWNER' )
26
-
27
- if not encrypted_secret :
28
- print ("ENCRYPTED_SECRET environment variable is not set or is empty." )
29
- # print(f'encrypted sec is: {encrypted_secret}')
30
- ist_now_formatted = current_ist_time ()
31
- github_repo_secret_endpoint = f"https://api.github.com/repos/{ organization } /{ repo_name } /actions/secrets/{ secret_name } "
32
-
33
- headers = {
34
- "Accept" : "application/vnd.github+json" ,
35
- "Authorization" : f"Bearer { os .getenv ('GH_TOKEN' )} " ,
36
- "X-GitHub-Api-Version" : "2022-11-28"
37
- }
38
- data = {
39
- "encrypted_value" : encrypted_secret ,
40
- "visibility" : "all" ,
41
- "key_id" : public_key_id
42
- }
43
- response = requests .put (github_repo_secret_endpoint , headers = headers , json = data )
44
- if response .status_code == 201 :
45
- print (f"Secret { secret_name } created on { repo_name } at { ist_now_formatted } " )
46
- else :
47
- print (f"Secret { secret_name } updated on { repo_name } at { ist_now_formatted } " )
20
+ """
21
+ Create or update org level secret in GitHub
22
+ Ref https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret
23
+
24
+ The token must have the following permission set: organization_secrets:write
25
+ """
26
+ encrypted_secret = secret_value
27
+ organization = os .getenv ('GITHUB_REPOSITORY_OWNER' )
28
+
29
+ if not encrypted_secret :
30
+ print ("ENCRYPTED_SECRET environment variable is not set or is empty." )
31
+ # print(f'encrypted sec is: {encrypted_secret}')
32
+ ist_now_formatted = current_ist_time ()
33
+ github_repo_secret_endpoint = f"https://api.github.com/repos/{ organization } /{ repo_name } /actions/secrets/{ secret_name } "
34
+
35
+ headers = {
36
+ "Accept" : "application/vnd.github+json" ,
37
+ "Authorization" : f"Bearer { os .getenv ('GH_TOKEN' )} " ,
38
+ "X-GitHub-Api-Version" : "2022-11-28"
39
+ }
40
+ data = {
41
+ "encrypted_value" : encrypted_secret ,
42
+ "visibility" : "all" ,
43
+ "key_id" : public_key_id
44
+ }
45
+ response = requests .put (github_repo_secret_endpoint , headers = headers , json = data )
46
+ if response .status_code == 201 :
47
+ print (f"Secret { secret_name } created on { repo_name } at { ist_now_formatted } " )
48
+ else :
49
+ print (f"Secret { secret_name } updated on { repo_name } at { ist_now_formatted } " )
48
50
49
51
50
52
def main ():
51
- """To test the code"""
53
+ """To test the code"""
54
+
55
+ organization = os .getenv ('GITHUB_REPOSITORY_OWNER' )
56
+ secret_name = os .getenv ('secret_name' )
52
57
53
- organization = os . getenv ( 'organization' )
54
- secret_name = os . getenv ( 'secret_name' )
58
+ # Function call
59
+ create_or_update_repository_secret_github ( organization , secret_name )
55
60
56
- # Function call
57
- create_or_update_repository_secret_github (organization , secret_name )
58
61
59
62
if __name__ == "__main__" :
60
- main ()
63
+ main ()
0 commit comments