- introduce new API allowing to write changelog directly to result file

- introduce test case for writing a file
This commit is contained in:
Mike Penz
2020-12-30 17:49:41 +01:00
parent 9a0355af98
commit a463f188c1
7 changed files with 13179 additions and 5 deletions
+27 -1
View File
@@ -1,6 +1,7 @@
import * as path from 'path'
import * as process from 'process'
import * as cp from 'child_process'
import * as fs from 'fs'
test('missing values should result in failure', () => {
process.env['GITHUB_WORKSPACE'] = '.'
@@ -17,7 +18,7 @@ test('missing values should result in failure', () => {
}
})
test('missing values should result in failure', () => {
test('complete input should succeed', () => {
process.env['GITHUB_WORKSPACE'] = '.'
process.env['INPUT_CONFIGURATION'] = 'configuration.json'
process.env['INPUT_OWNER'] = 'mikepenz'
@@ -31,4 +32,29 @@ test('missing values should result in failure', () => {
}
const result = cp.execSync(`node ${ip}`, options).toString()
// should succeed
expect(result).toBeDefined()
})
test('should write result to file', () => {
process.env['GITHUB_WORKSPACE'] = '.'
process.env['INPUT_CONFIGURATION'] = 'configuration.json'
process.env['INPUT_OWNER'] = 'mikepenz'
process.env['INPUT_REPO'] = 'release-changelog-builder-action'
process.env['INPUT_FROMTAG'] = 'v0.3.0'
process.env['INPUT_TOTAG'] = 'v0.5.0'
process.env['INPUT_OUTPUTFILE'] = 'test.md'
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecSyncOptions = {
env: process.env
}
const result = cp.execSync(`node ${ip}`, options).toString()
// should succeed
expect(result).toBeDefined()
const readOutput = fs.readFileSync("test.md")
fs.unlinkSync("test.md")
expect(readOutput.toString()).not.toBe('')
})
+2
View File
@@ -23,6 +23,8 @@ inputs:
failOnError:
description: 'Defines if the action should result in a build failure, if an error was discovered'
default: "false"
outputFile:
description: 'If defined, the changelog will get written to this file. (relative to the checkout dir)'
token:
description: 'Defines the token to use to execute the git API requests with, uses `env.GITHUB_TOKEN` by default'
outputs:
Generated Vendored
+23 -1
View File
@@ -331,6 +331,12 @@ function run() {
const failOnError = core.getInput('failOnError') === 'true';
const result = yield new releaseNotesBuilder_1.ReleaseNotesBuilder(token, repositoryPath, owner, repo, fromTag, toTag, failOnError, ignorePreReleases, configuration).build();
core.setOutput('changelog', result);
// write the result in changelog to file if possible
const outputFile = core.getInput('outputFile');
if (outputFile !== "") {
core.debug(`Enabled writing the changelog to disk`);
utils_1.writeOutput(repositoryPath, outputFile, result);
}
}
catch (error) {
core.setFailed(error.message);
@@ -1181,7 +1187,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.directoryExistsSync = exports.resolveConfiguration = exports.failOrError = exports.retrieveRepositoryPath = void 0;
exports.writeOutput = exports.directoryExistsSync = exports.resolveConfiguration = exports.failOrError = exports.retrieveRepositoryPath = void 0;
const fs = __importStar(__webpack_require__(5747));
const configuration_1 = __webpack_require__(5527);
const core = __importStar(__webpack_require__(2186));
@@ -1277,6 +1283,22 @@ function directoryExistsSync(inputPath, required) {
throw new Error(`Directory '${inputPath}' does not exist`);
}
exports.directoryExistsSync = directoryExistsSync;
/**
* Writes the changelog to the given the file
*/
function writeOutput(githubWorkspacePath, outputFile, changelog) {
if (outputFile && changelog) {
const outputPath = path.resolve(githubWorkspacePath, outputFile);
core.debug(`outputPath = '${outputPath}'`);
try {
fs.writeFileSync(outputPath, changelog);
}
catch (error) {
core.warning(`⚠️ Could not write the file to disk - ${error.message}`);
}
}
}
exports.writeOutput = writeOutput;
/***/ }),
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+13095 -1
View File
File diff suppressed because it is too large Load Diff
+12 -1
View File
@@ -1,5 +1,9 @@
import * as core from '@actions/core'
import {retrieveRepositoryPath, resolveConfiguration} from './utils'
import {
retrieveRepositoryPath,
resolveConfiguration,
writeOutput
} from './utils'
import * as github from '@actions/github'
import {ReleaseNotesBuilder} from './releaseNotesBuilder'
@@ -43,6 +47,13 @@ async function run(): Promise<void> {
).build()
core.setOutput('changelog', result)
// write the result in changelog to file if possible
const outputFile: string = core.getInput('outputFile')
if (outputFile !== '') {
core.debug(`Enabled writing the changelog to disk`)
writeOutput(repositoryPath, outputFile, result)
}
} catch (error) {
core.setFailed(error.message)
}
+19
View File
@@ -111,3 +111,22 @@ export function directoryExistsSync(
throw new Error(`Directory '${inputPath}' does not exist`)
}
/**
* Writes the changelog to the given the file
*/
export function writeOutput(
githubWorkspacePath: string,
outputFile: string,
changelog: string | null
): void {
if (outputFile && changelog) {
const outputPath = path.resolve(githubWorkspacePath, outputFile)
core.debug(`outputPath = '${outputPath}'`)
try {
fs.writeFileSync(outputPath, changelog)
} catch (error) {
core.warning(`⚠️ Could not write the file to disk - ${error.message}`)
}
}
}