diff --git a/delete-versions.js b/delete-versions.js index 689bf39..c78b0b7 100644 --- a/delete-versions.js +++ b/delete-versions.js @@ -1,18 +1,22 @@ -const graphql = require('./graphql'); -const queries = require('./queries'); +const { info } = require("@actions/core"); +const graphql = require("./graphql"); +const queries = require("./queries"); -async function findAll(token, query, params, findObject, elements = [], cursor = null) { +async function findAll( + token, + query, + params, + findObject, + elements = [], + cursor = null +) { while (true) { - let rsp = await graphql( - token, - query, - { - ...params, - after: cursor, - } - ); + let rsp = await graphql(token, query, { + ...params, + after: cursor, + }); const obj = findObject(rsp); - elements.push(...obj.edges.map(edge => edge.node)); + elements.push(...obj.edges.map((edge) => edge.node)); const pageInfo = obj.pageInfo; if (!pageInfo.hasNextPage) { break; @@ -23,7 +27,7 @@ async function findAll(token, query, params, findObject, elements = [], cursor = } async function findAllPackageVersions(token, package, owner, repo) { - let versions = package.versions.edges.map(edge => edge.node); + let versions = package.versions.edges.map((edge) => edge.node); const pageInfo = package.versions.pageInfo; if (pageInfo.hasNextPage) { await findAll( @@ -34,56 +38,60 @@ async function findAllPackageVersions(token, package, owner, repo) { repo, package: package.name, }, - rsp => rsp.repository.packages.edges[0].node.versions, + (rsp) => rsp.repository.packages.edges[0].node.versions, versions, - pageInfo.endCursor, + pageInfo.endCursor ); } return versions; } -async function deletePackageVersions(token, package, owner, repo, keepCnt, dryRun) { +async function deletePackageVersions( + token, + package, + owner, + repo, + keepCnt, + dryRun +) { const versions = await findAllPackageVersions(token, package, owner, repo); const majorVersions = {}; - versions.forEach(v => { - const mv = v.version.split('.')[0]; + versions.forEach((v) => { + const mv = v.version.split(".")[0]; if (!majorVersions[mv]) { majorVersions[mv] = []; } majorVersions[mv].push(v); }); - + const toDelete = []; for (const key in majorVersions) { let vs = majorVersions[key]; vs = vs.reverse(); if (vs.length <= keepCnt) { - continue + continue; } vs = vs.slice(0, vs.length - keepCnt); - vs.forEach(v => { + vs.forEach((v) => { toDelete.push(v); }); } const deleted = []; - for(let i = 0; i < toDelete.length; i++) { - // on dryRun just add as deleted - if (dryRun) { - deleted.push(toDelete[i]); - continue; - } - const rsp = await graphql( - token, - queries.deletePackageVersion, - { - packageVersionId: toDelete[i].id, - headers: { - Accept: 'application/vnd.github.package-deletes-preview+json', - } - } - ); + for (let i = 0; i < toDelete.length; i++) { + // on dryRun just add as deleted + if (dryRun) { + info("deletePackageVersion received dryRun as true"); + deleted.push(toDelete[i]); + continue; + } + const rsp = await graphql(token, queries.deletePackageVersion, { + packageVersionId: toDelete[i].id, + headers: { + Accept: "application/vnd.github.package-deletes-preview+json", + }, + }); if (rsp.deletePackageVersion.success) { deleted.push(toDelete[i]); } @@ -91,7 +99,7 @@ async function deletePackageVersions(token, package, owner, repo, keepCnt, dryRu return deleted; } -module.exports = async function(inputs) { +module.exports = async function (inputs) { let packages = await findAll( inputs.token, queries.packages, @@ -99,12 +107,14 @@ module.exports = async function(inputs) { owner: inputs.owner, repo: inputs.repo, }, - rsp => rsp.repository.packages - ) - packages = packages.filter(p => { - return !p.name.startsWith('deleted_'); + (rsp) => rsp.repository.packages + ); + info("All packages list:"); + packages = packages.filter((p) => { + info(p.name); + return !p.name.startsWith("deleted_"); }); - + const deleted = []; for (let i = 0; i < packages.length; i++) { const vs = await deletePackageVersions( @@ -113,7 +123,7 @@ module.exports = async function(inputs) { inputs.owner, inputs.repo, inputs.keepCnt, - inputs.dryRun, + inputs.dryRun ); if (vs.length > 0) { deleted.push({ @@ -123,4 +133,4 @@ module.exports = async function(inputs) { } } return deleted; -} +}; diff --git a/index.js b/index.js index a4daa09..4aff0a4 100644 --- a/index.js +++ b/index.js @@ -12,11 +12,11 @@ async function run() { token: getInput("token"), }; info("Input values:"); - Object.values(inputs).map((v) => info(v)); + Object.entries(inputs).map((v) => info(v[0] + ": " + v[1])); const deleted = await deleteVersions(inputs); if (inputs.dryRun) { info( - "Dry run is NOT deleting packages, list below is what would be deleted" + "Dry run is NOT deleting packages, list below is what would be deleted, dryRun: " + Number(inputs.dryRun) ); } deleted.forEach((package) => {