Merge pull request #48 from mikepenz/feature/move_from_tag_resolval
Adjust code to move fromTag resolving to main.ts
This commit is contained in:
@@ -1,29 +1,22 @@
|
||||
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
|
||||
/*
|
||||
test('test runs', () => {
|
||||
jest.setTimeout(180000);
|
||||
|
||||
process.env['INPUT_CONFIGURATION'] = 'configuration.json'
|
||||
const ip = path.join(__dirname, '..', 'lib', 'main.js')
|
||||
const options: cp.ExecSyncOptions = {
|
||||
env: process.env
|
||||
}
|
||||
console.log(cp.execSync(`node ${ip}`, options).toString())
|
||||
// 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({
|
||||
const releaseNotes = new ReleaseNotes(octokit, {
|
||||
owner: 'mikepenz',
|
||||
repo: 'release-changelog-builder-action',
|
||||
fromTag: 'v0.0.1',
|
||||
toTag: 'v0.0.2',
|
||||
ignorePreReleases: false,
|
||||
failOnError: false,
|
||||
configuration: configuration
|
||||
})
|
||||
@@ -37,36 +30,11 @@ it('Should match generated changelog (tags)', 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: '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
|
||||
|
||||
`)
|
||||
})
|
||||
|
||||
it('Should match generated changelog (unspecified fromTag)', async () => {
|
||||
jest.setTimeout(180000)
|
||||
|
||||
const configuration = resolveConfiguration('', 'configs/configuration.json')
|
||||
const releaseNotes = new ReleaseNotes({
|
||||
owner: 'mikepenz',
|
||||
repo: 'release-changelog-builder-action',
|
||||
fromTag: null,
|
||||
toTag: 'v0.0.3',
|
||||
ignorePreReleases: false,
|
||||
failOnError: false,
|
||||
configuration: configuration
|
||||
})
|
||||
@@ -85,12 +53,11 @@ it('Should match generated changelog (refs)', async () => {
|
||||
jest.setTimeout(180000)
|
||||
|
||||
const configuration = resolveConfiguration('', 'configs_test/configuration_all_placeholders.json')
|
||||
const releaseNotes = new ReleaseNotes({
|
||||
const releaseNotes = new ReleaseNotes(octokit, {
|
||||
owner: 'mikepenz',
|
||||
repo: 'release-changelog-builder-action',
|
||||
fromTag: '5ec7a2d86fe9f43fdd38d5e254a1117c8a51b4c3',
|
||||
toTag: 'fa3788c8c4b3373ef8424ce3eb008a5cd07cc5aa',
|
||||
ignorePreReleases: false,
|
||||
failOnError: false,
|
||||
configuration: configuration
|
||||
})
|
||||
@@ -117,12 +84,11 @@ it('Should match ordered ASC', async () => {
|
||||
jest.setTimeout(180000)
|
||||
|
||||
const configuration = resolveConfiguration('', 'configs_test/configuration_asc.json')
|
||||
const releaseNotes = new ReleaseNotes({
|
||||
const releaseNotes = new ReleaseNotes(octokit, {
|
||||
owner: 'mikepenz',
|
||||
repo: 'release-changelog-builder-action',
|
||||
fromTag: 'v0.3.0',
|
||||
toTag: 'v0.5.0',
|
||||
ignorePreReleases: false,
|
||||
failOnError: false,
|
||||
configuration: configuration
|
||||
})
|
||||
@@ -136,12 +102,11 @@ it('Should match ordered DESC', async () => {
|
||||
jest.setTimeout(180000)
|
||||
|
||||
const configuration = resolveConfiguration('', 'configs_test/configuration_desc.json')
|
||||
const releaseNotes = new ReleaseNotes({
|
||||
const releaseNotes = new ReleaseNotes(octokit, {
|
||||
owner: 'mikepenz',
|
||||
repo: 'release-changelog-builder-action',
|
||||
fromTag: 'v0.3.0',
|
||||
toTag: 'v0.5.0',
|
||||
ignorePreReleases: false,
|
||||
failOnError: false,
|
||||
configuration: configuration
|
||||
})
|
||||
+33
-38
@@ -300,6 +300,8 @@ const releaseNotes_1 = __webpack_require__(5882);
|
||||
const gitHelper_1 = __webpack_require__(353);
|
||||
const github = __importStar(__webpack_require__(5438));
|
||||
const configuration_1 = __webpack_require__(5527);
|
||||
const rest_1 = __webpack_require__(5375);
|
||||
const tags_1 = __webpack_require__(7532);
|
||||
function run() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
core.setOutput('failed', false); // mark the action not failed by default
|
||||
@@ -316,7 +318,7 @@ function run() {
|
||||
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';
|
||||
@@ -361,16 +363,34 @@ function run() {
|
||||
core.debug(`Resolved 'toTag' as ${toTag}`);
|
||||
}
|
||||
core.endGroup();
|
||||
const releaseNotes = new releaseNotes_1.ReleaseNotes({
|
||||
// load octokit instance
|
||||
const octokit = new rest_1.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_1.Tags(octokit);
|
||||
const previousTag = yield tagsApi.findPredecessorTag(owner, repo, toTag, ignorePreReleases, configuration.max_tags_to_fetch ||
|
||||
configuration_1.DefaultConfiguration.max_tags_to_fetch);
|
||||
if (previousTag == null) {
|
||||
utils_1.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_1.ReleaseNotes(octokit, {
|
||||
owner,
|
||||
repo,
|
||||
fromTag,
|
||||
toTag,
|
||||
ignorePreReleases,
|
||||
failOnError,
|
||||
configuration
|
||||
});
|
||||
core.setOutput('changelog', (yield releaseNotes.pull(token)) ||
|
||||
core.setOutput('changelog', (yield releaseNotes.pull()) ||
|
||||
configuration.empty_template ||
|
||||
configuration_1.DefaultConfiguration.empty_template);
|
||||
}
|
||||
@@ -620,47 +640,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.ReleaseNotes = void 0;
|
||||
const rest_1 = __webpack_require__(5375);
|
||||
const commits_1 = __webpack_require__(3916);
|
||||
const pullRequests_1 = __webpack_require__(4217);
|
||||
const transform_1 = __webpack_require__(1644);
|
||||
const core = __importStar(__webpack_require__(2186));
|
||||
const tags_1 = __webpack_require__(7532);
|
||||
const configuration_1 = __webpack_require__(5527);
|
||||
const utils_1 = __webpack_require__(918);
|
||||
class ReleaseNotes {
|
||||
constructor(options) {
|
||||
constructor(octokit, options) {
|
||||
this.octokit = octokit;
|
||||
this.options = options;
|
||||
}
|
||||
pull(token) {
|
||||
pull() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const octokit = new rest_1.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_1.Tags(octokit);
|
||||
const previousTag = yield tagsApi.findPredecessorTag(owner, repo, toTag, ignorePreReleases, configuration.max_tags_to_fetch ||
|
||||
configuration_1.DefaultConfiguration.max_tags_to_fetch);
|
||||
if (previousTag == null) {
|
||||
utils_1.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) {
|
||||
utils_1.failOrError(`💥 Missing or couldn't resolve 'fromTag'`, failOnError);
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
core.setOutput('fromTag', this.options.fromTag);
|
||||
}
|
||||
const { configuration } = this.options;
|
||||
core.startGroup(`🚀 Load pull requests`);
|
||||
const mergedPullRequests = yield this.getMergedPullRequests(octokit);
|
||||
const mergedPullRequests = yield this.getMergedPullRequests(this.octokit);
|
||||
core.endGroup();
|
||||
if (mergedPullRequests.length === 0) {
|
||||
core.warning(`⚠️ No pull requests found`);
|
||||
@@ -932,9 +927,7 @@ function buildChangelog(prs, config) {
|
||||
const transformedMap = new Map();
|
||||
// convert PRs to their text representation
|
||||
for (const pr of prs) {
|
||||
transformedMap.set(pr, transform(fillTemplate(pr, config.pr_template
|
||||
? config.pr_template
|
||||
: configuration_1.DefaultConfiguration.pr_template), validatedTransformers));
|
||||
transformedMap.set(pr, transform(fillTemplate(pr, config.pr_template || configuration_1.DefaultConfiguration.pr_template), validatedTransformers));
|
||||
}
|
||||
core.info(`ℹ️ Used ${validateTransfomers.length} transformers to adjust message`);
|
||||
core.info(`✒️ Wrote messages for ${prs.length} pull requests`);
|
||||
@@ -1009,7 +1002,9 @@ function transform(filled, transformers) {
|
||||
}
|
||||
let transformed = filled;
|
||||
for (const { target, pattern } of transformers) {
|
||||
transformed = transformed.replace(pattern, target);
|
||||
if (pattern) {
|
||||
transformed = transformed.replace(pattern, target);
|
||||
}
|
||||
}
|
||||
return transformed;
|
||||
}
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
+36
-4
@@ -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
@@ -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
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user