From 7fe924d614bcbaa0ad7a5bc6aa39111d8dd9153b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Ram=C3=A9?= <8195958+sneko@users.noreply.github.com> Date: Tue, 24 Mar 2020 13:32:52 +0100 Subject: [PATCH] Fix issue when no GitHub token is specified --- index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index e2a2f35..9f2b0a7 100644 --- a/index.js +++ b/index.js @@ -31,10 +31,7 @@ module.exports.getGithubCommit = async (build, octokit) => { // subscribe is the main function called by GCF. module.exports.subscribe = async (event) => { try { - const token = process.env.GITHUB_TOKEN; - const octokit = token && new Octokit({ - auth: `token ${token}`, - }); + // Format data const build = module.exports.eventToBuild(event.data); // Skip if the current status is not in the status list. @@ -43,9 +40,20 @@ module.exports.subscribe = async (event) => { return; } - const githubCommit = await module.exports.getGithubCommit(build, octokit); + // If specified get information from GitHub + const token = process.env.GITHUB_TOKEN; + let githubCommit = null; + if (token) { + const octokit = token && new Octokit({ + auth: `token ${token}`, + }); + githubCommit = await module.exports.getGithubCommit(build, octokit); + } + + // Format final message const message = await module.exports.createSlackMessage(build, githubCommit); + // Send message to slack. module.exports.webhook.send(message); } catch (err) {