diff --git a/.github/workflows/checksums-of-release-draft.yml b/.github/workflows/checksums-of-release-draft.yml new file mode 100644 index 0000000000..a251b99e31 --- /dev/null +++ b/.github/workflows/checksums-of-release-draft.yml @@ -0,0 +1,34 @@ +name: Checksums of release draft + +on: + workflow_run: + workflows: + - Package Neuron for Release Draft + types: + - completed + +jobs: + checksums: + runs-on: ubuntu-latest + if: github.event.workflow_run.conclusion == 'success' + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: 'latest' + + - name: Download binaries + run: | + mkdir -p /tmp/Neuron + node ./scripts/download-binaries-from-release-draft.js /tmp/Neuron + shell: pwsh + env: + TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + + - name: Checksums + run: node ./scripts/generate-checksum-table.js /tmp/Neuron + shell: pwsh diff --git a/scripts/download-binaries-from-release-draft.js b/scripts/download-binaries-from-release-draft.js new file mode 100644 index 0000000000..c85952a28f --- /dev/null +++ b/scripts/download-binaries-from-release-draft.js @@ -0,0 +1,57 @@ +const { pipeline } = require('node:stream/promises') +const { existsSync, createWriteStream } = require('node:fs') + +const { REPO, TOKEN } = process.env +const GITHUB_API_URL = 'https://api.github.com' + +const download = async (directory) => { + if (!existsSync(directory)) { + throw new Error(`Directory ${directory} does not exist`) + } + + const releases = await fetch(`${GITHUB_API_URL}/repos/${REPO}/releases`, { + headers: { Authorization: `Bearer ${TOKEN}` }, + }).then((res) => res.json()) + + const draft = releases.find((r) => r.draft) + + if (!draft) { + throw new Error('No release draft found') + } + + const assetList = await fetch(`${GITHUB_API_URL}/repos/${REPO}/releases/${draft.id}/assets`, { + headers: { Authorization: `Bearer ${TOKEN}` }, + }) + .then((res) => res.json()) + .then((res) => res.map(({ name, url }) => ({ name, url }))) + + assetList.forEach(async ({ name, url }) => { + const path = `${directory}/${name}` + + await pipeline( + await fetch(url, { + headers: { + Authorization: `Bearer ${TOKEN}`, + 'Content-Type': 'application/octet-stream', + Accept: 'application/octet-stream', + }, + }) + .then(async (res) => res.body) + .catch(() => { + throw new Error('Fail to fetch asset') + }), + + createWriteStream(path) + ) + }) +} + +if (process.argv.length < 3) { + throw new Error( + `Directory for binaries is required, use command as 'node ./download-binaries-from-release-draft.js ./binaries` + ) +} + +const directory = process.argv[2] + +download(directory)