From d2b9240a59553d34d7b8f8fde0abe0c64cffdeb4 Mon Sep 17 00:00:00 2001 From: Artem Shubovych Date: Mon, 22 Jul 2024 19:15:36 +1000 Subject: [PATCH] Split code into smaller functions --- dist/index.js | 75 +++++++++++++++-------------- src/lib.js | 129 +++++++++++++++++++++++++++----------------------- 2 files changed, 110 insertions(+), 94 deletions(-) diff --git a/dist/index.js b/dist/index.js index c358ea0..7fbe5c1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -10465,6 +10465,44 @@ const getReleaseURL = async (octokit, context) => { return uploadURL; }; +const getDownloadUrl = async (assetPath, uploadUrl) => { + // Determine content-length for header to upload asset + const stats = await promises_namespaceObject.stat(asset); + const contentLength = stats.size; + const contentType = 'binary/octet-stream'; + + // Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information + const headers = { + 'content-type': contentType, + 'content-length': contentLength, + }; + + const assetName = external_node_path_namespaceObject.basename(asset); + + core.info(`Uploading asset ${assetName}`); + + // Upload a release asset + // API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset + // Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset + + const content = await promises_namespaceObject.readFile(asset); + + const uploadAssetResponse = await octokit.repos.uploadReleaseAsset({ + url: uploadUrl, + headers, + name: assetName, + data: content, + }); + + // Get the browser_download_url for the uploaded release asset from the response + const { + data: { browser_download_url: browserDownloadUrl } + } = uploadAssetResponse; + + // Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs + return browserDownloadUrl; +}; + const run = async () => { try { // Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage @@ -10485,7 +10523,7 @@ const run = async () => { const paths = await Promise.all( assetPaths.flatMap(async (assetPath) => { if (assetPath.includes('*')) { - return await (0,promises_namespaceObject.glob)(assetPath, { nodir: true }); + return await promises_namespaceObject.glob(assetPath, { nodir: true }); } else { return assetPath; } @@ -10495,40 +10533,7 @@ const run = async () => { core.debug(`Expanded paths: ${paths}`); const downloadURLs = await Promise.all( - paths.map(async (asset) => { - // Determine content-length for header to upload asset - const stats = await (0,promises_namespaceObject.stat)(asset); - const contentLength = stats.size; - const contentType = 'binary/octet-stream'; - - // Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information - const headers = { - 'content-type': contentType, - 'content-length': contentLength, - }; - - const assetName = (0,external_node_path_namespaceObject.basename)(asset); - - core.info(`Uploading asset ${assetName}`); - - // Upload a release asset - // API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset - // Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset - const uploadAssetResponse = await octokit.repos.uploadReleaseAsset({ - url: uploadUrl, - headers, - name: assetName, - data: fs.readFileSync(asset), - }); - - // Get the browser_download_url for the uploaded release asset from the response - const { - data: { browser_download_url: browserDownloadUrl } - } = uploadAssetResponse; - - // Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs - return browserDownloadUrl; - }) + paths.map(asset => getDownloadUrl(asset, uploadUrl)) ); core.setOutput('browser_download_urls', JSON.stringify(downloadURLs)); diff --git a/src/lib.js b/src/lib.js index cf39f22..e6963be 100644 --- a/src/lib.js +++ b/src/lib.js @@ -1,8 +1,8 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; -import { basename } from 'node:path'; -import { glob, stat } from 'node:fs/promises'; +import * as path from 'node:path'; +import * as fsPromises from 'node:fs/promises'; const getReleaseURL = async (octokit, context) => { // Get owner and repo from context of payload that triggered the action @@ -28,6 +28,71 @@ const getReleaseURL = async (octokit, context) => { return uploadURL; }; +const getDownloadURL = async (core, asset, uploadUrl) => { + // Determine content-length for header to upload asset + const stats = await fsPromises.stat(asset); + const contentLength = stats.size; + const contentType = 'binary/octet-stream'; + + // Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information + const headers = { + 'content-type': contentType, + 'content-length': contentLength, + }; + + const assetName = path.basename(asset); + + core.info(`Uploading asset ${assetName}`); + + // Upload a release asset + // API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset + // Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset + + const content = await fsPromises.readFile(asset); + + const uploadAssetResponse = await octokit.repos.uploadReleaseAsset({ + url: uploadUrl, + headers, + name: assetName, + data: content, + }); + + // Get the browser_download_url for the uploaded release asset from the response + const { + data: { browser_download_url: browserDownloadUrl } + } = uploadAssetResponse; + + // Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs + return browserDownloadUrl; +}; + +const processAssets = async (core, assetPathsStr, uploadUrl) => { + const assetPaths = JSON.parse(assetPathsStr); + + if (!assetPaths || assetPaths.length == 0) { + core.setFailed('asset_paths must contain a JSON array of quoted paths'); + return; + } + + const paths = await Promise.all( + assetPaths.flatMap(async (assetPath) => { + if (assetPath.includes('*')) { + return await fsPromises.glob(assetPath, { nodir: true }); + } else { + return assetPath; + } + }) + ); + + core.debug(`Expanded paths: ${paths}`); + + const downloadURLs = await Promise.all( + paths.map(asset => getDownloadURL(core, asset, uploadUrl)) + ); + + return downloadURLs; +}; + const run = async () => { try { // Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage @@ -36,63 +101,9 @@ const run = async () => { const uploadUrl = await getReleaseURL(octokit, github.context); // Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs - const assetPathsSt = core.getInput('asset_paths', { required: true }); - - const assetPaths = JSON.parse(assetPathsSt); - - if (!assetPaths || assetPaths.length == 0) { - core.setFailed('asset_paths must contain a JSON array of quoted paths'); - return; - } - - const paths = await Promise.all( - assetPaths.flatMap(async (assetPath) => { - if (assetPath.includes('*')) { - return await glob(assetPath, { nodir: true }); - } else { - return assetPath; - } - }) - ); - - core.debug(`Expanded paths: ${paths}`); - - const downloadURLs = await Promise.all( - paths.map(async (asset) => { - // Determine content-length for header to upload asset - const stats = await stat(asset); - const contentLength = stats.size; - const contentType = 'binary/octet-stream'; - - // Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information - const headers = { - 'content-type': contentType, - 'content-length': contentLength, - }; - - const assetName = basename(asset); - - core.info(`Uploading asset ${assetName}`); - - // Upload a release asset - // API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset - // Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset - const uploadAssetResponse = await octokit.repos.uploadReleaseAsset({ - url: uploadUrl, - headers, - name: assetName, - data: fs.readFileSync(asset), - }); - - // Get the browser_download_url for the uploaded release asset from the response - const { - data: { browser_download_url: browserDownloadUrl } - } = uploadAssetResponse; - - // Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs - return browserDownloadUrl; - }) - ); + const assetPathsStr = core.getInput('asset_paths', { required: true }); + + const downloadURLs = await processAssets(core, assetPathsStr, uploadUrl); core.setOutput('browser_download_urls', JSON.stringify(downloadURLs)); } catch (error) {