From f4b43d8b2f965210489226f8fcbbb8a046653f19 Mon Sep 17 00:00:00 2001 From: Cafe137 <77121044+Cafe137@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:30:49 +0200 Subject: [PATCH] feat: upgrade to bee-js 8 (#215) * feat: upgrade to bee-js 8 * fix: fix pr-preview action yaml * fix: add noop to bash * fix: fix backtick escaping --- package-lock.json | 70 ++++++++++++++++--------------- package.json | 3 +- pr-preview/action.yaml | 6 +-- swarm-actions-libs/package.json | 14 ------- swarm-actions-libs/src/headers.ts | 21 ---------- swarm-actions-libs/src/index.ts | 1 - swarm-actions-libs/tsconfig.json | 12 ------ upload-dir/package.json | 3 -- upload-dir/src/main.ts | 36 +++++++++------- upload-dir/src/options.ts | 2 - write-feed/package.json | 3 +- write-feed/src/main.ts | 59 +++++++++++++------------- 12 files changed, 91 insertions(+), 139 deletions(-) delete mode 100644 swarm-actions-libs/package.json delete mode 100644 swarm-actions-libs/src/headers.ts delete mode 100644 swarm-actions-libs/src/index.ts delete mode 100644 swarm-actions-libs/tsconfig.json diff --git a/package-lock.json b/package-lock.json index 6fcf01b..b8d7d8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,8 @@ ], "dependencies": { "@actions/core": "^1.10.1", - "@ethersphere/bee-js": "^7.1.1" + "@ethersphere/bee-js": "^8.1.0", + "cafe-utility": "^23.9.0" }, "devDependencies": { "@tsconfig/node18": "^18.2.4", @@ -68,9 +69,9 @@ } }, "node_modules/@ethersphere/bee-js": { - "version": "7.1.1", - "resolved": "https://registry.npmjs.org/@ethersphere/bee-js/-/bee-js-7.1.1.tgz", - "integrity": "sha512-QSGDnIPof3lblzYgVj299QwVlZBYFsQqiI/a1G916VAeFcqvCV86LESf0SKweDVoJ9K3WCGPrFHZC/rDj4sxbA==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/@ethersphere/bee-js/-/bee-js-8.1.0.tgz", + "integrity": "sha512-26IFcA6dOmKrr6Cok0PDOEmF00V6ij1R8cLpUAhGyRkcVoRhZRj5tuk9H+Zj7pOoBR9ytzbMHpvEkC66sn2Y9Q==", "license": "BSD-3-Clause", "dependencies": { "@ethersphere/swarm-cid": "^0.1.0", @@ -81,8 +82,18 @@ "js-sha3": "^0.8.0", "semver": "^7.3.5", "ws": "^8.7.0" + }, + "engines": { + "bee": "2.2.0-06a0aca7", + "beeApiVersion": "7.1.0" } }, + "node_modules/@ethersphere/bee-js/node_modules/cafe-utility": { + "version": "21.5.0", + "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-21.5.0.tgz", + "integrity": "sha512-5u+9cf7fvcH3j2Q3jrd7nA3bUITUBj8b9Arg4eA6almqeA5+dwQA6NKba4GnW6zS9uL1iVCEQqM3z3tQVs2Xjw==", + "license": "MIT" + }, "node_modules/@ethersphere/swarm-cid": { "version": "0.1.0", "license": "BSD-3-Clause", @@ -590,9 +601,9 @@ } }, "node_modules/cafe-utility": { - "version": "21.5.0", - "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-21.5.0.tgz", - "integrity": "sha512-5u+9cf7fvcH3j2Q3jrd7nA3bUITUBj8b9Arg4eA6almqeA5+dwQA6NKba4GnW6zS9uL1iVCEQqM3z3tQVs2Xjw==", + "version": "23.9.0", + "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-23.9.0.tgz", + "integrity": "sha512-f9dzrm3HWPqnzxE9aEHLBfGMyfd37oUiwQRkTQyPwoO0R0wkjV3cyqOOGNB8vlfkDnHF3erxHnhNx58p685a0g==", "license": "MIT" }, "node_modules/callsites": { @@ -1861,10 +1872,6 @@ "node": ">=8" } }, - "node_modules/swarm-actions-libs": { - "resolved": "swarm-actions-libs", - "link": true - }, "node_modules/text-table": { "version": "0.2.0", "dev": true, @@ -2071,19 +2078,16 @@ } }, "swarm-actions-libs": { - "version": "0.0.0" + "version": "0.0.0", + "extraneous": true }, "upload-dir": { - "version": "0.0.0", - "dependencies": { - "swarm-actions-libs": "^0.0.0" - } + "version": "0.0.0" }, "write-feed": { "version": "0.0.0", "dependencies": { - "ethereumjs-util": "^7.1.4", - "swarm-actions-libs": "^0.0.0" + "ethereumjs-util": "^7.1.4" } } }, @@ -2124,9 +2128,9 @@ } }, "@ethersphere/bee-js": { - "version": "7.1.1", - "resolved": "https://registry.npmjs.org/@ethersphere/bee-js/-/bee-js-7.1.1.tgz", - "integrity": "sha512-QSGDnIPof3lblzYgVj299QwVlZBYFsQqiI/a1G916VAeFcqvCV86LESf0SKweDVoJ9K3WCGPrFHZC/rDj4sxbA==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/@ethersphere/bee-js/-/bee-js-8.1.0.tgz", + "integrity": "sha512-26IFcA6dOmKrr6Cok0PDOEmF00V6ij1R8cLpUAhGyRkcVoRhZRj5tuk9H+Zj7pOoBR9ytzbMHpvEkC66sn2Y9Q==", "requires": { "@ethersphere/swarm-cid": "^0.1.0", "axios": "^0.28.1", @@ -2136,6 +2140,13 @@ "js-sha3": "^0.8.0", "semver": "^7.3.5", "ws": "^8.7.0" + }, + "dependencies": { + "cafe-utility": { + "version": "21.5.0", + "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-21.5.0.tgz", + "integrity": "sha512-5u+9cf7fvcH3j2Q3jrd7nA3bUITUBj8b9Arg4eA6almqeA5+dwQA6NKba4GnW6zS9uL1iVCEQqM3z3tQVs2Xjw==" + } } }, "@ethersphere/swarm-cid": { @@ -2471,9 +2482,9 @@ } }, "cafe-utility": { - "version": "21.5.0", - "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-21.5.0.tgz", - "integrity": "sha512-5u+9cf7fvcH3j2Q3jrd7nA3bUITUBj8b9Arg4eA6almqeA5+dwQA6NKba4GnW6zS9uL1iVCEQqM3z3tQVs2Xjw==" + "version": "23.9.0", + "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-23.9.0.tgz", + "integrity": "sha512-f9dzrm3HWPqnzxE9aEHLBfGMyfd37oUiwQRkTQyPwoO0R0wkjV3cyqOOGNB8vlfkDnHF3erxHnhNx58p685a0g==" }, "callsites": { "version": "3.1.0", @@ -3291,9 +3302,6 @@ "has-flag": "^4.0.0" } }, - "swarm-actions-libs": { - "version": "file:swarm-actions-libs" - }, "text-table": { "version": "0.2.0", "dev": true @@ -3354,10 +3362,7 @@ "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" }, "upload-dir": { - "version": "file:upload-dir", - "requires": { - "swarm-actions-libs": "^0.0.0" - } + "version": "file:upload-dir" }, "uri-js": { "version": "4.4.1", @@ -3408,8 +3413,7 @@ "write-feed": { "version": "file:write-feed", "requires": { - "ethereumjs-util": "^7.1.4", - "swarm-actions-libs": "^0.0.0" + "ethereumjs-util": "^7.1.4" } }, "ws": { diff --git a/package.json b/package.json index 8811a58..d03c1b5 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "license": "BSD-3-Clause", "dependencies": { "@actions/core": "^1.10.1", - "@ethersphere/bee-js": "^7.1.1" + "@ethersphere/bee-js": "^8.1.0", + "cafe-utility": "^23.9.0" }, "devDependencies": { "@tsconfig/node18": "^18.2.4", diff --git a/pr-preview/action.yaml b/pr-preview/action.yaml index ecf24aa..4ca82a5 100644 --- a/pr-preview/action.yaml +++ b/pr-preview/action.yaml @@ -74,6 +74,7 @@ runs: if [ "${{ inputs.index-document }}" = "false" ] then # Do nothing (equivalent to setting document to empty string) + : elif [ ! -z "${{ inputs.index-document }}" ] then # Set document to the specified value @@ -143,14 +144,13 @@ runs: uses: peter-evans/create-or-update-comment@v1 with: token: ${{ inputs.token }} - comment-id: ${{ steps.comment.outputs.comment-id   - } } # Likely uses step output directly + comment-id: ${{ steps.comment.outputs.comment-id }} issue-number: ${{ github.event.pull_request.number }} body: | ## 🐝 PR preview in Swarm **Preview URL:** ${{ steps.metadata.outputs.bzz-link }} - **Swarm Hash:** `${{ steps.upload.outputs.reference }} + **Swarm Hash:** `${{ steps.upload.outputs.reference }}` **Commit Hash:** ${{ github.event.pull_request.head.sha }} **Commit Message:** ${{ steps.commit.outputs.result }} edit-mode: replace diff --git a/swarm-actions-libs/package.json b/swarm-actions-libs/package.json deleted file mode 100644 index 3290db5..0000000 --- a/swarm-actions-libs/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "private": true, - "name": "swarm-actions-libs", - "version": "0.0.0", - "description": "Libraries for swarm-actions", - "main": "build/index.js", - "scripts": { - "build": "tsc", - "format": "prettier --write src/*.ts", - "format-check": "prettier --check src/*.ts", - "lint": "eslint src/*.ts", - "all": "npm run build && npm run format && npm run lint" - } -} diff --git a/swarm-actions-libs/src/headers.ts b/swarm-actions-libs/src/headers.ts deleted file mode 100644 index f9be754..0000000 --- a/swarm-actions-libs/src/headers.ts +++ /dev/null @@ -1,21 +0,0 @@ -export const parseHeaders = (headers: string): Record => { - if (!headers) { - return {} - } - - const result: Record = {} - - for (const header of headers.trim().split('\n')) { - const index = header.indexOf(':') - const key = header.slice(0, index).trim() - const value = header.slice(index + 1).trim() - - if (typeof result[key] === 'undefined') { - result[key] = value - } else { - result[key] = result[key] + ',' + value - } - } - - return result -} diff --git a/swarm-actions-libs/src/index.ts b/swarm-actions-libs/src/index.ts deleted file mode 100644 index a1d390b..0000000 --- a/swarm-actions-libs/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './headers' diff --git a/swarm-actions-libs/tsconfig.json b/swarm-actions-libs/tsconfig.json deleted file mode 100644 index 80b835d..0000000 --- a/swarm-actions-libs/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "extends": "@tsconfig/node18/tsconfig.json", - "compilerOptions": { - "lib": ["es2022"], - "module": "node16", - "target": "es2021", - "outDir": "./build", - "rootDir": "./src", - "declaration": true - } -} - diff --git a/upload-dir/package.json b/upload-dir/package.json index ce93b92..36f1082 100644 --- a/upload-dir/package.json +++ b/upload-dir/package.json @@ -12,8 +12,5 @@ "package": "ncc build --source-map --license licenses.txt", "ci:package": "npm run build && npm run package", "all": "npm run build && npm run format && npm run lint && npm run package" - }, - "dependencies": { - "swarm-actions-libs": "^0.0.0" } } diff --git a/upload-dir/src/main.ts b/upload-dir/src/main.ts index 05a52c6..ae88250 100644 --- a/upload-dir/src/main.ts +++ b/upload-dir/src/main.ts @@ -1,8 +1,8 @@ import * as core from '@actions/core' import type { BatchId, BeeRequestOptions, CollectionUploadOptions } from '@ethersphere/bee-js' -import { BATCH_ID_HEX_LENGTH, Bee } from '@ethersphere/bee-js' -import { parseHeaders } from 'swarm-actions-libs' -import { toBoolean, toNumber, toString } from './options' +import { Bee } from '@ethersphere/bee-js' +import { Objects, Types } from 'cafe-utility' +import { toBoolean, toNumber } from './options' type Inputs = { beeUrl: string @@ -13,38 +13,42 @@ type Inputs = { requestOptions: BeeRequestOptions } -const run = async ({ beeUrl, postageBatchId, dir, headers, options, requestOptions }: Inputs): Promise => { - const bee = new Bee(beeUrl, { headers }) - const { reference, tagUid } = await bee.uploadFilesFromDirectory(postageBatchId, dir, options, requestOptions) +async function run(inputs: Inputs) { + const bee = new Bee(inputs.beeUrl, { headers: inputs.headers }) + const { reference, tagUid } = await bee.uploadFilesFromDirectory( + inputs.postageBatchId, + inputs.dir, + inputs.options, + inputs.requestOptions + ) core.setOutput('reference', reference) core.setOutput('tagUid', tagUid) } -const main = async (): Promise => { - const postageBatchId = core.getInput('postage-batch-id', { required: true }) - if (postageBatchId.length !== BATCH_ID_HEX_LENGTH) { - throw new Error(`postage-batch-id must be ${BATCH_ID_HEX_LENGTH} characters long`) - } +async function main() { + const postageBatchId = Types.asHexString(core.getInput('postage-batch-id', { required: true }), { + name: 'postage-batch-id', + byteLength: 32, + }) as BatchId const options: CollectionUploadOptions = { deferred: toBoolean(core.getInput('deferred')), encrypt: toBoolean(core.getInput('encrypt')), - errorDocument: toString(core.getInput('error-document')), - indexDocument: toString(core.getInput('index-document')), + errorDocument: core.getInput('error-document'), + indexDocument: core.getInput('index-document'), pin: toBoolean(core.getInput('pin')), tag: toNumber(core.getInput('tag')), } const requestOptions: BeeRequestOptions = { - retry: toNumber(core.getInput('retry')), timeout: toNumber(core.getInput('timeout')), } return run({ beeUrl: core.getInput('bee-url', { required: true }), dir: core.getInput('dir', { required: true }), - postageBatchId: postageBatchId as BatchId, - headers: parseHeaders(core.getInput('headers')), + postageBatchId, + headers: Objects.parseKeyValues(core.getMultilineInput('headers')), options, requestOptions, }) diff --git a/upload-dir/src/options.ts b/upload-dir/src/options.ts index 2d59c72..b40905c 100644 --- a/upload-dir/src/options.ts +++ b/upload-dir/src/options.ts @@ -25,5 +25,3 @@ export const toNumber = (value: string): number | undefined => { } return number } - -export const toString = (value: string): string => value diff --git a/write-feed/package.json b/write-feed/package.json index 8d4173d..7a21eea 100644 --- a/write-feed/package.json +++ b/write-feed/package.json @@ -14,7 +14,6 @@ "all": "npm run build && npm run format && npm run lint && npm run package" }, "dependencies": { - "ethereumjs-util": "^7.1.4", - "swarm-actions-libs": "^0.0.0" + "ethereumjs-util": "^7.1.4" } } diff --git a/write-feed/src/main.ts b/write-feed/src/main.ts index a0cd261..348822d 100644 --- a/write-feed/src/main.ts +++ b/write-feed/src/main.ts @@ -1,8 +1,8 @@ import * as core from '@actions/core' import type { BatchId, Reference } from '@ethersphere/bee-js' -import { BATCH_ID_HEX_LENGTH, Bee, REFERENCE_HEX_LENGTH } from '@ethersphere/bee-js' -import { privateToAddress, stripHexPrefix } from 'ethereumjs-util' -import { parseHeaders } from 'swarm-actions-libs' +import { Bee } from '@ethersphere/bee-js' +import { Objects, Types } from 'cafe-utility' +import { privateToAddress } from 'ethereumjs-util' type Inputs = { beeUrl: string @@ -17,44 +17,41 @@ const signerToAddress = (signer: string): string => { return privateToAddress(Buffer.from(signer, 'hex')).toString('hex') } -const run = async ({ - beeUrl, - postageBatchId, - topic: topicString, - signer: signerString, - reference, - headers, -}: Inputs): Promise => { - const bee = new Bee(beeUrl, { headers }) - const signer = stripHexPrefix(signerString) - const topic = bee.makeFeedTopic(topicString) - - const writer = bee.makeFeedWriter('sequence', topic, signer) - const response = await writer.upload(postageBatchId, reference) - const manifest = await bee.createFeedManifest(postageBatchId, 'sequence', topic, signerToAddress(signer)) +async function run(inputs: Inputs) { + const bee = new Bee(inputs.beeUrl, { headers: inputs.headers }) + const topic = bee.makeFeedTopic(inputs.topic) + + const writer = bee.makeFeedWriter('sequence', topic, inputs.signer) + const response = await writer.upload(inputs.postageBatchId, inputs.reference) + const manifest = await bee.createFeedManifest( + inputs.postageBatchId, + 'sequence', + topic, + signerToAddress(inputs.signer) + ) core.setOutput('reference', response) core.setOutput('manifest', manifest.reference) } -const main = async (): Promise => { - const postageBatchId = core.getInput('postage-batch-id', { required: true }) - if (postageBatchId.length !== BATCH_ID_HEX_LENGTH) { - throw new Error(`postage-batch-id must be ${BATCH_ID_HEX_LENGTH} characters long`) - } +async function main() { + const postageBatchId = Types.asHexString(core.getInput('postage-batch-id', { required: true }), { + name: 'postage-batch-id', + byteLength: 32, + }) as BatchId - const reference = core.getInput('reference', { required: true }) - if (reference.length !== REFERENCE_HEX_LENGTH) { - throw new Error(`reference must be ${REFERENCE_HEX_LENGTH} characters long`) - } + const reference = Types.asHexString(core.getInput('reference', { required: true }), { + name: 'reference', + byteLength: 32, + }) as Reference return run({ beeUrl: core.getInput('bee-url', { required: true }), - signer: core.getInput('signer', { required: true }), + signer: Types.asHexString(core.getInput('signer', { required: true }), { name: 'signer', byteLength: 32 }), topic: core.getInput('topic', { required: true }), - postageBatchId: postageBatchId as BatchId, - reference: reference as Reference, - headers: parseHeaders(core.getInput('headers')), + postageBatchId, + reference, + headers: Objects.parseKeyValues(core.getMultilineInput('headers')), }) }