From 1e2b03adad856d53f9d3cebf5bf53f4ddffc3b1e Mon Sep 17 00:00:00 2001 From: gomorizsolt Date: Wed, 25 Mar 2020 10:35:50 +0000 Subject: [PATCH] Update distified version --- dist/index.js | 132 +++++++++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 55 deletions(-) diff --git a/dist/index.js b/dist/index.js index cb77732..7989a74 100644 --- a/dist/index.js +++ b/dist/index.js @@ -528,10 +528,13 @@ function getConfigs() { ); return { - repoOptions: { + repo: { owner, repo, }, + pagination: { + perPage: 100, + }, maxAge: moment().subtract(age, units), skipTags: devEnv ? yn(process.env.SKIP_TAGS) @@ -569,7 +572,8 @@ async function run() { async function getTaggedCommits() { const listTagsRequest = octokit.repos.listTags.endpoint.merge({ - ...configs.repoOptions, + ...configs.repo, + per_page: configs.pagination.perPage, ref: "tags", }); @@ -591,67 +595,85 @@ async function run() { } const workflowRunsRequest = octokit.actions.listRepoWorkflowRuns.endpoint.merge( - configs.repoOptions + { + ...configs.repo, + per_page: configs.pagination.perPage, + } ); - return octokit.paginate(workflowRunsRequest).then(workflowRuns => { - const artifactPromises = workflowRuns - .filter(workflowRun => { - const skipWorkflow = - configs.skipTags && taggedCommits.includes(workflowRun.head_sha); + return octokit + .paginate(workflowRunsRequest, ({ data }, done) => { + const stopPagination = data.find(workflowRun => { + const createdAt = moment(workflowRun.created_at); - if (skipWorkflow) { - console.log(`Skipping tagged run ${workflowRun.head_sha}`); + return createdAt.isBefore(moment.utc().subtract(90, "days")); + }); - return false; - } + if (stopPagination) { + done(); + } - return true; - }) - .map(workflowRun => { - const workflowRunArtifactsRequest = octokit.actions.listWorkflowRunArtifacts.endpoint.merge( - { - ...configs.repoOptions, - run_id: workflowRun.id, + return data; + }) + .then(workflowRuns => { + const artifactPromises = workflowRuns + .filter(workflowRun => { + const skipWorkflow = + configs.skipTags && taggedCommits.includes(workflowRun.head_sha); + + if (skipWorkflow) { + console.log(`Skipping tagged run ${workflowRun.head_sha}`); + + return false; } - ); - return octokit.paginate(workflowRunArtifactsRequest).then(artifacts => - artifacts - .filter(artifact => { - 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} from being removed.` - ); - - resolve(); - }); - } - - return octokit.actions - .deleteArtifact({ - ...configs.repoOptions, - artifact_id: artifact.id, - }) - .then(() => { - console.log( - `Successfully removed artifact with id ${artifact.id}.` - ); - }); - }) - ); - }); + return true; + }) + .map(workflowRun => { + const workflowRunArtifactsRequest = octokit.actions.listWorkflowRunArtifacts.endpoint.merge( + { + ...configs.repo, + per_page: configs.pagination.perPage, + run_id: workflowRun.id, + } + ); + + return octokit.paginate(workflowRunArtifactsRequest).then(artifacts => + artifacts + .filter(artifact => { + 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} from being removed.` + ); + + resolve(); + }); + } - return Promise.all(artifactPromises).then(artifactDeletePromises => - Promise.all([].concat(...artifactDeletePromises)) - ); - }); + return octokit.actions + .deleteArtifact({ + ...configs.repo, + artifact_id: artifact.id, + }) + .then(() => { + console.log( + `Successfully removed artifact with id ${artifact.id}.` + ); + }); + }) + ); + }); + + return Promise.all(artifactPromises).then(artifactDeletePromises => + Promise.all([].concat(...artifactDeletePromises)) + ); + }); } run().catch(err => {