Skip to content

Commit

Permalink
Extract and test getPruningList()
Browse files Browse the repository at this point in the history
Relates to #3 and prepares for #4
  • Loading branch information
vlaurin committed Aug 5, 2021
1 parent 628a59c commit e1e8ddd
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 22 deletions.
23 changes: 1 addition & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const core = require('@actions/core');
const github = require('@actions/github');
const {getPruningList} = require('./src/pruning');

const MS_IN_DAY = 1000 * 60 * 60 * 24;
const PAGE_SIZE = 100;
Expand Down Expand Up @@ -45,28 +46,6 @@ const listOrgContainerVersions = (octokit) => (organization, container) => (page
state: 'active',
});

const getPruningList = (listVersions, pruningFilter) => async () => {
let pruningList = [];
let page = 1;
let lastPageSize = 0;

console.log('Crawling through all versions to build pruning list...');

do {
const {data: versions} = await listVersions(page);
lastPageSize = versions.length;

const pagePruningList = versions.filter(pruningFilter);
pruningList = [...pruningList, ...pagePruningList];

console.log(`Found ${pagePruningList.length} versions to prune out of ${lastPageSize} on page ${page}`);

page++;
} while (lastPageSize >= PAGE_SIZE);

return pruningList;
};

const deleteOrgContainerVersion = (octokit) => (organization, container) => (version) => octokit.rest.packages.deletePackageVersionForOrg({
package_type: 'container',
org: organization,
Expand Down
27 changes: 27 additions & 0 deletions src/pruning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const PAGE_SIZE = 100;

const getPruningList = (listVersions, pruningFilter) => async () => {
let pruningList = [];
let page = 1;
let lastPageSize = 0;

console.log('Crawling through all versions to build pruning list...');

do {
const {data: versions} = await listVersions(page);
lastPageSize = versions.length;

const pagePruningList = versions.filter(pruningFilter);
pruningList = [...pruningList, ...pagePruningList];

console.log(`Found ${pagePruningList.length} versions to prune out of ${lastPageSize} on page ${page}`);

page++;
} while (lastPageSize >= PAGE_SIZE);

return pruningList;
};

module.exports = {
getPruningList,
};
59 changes: 59 additions & 0 deletions src/pruning.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const {getPruningList} = require('./pruning');

describe('getPruningList', () => {

const version = (id, name, created_at, updated_at) => ({
id,
name,
created_at,
updated_at: updated_at || created_at,
metadata: {
package_type: 'container',
},
});

it('should return all versions to prune', async () => {
const listVersions = () => Promise.resolve({
data: [
version(245301, '1.0.4', '2019-11-05T22:49:04Z'),
version(209672, '1.0.3', '2019-10-29T15:42:11Z'),
],
});
const pruningFilter = () => true;

const pruningList = await getPruningList(listVersions, pruningFilter)();

expect(pruningList).toEqual([
version(245301, '1.0.4', '2019-11-05T22:49:04Z'),
version(209672, '1.0.3', '2019-10-29T15:42:11Z'),
]);
});

it('should filter out versions to keep', async () => {
const listVersions = () => Promise.resolve({
data: [
version(245301, '1.0.4', '2019-11-05T22:49:04Z'),
version(209672, '1.0.3', '2019-10-29T15:42:11Z'),
],
});
const pruningFilter = ({name}) => name === '1.0.3';

const pruningList = await getPruningList(listVersions, pruningFilter)();

expect(pruningList).toEqual([
version(209672, '1.0.3', '2019-10-29T15:42:11Z'),
]);
});

it('should crawl through pages of versions', async () => {
const listVersions = (page) => Promise.resolve({
data: Array(50 * (3 - page)).fill(0).map((_, i) => version(((page - 1) * 100) + i, `1.0.${i}`, '2019-11-05T22:49:04Z')),
});
const pruningFilter = ({id}) => id % 2 === 0;

const pruningList = await getPruningList(listVersions, pruningFilter)();

expect(pruningList.length).toEqual(75);
expect(pruningList[71]).toEqual(version(142, '1.0.42', '2019-11-05T22:49:04Z'));
});
});

0 comments on commit e1e8ddd

Please sign in to comment.