Skip to content

Commit a90bbc1

Browse files
DEVOPS-49 delete repo secret
1 parent 0c9ee0a commit a90bbc1

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: delete-github-repository-secrets-using-workflow
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
organization:
6+
type: string
7+
default: 'devwithkrishna'
8+
description: 'The GitHub organization where the Secret will be delted from.'
9+
required: true
10+
repository_name:
11+
type: string
12+
description: 'Repository from which secret to be deleted'
13+
required: true
14+
secret_name:
15+
type: string
16+
description: "Secret name to delete from org"
17+
required: true
18+
19+
run-name: ${{ github.actor }} deleting secret ${{ inputs.secret_name }} from ${{ inputs.repository_name }}
20+
jobs:
21+
delete-github-repository-secrets-using-workflow:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: git checkout
25+
uses: actions/checkout@v4
26+
- name: Set up Python
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: '3.11'
30+
- name: package installations
31+
run: |
32+
pip install pipenv
33+
pipenv install
34+
- name: delete github org secret
35+
env:
36+
GH_TOKEN: ${{ secrets.DEVWITHKRISHNA_PERSONAL_ACCESS_TOKEN }}
37+
repository_name: ${{ inputs.repository_name }}
38+
secret_name: ${{ inputs.secret_name }}
39+
organization: ${{ inputs.organization }}
40+
run: |
41+
pipenv run python3 delete_repo_secret.py
42+
echo "Secret deleted"
43+
- name: Completed
44+
run: |
45+
echo "program completed successfully"

create_or_update_repo_secret.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def current_ist_time():
1414
return ist_now_formatted
1515

1616

17-
def create_or_update_organization_secret_github(organization: str, secret_name: str):
17+
def create_or_update_repository_secret_github(organization: str, secret_name: str):
1818
"""
1919
Create or update org level secret in GitHub
2020
Ref https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret
@@ -48,15 +48,12 @@ def create_or_update_organization_secret_github(organization: str, secret_name:
4848

4949
def main():
5050
"""To test the code"""
51-
# Configuring to read ENCRYPTED_SECRET
52-
# encrypted_secret = os.getenv('ENCRYPTED_SECRET')
53-
# organization = 'devwithkrishna'
54-
# secret_name = 'noidea'
51+
5552
organization = os.getenv('organization')
5653
secret_name = os.getenv('secret_name')
5754

5855
# Function call
59-
create_or_update_organization_secret_github(organization, secret_name)
56+
create_or_update_repository_secret_github(organization, secret_name)
6057

6158
if __name__ == "__main__":
6259
main()

delete_repo_secret.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import requests
2+
import os
3+
from datetime import datetime
4+
import pytz
5+
6+
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)
11+
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
15+
16+
17+
def delete_repository_secret_github(organization: str, repository_name:str, secret_name: str):
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+
ist_now_formatted = current_ist_time()
25+
github_repo_secret_endpoint = f"https://api.github.com/repos/{organization}/{repository_name}/actions/secrets/{secret_name}"
26+
27+
headers = {
28+
"Accept": "application/vnd.github+json",
29+
"Authorization": f"Bearer {os.getenv('GH_TOKEN')}",
30+
"X-GitHub-Api-Version": "2022-11-28"
31+
}
32+
33+
response = requests.delete(github_repo_secret_endpoint, headers=headers)
34+
if response.status_code == 204:
35+
print(f"Secret {secret_name} deleted from {repository_name} at {ist_now_formatted} ")
36+
else:
37+
print(f"Something happened while deleting {secret_name} from {repository_name} at {ist_now_formatted} ")
38+
39+
40+
def main():
41+
"""To test the code"""
42+
43+
organization = os.getenv('organization')
44+
secret_name = os.getenv('secret_name')
45+
repository_name = os.getenv('repository_name')
46+
47+
# Function call
48+
delete_repository_secret_github(organization,repository_name, secret_name)
49+
50+
if __name__ == "__main__":
51+
main()

0 commit comments

Comments
 (0)