Repository: JasonEtco/todo
Pull request #231 (2019-08-01 06:21:16)

feat: github-enterprise
Support Github Enterprise.

- Read in `GHE_HOST` environment variable per https://probot.github.io/docs/configuration/
- Use Github Enterprise host in templates
- Fix `sha` / `commit_sha` reference
- ~~Update octokit and probot versions~~


Patch:
diff --git a/lib/templates/comment.js b/lib/templates/comment.js
index 18017e7..7092ecd 100644
--- a/lib/templates/comment.js
+++ b/lib/templates/comment.js
@@ -7,7 +7,7 @@ module.exports = `## {{ title }}
 
 {{/if}}
 {{#if range}}
-https://github.com/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}#{{ range }}
+https://{{ githubHost }}/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}#{{ range }}
 
 ---
 
diff --git a/lib/templates/index.js b/lib/templates/index.js
index 2cd588a..7f733d6 100644
--- a/lib/templates/index.js
+++ b/lib/templates/index.js
@@ -1,5 +1,8 @@
 const { handlebars } = require('hbs')
 
+// Register a githubHost global helper to make links respect the GHE_HOST env var
+handlebars.registerHelper('githubHost', () => process.env.GHE_HOST || 'github.com')
+
 const compile = filename =>
   handlebars.compile(require(`./${filename}`))
 
diff --git a/lib/templates/issue.js b/lib/templates/issue.js
index 78abaf1..b49d018 100644
--- a/lib/templates/issue.js
+++ b/lib/templates/issue.js
@@ -5,7 +5,7 @@ module.exports = `{{#if body}}
 
 {{/if}}
 {{#if range}}
-https://github.com/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}#{{ range }}
+https://{{ githubHost }}/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}#{{ range }}
 
 ---
 
diff --git a/lib/templates/issueFromMerge.js b/lib/templates/issueFromMerge.js
index c5f25f2..2c795bd 100644
--- a/lib/templates/issueFromMerge.js
+++ b/lib/templates/issueFromMerge.js
@@ -5,7 +5,7 @@ module.exports = `{{#if body}}
 
 {{/if}}
 {{#if range}}
-https://github.com/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}#{{ range }}
+https://{{ githubHost }}/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}#{{ range }}
 
 ---
 
diff --git a/lib/templates/reopenClosed.js b/lib/templates/reopenClosed.js
index 312e033..3ba8e57 100644
--- a/lib/templates/reopenClosed.js
+++ b/lib/templates/reopenClosed.js
@@ -1,4 +1,4 @@
-module.exports = `This issue has been reopened because the **\`{{ keyword }}\`** comment still exists in [**{{ filename }}**](https://github.com/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}), as of {{ sha }}.
+module.exports = `This issue has been reopened because the **\`{{ keyword }}\`** comment still exists in [**{{ filename }}**](https://{{ githubHost }}/{{ owner }}/{{ repo }}/blob/{{ sha }}/{{ filename }}), as of {{ sha }}.
 
 ---
 
diff --git a/lib/utils/get-diff.js b/lib/utils/get-diff.js
index a96204d..f2f708a 100644
--- a/lib/utils/get-diff.js
+++ b/lib/utils/get-diff.js
@@ -14,7 +14,7 @@ async function getCommit (context, method = 'GET') {
   if (context.event === 'push') {
     return context.github.repos.getCommit(context.repo({
       method,
-      sha: context.payload.head_commit.id,
+      commit_sha: context.payload.head_commit.id,
       headers: { Accept: 'application/vnd.github.diff' }
     }))
   } else {


Test patch:
diff --git a/tests/__snapshots__/push-handler.test.js.snap b/tests/__snapshots__/push-handler.test.js.snap
index c903689..67daded 100644
--- a/tests/__snapshots__/push-handler.test.js.snap
+++ b/tests/__snapshots__/push-handler.test.js.snap
@@ -61,6 +61,25 @@ Array [
 ]
 `;
 
+exports[`push-handler creates an issue and respects GHE_HOST 1`] = `
+Array [
+  Object {
+    "assignee": "JasonEtco",
+    "body": "https://fakegittillyoumakegit.com/JasonEtco/tests/blob/e06c237a0c041f5a0a61f1c361f7a1d6f3d669af/.travis.yml#L10-L15
+
+---
+
+###### This issue was generated by [todo](https://todo.jasonet.co) based on a \`TODO\` comment in e06c237a0c041f5a0a61f1c361f7a1d6f3d669af. It&#x27;s been assigned to @JasonEtco because they committed the code.",
+    "labels": Array [
+      "todo :spiral_notepad:",
+    ],
+    "owner": "JasonEtco",
+    "repo": "tests",
+    "title": "I am an example title",
+  },
+]
+`;
+
 exports[`push-handler creates an issue with a body line 1`] = `
 Array [
   Object {
diff --git a/tests/push-handler.test.js b/tests/push-handler.test.js
index e6c5268..b16064e 100644
--- a/tests/push-handler.test.js
+++ b/tests/push-handler.test.js
@@ -161,4 +161,12 @@ describe('push-handler', () => {
     await app.receive(event)
     expect(github.issues.create).not.toHaveBeenCalled()
   })
+
+  it('creates an issue and respects GHE_HOST', async () => {
+    process.env.GHE_HOST = 'fakegittillyoumakegit.com'
+    await app.receive(event)
+    expect(github.issues.create).toHaveBeenCalledTimes(1)
+    expect(github.issues.create.mock.calls[0]).toMatchSnapshot()
+    delete process.env.GHE_HOST
+  })
 })