From d8f2ea8e2af1e7434d01025c686dc4cfc5e7eef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Mon, 4 May 2020 11:33:08 +0200 Subject: [PATCH 1/9] Log skip-recent from input --- action.yml | 3 +++ index.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/action.yml b/action.yml index d7b9e3d..22a0aa0 100644 --- a/action.yml +++ b/action.yml @@ -15,3 +15,6 @@ inputs: skip-tags: description: 'true/false. If enabled, tag build artifacts (e.g. release artifacts) will be kept.' required: false + skip-recent: + description: 'Keep the specified number of artifacts even if they're older than the age.' + required: false diff --git a/index.js b/index.js index 5ff41ec..8f24ccb 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,8 @@ function getConfigs() { ")" ); + console.log(core.getInput("skip-recent")); + return { repo: { owner, @@ -39,6 +41,7 @@ function getConfigs() { skipTags: devEnv ? yn(process.env.SKIP_TAGS) : yn(core.getInput("skip-tags")), + skipRecent: core.getInput("skip-recent"), retriesEnabled: true, }; } From c526f73deb49ddb8c298d386c06fb9fb077ad730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:05:38 +0200 Subject: [PATCH 2/9] Extend .env.example with the new option. --- .env.example | 1 + 1 file changed, 1 insertion(+) diff --git a/.env.example b/.env.example index 4a19e6e..b9acd0e 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,4 @@ GITHUB_REPOSITORY= GITHUB_ACTION= AGE="1 seconds" SKIP_TAGS= +SKIP_RECENT= From ea5fa2da39b1310168d5acc5db30b575be3d0424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:05:46 +0200 Subject: [PATCH 3/9] Add feature "skip recent artifacts" --- index.js | 112 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 76 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 8f24ccb..1ad9cc8 100644 --- a/index.js +++ b/index.js @@ -6,16 +6,32 @@ const yn = require("yn"); const devEnv = process.env.NODE_ENV === "dev"; +const optionKeys = { + AGE: devEnv ? "AGE" : "age", + SKIP_TAGS: devEnv ? "SKIP_TAGS" : "skip-tags", + SKIP_RECENT: devEnv ? "SKIP_RECENT" : "skip-recent", +}; + if (devEnv) { // eslint-disable-next-line global-require, import/no-extraneous-dependencies require("dotenv-safe").config(); } +function readOption(key, isRequired = false) { + if (!optionKeys[key]) { + throw new Error(`${key} does not exist on the available options.`); + } + + if (devEnv) { + return process.env[key]; + } + + return core.getInput(key, { required: isRequired }); +} + function getConfigs() { const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); - const [age, units] = devEnv - ? process.env.AGE.split(" ") - : core.getInput("age", { required: true }).split(" "); + const [age, units] = readOption(optionKeys.AGE, true).split(" "); const maxAge = moment().subtract(age, units); console.log( @@ -27,7 +43,15 @@ function getConfigs() { ")" ); - console.log(core.getInput("skip-recent")); + const skipRecent = readOption(optionKeys.SKIP_RECENT); + + if (skipRecent) { + const parsedRecent = Number(skipRecent); + + if (Number.isNaN(parsedRecent)) { + throw new Error("skip-recent option must be type of number."); + } + } return { repo: { @@ -38,10 +62,8 @@ function getConfigs() { perPage: 100, }, maxAge: moment().subtract(age, units), - skipTags: devEnv - ? yn(process.env.SKIP_TAGS) - : yn(core.getInput("skip-tags")), - skipRecent: core.getInput("skip-recent"), + skipTags: yn(readOption(optionKeys.SKIP_TAGS)), + skipRecent: Number(skipRecent), retriesEnabled: true, }; } @@ -104,6 +126,8 @@ async function run() { } ); + let skippedArtifactsCounter = 0; + return octokit .paginate(workflowRunsRequest, ({ data }, done) => { const stopPagination = data.find(workflowRun => { @@ -121,10 +145,10 @@ async function run() { .then(workflowRuns => { const artifactPromises = workflowRuns .filter(workflowRun => { - const skipWorkflow = + const skipTaggedWorkflow = configs.skipTags && taggedCommits.includes(workflowRun.head_sha); - if (skipWorkflow) { + if (skipTaggedWorkflow) { console.log(`Skipping tagged run ${workflowRun.head_sha}`); return false; @@ -141,36 +165,52 @@ async function run() { } ); - return octokit.paginate(workflowRunArtifactsRequest).then(artifacts => - artifacts - .filter(artifact => { - const createdAt = moment(artifact.created_at); + return octokit + .paginate(workflowRunArtifactsRequest) + .then(artifacts => { + return artifacts + .filter(artifact => { + const skipRecentArtifact = + configs.skipRecent && + configs.skipRecent > skippedArtifactsCounter; - return createdAt.isBefore(configs.maxAge); - }) - .map(artifact => { - if (devEnv) { - return new Promise(resolve => { + if (skipRecentArtifact) { console.log( - `Recognized development environment, preventing ${artifact.id} from being removed.` + `Skipping recent artifact (id: ${artifact.id}, name: ${artifact.name}).` ); - resolve(); - }); - } - - return octokit.actions - .deleteArtifact({ - ...configs.repo, - artifact_id: artifact.id, - }) - .then(() => { - console.log( - `Successfully removed artifact with id ${artifact.id}.` - ); - }); - }) - ); + skippedArtifactsCounter += 1; + + return false; + } + + const createdAt = moment(artifact.created_at); + + return createdAt.isBefore(configs.maxAge); + }) + .map(artifact => { + if (devEnv) { + return new Promise(resolve => { + console.log( + `Recognized development environment, preventing artifact (id: ${artifact.id}, name: ${artifact.name}) from being removed.` + ); + + resolve(); + }); + } + + return octokit.actions + .deleteArtifact({ + ...configs.repo, + artifact_id: artifact.id, + }) + .then(() => { + console.log( + `Successfully removed artifact (id: ${artifact.id}, name: ${artifact.name}).` + ); + }); + }); + }); }); return Promise.all(artifactPromises).then(artifactDeletePromises => From 520bf2f3629f564ad5057c997c612d39e0da3a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:10:32 +0200 Subject: [PATCH 4/9] Fix action.yml --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 22a0aa0..c13c32f 100644 --- a/action.yml +++ b/action.yml @@ -16,5 +16,5 @@ inputs: description: 'true/false. If enabled, tag build artifacts (e.g. release artifacts) will be kept.' required: false skip-recent: - description: 'Keep the specified number of artifacts even if they're older than the age.' + description: 'Keep the specified number of artifacts even if they are older than the age.' required: false From 723beb705573f0af2d44495435e4b7896fc03e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:15:31 +0200 Subject: [PATCH 5/9] Fix readOptions(). --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index 1ad9cc8..f63beea 100644 --- a/index.js +++ b/index.js @@ -18,10 +18,6 @@ if (devEnv) { } function readOption(key, isRequired = false) { - if (!optionKeys[key]) { - throw new Error(`${key} does not exist on the available options.`); - } - if (devEnv) { return process.env[key]; } From 6f56ebd475f62dbae47f26478b4d3c08c347a6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:18:20 +0200 Subject: [PATCH 6/9] Use 'skip-recent' option --- .github/workflows/run.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/run.yml b/.github/workflows/run.yml index 9af715f..cc67367 100644 --- a/.github/workflows/run.yml +++ b/.github/workflows/run.yml @@ -32,6 +32,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} age: '1 second' skip-tags: true + skip-recent: 1 create-test-artifact: runs-on: ubuntu-latest From b66e06ad66241149316bf5ca0e7db4059d2a82ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:21:11 +0200 Subject: [PATCH 7/9] Improve namings --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index f63beea..c414d1b 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ const yn = require("yn"); const devEnv = process.env.NODE_ENV === "dev"; -const optionKeys = { +const inputKeys = { AGE: devEnv ? "AGE" : "age", SKIP_TAGS: devEnv ? "SKIP_TAGS" : "skip-tags", SKIP_RECENT: devEnv ? "SKIP_RECENT" : "skip-recent", @@ -17,7 +17,7 @@ if (devEnv) { require("dotenv-safe").config(); } -function readOption(key, isRequired = false) { +function readInput(key, isRequired = false) { if (devEnv) { return process.env[key]; } @@ -27,7 +27,7 @@ function readOption(key, isRequired = false) { function getConfigs() { const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); - const [age, units] = readOption(optionKeys.AGE, true).split(" "); + const [age, units] = readInput(inputKeys.AGE, true).split(" "); const maxAge = moment().subtract(age, units); console.log( @@ -39,7 +39,7 @@ function getConfigs() { ")" ); - const skipRecent = readOption(optionKeys.SKIP_RECENT); + const skipRecent = readInput(inputKeys.SKIP_RECENT); if (skipRecent) { const parsedRecent = Number(skipRecent); @@ -58,7 +58,7 @@ function getConfigs() { perPage: 100, }, maxAge: moment().subtract(age, units), - skipTags: yn(readOption(optionKeys.SKIP_TAGS)), + skipTags: yn(readInput(inputKeys.SKIP_TAGS)), skipRecent: Number(skipRecent), retriesEnabled: true, }; From 9179d14bfeb3761a7469240877fbc6922de3b42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Wed, 6 May 2020 11:23:50 +0200 Subject: [PATCH 8/9] Remove needless return statement --- index.js | 84 +++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/index.js b/index.js index c414d1b..ec4f5ad 100644 --- a/index.js +++ b/index.js @@ -161,52 +161,50 @@ async function run() { } ); - return octokit - .paginate(workflowRunArtifactsRequest) - .then(artifacts => { - return artifacts - .filter(artifact => { - const skipRecentArtifact = - configs.skipRecent && - configs.skipRecent > skippedArtifactsCounter; - - if (skipRecentArtifact) { + return octokit.paginate(workflowRunArtifactsRequest).then(artifacts => + artifacts + .filter(artifact => { + const skipRecentArtifact = + configs.skipRecent && + configs.skipRecent > skippedArtifactsCounter; + + if (skipRecentArtifact) { + console.log( + `Skipping recent artifact (id: ${artifact.id}, name: ${artifact.name}).` + ); + + skippedArtifactsCounter += 1; + + return false; + } + + const createdAt = moment(artifact.created_at); + + return createdAt.isBefore(configs.maxAge); + }) + .map(artifact => { + if (devEnv) { + return new Promise(resolve => { console.log( - `Skipping recent artifact (id: ${artifact.id}, name: ${artifact.name}).` + `Recognized development environment, preventing artifact (id: ${artifact.id}, name: ${artifact.name}) from being removed.` ); - skippedArtifactsCounter += 1; - - return false; - } - - const createdAt = moment(artifact.created_at); - - return createdAt.isBefore(configs.maxAge); - }) - .map(artifact => { - if (devEnv) { - return new Promise(resolve => { - console.log( - `Recognized development environment, preventing artifact (id: ${artifact.id}, name: ${artifact.name}) from being removed.` - ); - - resolve(); - }); - } - - return octokit.actions - .deleteArtifact({ - ...configs.repo, - artifact_id: artifact.id, - }) - .then(() => { - console.log( - `Successfully removed artifact (id: ${artifact.id}, name: ${artifact.name}).` - ); - }); - }); - }); + resolve(); + }); + } + + return octokit.actions + .deleteArtifact({ + ...configs.repo, + artifact_id: artifact.id, + }) + .then(() => { + console.log( + `Successfully removed artifact (id: ${artifact.id}, name: ${artifact.name}).` + ); + }); + }) + ); }); return Promise.all(artifactPromises).then(artifactDeletePromises => From e7ec32d41cffee12904a14771d6e62962a19042c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Go=CC=88mo=CC=88ri?= Date: Thu, 7 May 2020 09:24:14 +0200 Subject: [PATCH 9/9] Update sample code --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6237bac..233746c 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ jobs: uses: c-hive/gha-remove-artifacts@v1 with: age: '1 month' - skip-tags: true + # Optional inputs + # skip-tags: true + # skip-recent: 5 ``` ## Conventions