Skip to content

Commit

Permalink
♻️🤫hide organize and repo name
Browse files Browse the repository at this point in the history
  • Loading branch information
itchakhun committed Jun 25, 2020
1 parent 02db3ec commit f6030e3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 21 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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: [
{
Expand All @@ -31,46 +32,46 @@ 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) {
return error;
}
};

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}`,
},
},
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f6030e3

Please sign in to comment.