Skip to content

Commit

Permalink
Merge pull request #1470 from intuit/default-bump
Browse files Browse the repository at this point in the history
fix default bump type
  • Loading branch information
hipstersmoothie committed Aug 14, 2020
2 parents 3ab9e8d + af5efa9 commit c846138
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
Binary file added packages/cli/auto
Binary file not shown.
14 changes: 11 additions & 3 deletions packages/core/src/__tests__/semver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const semverMap = getVersionMap([
]);

test("ranks releases right", () => {
expect(getHigherSemverTag(SEMVER.major, "minor")).toBe("major");
expect(getHigherSemverTag(SEMVER.noVersion, "bar")).toBe("patch");
expect(getHigherSemverTag(SEMVER.minor, "patch")).toBe("minor");
expect(getHigherSemverTag(SEMVER.major, SEMVER.minor)).toBe("major");
expect(getHigherSemverTag(SEMVER.noVersion, SEMVER.patch)).toBe("patch");
expect(getHigherSemverTag(SEMVER.minor, SEMVER.patch)).toBe("minor");
});

describe("calculateSemVerBump", () => {
Expand Down Expand Up @@ -56,6 +56,14 @@ describe("calculateSemVerBump", () => {
).toBe(SEMVER.minor);
});

test("should be able to configure default no-version clean", () => {
expect(
calculateSemVerBump([[], []], semverMap, {
labels: [{ default: true, name: "docs", releaseType: "none" }],
})
).toBe(SEMVER.noVersion);
});

test("should not skip things before none", () => {
expect(calculateSemVerBump([["none"], ["major"]], semverMap)).toBe(
SEMVER.major
Expand Down
73 changes: 45 additions & 28 deletions packages/core/src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const labelDefinitionRequired = t.type({
name: t.string,
});

const releaseType = t.union([
t.literal("none"),
t.literal("skip"),
...releaseLabels.map((l) => t.literal(l)),
]);
type ReleaseType = t.TypeOf<typeof releaseType>;

const labelDefinitionOptional = t.partial({
/** A title to put in the changelog for the label */
changelogTitle: t.string,
Expand All @@ -53,11 +60,7 @@ const labelDefinitionOptional = t.partial({
/** The description of the label */
description: t.string,
/** What type of release this label signifies */
releaseType: t.union([
t.literal("none"),
t.literal("skip"),
...releaseLabels.map((l) => t.literal(l)),
]),
releaseType,
/** Whether to overwrite the base label */
overwrite: t.boolean,
/** Marks this label as the default label for unlabelled PRs */
Expand All @@ -70,6 +73,14 @@ export const labelDefinition = t.intersection([
]);
export type ILabelDefinition = t.TypeOf<typeof labelDefinition>;

const patchLabel: ILabelDefinition = {
name: "patch",
changelogTitle: "🐛 Bug Fix",
description: "Increment the patch version when merged",
releaseType: SEMVER.patch,
color: "#870048",
};

export const defaultLabels: ILabelDefinition[] = [
{
name: "major",
Expand All @@ -85,13 +96,7 @@ export const defaultLabels: ILabelDefinition[] = [
releaseType: SEMVER.minor,
color: "#F1A60E",
},
{
name: "patch",
changelogTitle: "🐛 Bug Fix",
description: "Increment the patch version when merged",
releaseType: SEMVER.patch,
color: "#870048",
},
patchLabel,
{
name: "skip-release",
description: "Preserve the current version when merged",
Expand Down Expand Up @@ -142,7 +147,7 @@ export const defaultLabels: ILabelDefinition[] = [
];

/** Given two labels determine the next SEMVER bump. */
export function getHigherSemverTag(left: SEMVER, right: string): SEMVER {
export function getHigherSemverTag(left: SEMVER, right: SEMVER): SEMVER {
if (left === SEMVER.major || right === SEMVER.major) {
return SEMVER.major;
}
Expand All @@ -151,9 +156,21 @@ export function getHigherSemverTag(left: SEMVER, right: string): SEMVER {
return SEMVER.minor;
}

return SEMVER.patch;
if (left === SEMVER.patch || right === SEMVER.patch) {
return SEMVER.patch;
}

return SEMVER.noVersion;
}

/** Get the semver bump for a release type */
const getBump = (releaseType?: ReleaseType) =>
releaseType === "none" || releaseType === "skip"
? SEMVER.noVersion
: releaseType === "release"
? SEMVER.patch
: releaseType || SEMVER.patch;

/**
* Determine the version bump from the labels on merged PRs.
* Respects skip-release labels and the "onlyPublishWithReleaseLabel"
Expand All @@ -170,15 +187,15 @@ export function calculateSemVerBump(
labels?: ILabelDefinition[];
} = {}
) {
const defaultLabel =
labels.find((l) => l.default)?.releaseType || SEMVER.patch;
const labelSet = new Set<string>();
const defaultLabel = labels.find((l) => l.default) || patchLabel;
const defaultReleaseType = defaultLabel.releaseType || SEMVER.patch;
const releaseTypes = new Set<ReleaseType>();
const skipReleaseLabels = labelMap.get("skip") || [];

prLabels.forEach((pr, index) => {
// If the head pr has no labels we default to a patch
if (pr.length === 0 && index === 0) {
labelSet.add(defaultLabel);
releaseTypes.add(defaultReleaseType);
}

pr.forEach((label) => {
Expand All @@ -187,7 +204,7 @@ export function calculateSemVerBump(
);

if (userLabel) {
labelSet.add(userLabel[0]);
releaseTypes.add(userLabel[0]);
}
});
});
Expand All @@ -198,22 +215,22 @@ export function calculateSemVerBump(
? !lastMergedCommitLabels.some((label) => releaseLabels.includes(label))
: lastMergedCommitLabels.some((label) => skipReleaseLabels.includes(label));

if (skipRelease) {
return SEMVER.noVersion;
}

// If PRs only have none or skip labels, skip the release
const onlyNoReleaseLabels = [...labelSet].reduce(
const onlyNoReleaseLabels = [...releaseTypes].reduce(
(condition, releaseType) =>
condition && (releaseType === "none" || releaseType === "skip"),
true
);

if (labelSet.size > 0 && onlyNoReleaseLabels) {
return SEMVER.noVersion;
}

const version = [...labelSet].reduce(getHigherSemverTag, SEMVER.patch);

if (skipRelease) {
if (releaseTypes.size > 0 && onlyNoReleaseLabels) {
return SEMVER.noVersion;
}

return version;
return [...releaseTypes]
.map(getBump)
.reduce(getHigherSemverTag, getBump(defaultReleaseType));
}

0 comments on commit c846138

Please sign in to comment.