Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: add back workflow to delete old pre-releases #571

Merged
merged 9 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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