Skip to content

Commit

Permalink
Add scripts from upload-release-asset
Browse files Browse the repository at this point in the history
According with:
	actions/upload-release-asset#78

This actions is now marked as not maintained.

Let's pick the code from it as basis, and then modify for our
needs.

The only change from the original script was to change the
script names.

Signed-off-by: Mauro Carvalho Chehab <[email protected]>
  • Loading branch information
mchehab committed Feb 17, 2021
1 parent 4cc4f8d commit 7b99b4d
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
25 changes: 25 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: 'Upload a Release Asset'
description: 'Upload a release asset to an existing release in your repository'
author: 'GitHub'
inputs:
upload_url:
description: 'The URL for uploading assets to the release'
required: true
asset_path:
description: 'The path to the asset you want to upload'
required: true
asset_name:
description: 'The name of the asset you want to upload'
required: true
asset_content_type:
description: 'The content-type of the asset you want to upload. See the supported Media Types here: https://www.iana.org/assignments/media-types/media-types.xhtml for more information'
required: true
outputs:
browser_download_url:
description: 'The URL users can navigate to in order to download the uploaded asset'
runs:
using: 'node12'
main: 'dist/index.js'
branding:
icon: 'package'
color: 'gray-dark'
44 changes: 44 additions & 0 deletions src/upload-on-trigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const core = require('@actions/core');
const { GitHub } = require('@actions/github');
const fs = require('fs');

async function run() {
try {
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
const github = new GitHub(process.env.GITHUB_TOKEN);

// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
const uploadUrl = core.getInput('upload_url', { required: true });
const assetPath = core.getInput('asset_path', { required: true });
const assetName = core.getInput('asset_name', { required: true });
const assetContentType = core.getInput('asset_content_type', { required: true });

// Determine content-length for header to upload asset
const contentLength = filePath => fs.statSync(filePath).size;

// Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-on-trigger for more information
const headers = { 'content-type': assetContentType, 'content-length': contentLength(assetPath) };

// 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-on-trigger
const uploadAssetResponse = await github.repos.uploadReleaseAsset({
url: uploadUrl,
headers,
name: assetName,
file: fs.readFileSync(assetPath)
});

// 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
core.setOutput('browser_download_url', browserDownloadUrl);
} catch (error) {
core.setFailed(error.message);
}
}

module.exports = run;
99 changes: 99 additions & 0 deletions tests/upload-on-trigger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
jest.mock('@actions/core');
jest.mock('@actions/github');
jest.mock('fs');

const core = require('@actions/core');
const { GitHub, context } = require('@actions/github');
const fs = require('fs');
const run = require('../src/upload-on-trigger');

/* eslint-disable no-undef */
describe('Upload Release Asset', () => {
let uploadReleaseAsset;
let content;

beforeEach(() => {
uploadReleaseAsset = jest.fn().mockReturnValueOnce({
data: {
browser_download_url: 'browserDownloadUrl'
}
});

fs.statSync = jest.fn().mockReturnValueOnce({
size: 527
});

content = Buffer.from('test content');
fs.readFileSync = jest.fn().mockReturnValueOnce(content);

context.repo = {
owner: 'owner',
repo: 'repo'
};

const github = {
repos: {
uploadReleaseAsset
}
};

GitHub.mockImplementation(() => github);
});

test('Upload release asset endpoint is called', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('upload_url')
.mockReturnValueOnce('asset_path')
.mockReturnValueOnce('asset_name')
.mockReturnValueOnce('asset_content_type');

await run();

expect(uploadReleaseAsset).toHaveBeenCalledWith({
url: 'upload_url',
headers: { 'content-type': 'asset_content_type', 'content-length': 527 },
name: 'asset_name',
file: content
});
});

test('Output is set', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('upload_url')
.mockReturnValueOnce('asset_path')
.mockReturnValueOnce('asset_name')
.mockReturnValueOnce('asset_content_type');

core.setOutput = jest.fn();

await run();

expect(core.setOutput).toHaveBeenNthCalledWith(1, 'browser_download_url', 'browserDownloadUrl');
});

test('Action fails elegantly', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('upload_url')
.mockReturnValueOnce('asset_path')
.mockReturnValueOnce('asset_name')
.mockReturnValueOnce('asset_content_type');

uploadReleaseAsset.mockRestore();
uploadReleaseAsset.mockImplementation(() => {
throw new Error('Error uploading release asset');
});

core.setOutput = jest.fn();

core.setFailed = jest.fn();

await run();

expect(uploadReleaseAsset).toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith('Error uploading release asset');
expect(core.setOutput).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 7b99b4d

Please sign in to comment.