-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |