Skip to content

Commit

Permalink
Split code into smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shybovycha committed Jul 22, 2024
1 parent 87aca09 commit d2b9240
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 94 deletions.
75 changes: 40 additions & 35 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down
129 changes: 70 additions & 59 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit d2b9240

Please sign in to comment.