Skip to content

Commit

Permalink
Author link, inactive events removal & templates
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Nov 3, 2023
1 parent 780b973 commit dbac969
Show file tree
Hide file tree
Showing 70 changed files with 685 additions and 503 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This repository contains the backend code for the DisGitHook project.

It contains the API for the website and the code for handling webhooks.

[![Discord](https://discord.com/api/v10/guilds/1168642348851396658/widget.png?style=banner4)](https://discord.gg/ADP7rn6Hz8)

## API routes

- `GET` /
Expand All @@ -30,3 +32,6 @@ Exceptions:
- `registry_package` event has no templates in favor of the `package` event as recommended by GitHub
- `repository_vulnerability_alert` because it is deprecated in favor of `dependabot_alert` as recommended by GitHub
- `team_add` because the `added_to_repository` action from `team` is the same
- `repository_dispatch` because it is not used for webhooks
- `requested_action` and `rerequested` actions of the `check_run` event because they are not used for webhooks
- `requested` and `rerequested` actions of the `check_suite` event because they are not used for webhooks
40 changes: 27 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ app.use((req, res, next) => {
next()
})

app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "https://disgithook.tomatenkuchen.com")
res.setHeader("Access-Control-Allow-Methods", "OPTIONS,GET,POST,DELETE")
res.setHeader("Access-Control-Allow-Headers", "Content-Type")
res.setHeader("Access-Control-Allow-Credentials", "true")
next()
})

app.listen(port)

// - Dashboard -
Expand All @@ -64,21 +72,26 @@ app.get("/servers", async (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send("Missing auth cookie")

const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
console.log(servers)
if (!servers || !servers.some) return res.status(401).send({success: false, error: "Invalid auth cookie"})

const filtered = servers.map(server => ({
id: server.id,
name: server.name,
icon: server.icon
}))
icon: server.icon,
active: bot.guilds.cache.has(server.id)
})).sort((a, b) => {
if (a.active && b.active) return 0
if (!a.active && b.active) return 1
return -1
})
res.send({servers: filtered})
})

app.get("/servers/:id/hooks", async (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send("Missing auth cookie")

const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers || !servers.some) return res.status(401).send({success: false, error: "Invalid auth cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

const [rows] = await pool.query("SELECT * FROM `hook` WHERE `server` = ?", [req.params.id])
Expand All @@ -104,7 +117,7 @@ app.post("/servers/:id/hooks", async (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers) return res.status(401).send({success: false, error: "Invalid auth cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

let id = oauth.generateToken(8)
Expand All @@ -117,7 +130,7 @@ app.post("/servers/:id/hooks", async (req, res) => {
const secret = oauth.generateToken()
await pool.query(
"INSERT INTO `hook` (`id`, `name`, `server`, `webhook`, `channel`, `message`, `secret`, `filterEvent`, `filterAction`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[id, req.body.name, req.params.id, req.body.webhook, req.body.channel, req.body.message, secret, JSON.stringify(req.body.filterEvent), JSON.stringify(req.body.filterAction)]
[id, req.body.name, req.params.id, req.body.webhook || null, req.body.channel || null, req.body.message || null, secret, JSON.stringify(req.body.filterEvent), JSON.stringify(req.body.filterAction)]
)
res.send({success: true, id, secret})
})
Expand All @@ -126,7 +139,7 @@ app.post("/servers/:id/hooks/:hook", async (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers) return res.status(401).send({success: false, error: "Invalid auth cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ?", [req.params.hook])
Expand All @@ -136,8 +149,8 @@ app.post("/servers/:id/hooks/:hook", async (req, res) => {
if (hook.server != req.params.id) return res.status(401).send({success: false, error: "Invalid server ID"})

await pool.query(
"UPDATE `hook` SET `webhook` = ?, `name` = ?, `channel` = ?, `message` = ?, `filterEvent` = ?, `filterAction` = ? WHERE `id` = ?",
[req.body.webhook, req.body.name, req.body.channel, req.body.message, JSON.stringify(req.body.filterEvent), JSON.stringify(req.body.filterAction), req.params.hook]
"UPDATE `hook` SET `name` = ?, `webhook` = ?, `channel` = ?, `message` = ?, `filterEvent` = ?, `filterAction` = ? WHERE `id` = ?",
[req.body.name, req.body.webhook || null, req.body.channel || null, req.body.message || null, JSON.stringify(req.body.filterEvent), JSON.stringify(req.body.filterAction), req.params.hook]
)
res.send({success: true})
})
Expand All @@ -146,7 +159,7 @@ app.delete("/servers/:id/hooks/:hook", async (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const [rows] = await pool.query("SELECT * FROM `user` WHERE `token` = ?", [req.signedCookies.auth])
if (rows.length == 0) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (rows.length == 0) return res.status(401).send({success: false, error: "Invalid auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})
Expand All @@ -165,7 +178,7 @@ app.post("/servers/:id/hooks/:hook/regen", async (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send({success: false, error: "Missing auth cookie"})

const servers = await oauth.getUserServers(req.signedCookies.auth, pool)
if (!servers) return res.status(401).send({success: false, error: "Invalid token cookie"})
if (!servers) return res.status(401).send({success: false, error: "Invalid auth cookie"})
if (!servers.some(server => server.id == req.params.id)) return res.status(401).send({success: false, error: "Invalid server ID"})

const [rows] = await pool.query("SELECT * FROM `hook` WHERE `id` = ?", [req.params.hook])
Expand Down Expand Up @@ -215,19 +228,20 @@ app.get("/login", async (req, res) => {

const token = oauth.generateToken()
res.cookie("auth", token, {signed: true, secure: true, httpOnly: true, expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 4), domain: "." + domain.split(".").slice(-2).join(".")})
res.cookie("avatar", "https://cdn.discordapp.com/avatars/" + user.id + "/" + user.avatar + ".png", {secure: true, expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 4), domain: "." + domain.split(".").slice(-2).join(".")})
res.cookie("avatar", "https://cdn.discordapp.com/avatars/" + user.id + "/" + user.avatar + ".webp?size=64", {secure: true, expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 4), domain: "." + domain.split(".").slice(-2).join(".")})

pool.query(
"INSERT INTO `user` (`id`, `token`, `access`, `refresh`, `expires`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `token` = ?, `access` = ?, `refresh` = ?, `expires` = ?",
[user.id, token, json.access_token, json.refresh_token, Date.now() + json.expires_in * 1000, token, json.access_token, json.refresh_token, Date.now() + json.expires_in * 1000]
)

res.send({token})
res.send({token, avatar: "https://cdn.discordapp.com/avatars/" + user.id + "/" + user.avatar + ".webp?size=64"})
})
app.get("/logout", (req, res) => {
if (!req.signedCookies.auth) return res.status(401).send("Missing auth cookie")

res.clearCookie("auth", {domain: "." + domain.split(".").slice(-2).join(".")})
res.clearCookie("avatar", {domain: "." + domain.split(".").slice(-2).join(".")})
res.send({success: true})

pool.query("DELETE FROM `user` WHERE `token` = ?", [req.signedCookies.auth])
Expand Down
10 changes: 6 additions & 4 deletions templates/branch_protection_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `branch_protection_configuration` (`disabled`)",
title: "[{{ repository.name }}] `branch_protection_configuration` (`disabled`)",
url: "{{ repository.html_url }}",
color: color("black")
}]
Expand All @@ -17,9 +18,10 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `branch_protection_configuration` (`enabled`)",
title: "[{{ repository.name }}] `branch_protection_configuration` (`enabled`)",
url: "{{ repository.html_url }}",
color: color("black")
}]
Expand Down
15 changes: 9 additions & 6 deletions templates/branch_protection_rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `branch_protection_rule` (`created`)",
title: "[{{ repository.name }}] `branch_protection_rule` (`created`)",
url: "{{ repository.html_url }}",
color: color("green")
}]
Expand All @@ -17,9 +18,10 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `branch_protection_rule` (`edited`)",
title: "[{{ repository.name }}] `branch_protection_rule` (`edited`)",
url: "{{ repository.html_url }}",
color: color("cyan")
}]
Expand All @@ -28,9 +30,10 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `branch_protection_rule` (`deleted`)",
title: "[{{ repository.name }}] `branch_protection_rule` (`deleted`)",
url: "{{ repository.html_url }}",
color: color("red")
}]
Expand Down
38 changes: 9 additions & 29 deletions templates/check_run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,24 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_run` (`completed`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ repository.name }}] **{{ check_run.name }}** check run completed {{ check_run.conclusion }}",
url: "{{ check_run.html_url }}",
color: color("magenta")
}]
},{
action: "created",
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_run` (`created`)",
url: "{{ repository.html_url }}",
title: "[{{ repository.name }}] **{{ check_run.name }}** check run {{ check_run.status }}",
url: "{{ check_run.html_url }}",
color: color("green")
}]
},{
action: "requested_action",
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_run` (`requested_action`)",
url: "{{ repository.html_url }}",
color: color("black")
}]
},{
action: "rerequested",
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_run` (`rerequested`)",
url: "{{ repository.html_url }}",
color: color("black")
}]
}
]
29 changes: 4 additions & 25 deletions templates/check_suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,12 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_suite` (`completed`)",
title: "[{{ repository.name }}] **{{ check_suite.latest_check_runs_count }}** check runs completed {{ check_suite.conclusion }}",
url: "{{ repository.html_url }}",
color: color("black")
}]
},{
action: "requested",
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_suite` (`requested`)",
url: "{{ repository.html_url }}",
color: color("black")
}]
},{
action: "rerequested",
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `check_suite` (`rerequested`)",
url: "{{ repository.html_url }}",
color: color("black")
color: color("magenta")
}]
}
]
18 changes: 12 additions & 6 deletions templates/code_scanning_alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** appeared in {{ ref }}",
url: "{{ alert.html_url }}",
Expand All @@ -17,7 +18,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** closed as {{ alert.dismissed_reason }}",
url: "{{ alert.html_url }}",
Expand All @@ -28,7 +30,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** created",
url: "{{ alert.html_url }}",
Expand All @@ -39,7 +42,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** fixed",
url: "{{ alert.html_url }}",
Expand All @@ -50,7 +54,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** reopened",
url: "{{ alert.html_url }}",
Expand All @@ -61,7 +66,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** reopened",
url: "{{ alert.html_url }}",
Expand Down
7 changes: 4 additions & 3 deletions templates/commit_comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `commit_comment` (`created`)",
url: "{{ repository.html_url }}",
title: "[{{ repository.name }}] New comment on `{{ comment.commit_id }}`",
url: "{{ comment.html_url }}",
color: color("green")
}]
}
Expand Down
3 changes: 2 additions & 1 deletion templates/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ repository.name }}] Git {{ ref_type }} created",
url: "{{ repository.html_url }}",
Expand Down
Loading

0 comments on commit dbac969

Please sign in to comment.