-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build: add back workflow to delete old pre-releases (#571)
- Loading branch information
1 parent
5204eac
commit b1f7ea1
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |
This file contains 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
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 |