- move fromTag resolving to main.ts and remove dependency on checks from ReleaseNotes.ts

- require fromTag for ReleaseNotes
- adjust tests, split into according test classes
- simplify transform.ts
This commit is contained in:
Mike Penz
2020-10-19 16:33:00 +02:00
parent 2fb7019ca5
commit d9c1239d34
5 changed files with 170 additions and 171 deletions
+7 -112
View File
@@ -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`)
})
*/
+117
View File
@@ -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`)
})
+36 -4
View File
@@ -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<void> {
core.setOutput('failed', false) // mark the action not failed by default
@@ -30,7 +32,7 @@ async function run(): Promise<void> {
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<void> {
}
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
)
+6 -51
View File
@@ -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<string | null> {
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<string | null> {
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}`,
+4 -4
View File
@@ -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
}