-
Notifications
You must be signed in to change notification settings - Fork 88
build: add min release age step #8164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tomdavies73
wants to merge
2
commits into
master
Choose a base branch
from
add_min_release_age_build_step
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| import fs from "node:fs/promises"; | ||
|
|
||
| const DEFAULT_REGISTRY = "https://registry.npmjs.org"; | ||
| const MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000; | ||
| const REGISTRY_REQUEST_TIMEOUT_MS = 30 * 1000; | ||
| const MAX_CONCURRENT_REQUESTS = 20; | ||
| const MAX_FAILURES_TO_PRINT = 25; | ||
|
|
||
| async function readMinReleaseAge() { | ||
| if (process.env.MIN_RELEASE_AGE) { | ||
| return Number.parseInt(process.env.MIN_RELEASE_AGE, 10); | ||
| } | ||
|
|
||
| let npmrc; | ||
| try { | ||
| npmrc = await fs.readFile(".npmrc", "utf8"); | ||
| } catch (error) { | ||
| if (error.code === "ENOENT") { | ||
| throw new Error("min-release-age not set in .npmrc"); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| const age = npmrc | ||
| .split("\n") | ||
| .map((line) => line.replace(/[ \t\r]/g, "")) | ||
| .filter((line) => /^min-release-age=[0-9]+$/.test(line)) | ||
| .at(-1) | ||
| ?.split("=")[1]; | ||
|
|
||
| if (!age) { | ||
| throw new Error("min-release-age not set in .npmrc"); | ||
| } | ||
|
|
||
| return Number.parseInt(age, 10); | ||
| } | ||
|
|
||
| function getPackageNameFromLockEntry(packagePath, packageInfo) { | ||
| if (packageInfo.name) { | ||
| return packageInfo.name; | ||
| } | ||
|
|
||
| const pathAfterNodeModules = packagePath.slice( | ||
| packagePath.lastIndexOf("node_modules/") + "node_modules/".length, | ||
| ); | ||
| const [firstSegment, secondSegment] = pathAfterNodeModules.split("/"); | ||
|
|
||
| return firstSegment.startsWith("@") | ||
| ? `${firstSegment}/${secondSegment}` | ||
| : firstSegment; | ||
| } | ||
|
|
||
| async function readLockedPackages() { | ||
| const lockfile = JSON.parse(await fs.readFile("package-lock.json", "utf8")); | ||
|
|
||
| if (!lockfile.packages) { | ||
| throw new Error("package-lock.json does not contain a packages map"); | ||
| } | ||
|
|
||
| const lockedPackages = new Map(); | ||
|
|
||
| for (const [packagePath, packageInfo] of Object.entries(lockfile.packages)) { | ||
| if ( | ||
| !packagePath.includes("node_modules/") || | ||
| packageInfo.link || | ||
| !packageInfo.version | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const name = getPackageNameFromLockEntry(packagePath, packageInfo); | ||
| lockedPackages.set(`${name}@${packageInfo.version}`, { | ||
| name, | ||
| version: packageInfo.version, | ||
| }); | ||
| } | ||
|
|
||
| return [...lockedPackages.values()]; | ||
| } | ||
|
|
||
| function getPackageUrl(registry, packageName) { | ||
| const encodedPackageName = encodeURIComponent(packageName); | ||
| return `${registry.replace(/\/$/, "")}/${encodedPackageName}`; | ||
| } | ||
|
|
||
| async function fetchPackageTimes(registry, packageName) { | ||
| let response; | ||
| try { | ||
| response = await fetch(getPackageUrl(registry, packageName), { | ||
| headers: { Accept: "application/json" }, | ||
| signal: AbortSignal.timeout(REGISTRY_REQUEST_TIMEOUT_MS), | ||
| }); | ||
| } catch (error) { | ||
| if (error.name === "TimeoutError" || error.name === "AbortError") { | ||
| throw new Error( | ||
| `Timed out fetching ${packageName} metadata after ${REGISTRY_REQUEST_TIMEOUT_MS / 1000} seconds`, | ||
| { cause: error }, | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Could not fetch ${packageName} metadata: ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
|
|
||
| const packageMetadata = await response.json(); | ||
|
|
||
| if (!packageMetadata.time) { | ||
| throw new Error(`Registry metadata for ${packageName} does not include time`); | ||
| } | ||
|
|
||
| return packageMetadata.time; | ||
| } | ||
|
|
||
| async function runConcurrently(items, worker) { | ||
| const results = []; | ||
| let nextIndex = 0; | ||
|
|
||
| async function runWorker() { | ||
| while (nextIndex < items.length) { | ||
| const currentIndex = nextIndex; | ||
| nextIndex += 1; | ||
| results[currentIndex] = await worker(items[currentIndex]); | ||
| } | ||
| } | ||
|
|
||
| await Promise.all( | ||
| Array.from( | ||
| { length: Math.min(MAX_CONCURRENT_REQUESTS, items.length) }, | ||
| runWorker, | ||
| ), | ||
| ); | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| function formatAge(ageInMilliseconds) { | ||
| return `${(ageInMilliseconds / MILLISECONDS_IN_DAY).toFixed(2)} days`; | ||
| } | ||
|
|
||
| async function main() { | ||
| const minReleaseAge = await readMinReleaseAge(); | ||
|
|
||
| if (!Number.isInteger(minReleaseAge) || minReleaseAge < 0) { | ||
| throw new Error("MIN_RELEASE_AGE must be a non-negative integer"); | ||
| } | ||
|
|
||
| const registry = | ||
| process.env.NPM_CONFIG_REGISTRY || | ||
| process.env.npm_config_registry || | ||
| DEFAULT_REGISTRY; | ||
| const lockedPackages = await readLockedPackages(); | ||
| const packageNames = [...new Set(lockedPackages.map(({ name }) => name))]; | ||
| const packageTimes = new Map(); | ||
|
|
||
| console.log( | ||
| `Checking ${lockedPackages.length} locked package releases against min-release-age=${minReleaseAge} days`, | ||
| ); | ||
|
|
||
| await runConcurrently(packageNames, async (packageName) => { | ||
| packageTimes.set(packageName, await fetchPackageTimes(registry, packageName)); | ||
| }); | ||
|
|
||
| const now = Date.now(); | ||
| const minimumAgeInMilliseconds = minReleaseAge * MILLISECONDS_IN_DAY; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const tooNewPackages = lockedPackages | ||
| .map(({ name, version }) => { | ||
| const publishedAt = packageTimes.get(name)[version]; | ||
|
|
||
| if (!publishedAt) { | ||
| throw new Error(`No publish time found for ${name}@${version}`); | ||
| } | ||
|
|
||
| const publishedAtTime = new Date(publishedAt).getTime(); | ||
| const ageInMilliseconds = now - publishedAtTime; | ||
|
|
||
| return { | ||
| name, | ||
| version, | ||
| publishedAt, | ||
| ageInMilliseconds, | ||
| isTooNew: ageInMilliseconds < minimumAgeInMilliseconds, | ||
| }; | ||
| }) | ||
| .filter(({ isTooNew }) => isTooNew) | ||
| .sort((a, b) => a.ageInMilliseconds - b.ageInMilliseconds); | ||
|
|
||
| if (tooNewPackages.length > 0) { | ||
| console.error( | ||
| `::error::${tooNewPackages.length} locked package release(s) do not meet min-release-age=${minReleaseAge} days`, | ||
| ); | ||
|
|
||
| tooNewPackages | ||
| .slice(0, MAX_FAILURES_TO_PRINT) | ||
| .forEach(({ name, version, publishedAt, ageInMilliseconds }) => { | ||
| console.error( | ||
| `- ${name}@${version} was published at ${publishedAt} (${formatAge(ageInMilliseconds)} old)`, | ||
| ); | ||
| }); | ||
|
|
||
| if (tooNewPackages.length > MAX_FAILURES_TO_PRINT) { | ||
| console.error( | ||
| `...and ${tooNewPackages.length - MAX_FAILURES_TO_PRINT} more package release(s)`, | ||
| ); | ||
| } | ||
|
|
||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log("All locked package releases meet the configured release age."); | ||
| } | ||
|
|
||
| main().catch((error) => { | ||
| console.error(`::error::${error.message}`); | ||
| process.exit(1); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure there is a real risk here, our checks are ran in an isolated enviroment so its not the end of the world if we do an npm install elsewhere here.
I think all that really matters is that we throw an exit code 1 and the build fails so we can't merge the PR 👍