Skip to content

Commit

Permalink
fix: Incorrect versioning in labeling system
Browse files Browse the repository at this point in the history
This error was due to lexographic tag ordering which messed up with SemVer labels.
  • Loading branch information
quant-eagle committed Jan 17, 2021
1 parent e4b25b2 commit 9c38021
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 22 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/internal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
push:
tags:
- "v*-internal"

jobs:
changelog_job:
runs-on: ubuntu-latest
name: Changelog generator
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
- name: Changelog generation step
uses: ./
id: changelog-generator
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
commit-types: "fix:Bug Fixes,feat:Features,refactor:Refactoring"
template-path: "CHANGELOG.tpl.md"
current-tag: ${{ steps.get_version.outputs.VERSION }}
tag-regex: "-internal"
- name: Print version
run: echo ${{ steps.get_version.outputs.VERSION }}
- name: Print changelog
run: echo "${{steps.changelog-generator.outputs.changelog}}"
9 changes: 8 additions & 1 deletion .github/workflows/sample.yml → .github/workflows/qa.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
tags:
- "v*"
- "v*-qa"

jobs:
changelog_job:
Expand All @@ -10,12 +10,19 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
- name: Changelog generation step
uses: ./
id: changelog-generator
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
commit-types: "fix:Bug Fixes,feat:Features,refactor:Refactoring"
template-path: "CHANGELOG.tpl.md"
current-tag: ${{ steps.get_version.outputs.VERSION }}
tag-regex: "-qa"
- name: Print version
run: echo ${{ steps.get_version.outputs.VERSION }}
- name: Print changelog
run: echo "${{steps.changelog-generator.outputs.changelog}}"
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
push:
tags:
- "v*-release"

