Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conventional commits releaseType calculation to include all conventionally committed commits in PR #1723

Merged
merged 1 commit into from Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -49,6 +49,7 @@ describe("parseCommit", () => {
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: { getCommitsForPR: () => Promise.resolve([]) } as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
Expand All @@ -73,6 +74,7 @@ describe("parseCommit", () => {
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: { getCommitsForPR: () => Promise.resolve([]) } as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
Expand All @@ -97,6 +99,7 @@ describe("parseCommit", () => {
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: { getCommitsForPR: () => Promise.resolve([]) } as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
Expand All @@ -121,6 +124,7 @@ describe("parseCommit", () => {
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: { getCommitsForPR: () => Promise.resolve([]) } as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
Expand All @@ -137,6 +141,38 @@ describe("parseCommit", () => {
});
});

test("should apply a PRs greatest semver label", async () => {
const conventionalCommitsPlugin = new ConventionalCommitsPlugin();
const autoHooks = makeHooks();
conventionalCommitsPlugin.apply({
hooks: autoHooks,
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: {
getCommitsForPR: () =>
Promise.resolve([
{ sha: "8", commit: { message: "docs: child commit" } },
{ sha: "1", commit: { message: "chore: child commit" } },
{ sha: "2", commit: { message: "feat!: another commit" } },
]),
} as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
autoHooks.onCreateLogParse.call({
hooks: logParseHooks,
} as LogParse);

const commit = makeCommitFromMsg("fix lint", {pullRequest: {number: 123}});
expect(
await logParseHooks.parseCommit.promise({ ...commit })
).toStrictEqual({
...commit,
labels: ["major"],
});
});

test("should skip when not a fix/feat/breaking change commit", async () => {
const conventionalCommitsPlugin = new ConventionalCommitsPlugin();
const autoHooks = makeHooks();
Expand All @@ -145,6 +181,7 @@ describe("parseCommit", () => {
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: { getCommitsForPR: () => Promise.resolve([]) } as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
Expand All @@ -171,6 +208,7 @@ describe("parseCommit", () => {
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: { getCommitsForPR: () => Promise.resolve([]) } as any,
} as Auto);

const logParseHooks = makeLogParseHooks();
Expand Down Expand Up @@ -204,7 +242,8 @@ describe("normalizeCommit", () => {
getPr: jest.fn(),
getCommitsForPR: () =>
Promise.resolve([
{ sha: "1", commit: { message: "fix: child commit" } },
{ sha: "1", commit: { message: "chore: child commit" } },
{ sha: "2", commit: { message: "chore: another commit" } },
]),
} as unknown) as Git;
conventionalCommitsPlugin.apply({
Expand Down
1 change: 1 addition & 0 deletions plugins/conventional-commits/package.json
Expand Up @@ -42,6 +42,7 @@
"conventional-changelog-core": "^4.2.0",
"conventional-changelog-preset-loader": "^2.3.4",
"conventional-commits-parser": "^3.1.0",
"endent": "^2.0.1",
"fp-ts": "^2.5.3",
"io-ts": "^2.1.2",
"tslib": "2.1.0"
Expand Down
53 changes: 42 additions & 11 deletions plugins/conventional-commits/src/index.ts
Expand Up @@ -12,6 +12,7 @@ import {
getReleaseType,
getHigherSemverTag,
} from "@auto-it/core";
import endent from "endent";

/** Resolve a conventional commit preset */
function presetResolver(presetPackage: CoreOptions.Config) {
Expand Down Expand Up @@ -201,19 +202,12 @@ export default class ConventionalCommitsPlugin implements IPlugin {

auto.hooks.onCreateLogParse.tap(this.name, (logParse) => {
logParse.hooks.parseCommit.tapPromise(this.name, async (commit) => {
if (!auto.semVerLabels) {
if (!auto.semVerLabels || !auto.git) {
return commit;
}

try {
const message = `${commit.subject}\n\n${commit.rawBody}`;
const label = await getBump(message);

if (!label) {
return commit;
}

const incrementLabel = auto.semVerLabels.get(label);
const allSemVerLabels = [
auto.semVerLabels.get(SEMVER.major),
auto.semVerLabels.get(SEMVER.minor),
Expand All @@ -222,13 +216,50 @@ export default class ConventionalCommitsPlugin implements IPlugin {
(acc, labels) => (labels ? [...acc, ...labels] : acc),
[]
);
const prHasSemverLabel = commit.labels.some((l) =>
allSemVerLabels.includes(l)
);
let bump = await getBump(message);

// Take into account all conventional commit message in each commit for a PR
if (
incrementLabel &&
!commit.labels.some((l) => allSemVerLabels.includes(l))
commit.pullRequest &&
// If the PR already has a semver label it takes precedence over conventional
// commit messages
!prHasSemverLabel
) {
const prCommits = await auto.git.getCommitsForPR(
commit.pullRequest.number
);
const prBumps = (
await Promise.all(prCommits.map((c) => getBump(c.commit.message)))
).filter((bump): bump is
| SEMVER.major
| SEMVER.minor
| SEMVER.patch => Boolean(bump && bump !== "skip"));

if (prBumps.includes(SEMVER.major)) {
bump = SEMVER.major;
} else if (prBumps.includes(SEMVER.minor)) {
bump = SEMVER.minor;
} else if (prBumps.includes(SEMVER.patch)) {
bump = SEMVER.patch;
}
}

if (!bump) {
return commit;
}

const incrementLabel = auto.semVerLabels.get(bump);

if (incrementLabel && !prHasSemverLabel) {
auto.logger.verbose.log(
`Found "${label}" from conventional commit message: ${message}`
endent`
Found "${bump}" from conventional commit message: ${message}

Applying "${incrementLabel}"
`
);

commit.labels = [...commit.labels, incrementLabel[0]];
Expand Down