From 6906f3505aefbb4d373c1e5816dfcadff1aadaf4 Mon Sep 17 00:00:00 2001 From: Yotam Cohen Date: Sun, 22 Mar 2020 11:20:58 +0100 Subject: [PATCH 1/3] Add Support for on-premise GitHub deployments --- README.md | 3 +++ src/github-status.js | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 24c41a9..ea3ab52 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ The following environment variables are expected. If a `.env` file is present, # Hostname of your Jira instance. JIRA_URL=unicode-org.atlassian.net +# Hostname of your GitHub instance +GITHUB_URL=https://github.mycompany.com/api/v3 + # Authentication for your Jira instance. # If not present, authentication for Jira will not be used. # diff --git a/src/github-status.js b/src/github-status.js index f0de679..fbd28a9 100644 --- a/src/github-status.js +++ b/src/github-status.js @@ -15,8 +15,17 @@ async function getAuthenticatedOctokitClient(token) { throw new Error("Need either GITHUB_TOKEN or GITHUB_APP_ID"); } } + + let github_url; + if (process.env.GITHUB_URL) { + github_url = process.env.GITHUB_URL; + } else { + github_url = "https://api.github.com"; + } + return new Octokit({ - auth: token + auth: token, + baseUrl: github_url }); } From eec9014e30998c27b90ac92283746cd9aab8ef4f Mon Sep 17 00:00:00 2001 From: Yotam Cohen Date: Sun, 22 Mar 2020 21:45:03 +0100 Subject: [PATCH 2/3] Allow validated issue status to be set in configuration --- README.md | 9 ++++++--- app.js | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ea3ab52..6d5b3dc 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,6 @@ The following environment variables are expected. If a `.env` file is present, # Hostname of your Jira instance. JIRA_URL=unicode-org.atlassian.net -# Hostname of your GitHub instance -GITHUB_URL=https://github.mycompany.com/api/v3 - # Authentication for your Jira instance. # If not present, authentication for Jira will not be used. # @@ -44,6 +41,9 @@ GITHUB_URL=https://github.mycompany.com/api/v3 JIRA_USERNAME=foo@example.com JIRA_PASSWORD=bar +# List of issue statuses that would be accepted +JIRA_APPROVED_STATUSES="Approved, Progress" + # URL prefix used for hyperlinks. URL_PREFIX=http://localhost:3000 @@ -65,6 +65,9 @@ COMMITTER_EMAIL=foo@example.com # Secret for the cookie session, used to store the user's GitHub access token in a cookie. COOKIE_SECRET=xxxxxxxxxx +# Hostname of your GitHub instance +GITHUB_URL=https://github.mycompany.com/api/v3 + # GitHub permissions: # Option 1: Personal Access Token; easiest and useful for testing. diff --git a/app.js b/app.js index 3512930..ad817e8 100644 --- a/app.js +++ b/app.js @@ -74,11 +74,11 @@ async function getJiraInfo(pullRequest) { const isMaintMerge = (pullRequest.base.ref === "master" && pullRequest.head.ref.match(/^maint\//) && pullRequest.base.repo.full_name == pullRequest.head.repo.full_name); + let jiraApprovedStatuses = process.env.JIRA_APPROVED_STATUSES || "Accepted, Reviewing, Review Feedback" + const jiraApprovedStatusesArray = jiraApprovedStatuses.split(",").map(status => status.trim()) // Check Jira ticket for validity - if (jiraStatus !== "Accepted" && - jiraStatus !== "Reviewing" && - jiraStatus !== "Review Feedback") { + if (!jiraApprovedStatuses.includes(jiraStatus)) { return { issueKey, jiraStatus, @@ -121,7 +121,7 @@ async function getJiraInfo(pullRequest) { } } - // Since we can't easilly check more than 100 commits, reject PRs with more than 100 commits + // Since we can't easily check more than 100 commits, reject PRs with more than 100 commits if (commits.length === 100) { return { issueKey, From f3067dee2ce0b97cde11071590568bd349dd8a8a Mon Sep 17 00:00:00 2001 From: Yotam Cohen Date: Tue, 24 Mar 2020 18:51:18 +0100 Subject: [PATCH 3/3] Add env vars to enable/disable checks --- README.md | 8 ++++++++ app.js | 61 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 6d5b3dc..17f14d9 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,16 @@ JIRA_USERNAME=foo@example.com JIRA_PASSWORD=bar # List of issue statuses that would be accepted +JIRA_STATUS_CHECK=TRUE JIRA_APPROVED_STATUSES="Approved, Progress" +# Search for the Jira Issue ID in commit message +SEARCH_JIRA_ISSUE_IN_COMMIT=TRUE + +# Optional variable to allow multiple commits in the same PR +ALLOW_MANY_COMMITS=TRUE + + # URL prefix used for hyperlinks. URL_PREFIX=http://localhost:3000 diff --git a/app.js b/app.js index ad817e8..5073cc5 100644 --- a/app.js +++ b/app.js @@ -78,7 +78,7 @@ async function getJiraInfo(pullRequest) { const jiraApprovedStatusesArray = jiraApprovedStatuses.split(",").map(status => status.trim()) // Check Jira ticket for validity - if (!jiraApprovedStatuses.includes(jiraStatus)) { + if (!jiraApprovedStatuses.includes(jiraStatus) && process.env.JIRA_STATUS_CHECK === "TRUE") { return { issueKey, jiraStatus, @@ -93,34 +93,37 @@ async function getJiraInfo(pullRequest) { } // Check for consistency with the commit messages - for (const commitInfo of commits) { - const commitIssueKey = parseMessage(commitInfo.commit.message); - if (commitIssueKey === null) { - return { - issueKey, - jiraStatus, - numCommits, - isMaintMerge, - prFlags, - pass: false, - description: "Commit message for " + commitInfo.sha.substr(0, 7) + " fails validation", - badCommit: commitInfo - }; - } else if (commitIssueKey !== issueKey && !prFlags["DISABLE_JIRA_ISSUE_MATCH"] && !isMaintMerge) { - return { - issueKey, - jiraStatus, - numCommits, - isMaintMerge, - prFlags, - pass: false, - description: "Commit " + commitInfo.sha.substr(0, 7) + " is for " + commitIssueKey + ", but the PR is for " + issueKey, - extendedDescription: "Please fix your commit message to have the same ticket number as the pull request. If the inconsistency is intentional, you can disable this warning with DISABLE_JIRA_ISSUE_MATCH=true in the PR description.", - badCommit: commitInfo - }; + if(process.env.SEARCH_JIRA_ISSUE_IN_COMMIT === "TRUE") { + for (const commitInfo of commits) { + const commitIssueKey = parseMessage(commitInfo.commit.message); + if (commitIssueKey === null) { + return { + issueKey, + jiraStatus, + numCommits, + isMaintMerge, + prFlags, + pass: false, + description: "Commit message for " + commitInfo.sha.substr(0, 7) + " fails validation", + badCommit: commitInfo + }; + } else if (commitIssueKey !== issueKey && !prFlags["DISABLE_JIRA_ISSUE_MATCH"] && !isMaintMerge) { + return { + issueKey, + jiraStatus, + numCommits, + isMaintMerge, + prFlags, + pass: false, + description: "Commit " + commitInfo.sha.substr(0, 7) + " is for " + commitIssueKey + ", but the PR is for " + issueKey, + extendedDescription: "Please fix your commit message to have the same ticket number as the pull request. If the inconsistency is intentional, you can disable this warning with DISABLE_JIRA_ISSUE_MATCH=true in the PR description.", + badCommit: commitInfo + }; + } } } + // Since we can't easily check more than 100 commits, reject PRs with more than 100 commits if (commits.length === 100) { return { @@ -222,8 +225,12 @@ async function touch(pullRequest, jiraInfo) { const multiCommitMessage = (jiraInfo.numCommits === 0) ? "No commits found on PR" : (jiraInfo.numCommits === 1) ? "This PR includes exactly 1 commit!" : "This PR has " + jiraInfo.numCommits + " commits" + (multiCommitPass ? "" : "; consider squashing."); const promises = [ github.createStatus("jira-ticket", pullRequest, jiraInfo.pass, url, jiraInfo.description), - github.createStatus("single-commit", pullRequest, multiCommitPass, undefined, multiCommitMessage) ]; + + if (!(process.env.ALLOW_MANY_COMMITS === "TRUE")) { + promises.push(github.createStatus("single-commit", pullRequest, multiCommitPass, undefined, multiCommitMessage)) + } + if (jiraInfo.isMaintMerge) { promises.push(github.createStatus("maint-merge", pullRequest, false, undefined, "Reminder: use a MERGE COMMIT and new ticket in the message.")); }