// Pruning rules:
// 1. only keep the earliest release of the month
// 2. to keep the newest 3 nightlies
```
import { Octokit, App } from "octokit";
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ });
// In case node 21 is not used.
function groupBy(array, keyOrIterator) {
var iterator;
// use the function passed in, or create one
if(typeof keyOrIterator !== 'function') {
const key = String(keyOrIterator);
iterator = function (item) { return item[key]; };
} else {
iterator = keyOrIterator;
}
return array.reduce(function (memo, item) {
const key = iterator(item);
memo[key] = memo[key] || [];
memo[key].push(item);
return memo;
}, {});
}
async function separateReleases({ github, context }) {
console.log("Pruning old prereleases");
// doc: https://docs.github.com/en/rest/releases/releases
const { data: releases } = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
});
let nightlies = releases.filter(
release =>
// Only consider releases tagged `nightly-${SHA}` for deletion
release.tag_name.includes("nightly") &&
release.tag_name !== "nightly"
);
// group releases by months
const groups = groupBy(nightlies, i => i.created_at.slice(0, 7));
// Pruning rules:
// 1. only keep the earliest release of the month
// 2. to keep the newest 3 nightlies
const toPrune = Object.values(groups)
.reduce((acc, cur) => acc.concat(cur.slice(0, -1)), [])
.slice(3);
const toKeep = Object.values(groups).reduce((acc, cur) => acc.concat(cur.slice(-1)), []);
return {
toPrune,
toKeep,
};
};
(async() => {
const releases = await separateReleases({
github : octokit,
context : {
repo : { owner: "foundry-rs", repo: "foundry" }
},
});
console.log("To prune:", releases.toPrune.map(i => i.name));
console.log("To keep:", releases.toKeep.map(i => i.name));
})();
```
```
$ node index.mjs
Pruning old prereleases
To prune: [ 'Nightly (2023-11-01)' ]
To keep: [
'Nightly (2024-01-12)',
'Nightly (2023-12-02)',
'Nightly (2023-11-02)',
'Nightly (2023-10-02)',
'Nightly (2023-08-02)',
'Nightly (2023-07-02)',
'Nightly (2023-06-02)',
'Nightly (2023-05-02)',
'Nightly (2023-04-02)',
'Nightly (2023-03-02)',
'Nightly (2023-01-03)'
]
```