Skip to content

Commit 860c663

Browse files
DEVOPS-49 retrieve repo public key
1 parent b9a7185 commit 860c663

File tree

5 files changed

+281
-3
lines changed

5 files changed

+281
-3
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+
$GITHUB_ENV

Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
11+
[requires]
12+
python_version = "3"

Pipfile.lock

Lines changed: 166 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# programatically-create-delete-update-github-erpository-secrets
2-
programatically-create-delete-update-github-erpository-secrets
1+
# programatically-create-delete-update-github-repository-secrets
2+
programatically-create-delete-update-github-repository-secrets
3+

get_repository_public_key.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)