diff --git a/Readme.md b/Readme.md index 3e5cbcd..4e7c800 100644 --- a/Readme.md +++ b/Readme.md @@ -14,6 +14,7 @@ First deployment, create .env.yaml providing access url and token ``` DISCORD_WEBHOOK_URL: __URL__ GITHUB_API_TOKEN: __TOKEN__ +OWNER_NAME: __NAME__ ``` and run following command, diff --git a/index.js b/index.js index dbfdca1..ae3a8e5 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,12 @@ const BLUE = 3447003; const RED = 15158332; const GREEN = 3066993; -const addFeild = (inline = false) => name => value => ({ inline, name, value }); +const addFeild = (inline = false) => + (name) => (value) => ({ inline, name, value }); -module.exports.subscribeDiscord = function(event, context) { +module.exports.subscribeDiscord = function (event, context) { const build = eventToBuild(event.data); - const status = ["WORKING", "SUCCESS", "FAILURE", "INTERNAL_ERROR", "TIMEOUT"]; + const status = ["WORKING", "SUCCESS", "FAILURE", "INTERNAL_ERROR", "TIMEOUT", "CANCELLED"]; if (!status.includes(build.status)) return; @@ -17,12 +18,12 @@ module.exports.subscribeDiscord = function(event, context) { const branch = substitutions.BRANCH_NAME; const repo = substitutions.REPO_NAME; const commit = substitutions.COMMIT_SHA; - const commitUrl = `https://github.com/radiuszon/${repo}/commit/${commit}`; + const owner = process.env.OWNER_NAME; const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL; const inlineField = addFeild(true); - fetchGitMessage(commit).then(result => { + fetchGitMessage({ commit, repo, owner }).then((result) => { axios.post(DISCORD_WEBHOOK_URL, { embeds: [ { @@ -31,22 +32,22 @@ module.exports.subscribeDiscord = function(event, context) { inlineField("Repo")(repo), inlineField("Branch")(branch), inlineField("Status")(buildStatus), - addFeild()("Commit")(getMessage(result)) - ] - } - ] + addFeild()("Commit")(getMessage(result)), + ], + }, + ], }); }); }; -const eventToBuild = data => { +const eventToBuild = (data) => { return JSON.parse(Buffer.from(data, "base64").toString()); }; -const getColor = statusIndex => - ({ SUCCESS: GREEN, WORKING: BLUE }[statusIndex] || RED); +const getColor = + (statusIndex) => ({ SUCCESS: GREEN, WORKING: BLUE }[statusIndex] || RED); -const getMessage = result => { +const getMessage = (result) => { try { return result.data.data.repository.object.message; } catch (error) { @@ -54,23 +55,23 @@ const getMessage = result => { } }; -const fetchGitMessage = commitSha => +const fetchGitMessage = ({ commit, repo, owner }) => axios.post( "https://api.github.com/graphql", { query: `{ - repository(owner:"radiuszon",name:"nimble-web") { - object(oid: "${commitSha}") { + repository(owner:"${owner}",name:"${repo}") { + object(oid: "${commit}") { ... on Commit { message } } } - }` + }`, }, { headers: { - Authorization: `bearer ${process.env.GITHUB_API_TOKEN}` - } - } + Authorization: `bearer ${process.env.GITHUB_API_TOKEN}`, + }, + }, ); diff --git a/package.json b/package.json index 44c0bed..9888c8f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "deploy": "gcloud functions deploy subscribeDiscord" + "deploy": "gcloud functions deploy subscribeDiscord --env-vars-file .env.yaml" }, "author": "", "license": "ISC",