generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hardcode known wrapper checksums to avoid network requests
- Loading branch information
1 parent
2572bdd
commit bb2ea9b
Showing
8 changed files
with
1,208 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,92 @@ | ||
/* | ||
* Updates the `wrapper-checksums.json` file | ||
* | ||
* This is intended to be executed by the GitHub workflow, but can also be run | ||
* manually. | ||
*/ | ||
|
||
// @ts-check | ||
|
||
const httpm = require('typed-rest-client/HttpClient') | ||
|
||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
/** | ||
* @returns {Promise<void>} | ||
*/ | ||
async function main() { | ||
const httpc = new httpm.HttpClient( | ||
'gradle/wrapper-validation-action/update-checksums-workflow', | ||
undefined, | ||
{allowRetries: true, maxRetries: 3} | ||
) | ||
|
||
/** | ||
* @param {string} url | ||
* @returns {Promise<string>} | ||
*/ | ||
async function httpGetText(url) { | ||
const response = await httpc.get(url) | ||
return await response.readBody() | ||
} | ||
|
||
/** | ||
* @typedef {Object} ApiVersionEntry | ||
* @property {string} version - version name | ||
* @property {string=} wrapperChecksumUrl - wrapper checksum URL; not present for old versions | ||
* @property {boolean} snapshot - whether this is a snapshot version | ||
*/ | ||
|
||
/** | ||
* @returns {Promise<ApiVersionEntry[]>} | ||
*/ | ||
async function httpGetVersions() { | ||
return JSON.parse( | ||
await httpGetText('https://services.gradle.org/versions/all') | ||
) | ||
} | ||
|
||
const versions = (await httpGetVersions()) | ||
// Only include versions with checksum | ||
.filter(e => e.wrapperChecksumUrl !== undefined) | ||
// Ignore snapshots; they are changing frequently so no point in including them in checksums file | ||
.filter(e => !e.snapshot) | ||
console.info(`Got ${versions.length} relevant Gradle versions`) | ||
|
||
// Note: For simplicity don't sort the entries but keep the order from the API; this also has the | ||
// advantage that the latest versions come first, so compared to appending versions at the end | ||
// this will not cause redundant Git diff due to trailing `,` being forbidden by JSON | ||
|
||
/** | ||
* @typedef {Object} FileVersionEntry | ||
* @property {string} version | ||
* @property {string} checksum | ||
*/ | ||
/** @type {FileVersionEntry[]} */ | ||
const fileVersions = [] | ||
for (const entry of versions) { | ||
/** @type {string} */ | ||
// @ts-ignore | ||
const checksumUrl = entry.wrapperChecksumUrl | ||
const checksum = await httpGetText(checksumUrl) | ||
fileVersions.push({version: entry.version, checksum}) | ||
} | ||
|
||
const jsonPath = path.resolve( | ||
__dirname, | ||
'..', | ||
'..', | ||
'src', | ||
'wrapper-checksums.json' | ||
) | ||
console.info(`Writing checksums file to ${jsonPath}`) | ||
// Write pretty-printed JSON (and add trailing line break) | ||
fs.writeFileSync(jsonPath, JSON.stringify(fileVersions, null, 2) + '\n') | ||
} | ||
|
||
main().catch(e => { | ||
console.error(e) | ||
// Manually set error exit code, otherwise error is logged but script exits successfully | ||
process.exitCode = 1 | ||
}) |
This file contains 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,49 @@ | ||
name: 'Update Wrapper checksums file' | ||
|
||
on: | ||
schedule: | ||
# Run weekly (at arbitrary time) | ||
- cron: '24 5 * * 6' | ||
# Support running workflow manually | ||
workflow_dispatch: | ||
|
||
jobs: | ||
update-checksums: | ||
name: Update checksums | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20.x | ||
cache: npm | ||
|
||
- name: Install dependencies | ||
run: | | ||
npm install typed-rest-client@1.8.11 --no-save | ||
- name: Update checksums file | ||
run: node ./.github/workflows/update-checksums-file.js | ||
|
||
# If there are no changes, this action will not create a pull request | ||
- name: Create or update pull request | ||
uses: peter-evans/create-pull-request@v6 | ||
with: | ||
branch: wrapper-checksums-update | ||
commit-message: Update known wrapper checksums | ||
title: Update known wrapper checksums | ||
# Note: Unfortunately this action cannot trigger the regular workflows for the PR automatically, see | ||
# https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#triggering-further-workflow-runs | ||
# Therefore suggest below to close and then reopen the PR | ||
body: | | ||
Automatically generated pull request to update the known wrapper checksums. | ||
In case of conflicts, manually run the workflow from the [Actions tab](https://github.com/gradle/wrapper-validation-action/actions/workflows/update-checksums-file.yml), the changes will then be force-pushed onto this pull request branch. | ||
Do not manually update the pull request branch; those changes might get overwritten. | ||
> [!IMPORTANT] | ||
> GitHub workflows have not been executed for this pull request yet. Before merging, close and then directly reopen this pull request to trigger the workflows. |
This file contains 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 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 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 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
Oops, something went wrong.