From 04805706a738ebf49e61a2efc9b4877297437675 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Thu, 7 May 2020 09:00:12 -0700 Subject: [PATCH 1/4] path.join is not needed --- lib/server.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index 12f8832..6ec3de9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,6 +1,5 @@ // External dependencies const fs = require("fs") -const path = require("path") const yaml = require("js-yaml") const express = require("express") const { createWebhooksApi } = require("@octokit/webhooks") @@ -8,10 +7,10 @@ const { createAppAuth } = require("@octokit/auth-app") const { graphql } = require("@octokit/graphql") // Local dependencies -const smeeClient = require(path.join(__dirname, "smee.js")) -const emojify = require(path.join(__dirname, "emojify.js")) -const hasCommand = require(path.join(__dirname, "command.js")) -const updateBodyMutationFor = require(path.join(__dirname, "mutations.js")) +const smeeClient = require("./smee") +const emojify = require("./emojify") +const hasCommand = require("./command") +const updateBodyMutationFor = require("./mutations") // Setup const port = 64897 From aaabb40e02ced799c1d2e8b5c3472a799c94b71a Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Thu, 7 May 2020 09:01:07 -0700 Subject: [PATCH 2/4] you can require .json files, no need for fs.readFile* --- lib/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index 6ec3de9..5f8e78c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -16,7 +16,7 @@ const updateBodyMutationFor = require("./mutations") const port = 64897 const app = express() const privateKey = fs.readFileSync("gh-app.pem", "utf8") -const config = JSON.parse(fs.readFileSync("config.json", "utf8")) +const config = require('../config.json') var emojifierConfig = yaml.safeLoad(fs.readFileSync("emojifier-default.yml", "utf8")) const smee = smeeClient(config.webproxy_url, port) From 6a0a85cb8c193dd848016ddcbcc2ef8f7f228509 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Thu, 7 May 2020 09:01:45 -0700 Subject: [PATCH 3/4] createAppAuth is a sync method, see https://github.com/octokit/auth-app.js/\#usage --- lib/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index 5f8e78c..989662c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -29,7 +29,7 @@ app.use(webhooks.middleware) webhooks.on(["issue_comment.created", "issues.opened", "pull_request.opened"], async (event) => { const { payload } = event - const auth = await createAppAuth({ + const auth = createAppAuth({ id: config.github_app_id, privateKey: privateKey, installationId: payload.installation.id From 82bf8ba0c7e1c0419915c72f6f3e453952835daa Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Thu, 7 May 2020 09:03:34 -0700 Subject: [PATCH 4/4] get installation access token directly and set default authorization header, instead of using hook API. I think that code aligns better with the docs on *.github.com --- lib/server.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index 989662c..65a5398 100644 --- a/lib/server.js +++ b/lib/server.js @@ -35,9 +35,10 @@ webhooks.on(["issue_comment.created", "issues.opened", "pull_request.opened"], a installationId: payload.installation.id }) + const { token } = await auth({ type: "installation" }); const graphqlWithAuth = graphql.defaults({ - request: { - hook: auth.hook + headers: { + authorization: `token ${token}` } })