Debug info

This commit is contained in:
Martyn Janes
2019-12-19 12:13:15 +01:00
parent 3314e52d48
commit e578215a93
3 changed files with 20 additions and 135 deletions

38
dist/index.js vendored
View File

@@ -10352,43 +10352,15 @@ module.exports = set;
/***/ (function(module, __unusedexports, __webpack_require__) {
const core = __webpack_require__(470);
const { GitHub } = __webpack_require__(469);
const fs = __webpack_require__(747);
const { context } = __webpack_require__(469);
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 owner and repo from context of payload that triggered the action
const { owner, repo } = context.repo;
// 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-release-asset 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-release-asset
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);
core.debug(JSON.stringify(owner));
core.debug(JSON.stringify(repo));
} catch (error) {
core.setFailed(error.message);
}

View File

@@ -1,41 +1,13 @@
const core = require('@actions/core');
const { GitHub } = require('@actions/github');
const fs = require('fs');
const { context } = require('@actions/github');
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 owner and repo from context of payload that triggered the action
const { owner, repo } = context.repo;
// 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-release-asset 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-release-asset
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);
core.debug(JSON.stringify(owner));
core.debug(JSON.stringify(repo));
} catch (error) {
core.setFailed(error.message);
}

View File

@@ -1,31 +1,23 @@
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/tangle-release');
const run = require('../src/tangle-release.js');
/* eslint-disable no-undef */
describe('Upload Release Asset', () => {
let uploadReleaseAsset;
let content;
describe('Tangle Release', () => {
let tangleRelease;
beforeEach(() => {
uploadReleaseAsset = jest.fn().mockReturnValueOnce({
tangleRelease = jest.fn().mockReturnValueOnce({
data: {
browser_download_url: 'browserDownloadUrl'
id: 'releaseId',
html_url: 'htmlUrl',
upload_url: 'uploadUrl'
}
});
fs.statSync = jest.fn().mockReturnValueOnce({
size: 527
});
content = Buffer.from('test content');
fs.readFileSync = jest.fn().mockReturnValueOnce(content);
context.repo = {
owner: 'owner',
repo: 'repo'
@@ -33,67 +25,16 @@ describe('Upload Release Asset', () => {
const github = {
repos: {
uploadReleaseAsset
tangleRelease
}
};
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');
test('Test 1', async () => {
core.getInput = jest.fn();
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);
});
});