diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index cfa86bd..cc33507 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,5 +1,6 @@ import {ReleaseNotes} from '../src/releaseNotes' import { resolveConfiguration } from '../src/utils'; +import { Octokit } from '@octokit/rest'; // shows how the runner will run a javascript action with env / stdout protocol /* @@ -14,59 +15,22 @@ test('test runs', () => { console.log(cp.execSync(`node ${ip}`, options).toString()) }) */ -it('Should have empty changelog (tags)', async () => { - jest.setTimeout(180000) - const configuration = resolveConfiguration('', 'configs/configuration.json') - const releaseNotes = new ReleaseNotes({ - owner: 'mikepenz', - repo: 'release-changelog-builder-action', - fromTag: 'v0.0.1', - toTag: 'v0.0.2', - ignorePreReleases: false, - failOnError: false, - configuration: configuration - }) - - const changeLog = await releaseNotes.pull() - console.log(changeLog) - expect(changeLog).toStrictEqual(null) -}) - -it('Should match generated changelog (tags)', async () => { - jest.setTimeout(180000) - - const configuration = resolveConfiguration('', 'configs/configuration.json') - const releaseNotes = new ReleaseNotes({ - owner: 'mikepenz', - repo: 'release-changelog-builder-action', - fromTag: 'v0.0.1', - toTag: 'v0.0.3', - ignorePreReleases: false, - failOnError: false, - configuration: configuration - }) - - const changeLog = await releaseNotes.pull() - console.log(changeLog) - expect(changeLog).toStrictEqual(`## ๐Ÿงช Tests - -- [CI] Specify Test Case - - PR: #10 - -`) +// load octokit instance +const octokit = new Octokit({ + auth: `token ${process.env.GITHUB_TOKEN}` }) +/* it('Should match generated changelog (unspecified fromTag)', async () => { jest.setTimeout(180000) const configuration = resolveConfiguration('', 'configs/configuration.json') - const releaseNotes = new ReleaseNotes({ + const releaseNotes = new ReleaseNotes(octokit, { owner: 'mikepenz', repo: 'release-changelog-builder-action', fromTag: null, toTag: 'v0.0.3', - ignorePreReleases: false, failOnError: false, configuration: configuration }) @@ -80,73 +44,4 @@ it('Should match generated changelog (unspecified fromTag)', async () => { `) }) - -it('Should match generated changelog (refs)', async () => { - jest.setTimeout(180000) - - const configuration = resolveConfiguration('', 'configs_test/configuration_all_placeholders.json') - const releaseNotes = new ReleaseNotes({ - owner: 'mikepenz', - repo: 'release-changelog-builder-action', - fromTag: '5ec7a2d86fe9f43fdd38d5e254a1117c8a51b4c3', - toTag: 'fa3788c8c4b3373ef8424ce3eb008a5cd07cc5aa', - ignorePreReleases: false, - failOnError: false, - configuration: configuration - }) - - const changeLog = await releaseNotes.pull() - console.log(changeLog) - expect(changeLog).toStrictEqual(`## ๐Ÿงช Tests - -[CI] Specify Test Case -10 -https://github.com/mikepenz/release-changelog-builder-action/pull/10 -2020-10-16T13:59:36.000Z -mikepenz -test -1.0.0 -- specify test case -mikepenz, nhoelzl -nhoelzl - -`) -}) - -it('Should match ordered ASC', async () => { - jest.setTimeout(180000) - - const configuration = resolveConfiguration('', 'configs_test/configuration_asc.json') - const releaseNotes = new ReleaseNotes({ - owner: 'mikepenz', - repo: 'release-changelog-builder-action', - fromTag: 'v0.3.0', - toTag: 'v0.5.0', - ignorePreReleases: false, - failOnError: false, - configuration: configuration - }) - - const changeLog = await releaseNotes.pull() - console.log(changeLog) - expect(changeLog).toStrictEqual(`## ๐Ÿš€ Features\n\n22\n24\n25\n26\n28\n\n## ๐Ÿ› Fixes\n\n23\n\n`) -}) - -it('Should match ordered DESC', async () => { - jest.setTimeout(180000) - - const configuration = resolveConfiguration('', 'configs_test/configuration_desc.json') - const releaseNotes = new ReleaseNotes({ - owner: 'mikepenz', - repo: 'release-changelog-builder-action', - fromTag: 'v0.3.0', - toTag: 'v0.5.0', - ignorePreReleases: false, - failOnError: false, - configuration: configuration - }) - - const changeLog = await releaseNotes.pull() - console.log(changeLog) - expect(changeLog).toStrictEqual(`## ๐Ÿš€ Features\n\n28\n26\n25\n24\n22\n\n## ๐Ÿ› Fixes\n\n23\n\n`) -}) \ No newline at end of file +*/ \ No newline at end of file diff --git a/__tests__/releaseNotes.test.ts b/__tests__/releaseNotes.test.ts new file mode 100644 index 0000000..e54fa2a --- /dev/null +++ b/__tests__/releaseNotes.test.ts @@ -0,0 +1,117 @@ +import {ReleaseNotes} from '../src/releaseNotes' +import { resolveConfiguration } from '../src/utils'; +import { Octokit } from '@octokit/rest'; + +// load octokit instance +const octokit = new Octokit({ + auth: `token ${process.env.GITHUB_TOKEN}` +}) + + +it('Should have empty changelog (tags)', async () => { + jest.setTimeout(180000) + + const configuration = resolveConfiguration('', 'configs/configuration.json') + const releaseNotes = new ReleaseNotes(octokit, { + owner: 'mikepenz', + repo: 'release-changelog-builder-action', + fromTag: 'v0.0.1', + toTag: 'v0.0.2', + failOnError: false, + configuration: configuration + }) + + const changeLog = await releaseNotes.pull() + console.log(changeLog) + expect(changeLog).toStrictEqual(null) +}) + +it('Should match generated changelog (tags)', async () => { + jest.setTimeout(180000) + + const configuration = resolveConfiguration('', 'configs/configuration.json') + const releaseNotes = new ReleaseNotes(octokit, { + owner: 'mikepenz', + repo: 'release-changelog-builder-action', + fromTag: 'v0.0.1', + toTag: 'v0.0.3', + failOnError: false, + configuration: configuration + }) + + const changeLog = await releaseNotes.pull() + console.log(changeLog) + expect(changeLog).toStrictEqual(`## ๐Ÿงช Tests + +- [CI] Specify Test Case + - PR: #10 + +`) +}) + +it('Should match generated changelog (refs)', async () => { + jest.setTimeout(180000) + + const configuration = resolveConfiguration('', 'configs_test/configuration_all_placeholders.json') + const releaseNotes = new ReleaseNotes(octokit, { + owner: 'mikepenz', + repo: 'release-changelog-builder-action', + fromTag: '5ec7a2d86fe9f43fdd38d5e254a1117c8a51b4c3', + toTag: 'fa3788c8c4b3373ef8424ce3eb008a5cd07cc5aa', + failOnError: false, + configuration: configuration + }) + + const changeLog = await releaseNotes.pull() + console.log(changeLog) + expect(changeLog).toStrictEqual(`## ๐Ÿงช Tests + +[CI] Specify Test Case +10 +https://github.com/mikepenz/release-changelog-builder-action/pull/10 +2020-10-16T13:59:36.000Z +mikepenz +test +1.0.0 +- specify test case +mikepenz, nhoelzl +nhoelzl + +`) +}) + +it('Should match ordered ASC', async () => { + jest.setTimeout(180000) + + const configuration = resolveConfiguration('', 'configs_test/configuration_asc.json') + const releaseNotes = new ReleaseNotes(octokit, { + owner: 'mikepenz', + repo: 'release-changelog-builder-action', + fromTag: 'v0.3.0', + toTag: 'v0.5.0', + failOnError: false, + configuration: configuration + }) + + const changeLog = await releaseNotes.pull() + console.log(changeLog) + expect(changeLog).toStrictEqual(`## ๐Ÿš€ Features\n\n22\n24\n25\n26\n28\n\n## ๐Ÿ› Fixes\n\n23\n\n`) +}) + +it('Should match ordered DESC', async () => { + jest.setTimeout(180000) + + const configuration = resolveConfiguration('', 'configs_test/configuration_desc.json') + const releaseNotes = new ReleaseNotes(octokit, { + owner: 'mikepenz', + repo: 'release-changelog-builder-action', + fromTag: 'v0.3.0', + toTag: 'v0.5.0', + failOnError: false, + configuration: configuration + }) + + const changeLog = await releaseNotes.pull() + console.log(changeLog) + expect(changeLog).toStrictEqual(`## ๐Ÿš€ Features\n\n28\n26\n25\n24\n22\n\n## ๐Ÿ› Fixes\n\n23\n\n`) +}) \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 7fa9928..b5951a8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,6 +8,8 @@ import {ReleaseNotes} from './releaseNotes' import {createCommandManager} from './gitHelper' import * as github from '@actions/github' import {DefaultConfiguration} from './configuration' +import {Octokit} from '@octokit/rest' +import {Tags} from './tags' async function run(): Promise { core.setOutput('failed', false) // mark the action not failed by default @@ -30,7 +32,7 @@ async function run(): Promise { const owner = core.getInput('owner') || github.context.repo.owner const repo = core.getInput('repo') || github.context.repo.repo // read in from, to tag inputs - const fromTag = core.getInput('fromTag') + let fromTag = core.getInput('fromTag') let toTag = core.getInput('toTag') // read in flags const ignorePreReleases = core.getInput('ignorePreReleases') === 'true' @@ -80,19 +82,49 @@ async function run(): Promise { } core.endGroup() - const releaseNotes = new ReleaseNotes({ + // load octokit instance + const octokit = new Octokit({ + auth: `token ${token || process.env.GITHUB_TOKEN}` + }) + + // ensure to resolve the fromTag if it was not provided specifically + if (!fromTag) { + core.startGroup(`๐Ÿ”– Resolve previous tag`) + core.debug(`fromTag undefined, trying to resolve via API`) + const tagsApi = new Tags(octokit) + + const previousTag = await tagsApi.findPredecessorTag( + owner, + repo, + toTag, + ignorePreReleases, + configuration.max_tags_to_fetch || + DefaultConfiguration.max_tags_to_fetch + ) + if (previousTag == null) { + failOrError( + `๐Ÿ’ฅ Unable to retrieve previous tag given ${toTag}`, + failOnError + ) + return + } + fromTag = previousTag.name + core.debug(`fromTag resolved via previousTag as: ${previousTag.name}`) + core.endGroup() + } + + const releaseNotes = new ReleaseNotes(octokit, { owner, repo, fromTag, toTag, - ignorePreReleases, failOnError, configuration }) core.setOutput( 'changelog', - (await releaseNotes.pull(token)) || + (await releaseNotes.pull()) || configuration.empty_template || DefaultConfiguration.empty_template ) diff --git a/src/releaseNotes.ts b/src/releaseNotes.ts index 5b232f2..853e197 100755 --- a/src/releaseNotes.ts +++ b/src/releaseNotes.ts @@ -3,71 +3,26 @@ import {Commits, CommitInfo} from './commits' import {PullRequestInfo, PullRequests} from './pullRequests' import {buildChangelog} from './transform' import * as core from '@actions/core' -import {Tags} from './tags' import {Configuration, DefaultConfiguration} from './configuration' import {failOrError} from './utils' export interface ReleaseNotesOptions { owner: string // the owner of the repository repo: string // the repository - fromTag: string | null // the tag/ref to start from + fromTag: string // the tag/ref to start from toTag: string // the tag/ref up to - ignorePreReleases: boolean // defines if we should ignore any pre-releases for matching, only relevant if fromTag is null failOnError: boolean // defines if we should fail the action in case of an error configuration: Configuration // the configuration as defined in `configuration.ts` } export class ReleaseNotes { - constructor(private options: ReleaseNotesOptions) {} + constructor(private octokit: Octokit, private options: ReleaseNotesOptions) {} - async pull(token?: string): Promise { - const octokit = new Octokit({ - auth: `token ${token || process.env.GITHUB_TOKEN}` - }) - - const { - owner, - repo, - toTag, - ignorePreReleases, - failOnError, - configuration - } = this.options - - if (!this.options.fromTag) { - core.startGroup(`๐Ÿ”– Resolve previous tag`) - core.debug(`fromTag undefined, trying to resolve via API`) - const tagsApi = new Tags(octokit) - - const previousTag = await tagsApi.findPredecessorTag( - owner, - repo, - toTag, - ignorePreReleases, - configuration.max_tags_to_fetch || - DefaultConfiguration.max_tags_to_fetch - ) - if (previousTag == null) { - failOrError( - `๐Ÿ’ฅ Unable to retrieve previous tag given ${toTag}`, - failOnError - ) - return null - } - this.options.fromTag = previousTag.name - core.debug(`fromTag resolved via previousTag as: ${previousTag.name}`) - core.endGroup() - } - - if (!this.options.fromTag) { - failOrError(`๐Ÿ’ฅ Missing or couldn't resolve 'fromTag'`, failOnError) - return null - } else { - core.setOutput('fromTag', this.options.fromTag) - } + async pull(): Promise { + const {configuration} = this.options core.startGroup(`๐Ÿš€ Load pull requests`) - const mergedPullRequests = await this.getMergedPullRequests(octokit) + const mergedPullRequests = await this.getMergedPullRequests(this.octokit) core.endGroup() if (mergedPullRequests.length === 0) { @@ -97,7 +52,7 @@ export class ReleaseNotes { const commitsApi = new Commits(octokit) let commits: CommitInfo[] try { - commits = await commitsApi.getDiff(owner, repo, fromTag!!, toTag) + commits = await commitsApi.getDiff(owner, repo, fromTag, toTag) } catch (error) { failOrError( `๐Ÿ’ฅ Failed to retrieve - Invalid tag? - Because of: ${error}`, diff --git a/src/transform.ts b/src/transform.ts index d08e625..39eb276 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -26,9 +26,7 @@ export function buildChangelog( transform( fillTemplate( pr, - config.pr_template - ? config.pr_template - : DefaultConfiguration.pr_template + config.pr_template || DefaultConfiguration.pr_template ), validatedTransformers ) @@ -133,7 +131,9 @@ function transform(filled: string, transformers: RegexTransformer[]): string { } let transformed = filled for (const {target, pattern} of transformers) { - transformed = transformed.replace(pattern!!, target) + if (pattern) { + transformed = transformed.replace(pattern, target) + } } return transformed }