Skip to content

Commit

Permalink
Update prune-prereleases.js pruning rules (#6806)
Browse files Browse the repository at this point in the history
* Update prune-prereleases.js pruning rules

    // 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)'
]
```

* Update prune-prereleases.js

* Update prune-prereleases.js

* Update prune-prereleases.js
  • Loading branch information
hellwolf authored Jan 16, 2024
1 parent 36044da commit f180a13
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions .github/scripts/prune-prereleases.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// 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;
}, {});
}

module.exports = async ({ github, context }) => {
console.log("Pruning old prereleases");

Expand All @@ -11,16 +31,25 @@ module.exports = async ({ github, context }) => {
release =>
// Only consider releases tagged `nightly-${SHA}` for deletion
release.tag_name.includes("nightly") &&
release.tag_name !== "nightly" &&
// ref: https://github.com/foundry-rs/foundry/issues/3881
// Skipping pruning the build on 1st day of each month
!release.created_at.includes("-01T")
release.tag_name !== "nightly"
);

// Keep newest 3 nightlies
nightlies = nightlies.slice(3);
// Pruning rules:
// 1. only keep the earliest (by created_at) release of the month
// 2. to keep the newest 3 nightlies
// Notes:
// - This addresses https://github.com/foundry-rs/foundry/issues/6732
// - Name of the release may deviate from created_at due to the usage of different timezones.

// Group releases by months.
// Per doc:
// > The latest release is the most recent non-prerelease, non-draft release, sorted by the created_at attribute.
const groups = groupBy(nightlies, i => i.created_at.slice(0, 7));
const nightliesToPrune = Object.values(groups)
.reduce((acc, cur) => acc.concat(cur.slice(0, -1)), []) // rule 1
.slice(3); // rule 2

for (const nightly of nightlies) {
for (const nightly of nightliesToPrune) {
console.log(`Deleting nightly: ${nightly.tag_name}`);
await github.rest.repos.deleteRelease({
owner: context.repo.owner,
Expand Down

0 comments on commit f180a13

Please sign in to comment.