From e4b25b2089ee76b3124ec49e18484b91f8afe914 Mon Sep 17 00:00:00 2001 From: Jirka Helmich Date: Thu, 14 Jan 2021 21:32:32 +0100 Subject: [PATCH] feat: create tag name regex option for more precise changelogs --- README.MD | 13 +++++++------ action.yml | 5 ++++- dist/index.js | 25 ++++++++++++++++++++++--- src/context.js | 2 ++ src/history-parser.js | 23 ++++++++++++++++++++--- 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/README.MD b/README.MD index ace6f27..e51d3f1 100644 --- a/README.MD +++ b/README.MD @@ -1,4 +1,4 @@ -[![version](https://img.shields.io/badge/version-1.0.4-yellow.svg)](https://semver.org) +[![version](https://img.shields.io/badge/version-1.0.5-yellow.svg)](https://semver.org) ### :pencil2: :page_with_curl: Conventional Commit Changelog Generator (GitHub Action) @@ -9,17 +9,18 @@ The changelog is accessible as action output available to other actions via [out ## Example usage ```yaml -uses: Helmisek/conventional-changelog-generator@v1.0.4 +uses: Helmisek/conventional-changelog-generator@v1.0.5 with: token: ${{ secrets.GITHUB_TOKEN }} ``` ## Inputs -| Name | Description | Required | Example | -| --------------- | -------------------------------- | -------- | ----------------------------- | -| `commit-types` | Commit -> changelog mapping | Yes | `feat:Features,bug:Bug Fixes` | -| `template-path` | Path to a custom template to use | No | `CHANGELOG.tpl.md` | +| Name | Description | Required | Example | +| --------------- | ------------------------------------ | -------- | ----------------------------- | +| `commit-types` | Commit -> changelog mapping | Yes | `feat:Features,bug:Bug Fixes` | +| `template-path` | Path to a custom template to use | No | `CHANGELOG.tpl.md` | +| `tag-regex` | A custom regex to filter versions by | No | `internal` | ## Outputs diff --git a/action.yml b/action.yml index 4f67165..5b473b3 100644 --- a/action.yml +++ b/action.yml @@ -9,7 +9,10 @@ inputs: required: true template-path: description: Changelog template - required: true + required: false + tag-regex: + description: Specific regex to filter tags by and to version just between these (useful for SemVer labels) + required: false outputs: changelog: description: Generated changelog for the latest push version tag diff --git a/dist/index.js b/dist/index.js index 575c1d0..62aa80f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5995,6 +5995,7 @@ const workspace = process.env.GITHUB_WORKSPACE; const token = core.getInput("repo-token", { required: true }); const commitTypes = core.getInput("commit-types", { required: true }); const templateFilePath = core.getInput("template-path", { required: false }); +const tagRegex = core.getInput("tag-regex", { required: false }); if (!token) { throw Error('"token" input is missing!'); @@ -6010,6 +6011,7 @@ module.exports = { commitTypes, workspace, templateFilePath, + tagRegex, }; @@ -6161,7 +6163,7 @@ module.exports = generator; const core = __webpack_require__(6024); const compareVersions = __webpack_require__(7597); const octokit = __webpack_require__(7323); -const { repo, owner } = __webpack_require__(348); +const { repo, owner, tagRegex } = __webpack_require__(348); const rangedCommits = __webpack_require__(2259); /** @@ -6194,8 +6196,9 @@ const releaseCommitRange = async (releaseTags) => { const currentReleaseIndex = 0; const toSHA = releases[currentReleaseIndex].commit.sha; core.info(`current release sha: "${toSHA}"`); - const previousReleaseIndex = releases.findIndex( - (release, index) => index > currentReleaseIndex + const previousReleaseIndex = findPreviousReleaseIndex( + releases, + currentReleaseIndex ); const fromSHA = await findBaseSha(previousReleaseIndex, releases, toSHA); core.info(`previous release sha: "${fromSHA}"`); @@ -6205,6 +6208,22 @@ const releaseCommitRange = async (releaseTags) => { }; }; +/*** + * If there is a `tagRegex` option set then perform a RegExp search while filtering release. + * Otherwise just find a release with higher index than `currentReleaseIndex`. + */ +function findPreviousReleaseIndex(releases, currentReleaseIndex) { + if (tagRegex == null) { + return releases.findIndex((_, index) => index > currentReleaseIndex); + } else { + const versionRegExp = new RegExp(`${tagRegex}`, "g"); + return releases.findIndex( + (release, index) => + index > currentReleaseIndex && release.name.match(versionRegExp) != null + ); + } +} + /** * If there is no previous release (thus meaning there is only a 1 tag in the repository) then * set the base sha to the first commit in the repository. diff --git a/src/context.js b/src/context.js index b4ce4ce..fe9e530 100644 --- a/src/context.js +++ b/src/context.js @@ -25,6 +25,7 @@ const workspace = process.env.GITHUB_WORKSPACE; const token = core.getInput("repo-token", { required: true }); const commitTypes = core.getInput("commit-types", { required: true }); const templateFilePath = core.getInput("template-path", { required: false }); +const tagRegex = core.getInput("tag-regex", { required: false }); if (!token) { throw Error('"token" input is missing!'); @@ -40,4 +41,5 @@ module.exports = { commitTypes, workspace, templateFilePath, + tagRegex, }; diff --git a/src/history-parser.js b/src/history-parser.js index c15c38e..cfe329e 100644 --- a/src/history-parser.js +++ b/src/history-parser.js @@ -1,7 +1,7 @@ const core = require("@actions/core"); const compareVersions = require("compare-versions"); const octokit = require("./octokit"); -const { repo, owner } = require("./context"); +const { repo, owner, tagRegex } = require("./context"); const rangedCommits = require("./commits"); /** @@ -34,8 +34,9 @@ const releaseCommitRange = async (releaseTags) => { const currentReleaseIndex = 0; const toSHA = releases[currentReleaseIndex].commit.sha; core.info(`current release sha: "${toSHA}"`); - const previousReleaseIndex = releases.findIndex( - (release, index) => index > currentReleaseIndex + const previousReleaseIndex = findPreviousReleaseIndex( + releases, + currentReleaseIndex ); const fromSHA = await findBaseSha(previousReleaseIndex, releases, toSHA); core.info(`previous release sha: "${fromSHA}"`); @@ -45,6 +46,22 @@ const releaseCommitRange = async (releaseTags) => { }; }; +/*** + * If there is a `tagRegex` option set then perform a RegExp search while filtering release. + * Otherwise just find a release with higher index than `currentReleaseIndex`. + */ +function findPreviousReleaseIndex(releases, currentReleaseIndex) { + if (tagRegex == null) { + return releases.findIndex((_, index) => index > currentReleaseIndex); + } else { + const versionRegExp = new RegExp(`${tagRegex}`, "g"); + return releases.findIndex( + (release, index) => + index > currentReleaseIndex && release.name.match(versionRegExp) != null + ); + } +} + /** * If there is no previous release (thus meaning there is only a 1 tag in the repository) then * set the base sha to the first commit in the repository.