jobs:
changelog_job:
runs-on: ubuntu-latest
name: Changelog generator
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
- name: Changelog generation step
uses: ./
id: changelog-generator
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
commit-types: "fix:Bug Fixes,feat:Features,refactor:Refactoring"
template-path: "CHANGELOG.tpl.md"
current-tag: ${{ steps.get_version.outputs.VERSION }}
tag-regex: "-release"
- name: Print version
run: echo ${{ steps.get_version.outputs.VERSION }}
- name: Print changelog
run: echo "${{steps.changelog-generator.outputs.changelog}}"
5 changes: 3 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![version](https://img.shields.io/badge/version-1.0.5-yellow.svg)](https://semver.org)
[![version](https://img.shields.io/badge/version-1.0.6-yellow.svg)](https://semver.org)

### :pencil2: :page_with_curl: Conventional Commit Changelog Generator (GitHub Action)

Expand All @@ -9,7 +9,7 @@ The changelog is accessible as action output available to other actions via [out
## Example usage

```yaml
uses: Helmisek/conventional-changelog-generator@v1.0.5
uses: Helmisek/conventional-changelog-generator@v1.0.6
with:
token: ${{ secrets.GITHUB_TOKEN }}
```
Expand All @@ -19,6 +19,7 @@ with:
| Name | Description | Required | Example |
| --------------- | ------------------------------------ | -------- | ----------------------------- |
| `commit-types` | Commit -> changelog mapping | Yes | `feat:Features,bug:Bug Fixes` |
| `current-tag` | Currently selected tag | No | `v1.0.0+1-internal` |
| `template-path` | Path to a custom template to use | No | `CHANGELOG.tpl.md` |
| `tag-regex` | A custom regex to filter versions by | No | `internal` |

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ inputs:
repo-token:
description: Github repository token
required: true
current-tag:
description: Current tag selected to be the start of the changelog range
required: false
commit-types:
description: Conventional commit type mapping
required: true
Expand Down
38 changes: 29 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5996,6 +5996,7 @@ 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 });
const currentTag = core.getInput("current-tag", { required: false });

if (!token) {
throw Error('"token" input is missing!');
Expand All @@ -6012,6 +6013,7 @@ module.exports = {
workspace,
templateFilePath,
tagRegex,
currentTag,
};


Expand Down Expand Up @@ -6163,7 +6165,7 @@ module.exports = generator;
const core = __webpack_require__(6024);
const compareVersions = __webpack_require__(7597);
const octokit = __webpack_require__(7323);
const { repo, owner, tagRegex } = __webpack_require__(348);
const { repo, owner, tagRegex, currentTag } = __webpack_require__(348);
const rangedCommits = __webpack_require__(2259);

/**
Expand All @@ -6177,11 +6179,12 @@ const releaseTags = async () => {
repo,
});
core.info(`fetched all ${tags.length} tags in repository.`);
const releaseTags = tags
.filter((tag) => compareVersions.validate(tag.name))
.sort((a, b) => compareVersions(a.name, b.name))
.reverse();
const releaseTags = tags.filter((tag) => compareVersions.validate(tag.name));
core.info(`sorted tags by semver.`);
core.info(`showing up to 10 last tags...`);
releaseTags.slice(0, 10).forEach((release, index) => {
core.info(`tag ${release.name}`);
});
return releaseTags;
};

Expand All @@ -6193,23 +6196,40 @@ const releaseCommitRange = async (releaseTags) => {
core.info("fetch all releases...");
const releases = releaseTags;
core.info(`fetched all ${releases.length} releases.`);
const currentReleaseIndex = 0;
const currentReleaseIndex = findCurrentReleaseIndex(releases);
core.info(`current release index: ${currentReleaseIndex}`);
const toSHA = releases[currentReleaseIndex].commit.sha;
core.info(`current release sha: "${toSHA}"`);
const previousReleaseIndex = findPreviousReleaseIndex(
releases,
currentReleaseIndex
);
core.info(`previous release index: ${previousReleaseIndex}`);
const fromSHA = await findBaseSha(previousReleaseIndex, releases, toSHA);
core.info(`previous release sha: "${fromSHA}"`);
return {
releaseName: releases[currentReleaseIndex].name,
fromSHA,
toSHA,
};
};

/***
* If there is a `tagRegex` option set then perform a RegExp search while filtering release.
/**
* If there is a `current-tag` option set then find the index of the provided tag.
* Otherwise fallback to the first provided tag (0).
*/
function findCurrentReleaseIndex(releases) {
if (currentTag == null) {
return 0;
} else {
return releases.findIndex((release, _) =>
release.name.includes(currentTag)
);
}
}

/**
* If there is a `tag-regex` option set then perform a RegExp search while filtering release.
* Otherwise just find a release with higher index than `currentReleaseIndex`.
*/
function findPreviousReleaseIndex(releases, currentReleaseIndex) {
Expand Down Expand Up @@ -6258,7 +6278,7 @@ const commitHistory = async () => {
const commits = await rangedCommits(commitRange.fromSHA, commitRange.toSHA);
return {
commits,
versionName: releases[0].name,
versionName: commitRange.releaseName,
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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 });
const currentTag = core.getInput("current-tag", { required: false });

if (!token) {
throw Error('"token" input is missing!');
Expand All @@ -42,4 +43,5 @@ module.exports = {
workspace,
templateFilePath,
tagRegex,
currentTag,
};
37 changes: 27 additions & 10 deletions src/history-parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const core = require("@actions/core");
const compareVersions = require("compare-versions");
const octokit = require("./octokit");
const { repo, owner, tagRegex } = require("./context");
const { repo, owner, tagRegex, currentTag } = require("./context");
const rangedCommits = require("./commits");

/**
Expand All @@ -15,11 +15,11 @@ const releaseTags = async () => {
repo,
});
core.info(`fetched all ${tags.length} tags in repository.`);
const releaseTags = tags
.filter((tag) => compareVersions.validate(tag.name))
.sort((a, b) => compareVersions(a.name, b.name))
.reverse();
core.info(`sorted tags by semver.`);
const releaseTags = tags.filter((tag) => compareVersions.validate(tag.name));
core.info(`showing up to 10 last tags...`);
releaseTags.slice(0, 10).forEach((release, index) => {
core.info(`tag ${release.name}`);
});
return releaseTags;
};

Expand All @@ -31,23 +31,40 @@ const releaseCommitRange = async (releaseTags) => {
core.info("fetch all releases...");
const releases = releaseTags;
core.info(`fetched all ${releases.length} releases.`);
const currentReleaseIndex = 0;
const currentReleaseIndex = findCurrentReleaseIndex(releases);
core.info(`current release index: ${currentReleaseIndex}`);
const toSHA = releases[currentReleaseIndex].commit.sha;
core.info(`current release sha: "${toSHA}"`);
const previousReleaseIndex = findPreviousReleaseIndex(
releases,
currentReleaseIndex
);
core.info(`previous release index: ${previousReleaseIndex}`);
const fromSHA = await findBaseSha(previousReleaseIndex, releases, toSHA);
core.info(`previous release sha: "${fromSHA}"`);
return {
releaseName: releases[currentReleaseIndex].name,
fromSHA,
toSHA,
};
};

/***
* If there is a `tagRegex` option set then perform a RegExp search while filtering release.
/**
* If there is a `current-tag` option set then find the index of the provided tag.
* Otherwise fallback to the first provided tag (0).
*/
function findCurrentReleaseIndex(releases) {
if (currentTag == null) {
return 0;
} else {
return releases.findIndex((release, _) =>
release.name.includes(currentTag)
);
}
}

/**
* If there is a `tag-regex` option set then perform a RegExp search while filtering release.
* Otherwise just find a release with higher index than `currentReleaseIndex`.
*/
function findPreviousReleaseIndex(releases, currentReleaseIndex) {
Expand Down Expand Up @@ -96,7 +113,7 @@ const commitHistory = async () => {
const commits = await rangedCommits(commitRange.fromSHA, commitRange.toSHA);
return {
commits,
versionName: releases[0].name,
versionName: commitRange.releaseName,
};
};

Expand Down

0 comments on commit 9c38021

Please sign in to comment.