Skip to content

Commit

Permalink
Build: add back workflow to delete old pre-releases (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexklibisz authored Nov 6, 2023
1 parent 5204eac commit b1f7ea1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/scripts/delete_old_pre_releases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import datetime
import json
import requests
import os

def get_releases(repo_owner, repo_name, github_token):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases"
headers = {"Authorization": f"token {github_token}"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception("Failed to get releases: {}".format(response.content))
releases = json.loads(response.content)
return releases

def delete_release(repo_owner, repo_name, release_id, github_token):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_id}"
headers = {"Authorization": f"token {github_token}"}
response = requests.delete(url, headers=headers)
if response.status_code != 204:
raise Exception("Failed to delete release: {}".format(response.content))

def delete_old_pre_releases(repo_owner, repo_name, github_token):
releases = get_releases(repo_owner, repo_name, github_token)
now = datetime.utcnow()
for release in releases:
published_at = datetime.strptime(release["published_at"], '%Y-%m-%dT%H:%M:%SZ')
if release["prerelease"] and (now - published_at).days >= 7:
print(f"Deleting {release['name']} ({release['id']})")
delete_release(repo_owner, repo_name, release["id"], github_token)

if __name__ == "__main__":
github_token = os.environ["GITHUB_TOKEN"]
REPO_OWNER = "alexklibisz"
REPO_NAME = "elastiknn"
delete_old_pre_releases(REPO_OWNER, REPO_NAME, github_token)
21 changes: 21 additions & 0 deletions .github/workflows/delete-old-pre-releases.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "Delete Old Pre-releases"

on:
push:
branches:
- main
schedule:
- cron: '0 0 * * 0'
workflow_dispatch:

jobs:
cleanup-snapshots:
name: Cleanup Snapshots
runs-on:
- ubuntu-22.04
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- run: python3 -m pip install requests
- run: python3 .github/scripts/delete_old_pre_releases.py

0 comments on commit b1f7ea1

Please sign in to comment.