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

Update: commit-message enforces issue ref style (fixes #56) #70

Merged
merged 1 commit into from
Aug 5, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/plugins/commit-message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
const { getCommitMessageForPR } = require("../utils");

const TAG_REGEX = /^((?:Breaking|Build|Chore|Docs|Fix|New|Update|Upgrade):|Revert )/;

const POTENTIAL_ISSUE_REF_REGEX = /#\d+/;

const VALID_ISSUE_REF = "(?:(?:fixes|refs) #\\d+)";
const CORRECT_ISSUE_REF_REGEX = new RegExp(` \\(${VALID_ISSUE_REF}(?:, ${VALID_ISSUE_REF})*\\)$`);

const MESSAGE_LENGTH_LIMIT = 72;

const EXCLUDED_REPOSITORY_NAMES = new Set([
Expand All @@ -23,7 +29,21 @@ const EXCLUDED_REPOSITORY_NAMES = new Set([
* @private
*/
function checkCommitMessage(message) {
return TAG_REGEX.test(message) && message.split(/\r?\n/)[0].length <= MESSAGE_LENGTH_LIMIT;

// First, check tag and summary length
let isValid = TAG_REGEX.test(message) && message.split(/\r?\n/)[0].length <= MESSAGE_LENGTH_LIMIT;

// Then, if there appears to be an issue reference, test for correctness
if (isValid && POTENTIAL_ISSUE_REF_REGEX.test(message)) {
const issueSuffixMatch = CORRECT_ISSUE_REF_REGEX.exec(message);

// If no suffix, or issue ref occurs before suffix, message is invalid
if (!issueSuffixMatch || POTENTIAL_ISSUE_REF_REGEX.test(message.slice(0, issueSuffixMatch.index))) {
isValid = false;
}
}

return isValid;
}

/**
Expand Down
69 changes: 69 additions & 0 deletions tests/plugins/commit-message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,75 @@ describe("commit-message", () => {
expect(nockScope.isDone()).toBeTruthy();
});

// Tests for malformed issue references
[
"#1",
"fixes #1",
"(fix #1)",
"(ref #1)",
"(closes #1)",
"(fixes #1, #2)",

// Unexpected issue number with valid suffix should fail
"#1 (fixes #1)"
].forEach(suffix => {
test(`Posts failure status if the commit message references issue improperly: ${suffix}`, async() => {
mockSingleCommitWithMessage(
`New: foo ${suffix}`
);

const nockScope = nock("https://api.github.com")
.post("/repos/test/repo-test/statuses/first-sha", req => req.state === "failure")
.reply(201);

await emitBotEvent(bot, { action });
expect(nockScope.isDone()).toBeTruthy();
});

test(`Posts failure status if multiple commits and PR title references issue improperly: ${suffix}`, async() => {
mockMultipleCommits();

const nockScope = nock("https://api.github.com")
.post("/repos/test/repo-test/statuses/second-sha", req => req.state === "failure")
.reply(201);

await emitBotEvent(bot, { action, pull_request: { number: 1, title: `New: foo ${suffix}` } });
expect(nockScope.isDone()).toBeTruthy();
});
});

// Tests for correct issue references
[
"(fixes #1)",
"(refs #1)",
"(fixes #1, fixes #2)",
"(fixes #1, refs #2, fixes #3)"
].forEach(suffix => {
test(`Posts success status if the commit message references issue correctly: ${suffix}`, async() => {
mockSingleCommitWithMessage(
`New: foo ${suffix}`
);

const nockScope = nock("https://api.github.com")
.post("/repos/test/repo-test/statuses/first-sha", req => req.state === "success")
.reply(201);

await emitBotEvent(bot, { action });
expect(nockScope.isDone()).toBeTruthy();
});

test(`Posts success status if multiple commits and PR title references issue correctly: ${suffix}`, async() => {
mockMultipleCommits();

const nockScope = nock("https://api.github.com")
.post("/repos/test/repo-test/statuses/second-sha", req => req.state === "success")
.reply(201);

await emitBotEvent(bot, { action, pull_request: { number: 1, title: `New: foo ${suffix}` } });
expect(nockScope.isDone()).toBeTruthy();
});
});

test("Does not post a status if the repository is excluded", async() => {
await emitBotEvent(bot, {
action: "opened",
Expand Down