Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Refactor main.js for test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmHughes committed Sep 27, 2019
1 parent 7b2a12a commit 48c0418
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 98 deletions.
102 changes: 54 additions & 48 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,56 +378,11 @@ module.exports._enoent = enoent;
/***/ }),

/***/ 31:
/***/ (function(module, __unusedexports, __webpack_require__) {

const core = __webpack_require__(470);
const { GitHub } = __webpack_require__(469);
const fs = __webpack_require__(747);

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-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: 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);
}
}
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

module.exports = run;
const run = __webpack_require__(379);

if (require.main === require.cache[eval('__filename')]) {
run();
}
run();


/***/ }),
Expand Down Expand Up @@ -4153,6 +4108,57 @@ function octokitDebug (octokit) {
}


/***/ }),

/***/ 379:
/***/ (function(module, __unusedexports, __webpack_require__) {

const core = __webpack_require__(470);
const { GitHub } = __webpack_require__(469);
const fs = __webpack_require__(747);

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-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: 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;


/***/ }),

/***/ 385:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "dist/index.js",
"scripts": {
"lint": "eslint 'src/**.js' 'tests/**.js' --fix",
"test": "eslint 'src/**.js' 'tests/**.js' --fix && jest",
"test": "eslint 'src/**.js' 'tests/**.js' && jest --coverage",
"build": "ncc build src/main.js",
"precommit": "npm run build && git add dist/"
},
Expand Down Expand Up @@ -41,7 +41,7 @@
"jest": {
"testEnvironment": "node",
"collectCoverageFrom": [
"src/main.js"
"src/upload-release-asset.js"
],
"coverageThreshold": {
"global": {
Expand Down
49 changes: 2 additions & 47 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
const core = require('@actions/core');
const { GitHub } = require('@actions/github');
const fs = require('fs');
const run = require('./upload-release-asset');

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-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: 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;

if (require.main === module) {
run();
}
run();
44 changes: 44 additions & 0 deletions src/upload-release-asset.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-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: 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;
2 changes: 1 addition & 1 deletion tests/main.test.js → tests/upload-release-asset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('fs');
const core = require('@actions/core');
const { GitHub, context } = require('@actions/github');
const fs = require('fs');
const run = require('../src/main.js');
const run = require('../src/upload-release-asset');

/* eslint-disable no-undef */
describe('Upload Release Asset', () => {
Expand Down

0 comments on commit 48c0418

Please sign in to comment.