Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
✨ Add feature to delete by tag pattern (#8)
Browse files Browse the repository at this point in the history
* Implement pattern to match logic

* more logging, more items

* Apply suggestions from code review

Co-authored-by: Thad Guidry <thadguidry@gmail.com>

* fix logging mistake

* Apply suggestions from code review

Co-authored-by: REDDY PRASAD <dev.drprasad@aim.com>

* fix after suggestions

Co-authored-by: Dennis Trümper <privat@truemper-nord.de>
Co-authored-by: Thad Guidry <thadguidry@gmail.com>
Co-authored-by: REDDY PRASAD <dev.drprasad@aim.com>
  • Loading branch information
4 people authored Dec 7, 2020
1 parent b1e01bf commit e3510b2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ Specifies whether to delete tags associated to older releases or not. Older tags
| false | repo executing action |

Repo name in the format of `<owner>/<repoName>`. Defaults to the repo that executing this action

#### `delete_tag_pattern`

| required | default |
| -------- | ------------ |
| false | empty string |

Specifies a pattern to match. If not specified then every release will be targeted. If specified then every release containing the pattern will be targeted. Use this option for example to remove old beta releases.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ inputs:
delete_tags:
description: whether to delete tags associated to older releases or not
required: false
delete_tag_pattern:
description: part of the tag name. Example, if you want to delete 0.0.1-beta and 0.0.2-beta but not 0.0.1 then set this to just "beta". If not set then it will target all releases.
required: false

runs:
using: "node12"
Expand Down
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ if (shouldDeleteTags) {
console.log("🔖 corresponding tags also will be deleted");
}

let deletePattern = process.env.INPUT_DELETE_TAG_PATTERN || "";
if (deletePattern) {
console.log(`releases containing ${deletePattern} will be targeted`);
}
const commonOpts = {
host: "api.github.com",
port: 443,
Expand All @@ -69,19 +73,26 @@ async function deleteOlderReleases(keepLatest) {
try {
let data = await fetch({
...commonOpts,
path: `/repos/${owner}/${repo}/releases`,
path: `/repos/${owner}/${repo}/releases?per_page=100`,
method: "GET",
});
data = data || [];
const activeReleases = data.filter(({ draft }) => !draft);
if (activeReleases.length === 0) {
// filter for delete_pattern
const activeMatchedReleases = data.filter(
({ draft, tag_name }) => !draft && tag_name.indexOf(deletePattern) !== -1
);

if (activeMatchedReleases.length === 0) {
console.log(`😕 no active releases found. exiting...`);
return;
}

const matchingLoggingAddition = deletePattern.length > 0 ? " matching" : "";

console.log(
`💬 found total of ${activeReleases.length} active release(s)`
`💬 found total of ${activeMatchedReleases.length}${matchingLoggingAddition} active release(s)`
);
releaseIdsAndTags = activeReleases
releaseIdsAndTags = activeMatchedReleases
.map(({ id, tag_name: tagName }) => ({ id, tagName }))
.slice(keepLatest);
} catch (error) {
Expand All @@ -102,6 +113,8 @@ async function deleteOlderReleases(keepLatest) {
const { id: releaseId, tagName } = releaseIdsAndTags[i];

try {
console.log(`starting to delete ${tagName} with id ${releaseId}`);

const _ = await fetch({
...commonOpts,
path: `/repos/${owner}/${repo}/releases/${releaseId}`,
Expand Down

0 comments on commit e3510b2

Please sign in to comment.