Created
October 8, 2021 04:05
-
-
Save vietanhduong/0a4d67c739221c6e895d30e7929c9ca3 to your computer and use it in GitHub Desktop.
Quick remove github release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import requests | |
import os | |
import sys | |
import json | |
def err(msg, **kwargs): | |
print(msg, file=sys.stderr, **kwargs) | |
def required(value: str, key: str): | |
if value is None or not len(value): | |
err(f'${key} is required') | |
exit(1) | |
token=os.getenv('GH_TOKEN') | |
repo = os.getenv('GH_REPO') | |
owner = os.getenv('GH_OWNER') | |
# By default, remove all draft release | |
# If you want to remove all release | |
# export REMOVE_ALL=1 | |
remove_all = os.getenv('REMOVE_ALL') | |
required(token, 'GH_TOKEN') | |
required(repo, 'GH_REPO') | |
required(owner, 'GH_OWNER') | |
GH_API='https://api.github.com' | |
RELEASE_ENDPOINT=f'{GH_API}/repos/{owner}/{repo}/releases' | |
r = requests.get(RELEASE_ENDPOINT, headers={"Authorization": f'token {token}'}) | |
if r.status_code >= 400: | |
err(f'Request failed with code: {r.status_code}') | |
exit(1) | |
resp = r.json() | |
for item in resp: | |
if remove_all is None and not item.get('draft'): | |
continue | |
release_id = item.get('id') | |
r = requests.delete(f'{RELEASE_ENDPOINT}/{release_id}', headers={'Authorization': f'token {token}'}) | |
if r.status_code >= 400: | |
err(f'Request failed with code: {r.status_code}') | |
exit(1) | |
else: | |
print(f'Release {release_id} was removed') | |
print('Done !!') | |
exit(0) |
Create your PAT (Personal Access Token) at https://github.com/settings/tokens
Remove remote tags
git tag -l | xargs -n 1 git push --delete origin
Remove local tags
git tag -l | xargs -n 1 git tag -d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be careful with
REMOVE_ALL
:)