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

#3 fix usage of colon in PR title #5

Merged
merged 1 commit into from
Sep 24, 2020
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
71 changes: 53 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ async function check() {
if (helpers.checkPrBranch(prBranch) === false) {
core.setFailed("PR branch has wrong name");
return;
} else {
core.info(" - OK");
}

if (helpers.checkPrTitle(prTitle) === false) {
core.setFailed("PR title doesn't start with issue number");
return;
} else {
core.info(" - OK");
}

if (helpers.checkPrDescription(prDescription) == false) {
core.setFailed("PR description doesn't contain 'fixes #issue' phrase");
return;
} else {
core.info(" - OK");
}

if (
Expand All @@ -37,9 +43,11 @@ async function check() {
) === false
) {
core.setFailed(
"PR title and description contain different issue numbers"
"Branch name, PR title and PR description contain different issue numbers"
);
return;
} else {
core.info(" - OK");
}
} catch (error) {
core.error(error);
Expand Down Expand Up @@ -387,55 +395,82 @@ exports.getState = getState;
/***/ }),

/***/ 505:
/***/ ((__unused_webpack_module, exports) => {
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

const core = __webpack_require__(186);

function checkPrBranch(prBranch) {
core.info(`Checking branch name formatting\n - "${prBranch}"`);

let prBranchRegexp = /^\d+(-[^\W_]+)+$/;
return prBranchRegexp.test(prBranch);
}

function checkPrTitle(prTitle) {
let prTitleRegexp = /^#?\d+\s+.+$/;
core.info(`Checking PR title formatting\n - "${prTitle}"`);

let prTitleRegexp = /^#?\d+:?\s+.+$/;
return prTitleRegexp.test(prTitle);
}

function checkPrDescription(prDescription) {
let prDescriptionRegexp = /((fix(e[ds])?)|(close[ds]?)|(resolve[ds]?))( #)\d+/i;
core.info(`Checking PR description formatting\n - "${prDescription}"`);

let prDescriptionRegexp = /((fix(e[ds])?)|(close[ds]?)|(resolve[ds]?))(:? #)\d+/i;
return prDescriptionRegexp.test(prDescription);
}

function compareTitleDescriptionBranchIssue(prBranch, prTitle, prDescription) {
core.info("Extracting issue number from");

let branchIssue = extractBranchIssue(prBranch);
core.info(` - branch name - "${branchIssue}"`);

let titleIssue = extractTitleIssue(prTitle);
core.info(` - PR title - "${titleIssue}"`);

let descriptionIssue = extractDescriptionIssue(prDescription);
core.info(` - PR description - "${descriptionIssue}"`);

return branchIssue === titleIssue && titleIssue === descriptionIssue;
}

function extractBranchIssue(prBranch) {
let prBranchRegexp = /^\d+(?=((-[^\W_]+)+$))/;
let issueNumber = prBranch.match(prBranchRegexp);
return parseInt(issueNumber);
let issueStr = prBranch.match(prBranchRegexp);
let issueNumber = parseInt(issueStr, 10);

return issueNumber;
}

function extractTitleIssue(prTitle) {
let prTitleRegexp = /(((?<=^)\d+)|((?<=^#)\d+))/;
let issueNumber = prTitle.match(prTitleRegexp);
return parseInt(issueNumber, 10);
let issueStr = prTitle.match(prTitleRegexp);
let issueNumber = parseInt(issueStr, 10);

return issueNumber;
}

function extractDescriptionIssue(prDescription) {
let prDescriptionRegexp = /(?<=[Ff]ixes #)\d+/;
let issueNumber = prDescription.match(prDescriptionRegexp);
return parseInt(issueNumber, 10);
}
// Firstly extract "Fixes #issue" phrase
let prDescriptionRegexp = /((fix(e[ds])?)|(close[ds]?)|(resolve[ds]?))(:? #)\d+/i;
let fixesIssueStr = prDescription.match(prDescriptionRegexp);

function compareTitleDescriptionBranchIssue(prBranch, prTitle, prDescription) {
let branchIssue = extractBranchIssue(prBranch);
let titleIssue = extractTitleIssue(prTitle);
let descriptionIssue = extractDescriptionIssue(prDescription);
return branchIssue === titleIssue && titleIssue === descriptionIssue;
// Next extract issue number
let issueNumberRegexp = /\d+/;
let issueStr = fixesIssueStr[0].match(issueNumberRegexp);
let issueNumber = parseInt(issueStr, 10);

return issueNumber;
}

exports.checkPrBranch = checkPrBranch;
exports.checkPrTitle = checkPrTitle;
exports.checkPrDescription = checkPrDescription;
exports.compareTitleDescriptionBranchIssue = compareTitleDescriptionBranchIssue;
exports.extractBranchIssue = extractBranchIssue;
exports.extractTitleIssue = extractTitleIssue;
exports.extractDescriptionIssue = extractDescriptionIssue;
exports.compareTitleDescriptionBranchIssue = compareTitleDescriptionBranchIssue;


/***/ }),
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ async function check() {
if (helpers.checkPrBranch(prBranch) === false) {
core.setFailed("PR branch has wrong name");
return;
} else {
core.info(" - OK");
}

if (helpers.checkPrTitle(prTitle) === false) {
core.setFailed("PR title doesn't start with issue number");
return;
} else {
core.info(" - OK");
}

if (helpers.checkPrDescription(prDescription) == false) {
core.setFailed("PR description doesn't contain 'fixes #issue' phrase");
return;
} else {
core.info(" - OK");
}

if (
Expand All @@ -30,9 +36,11 @@ async function check() {
) === false
) {
core.setFailed(
"PR title and description contain different issue numbers"
"Branch name, PR title and PR description contain different issue numbers"
);
return;
} else {
core.info(" - OK");
}
} catch (error) {
core.error(error);
Expand Down
55 changes: 41 additions & 14 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,74 @@
const core = require("@actions/core");

function checkPrBranch(prBranch) {
core.info(`Checking branch name formatting\n - "${prBranch}"`);

let prBranchRegexp = /^\d+(-[^\W_]+)+$/;
return prBranchRegexp.test(prBranch);
}

function checkPrTitle(prTitle) {
core.info(`Checking PR title formatting\n - "${prTitle}"`);

let prTitleRegexp = /^#?\d+:?\s+.+$/;
return prTitleRegexp.test(prTitle);
}

function checkPrDescription(prDescription) {
core.info(`Checking PR description formatting\n - "${prDescription}"`);

let prDescriptionRegexp = /((fix(e[ds])?)|(close[ds]?)|(resolve[ds]?))(:? #)\d+/i;
return prDescriptionRegexp.test(prDescription);
}

function compareTitleDescriptionBranchIssue(prBranch, prTitle, prDescription) {
core.info("Extracting issue number from");

let branchIssue = extractBranchIssue(prBranch);
core.info(` - branch name - "${branchIssue}"`);

let titleIssue = extractTitleIssue(prTitle);
core.info(` - PR title - "${titleIssue}"`);

let descriptionIssue = extractDescriptionIssue(prDescription);
core.info(` - PR description - "${descriptionIssue}"`);

return branchIssue === titleIssue && titleIssue === descriptionIssue;
}

function extractBranchIssue(prBranch) {
let prBranchRegexp = /^\d+(?=((-[^\W_]+)+$))/;
let issueNumber = prBranch.match(prBranchRegexp);
return parseInt(issueNumber);
let issueStr = prBranch.match(prBranchRegexp);
let issueNumber = parseInt(issueStr, 10);

return issueNumber;
}

function extractTitleIssue(prTitle) {
let prTitleRegexp = /(((?<=^)\d+)|((?<=^#)\d+))/;
let issueNumber = prTitle.match(prTitleRegexp);
return parseInt(issueNumber, 10);
let issueStr = prTitle.match(prTitleRegexp);
let issueNumber = parseInt(issueStr, 10);

return issueNumber;
}

function extractDescriptionIssue(prDescription) {
let prDescriptionRegexp = /(?<=[Ff]ixes #)\d+/;
let issueNumber = prDescription.match(prDescriptionRegexp);
return parseInt(issueNumber, 10);
}
// Firstly extract "Fixes #issue" phrase
let prDescriptionRegexp = /((fix(e[ds])?)|(close[ds]?)|(resolve[ds]?))(:? #)\d+/i;
let fixesIssueStr = prDescription.match(prDescriptionRegexp);

function compareTitleDescriptionBranchIssue(prBranch, prTitle, prDescription) {
let branchIssue = extractBranchIssue(prBranch);
let titleIssue = extractTitleIssue(prTitle);
let descriptionIssue = extractDescriptionIssue(prDescription);
return branchIssue === titleIssue && titleIssue === descriptionIssue;
// Next extract issue number
let issueNumberRegexp = /\d+/;
let issueStr = fixesIssueStr[0].match(issueNumberRegexp);
let issueNumber = parseInt(issueStr, 10);

return issueNumber;
}

exports.checkPrBranch = checkPrBranch;
exports.checkPrTitle = checkPrTitle;
exports.checkPrDescription = checkPrDescription;
exports.compareTitleDescriptionBranchIssue = compareTitleDescriptionBranchIssue;
exports.extractBranchIssue = extractBranchIssue;
exports.extractTitleIssue = extractTitleIssue;
exports.extractDescriptionIssue = extractDescriptionIssue;
exports.compareTitleDescriptionBranchIssue = compareTitleDescriptionBranchIssue;
30 changes: 25 additions & 5 deletions tests/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const core = require("@actions/core");
const helpers = require("../src/helpers");

// Mute core.info
core.info = jest.fn();

describe("checkPrBranch function", () => {
it("checks if PR branch starts with an issue number", () => {
expect(
Expand Down Expand Up @@ -47,10 +51,12 @@ describe("checkPrDescription function", () => {
expect(helpers.checkPrDescription("Closed #123")).toBeTruthy();
expect(helpers.checkPrDescription("Closed: #123")).toBeTruthy();
expect(helpers.checkPrDescription("This PR closes #123")).toBeTruthy();

expect(helpers.checkPrDescription("Fix #123")).toBeTruthy();
expect(helpers.checkPrDescription("fix: #123")).toBeTruthy();
expect(helpers.checkPrDescription("fixed #123")).toBeTruthy();
expect(helpers.checkPrDescription("This PR fixes #123")).toBeTruthy();

expect(helpers.checkPrDescription("Resolve #123")).toBeTruthy();
expect(helpers.checkPrDescription("Resolved #123")).toBeTruthy();
expect(helpers.checkPrDescription("This PR resolves #123")).toBeTruthy();
Expand All @@ -62,13 +68,15 @@ describe("checkPrDescription function", () => {
expect(helpers.checkPrDescription("rEsOlvEs #123")).toBeTruthy();

expect(helpers.checkPrDescription("Closes 123")).toBeFalsy();
expect(helpers.checkPrDescription("fixed 123")).toBeFalsy();
expect(helpers.checkPrDescription("Resolve ##123")).toBeFalsy();
expect(helpers.checkPrDescription("closed123")).toBeFalsy();
expect(helpers.checkPrDescription("Fix123")).toBeFalsy();
expect(helpers.checkPrDescription("resolves#123")).toBeFalsy();
expect(helpers.checkPrDescription("Closed:: #123")).toBeFalsy();

expect(helpers.checkPrDescription("fixed 123")).toBeFalsy();
expect(helpers.checkPrDescription("Fix123")).toBeFalsy();
expect(helpers.checkPrDescription("fix:: #123")).toBeFalsy();

expect(helpers.checkPrDescription("Resolve ##123")).toBeFalsy();
expect(helpers.checkPrDescription("resolves#123")).toBeFalsy();
expect(helpers.checkPrDescription("This PR resolves:: #123")).toBeFalsy();
});
});
Expand All @@ -93,14 +101,26 @@ describe("extractTitleIssue function", () => {
expect(helpers.extractTitleIssue("23 pr title")).toEqual(23);
expect(helpers.extractTitleIssue("#123 pr title")).toEqual(123);
expect(helpers.extractTitleIssue("#12 pr title")).toEqual(12);
expect(helpers.extractTitleIssue("#1234: pr title")).toEqual(1234);
});
});

describe("extractDescriptionIssue function", () => {
it("extracts issue number from PR description", () => {
expect(helpers.extractDescriptionIssue("Fixes #123")).toEqual(123);
expect(helpers.extractDescriptionIssue("Close #123")).toEqual(123);
expect(helpers.extractDescriptionIssue("closes #12")).toEqual(12);
expect(helpers.extractDescriptionIssue("This PR closes #23")).toEqual(23);
expect(helpers.extractDescriptionIssue("Closed: #1234")).toEqual(1234);

expect(helpers.extractDescriptionIssue("Fix #123")).toEqual(123);
expect(helpers.extractDescriptionIssue("fixes #12")).toEqual(12);
expect(helpers.extractDescriptionIssue("This PR fixes #23")).toEqual(23);
expect(helpers.extractDescriptionIssue("Fixed: #1234")).toEqual(1234);

expect(helpers.extractDescriptionIssue("Resolve #123")).toEqual(123);
expect(helpers.extractDescriptionIssue("resolves #12")).toEqual(12);
expect(helpers.extractDescriptionIssue("This PR resolves #23")).toEqual(23);
expect(helpers.extractDescriptionIssue("Resolved: #1234")).toEqual(1234);
});
});

Expand Down