Skip to content

Commit

Permalink
add further logging for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaresincic committed Sep 7, 2022
1 parent 434c0b4 commit 921cdca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
100 changes: 55 additions & 45 deletions delete-versions.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
Expand All @@ -34,77 +38,83 @@ 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]);
}
}
return deleted;
}

module.exports = async function(inputs) {
module.exports = async function (inputs) {
let packages = await findAll(
inputs.token,
queries.packages,
{
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(
Expand All @@ -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({
Expand All @@ -123,4 +133,4 @@ module.exports = async function(inputs) {
}
}
return deleted;
}
};
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 921cdca

Please sign in to comment.