Skip to content

Commit 7b99b4d

Browse files
committed
Add scripts from upload-release-asset
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 <mchehab+huawei@kernel.org>
1 parent 4cc4f8d commit 7b99b4d

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

action.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: 'Upload a Release Asset'
2+
description: 'Upload a release asset to an existing release in your repository'
3+
author: 'GitHub'
4+
inputs:
5+
upload_url:
6+
description: 'The URL for uploading assets to the release'
7+
required: true
8+
asset_path:
9+
description: 'The path to the asset you want to upload'
10+
required: true
11+
asset_name:
12+
description: 'The name of the asset you want to upload'
13+
required: true
14+
asset_content_type:
15+
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'
16+
required: true
17+
outputs:
18+
browser_download_url:
19+
description: 'The URL users can navigate to in order to download the uploaded asset'
20+
runs:
21+
using: 'node12'
22+
main: 'dist/index.js'
23+
branding:
24+
icon: 'package'
25+
color: 'gray-dark'

src/upload-on-trigger.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const core = require('@actions/core');
2+
const { GitHub } = require('@actions/github');
3+
const fs = require('fs');
4+
5+
async function run() {
6+
try {
7+
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
8+
const github = new GitHub(process.env.GITHUB_TOKEN);
9+
10+
// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
11+
const uploadUrl = core.getInput('upload_url', { required: true });
12+
const assetPath = core.getInput('asset_path', { required: true });
13+
const assetName = core.getInput('asset_name', { required: true });
14+
const assetContentType = core.getInput('asset_content_type', { required: true });
15+
16+
// Determine content-length for header to upload asset
17+
const contentLength = filePath => fs.statSync(filePath).size;
18+
19+
// Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-on-trigger for more information
20+
const headers = { 'content-type': assetContentType, 'content-length': contentLength(assetPath) };
21+
22+
// Upload a release asset
23+
// API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
24+
// Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-on-trigger
25+
const uploadAssetResponse = await github.repos.uploadReleaseAsset({
26+
url: uploadUrl,
27+
headers,
28+
name: assetName,
29+
file: fs.readFileSync(assetPath)
30+
});
31+
32+
// Get the browser_download_url for the uploaded release asset from the response
33+
const {
34+
data: { browser_download_url: browserDownloadUrl }
35+
} = uploadAssetResponse;
36+
37+
// Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
38+
core.setOutput('browser_download_url', browserDownloadUrl);
39+
} catch (error) {
40+
core.setFailed(error.message);
41+
}
42+
}
43+
44+
module.exports = run;

tests/upload-on-trigger.test.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
jest.mock('@actions/core');
2+
jest.mock('@actions/github');
3+
jest.mock('fs');
4+
5+
const core = require('@actions/core');
6+
const { GitHub, context } = require('@actions/github');
7+
const fs = require('fs');
8+
const run = require('../src/upload-on-trigger');
9+
10+
/* eslint-disable no-undef */
11+
describe('Upload Release Asset', () => {
12+
let uploadReleaseAsset;
13+
let content;
14+
15+
beforeEach(() => {
16+
uploadReleaseAsset = jest.fn().mockReturnValueOnce({
17+
data: {
18+
browser_download_url: 'browserDownloadUrl'
19+
}
20+
});
21+
22+
fs.statSync = jest.fn().mockReturnValueOnce({
23+
size: 527
24+
});
25+
26+
content = Buffer.from('test content');
27+
fs.readFileSync = jest.fn().mockReturnValueOnce(content);
28+
29+
context.repo = {
30+
owner: 'owner',
31+
repo: 'repo'
32+
};
33+
34+
const github = {
35+
repos: {
36+
uploadReleaseAsset
37+
}
38+
};
39+
40+
GitHub.mockImplementation(() => github);
41+
});
42+
43+
test('Upload release asset endpoint is called', async () => {
44+
core.getInput = jest
45+
.fn()
46+
.mockReturnValueOnce('upload_url')
47+
.mockReturnValueOnce('asset_path')
48+
.mockReturnValueOnce('asset_name')
49+
.mockReturnValueOnce('asset_content_type');
50+
51+
await run();
52+
53+
expect(uploadReleaseAsset).toHaveBeenCalledWith({
54+
url: 'upload_url',
55+
headers: { 'content-type': 'asset_content_type', 'content-length': 527 },
56+
name: 'asset_name',
57+
file: content
58+
});
59+
});
60+
61+
test('Output is set', async () => {
62+
core.getInput = jest
63+
.fn()
64+
.mockReturnValueOnce('upload_url')
65+
.mockReturnValueOnce('asset_path')
66+
.mockReturnValueOnce('asset_name')
67+
.mockReturnValueOnce('asset_content_type');
68+
69+
core.setOutput = jest.fn();
70+
71+
await run();
72+
73+
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'browser_download_url', 'browserDownloadUrl');
74+
});
75+
76+
test('Action fails elegantly', async () => {
77+
core.getInput = jest
78+
.fn()
79+
.mockReturnValueOnce('upload_url')
80+
.mockReturnValueOnce('asset_path')
81+
.mockReturnValueOnce('asset_name')
82+
.mockReturnValueOnce('asset_content_type');
83+
84+
uploadReleaseAsset.mockRestore();
85+
uploadReleaseAsset.mockImplementation(() => {
86+
throw new Error('Error uploading release asset');
87+
});
88+
89+
core.setOutput = jest.fn();
90+
91+
core.setFailed = jest.fn();
92+
93+
await run();
94+
95+
expect(uploadReleaseAsset).toHaveBeenCalled();
96+
expect(core.setFailed).toHaveBeenCalledWith('Error uploading release asset');
97+
expect(core.setOutput).toHaveBeenCalledTimes(0);
98+
});
99+
});

0 commit comments

Comments
 (0)