Skip to content

Commit

Permalink
Add job summary
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaurin committed Oct 23, 2022
1 parent 51c0937 commit 247a3b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
30 changes: 29 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ const dryRunDelete = (version) => new Promise((resolve) => {
resolve();
});

const writeSummary = async (container, dryRun, pruningVersions, prunedVersions) => {
const allPruned = pruningVersions.length === prunedVersions.length;

let summary = core.summary.addHeading(`GHCR pruning for \`${container}\``, 2);

if (dryRun) {
summary = summary.addRaw(':warning: This is a dry run, no container versions were actually deleted.')
.addBreak();
}

summary = summary.addRaw(`${allPruned ? ':white_check_mark:' : ':x:'} ${prunedList.length} out of ${pruningList.length} identified versions were pruned successfully.`);

await summary.addHeading('Pruned versions', 3)
.addRaw(`The following ${prunedList.length} versions were successfully pruned:`)
.addTable([
[{data: 'ID', header: true}, {data: 'Name', header: true}, {data: 'Created at', header: true}, {data: 'Tags', header: true}],
...prunedVersions.map((version) => ([
String(version.id),
version.name,
version.created_at,
version.metadata.container.tags.join(', '),
])),
])
.write();
};

const run = async () => {
try {
const token = core.getInput('token');
Expand Down Expand Up @@ -71,12 +97,14 @@ const run = async () => {

const prunedList = await prune(pruneVersion)(pruningList);

await writeSummary(container, dryRun, pruningList, prunedList);

if (prunedList.length !== pruningList.length) {
core.setFailed(`Failed to prune some versions: ${prunedList.length} out of ${pruningList.length} versions were pruned`);
}

core.setOutput('count', prunedList.length);
core.setOutput('prunedVersionIds', prunedList);
core.setOutput('prunedVersionIds', prunedList.map((version) => version.id));
core.setOutput('dryRun', dryRun);
} catch (error) {
core.setFailed(error.message);
Expand Down
2 changes: 1 addition & 1 deletion src/pruning.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const prune = (pruneVersion) => async (pruningList) => {
for (const version of pruningList) {
core.info(`Pruning version #${version.id} named '${version.name}'...`);
await pruneVersion(version);
pruned.push(version.id);
pruned.push(version);
}

core.endGroup();
Expand Down
4 changes: 2 additions & 2 deletions src/pruning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('prune', () => {

const pruned = await prune(pruneVersion)(pruningList);

expect(pruned).toEqual([100001, 100000]);
expect(pruned).toEqual(pruningList);
expect(pruneVersion).toHaveBeenCalledTimes(2);
expect(pruneVersion).nthCalledWith(1, pruningList[0]);
expect(pruneVersion).nthCalledWith(2, pruningList[1]);
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('prune', () => {

const pruned = await prune(pruneVersion)(pruningList);

expect(pruned).toEqual([100000]);
expect(pruned).toEqual([pruningList[0]]);
expect(pruneVersion).toHaveBeenCalledTimes(2);
expect(pruneVersion).nthCalledWith(1, pruningList[0]);
expect(pruneVersion).nthCalledWith(2, pruningList[1]);
Expand Down

0 comments on commit 247a3b9

Please sign in to comment.