Repository: eslint/eslint-github-bot
Pull request #55 (2018-01-02 05:50:10)

Chore: Extracting commit message/PR title into utils module
Let me know in case of any questions.

gyandeeps: Maybe we should move the util.js file to be inside src/plugin folder since these utils are specific to plugins.
platinumazure: @gyandeeps I could move the file if you like, but I didn't want people to think the utils itself might be a plugin. (We can do `require("plugins/commitMessage")` due to how CommonJS `require`works in Node. By moving the utils, we can also do `require("plugins/utils")`, which feels misleading to me.)
gyandeeps: All the plugins are exposed using the index file inside the plugins folder and none of them should be consumed directly. Again, I am not opposed ot keeping the way you have it, i juts thought that util.js which has utils for plugins would make more sense to be inside plugins folder.
platinumazure: @gyandeeps Fair enough. Given the index file, I'll go ahead and make all suggested changes tonight.
platinumazure: @gyandeeps This should now be ready to merge, please take a look. Thanks!

Patch:
diff --git a/src/plugins/commit-message/index.js b/src/plugins/commit-message/index.js
index daa6574..ba027f5 100644
--- a/src/plugins/commit-message/index.js
+++ b/src/plugins/commit-message/index.js
@@ -6,6 +6,8 @@
 
 "use strict";
 
+const { getCommitMessageForPR } = require("../utils");
+
 const TAG_REGEX = /^(?:Breaking|Build|Chore|Docs|Fix|New|Update|Upgrade):/;
 const MESSAGE_LENGTH_LIMIT = 72;
 
@@ -45,9 +47,7 @@ async function processCommitMessage(context) {
     }
 
     const allCommits = await github.pullRequests.getCommits(context.issue());
-    const messageToCheck = allCommits.data.length === 1
-        ? allCommits.data[0].commit.message
-        : payload.pull_request.title;
+    const messageToCheck = getCommitMessageForPR(allCommits.data, payload.pull_request);
     const isValid = checkCommitMessage(messageToCheck);
     let description;
     let state;
diff --git a/src/plugins/release-monitor/index.js b/src/plugins/release-monitor/index.js
index 35f016f..7afb81b 100644
--- a/src/plugins/release-monitor/index.js
+++ b/src/plugins/release-monitor/index.js
@@ -5,6 +5,8 @@
 
 "use strict";
 
+const { getCommitMessageForPR } = require("../utils");
+
 const PATCH_COMMIT_MESSAGE_REGEX = /^(?:Build|Chore|Docs|Fix):/;
 const POST_RELEASE_LABEL = "patch release pending";
 const RELEASE_LABEL = "release";
@@ -19,19 +21,6 @@ function isMessageValidForPatchRelease(message) {
     return PATCH_COMMIT_MESSAGE_REGEX.test(message);
 }
 
-/**
- * Get the correct commit message for a PR
- * @param {Object[]} allCommits A list of commit objects from GitHub's API
- * @param {Object} pr - Pull request object
- * @returns {string} Commit message
- * @private
- */
-function getCommitMessageForPR(allCommits, pr) {
-    return allCommits.length === 1
-        ? allCommits[0].commit.message
-        : pr.title;
-}
-
 /**
  * Get the sha value from the latest commit
  * @param {Object[]} allCommits A list of commit objects from GitHub's API
diff --git a/src/plugins/utils.js b/src/plugins/utils.js
new file mode 100644
index 0000000..d40e177
--- /dev/null
+++ b/src/plugins/utils.js
@@ -0,0 +1,22 @@
+/**
+ * @fileoverview Utilities shared across multiple plugins.
+ * @author Kevin Partington
+ */
+
+"use strict";
+
+/**
+ * Get the correct commit message for a PR
+ * @param {Object[]} allCommits A list of commit objects from GitHub's API
+ * @param {Object} pr - Pull request object
+ * @returns {string} Commit message
+ */
+function getCommitMessageForPR(allCommits, pr) {
+    return allCommits.length === 1
+        ? allCommits[0].commit.message
+        : pr.title;
+}
+
+module.exports = {
+    getCommitMessageForPR
+};


Test patch:
diff --git a/tests/plugins/utils.js b/tests/plugins/utils.js
new file mode 100644
index 0000000..44996ad
--- /dev/null
+++ b/tests/plugins/utils.js
@@ -0,0 +1,45 @@
+"use strict";
+
+const utils = require("../../src/plugins/utils");
+
+const COMMIT_MESSAGE = "Commit message";
+const PULL_REQUEST_TITLE = "PR title";
+
+const SINGLE_COMMIT = [
+    {
+        commit: {
+            message: COMMIT_MESSAGE
+        }
+    }
+];
+
+const MULTIPLE_COMMITS = [
+    {
+        commit: {
+            message: COMMIT_MESSAGE
+        }
+    },
+    {
+        commit: {
+            message: COMMIT_MESSAGE
+        }
+    }
+];
+
+const PULL_REQUEST = {
+    title: PULL_REQUEST_TITLE
+};
+
+describe("utils", () => {
+    describe("getCommitMessageForPR", () => {
+        test("Should return first commit summary if PR has one commit", () => {
+            expect(utils.getCommitMessageForPR(SINGLE_COMMIT, PULL_REQUEST))
+                .toBe(COMMIT_MESSAGE);
+        });
+
+        test("Should return PR title if PR has multiple commits", () => {
+            expect(utils.getCommitMessageForPR(MULTIPLE_COMMITS, PULL_REQUEST))
+                .toBe(PULL_REQUEST_TITLE);
+        });
+    });
+});