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) {