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

feat: conventional commit plugin will label an unlabeled PR #1758

Merged
merged 5 commits into from Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
120 changes: 120 additions & 0 deletions plugins/conventional-commits/__tests__/conventional-commits.test.ts
Expand Up @@ -313,3 +313,123 @@ test("should skip when not a fix/feat/breaking change commit", async () => {
labels: ["skip-release"],
});
});

test("should not semver label to pr if semver label exists", async () => {
const conventionalCommitsPlugin = new ConventionalCommitsPlugin();
const autoHooks = makeHooks();
const addLabelToPr = jest.fn();
const auto = ({
hooks: autoHooks,
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: {
getLabels: async () => ["minor"],
getCommitsForPR: async () => {
return [
{
sha: "1234",
commit: {
message: "fix: normal commit",
},
},
];
},
addLabelToPr,
},
} as unknown) as Auto;

conventionalCommitsPlugin.apply(auto);

await auto.hooks.prCheck.promise({
pr: {
number: 1,
} as any,
});

expect(addLabelToPr).toHaveBeenCalledTimes(0);
});

test("should add correct semver label to pr - one commit", async () => {
const conventionalCommitsPlugin = new ConventionalCommitsPlugin();
const autoHooks = makeHooks();
const addLabelToPr = jest.fn();
const auto = ({
hooks: autoHooks,
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: {
getLabels: async () => [],
getCommitsForPR: async () => {
return [
{
sha: "1234",
commit: {
message: "fix: normal commit",
},
},
];
},
addLabelToPr,
},
} as unknown) as Auto;

conventionalCommitsPlugin.apply(auto);

await auto.hooks.prCheck.promise({
pr: {
number: 1,
} as any,
});

expect(addLabelToPr).toHaveBeenCalledWith(1, "patch");
});

test("should add correct semver label to pr - multiple commit", async () => {
const conventionalCommitsPlugin = new ConventionalCommitsPlugin();
const autoHooks = makeHooks();
const addLabelToPr = jest.fn();
const auto = ({
hooks: autoHooks,
labels: defaultLabels,
semVerLabels: versionLabels,
logger: dummyLog(),
git: {
getLabels: async () => [],
getCommitsForPR: async () => {
return [
{
sha: "1234",
commit: {
message: "fix: normal commit",
},
},
{
sha: "12345",
commit: {
message: "minor: normal commit",
},
},
{
sha: "123456",
commit: {
message: "minor: normal commit",
},
},
];
},
addLabelToPr,
},
} as unknown) as Auto;

conventionalCommitsPlugin.apply(auto);

await auto.hooks.prCheck.promise({
pr: {
number: 1,
} as any,
});

expect(addLabelToPr).toHaveBeenCalledWith(1, "patch");
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved
});
50 changes: 50 additions & 0 deletions plugins/conventional-commits/src/index.ts
Expand Up @@ -135,6 +135,56 @@ export default class ConventionalCommitsPlugin implements IPlugin {
return this.storedGetBump(message);
};

auto.hooks.prCheck.tapPromise(this.name, async ({ pr }) => {
if (!auto.git) {
return;
}

const VERSIONS = [
SEMVER.major,
SEMVER.minor,
SEMVER.patch,
"skip",
] as const;
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved

const labels = await auto.git.getLabels(pr.number);

// check if semver label is already on PR
if (labels.filter((l) => VERSIONS.includes(l as any)).length > 0) {
return;
}

const commits = await auto.git?.getCommitsForPR(pr.number);

const bumps = await Promise.all(
commits.map(async (commit) => {
try {
return await getBump(commit.commit.message);
} catch (error) {
auto.logger.verbose.info(
`No conventional commit message found for ${commit.sha}`
);
}
})
);

const sorted = bumps.sort((bump1, bump2) => {
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved
if (bump1 === undefined) {
return -1;
}

if (bump2 === undefined) {
return 1;
}

return VERSIONS.indexOf(bump1) - VERSIONS.indexOf(bump2);
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved
});

if (sorted[0] !== undefined) {
await auto.git.addLabelToPr(pr.number, sorted[0]);
}
});

auto.hooks.onCreateLogParse.tap(this.name, (logParse) => {
logParse.hooks.parseCommit.tapPromise(this.name, async (commit) => {
if (!auto.semVerLabels) {
Expand Down