Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
feat(sentry-api): various changes
Browse files Browse the repository at this point in the history
- Shorten `POST` code
- Add `DELETE` endpoint
- Host images statically
- Move util functions to `sentry-api` folder
  • Loading branch information
wdhdev committed Jul 5, 2023
1 parent 28af84d commit 4d0a4a4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 44 deletions.
9 changes: 9 additions & 0 deletions src/sentry-api/endpoints/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const schema = require("../../models/sentrySchema");

module.exports = async (req, res) => {
if(!await schema.exists({ _id: req.params.secret })) return res.status(400).json({ "message": "Invalid capture ID.", "code": "INVALID_ID" });

await schema.findOneAndDelete({ _id: req.params.secret });

res.status(200).json({ "success": true });
}
45 changes: 21 additions & 24 deletions src/sentry-api/endpoints/post.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const Discord = require("discord.js");

const cap = require("../../util/cap");
const getColor = require("../../util/sentry/color");
const parser = require("../../util/sentry/parser");
const cap = require("../util/cap");
const parser = require("../util/parser");

const schema = require("../../models/sentrySchema");

Expand All @@ -14,27 +13,21 @@ module.exports = async (req, res, client) => {
const event = req.body;

const embed = new Discord.EmbedBuilder()
.setAuthor({ name: event.project_name, iconURL: "attachment://sentry-glyph-light-400x367.png", url: parser.getProjectLink(event) })

const logo = new Discord.AttachmentBuilder("src/assets/sentry-api/sentry-glyph-light-400x367.png", { name: "sentry-glyph-light-400x367.png" });
.setColor(parser.getColor(parser.getLevel(event)))
.setAuthor({ name: event.project_name, iconURL: "https://gc-sentry-api.wdh.gg/assets/sentry-glyph-light-400x367.png", url: parser.getProjectLink(event) })
.setTimestamp(parser.getTime(event))

const projectName = parser.getProject(event);
const eventTitle = parser.getTitle(event);

if(projectName) {
const embedTitle = `[${projectName}] ${eventTitle}`;
embed.setTitle(cap(embedTitle, 250));
} else {
embed.setTitle(cap(eventTitle, 250));
}
embed.setTitle(cap(eventTitle, 250));

if(projectName) embed.setTitle(cap(`[${projectName}] ${eventTitle}`, 250));

const link = parser.getLink(event);

if(link.startsWith("https://") || link.startsWith("http://")) embed.setURL(parser.getLink(event));

embed.setTimestamp(parser.getTime(event));
embed.setColor(getColor(parser.getLevel(event)));

const fileLocation = parser.getFileLocation(event);
const snippet = cap(parser.getErrorCodeSnippet(event), 3900);

Expand All @@ -52,7 +45,7 @@ module.exports = async (req, res, client) => {
fields.push({
name: "Stack",
value: `\`\`\`${cap(location.join("\n"), 1000)}\n\`\`\``
});
})
}

const user = parser.getUser(event);
Expand All @@ -62,7 +55,7 @@ module.exports = async (req, res, client) => {
name: "User",
value: cap(`${user.username} ${user.id ? `(${user.id})` : ""}`, 1024),
inline: true
});
})
}

const tags = parser.getTags(event);
Expand All @@ -72,7 +65,7 @@ module.exports = async (req, res, client) => {
name: "Tags",
value: cap(tags.map(([key, value]) => `**${key}**: ${value}`).join("\n"), 1024),
inline: true
});
})
}

const extras = parser.getExtras(event);
Expand All @@ -82,7 +75,7 @@ module.exports = async (req, res, client) => {
name: "Extras",
value: cap(extras.join("\n"), 1024),
inline: true
});
})
}

const contexts = parser.getContexts(event);
Expand All @@ -92,7 +85,7 @@ module.exports = async (req, res, client) => {
name: "Contexts",
value: cap(contexts.join("\n"), 1024),
inline: true
});
})
}

const release = parser.getRelease(event);
Expand All @@ -102,7 +95,7 @@ module.exports = async (req, res, client) => {
name: "Release",
value: cap(release, 1024),
inline: true
});
})
}

embed.addFields(fields);
Expand All @@ -127,7 +120,11 @@ module.exports = async (req, res, client) => {

const channel = client.channels.cache.get(data.channel);

channel.send({ embeds: [embed], components: [actions], files: [logo] });

res.status(200).json({ "message": "The event has been received.", "code": "EVENT_RECEIVED" });
try {
await channel.send({ embeds: [embed], components: [actions] });
res.status(200).json({ "success": true });
} catch(err) {
client.sentry.captureException(err);
res.status(500).json({ "success": false });
}
}
9 changes: 8 additions & 1 deletion src/sentry-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ module.exports = async (client) => {
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());

const routes = require("./util/routes");
// Host public files
app.use(express.static(__dirname + "/public"));

const routes = require("./routes");

app.delete("/:secret", async (req, res) => {
routes.delete(req, res);
})

app.get("/:secret", async (req, res) => {
routes.get(req, res);
Expand Down
5 changes: 5 additions & 0 deletions src/sentry-api/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
delete: require("./endpoints/delete"),
get: require("./endpoints/get"),
post: require("./endpoints/post")
}
File renamed without changes.
16 changes: 16 additions & 0 deletions src/util/sentry/parser.js → src/sentry-api/util/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// https://github.com/IanMitchell/sentrydiscord.dev/blob/867b889e15c6b101d619610d07b4663c6a73fe6a/lib/message.ts

module.exports.getColor = function (level) {
switch (level) {
case "debug":
return parseInt("fbe14f", 16);
case "info":
return parseInt("2788ce", 16);
case "warning":
return parseInt("f18500", 16);
case "fatal":
return parseInt("d20f2a", 16);
case "error":
default:
return parseInt("e03e2f", 16);
}
}

module.exports.getContexts = function (issue) {
const contexts = this.getEvent(issue)?.contexts ?? {};

Expand Down
4 changes: 0 additions & 4 deletions src/sentry-api/util/routes.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/util/sentry/color.js

This file was deleted.

0 comments on commit 4d0a4a4

Please sign in to comment.