From 866dbd78d4e85c88ab4f5c46cfc796489713e3ab Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:45:36 -0400 Subject: [PATCH] fix: replace unmaintained dev-drprasad actions --- .github/workflows/ci.yml | 8 ++--- action.yml | 72 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 47c2359..14704de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,8 +66,8 @@ jobs: run: sleep 60 - name: Delete Release + env: + GITHUB_TOKEN: ${{ secrets.GH_BOT_TOKEN }} if: ${{ always() && github.event_name == 'pull_request' }} - uses: dev-drprasad/delete-tag-and-release@v1.1 - with: - github_token: ${{ secrets.GH_BOT_TOKEN }} - tag_name: ${{ steps.vars.outputs.release_tag }} + run: | + gh release delete ${{ steps.vars.outputs.release_tag }} --cleanup-tag --yes diff --git a/action.yml b/action.yml index e0ab205..2d19f7c 100644 --- a/action.yml +++ b/action.yml @@ -86,11 +86,69 @@ runs: if: >- inputs.deleteOtherPreReleases == 'true' && (github.event_name == 'push' && github.ref == 'refs/heads/master') - uses: dev-drprasad/delete-older-releases@v0.3.4 - with: - keep_latest: ${{ inputs.keepPreReleaseCount }} - delete_prerelease_only: true - delete_tag_pattern: v(\d{4,})\.(\d{1,4})\.(\d{1,6})(\.(\d{1,2}))? - delete_tags: ${{ inputs.deletePreReleaseTags }} + uses: actions/github-script@v7 env: - GITHUB_TOKEN: ${{ inputs.token }} + DELETE_TAGS: ${{ inputs.deletePreReleaseTags }} + KEEP_LATEST: ${{ inputs.keepPreReleaseCount }} + with: + github-token: ${{ inputs.token }} + script: | + // process input + const DELETE_TAGS = process.env.DELETE_TAGS.toLowerCase() === 'true'; + const KEEP_LATEST = parseInt(process.env.KEEP_LATEST, 10); + + console.log(`DELETE_TAGS: ${DELETE_TAGS}`); + console.log(`KEEP_LATEST: ${KEEP_LATEST}`); + + // list releases + const repoOpts = github.rest.repos.listReleases.endpoint.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + }); + const allReleases = await github.paginate(repoOpts); + + let regexPattern = new RegExp('^v(\\d{4,})\\.(\\d{1,4})\\.(\\d{1,6})(\\.(\\d{1,2}))?$'); + let preReleases = allReleases.filter(release => release.prerelease && regexPattern.test(release.tag_name)); + + // sort by tag/version number (e.g. v1.2.3 or v1.2.3.4) + preReleases.sort((a, b) => { + const aParts = a.tag_name.split('.').map(Number); + const bParts = b.tag_name.split('.').map(Number); + for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) { + if (aParts[i] === undefined) return -1; + if (bParts[i] === undefined) return 1; + if (aParts[i] < bParts[i]) return -1; + if (aParts[i] > bParts[i]) return 1; + } + return 0; + }); + + // delete all but the last n pre-releases + for (let i = 0; i < preReleases.length - KEEP_LATEST; i++) { + const release = preReleases[i]; + console.log(`Deleting release: ${release.tag_name}`); + try { + await github.rest.repos.deleteRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + release_id: release.id + }); + } catch (error) { + console.error(`Failed to delete release: ${release.tag_name}`); + console.error(error); + } + + if (DELETE_TAGS) { + console.log(`Deleting tag: ${release.tag_name}`); + try { + await github.rest.git.deleteRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: `tags/${release.tag_name}` + }); + } catch (error) { + console.error(`Failed to delete tag: ${release.tag_name}`); + console.error(error); + } + } + }