diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 4111a9c..b782d87 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -31,6 +31,7 @@ it('Should be true', async () => { repo: 'release-changelog-builder-action', fromTag: null, toTag: 'v0.0.3', + ignorePreReleases: false, configuration: configuration }) diff --git a/action.yml b/action.yml index fbd3ab0..9a0c94b 100644 --- a/action.yml +++ b/action.yml @@ -19,6 +19,9 @@ inputs: description: 'the previous tag to compare against' toTag: description: 'the new tag created' + ignorePreReleases: + description: 'defines if only full releases should be considered to compare against (Only used if fromTag is not defined). E.g. for 1.0.1... 1.0.0-rc02 <- ignore, 1.0.0 <- pick' + default: "false" token: description: 'the token to use to execute the git API requests' outputs: diff --git a/src/main.ts b/src/main.ts index 70c1633..46764bb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,6 +33,8 @@ async function run(): Promise { const fromTag = core.getInput('fromTag') let toTag = core.getInput('toTag') + const ignorePreReleases = core.getInput('ignorePreReleases') + if (!toTag) { // if not specified try to retrieve tag from git const gitHelper = await createCommandManager(repositoryPath) @@ -87,6 +89,7 @@ async function run(): Promise { repo, fromTag, toTag, + ignorePreReleases: ignorePreReleases === 'true', configuration }) diff --git a/src/releaseNotes.ts b/src/releaseNotes.ts index 3e376ce..5a3afb3 100755 --- a/src/releaseNotes.ts +++ b/src/releaseNotes.ts @@ -7,11 +7,12 @@ import {Tags} from './tags' import {Configuration, DefaultConfiguration} from './configuration' export interface ReleaseNotesOptions { - owner: string - repo: string - fromTag: string | null - toTag: string - configuration: Configuration + owner: string // the owner of the repository + repo: string // the repository + fromTag: string | null // 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 + configuration: Configuration // the configuration as defined in `configuration.ts` } export class ReleaseNotes { @@ -22,7 +23,7 @@ export class ReleaseNotes { auth: `token ${token || process.env.GITHUB_TOKEN}` }) - const {owner, repo, toTag, configuration} = this.options + const {owner, repo, toTag, ignorePreReleases, configuration} = this.options if (!this.options.fromTag) { core.debug(`fromTag undefined, trying to resolve via API`) @@ -32,6 +33,7 @@ export class ReleaseNotes { owner, repo, toTag, + ignorePreReleases, configuration.max_tags_to_fetch ? configuration.max_tags_to_fetch : DefaultConfiguration.max_tags_to_fetch diff --git a/src/tags.ts b/src/tags.ts index 8ea4573..e780d47 100755 --- a/src/tags.ts +++ b/src/tags.ts @@ -49,19 +49,32 @@ export class Tags { owner: string, repo: string, tag: string, + ignorePreReleases: boolean, maxTagsToFetch: number ): Promise { const tags = this.sortTags(await this.getTags(owner, repo, maxTagsToFetch)) - const length = tags.length - for (let i = 0; i < length; i++) { - if (tags[i].name.toLowerCase() === tag.toLowerCase()) { - return tags[i + 1] + try { + const length = tags.length + for (let i = 0; i < length; i++) { + if (tags[i].name.toLowerCase() === tag.toLowerCase()) { + if (ignorePreReleases) { + core.info( + `Enabled 'ignorePreReleases', searching for the closest release` + ) + for (let ii = i + 1; ii < length; ii++) { + if (!tags[ii].name.includes('-')) { + return tags[ii] + } + } + } + return tags[i + 1] + } } + return tags[0] + } catch (error) { + return null } - - // not found, throw exception? - return tags[0] } private sortTags(commits: TagInfo[]): TagInfo[] {