From 061db02c9a6b96720ee3a98971315ca24310ebcb Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 18:41:59 -0500 Subject: [PATCH 1/7] Build: add back workflow to delete old pre-releases --- .github/scripts/delete_old_pre_releases.py | 34 +++++++++++++++++++ .../workflows/delete-old-pre-releases.yaml | 20 +++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .github/scripts/delete_old_pre_releases.py create mode 100644 .github/workflows/delete-old-pre-releases.yaml diff --git a/.github/scripts/delete_old_pre_releases.py b/.github/scripts/delete_old_pre_releases.py new file mode 100644 index 000000000..e5c2e795c --- /dev/null +++ b/.github/scripts/delete_old_pre_releases.py @@ -0,0 +1,34 @@ +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_name, github_token): + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_name}" + 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.now() + 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: + delete_release(repo_owner, repo_name, release["name"], 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) diff --git a/.github/workflows/delete-old-pre-releases.yaml b/.github/workflows/delete-old-pre-releases.yaml new file mode 100644 index 000000000..6c6bb2af2 --- /dev/null +++ b/.github/workflows/delete-old-pre-releases.yaml @@ -0,0 +1,20 @@ +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: ./.github/scripts/delete_old_pre_releases.py \ No newline at end of file From 4143b7d2b25bb7db10f1ea2c26b4d922f061cacf Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 18:51:36 -0500 Subject: [PATCH 2/7] Comment out to test --- .github/scripts/delete_old_pre_releases.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/scripts/delete_old_pre_releases.py b/.github/scripts/delete_old_pre_releases.py index e5c2e795c..942aea414 100644 --- a/.github/scripts/delete_old_pre_releases.py +++ b/.github/scripts/delete_old_pre_releases.py @@ -24,8 +24,10 @@ def delete_old_pre_releases(repo_owner, repo_name, github_token): now = datetime.now() 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: - delete_release(repo_owner, repo_name, release["name"], github_token) + # TODO increase to 7 days after verifying it works. + if release["prerelease"] and (now - published_at).days >= 0: + print(f"Deleting {release['name']}") + # delete_release(repo_owner, repo_name, release["name"], github_token) if __name__ == "__main__": github_token = os.environ["GITHUB_TOKEN"] From 15abe197d0a7535c1cf5125b139159806372ca91 Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 19:03:56 -0500 Subject: [PATCH 3/7] Run on PR --- .github/scripts/delete_old_pre_releases.py | 12 ++++++------ .github/workflows/delete-old-pre-releases.yaml | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/scripts/delete_old_pre_releases.py b/.github/scripts/delete_old_pre_releases.py index 942aea414..ecafb3843 100644 --- a/.github/scripts/delete_old_pre_releases.py +++ b/.github/scripts/delete_old_pre_releases.py @@ -12,12 +12,12 @@ def get_releases(repo_owner, repo_name, github_token): releases = json.loads(response.content) return releases -def delete_release(repo_owner, repo_name, release_name, github_token): - url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_name}" - 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_release(repo_owner, repo_name, release_name, github_token): +# url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_name}" +# 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) diff --git a/.github/workflows/delete-old-pre-releases.yaml b/.github/workflows/delete-old-pre-releases.yaml index 6c6bb2af2..676b04d12 100644 --- a/.github/workflows/delete-old-pre-releases.yaml +++ b/.github/workflows/delete-old-pre-releases.yaml @@ -6,6 +6,9 @@ on: - main schedule: - cron: '0 0 * * 0' + pull_request: + branches: + - main workflow_dispatch: jobs: From 77ac8b0833f92fd64de50099c6aaf7afe156fa80 Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 19:05:51 -0500 Subject: [PATCH 4/7] use python --- .github/workflows/delete-old-pre-releases.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/delete-old-pre-releases.yaml b/.github/workflows/delete-old-pre-releases.yaml index 676b04d12..2a621dedc 100644 --- a/.github/workflows/delete-old-pre-releases.yaml +++ b/.github/workflows/delete-old-pre-releases.yaml @@ -20,4 +20,5 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v3 - - run: ./.github/scripts/delete_old_pre_releases.py \ No newline at end of file + - run: python3 -m pip install requests + - run: python3 .github/scripts/delete_old_pre_releases.py \ No newline at end of file From 9a3785e77dfa3d30c1f1cf7bccf15f822fe0f315 Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 19:09:02 -0500 Subject: [PATCH 5/7] Actually delete --- .github/scripts/delete_old_pre_releases.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/scripts/delete_old_pre_releases.py b/.github/scripts/delete_old_pre_releases.py index ecafb3843..420b500f4 100644 --- a/.github/scripts/delete_old_pre_releases.py +++ b/.github/scripts/delete_old_pre_releases.py @@ -12,12 +12,12 @@ def get_releases(repo_owner, repo_name, github_token): releases = json.loads(response.content) return releases -# def delete_release(repo_owner, repo_name, release_name, github_token): -# url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_name}" -# 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_release(repo_owner, repo_name, release_name, github_token): + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_name}" + 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) @@ -27,7 +27,7 @@ def delete_old_pre_releases(repo_owner, repo_name, github_token): # TODO increase to 7 days after verifying it works. if release["prerelease"] and (now - published_at).days >= 0: print(f"Deleting {release['name']}") - # delete_release(repo_owner, repo_name, release["name"], github_token) + delete_release(repo_owner, repo_name, release["name"], github_token) if __name__ == "__main__": github_token = os.environ["GITHUB_TOKEN"] From 26696978d4302a688a28d1058f6e0fea84870fa5 Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 19:16:28 -0500 Subject: [PATCH 6/7] Working locally --- .github/scripts/delete_old_pre_releases.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/scripts/delete_old_pre_releases.py b/.github/scripts/delete_old_pre_releases.py index 420b500f4..90639574e 100644 --- a/.github/scripts/delete_old_pre_releases.py +++ b/.github/scripts/delete_old_pre_releases.py @@ -12,8 +12,8 @@ def get_releases(repo_owner, repo_name, github_token): releases = json.loads(response.content) return releases -def delete_release(repo_owner, repo_name, release_name, github_token): - url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/{release_name}" +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: @@ -21,13 +21,12 @@ def delete_release(repo_owner, repo_name, release_name, github_token): def delete_old_pre_releases(repo_owner, repo_name, github_token): releases = get_releases(repo_owner, repo_name, github_token) - now = datetime.now() + now = datetime.utcnow() for release in releases: published_at = datetime.strptime(release["published_at"], '%Y-%m-%dT%H:%M:%SZ') - # TODO increase to 7 days after verifying it works. - if release["prerelease"] and (now - published_at).days >= 0: - print(f"Deleting {release['name']}") - delete_release(repo_owner, repo_name, release["name"], github_token) + 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"] From d0a290f997f48bd9d9d5fb25e297c8dff83ee5f4 Mon Sep 17 00:00:00 2001 From: Alex Klibisz Date: Sun, 5 Nov 2023 19:21:58 -0500 Subject: [PATCH 7/7] Don't run delete script on PRs --- .github/workflows/delete-old-pre-releases.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/delete-old-pre-releases.yaml b/.github/workflows/delete-old-pre-releases.yaml index 2a621dedc..02d99976d 100644 --- a/.github/workflows/delete-old-pre-releases.yaml +++ b/.github/workflows/delete-old-pre-releases.yaml @@ -6,9 +6,6 @@ on: - main schedule: - cron: '0 0 * * 0' - pull_request: - branches: - - main workflow_dispatch: jobs: