Skip to content

Commit

Permalink
ES6 imports
Browse files Browse the repository at this point in the history
  • Loading branch information
rushi committed Sep 5, 2024
1 parent 446ad66 commit 255945f
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 616 deletions.
961 changes: 420 additions & 541 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@
"name": "snitch",
"version": "0.2.0",
"description": "GoCD Slack Bot for notifications",
"type": "module",
"scripts": {
"start": "node src/index.js",
"monitor": "node src/monitor-elastic-agents.js start",
"dev": "nodemon --delay 3 --watch src src/index.js",
"format": "prettier -l --write src"
},
"dependencies": {
"@slack/bolt": "^3.3.0",
"axios": "^1.7.7",
"chalk": "^5.3.0",
"cheerio": "^1.0.0",
"config": "^3.3.6",
"cron": "^3.1.7",
"dayjs": "^1.11.13",
"junit-viewer": "^4.11.1",
"lodash-es": "^4.17.21",
"md5": "^2.3.0",
"node-notifier": "^10.0.1"
},
"devDependencies": {
"nodemon": "^3.1.4",
"prettier": "^3.3.3"
Expand All @@ -21,16 +36,5 @@
"arrowParens": "always",
"bracketSameLine": false,
"tabWidth": 4
},
"dependencies": {
"@slack/bolt": "^3.3.0",
"axios": "^0.21.1",
"chalk": "^3.0.0",
"config": "^3.3.6",
"dayjs": "^1.11.13",
"fastify": "^3.14.2",
"junit-viewer": "^4.11.1",
"lodash": "^4.17.21",
"md5": "^2.3.0"
}
}
27 changes: 13 additions & 14 deletions src/handlers/AgentHandler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Handler = require("./Handler");
const _ = require("lodash");
const chalk = require("chalk");
const dayjs = require("dayjs");
const config = require("config");
const util = require("util");
const AgentUpdatedNotification = require("../templates/AgentUpdatedNotification");
import Handler from "./Handler.js";
import { omit } from "lodash-es";
import chalk from "chalk";
import config from "config";
import dayjs from "dayjs";
import { inspect } from "util";
import AgentUpdatedNotification from "../templates/AgentUpdatedNotification.js";

class AgentHandler extends Handler {
static shouldHandle(request) {
Expand All @@ -13,7 +13,7 @@ class AgentHandler extends Handler {
}

async handle(request) {
const body = _.omit(request.body, ["operating_system"]);
const body = omit(request.body, ["operating_system"]);
const isElastic = body.is_elastic;
const isDeployAgent = !isElastic && !body.host_name.includes("i-") && !body.host_name.startsWith("ip-");

Expand All @@ -35,9 +35,9 @@ class AgentHandler extends Handler {
console.log(chalk.bgRed.white(`[${now}] Deployment Agent lost contact`));
this.doNotify(body, `Agent **${host_name}** in ${agent_state} state`);
} else if (agent_state === "Idle" && build_state === "Idle") {
AgentHandler.log(body);
console.log(chalk.bgGreen.white(`[${now}] Deployment Agent came online?`));
this.doNotify(body, `**${host_name}** has come back online`);
// AgentHandler.log(body);
// console.log(chalk.bgGreen.white(`[${now}] Deployment Agent came online?`));
// this.doNotify(body, `**${host_name}** has come back online`);
}
} else {
this.doNotify(body, "Testing");
Expand All @@ -63,12 +63,11 @@ class AgentHandler extends Handler {
console.log("Error sending slack message", chalk.red(error.message));
console.log(notification);
}
process.exit(0);
}

static log(data, options = {}) {
console.log(
util.inspect(data, {
inspect(data, {
colors: true,
sorted: true,
breakLength: 1000,
Expand All @@ -80,4 +79,4 @@ class AgentHandler extends Handler {
}
}

module.exports = AgentHandler;
export default AgentHandler;
4 changes: 2 additions & 2 deletions src/handlers/Handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { App } = require("@slack/bolt");
import App from "@slack/bolt";

class Handler {
/**
Expand All @@ -17,4 +17,4 @@ class Handler {
}
}

module.exports = Handler;
export default Handler;
16 changes: 8 additions & 8 deletions src/handlers/PipelineUpdateHandler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require("path");
const Pipeline = require("../models/Pipeline");
const PipelineFailedNotification = require("../templates/PipelineFailedNotification");
const config = require("config");
const Handler = require("./Handler");
const Go = require("../services/go");
import { basename } from "path";
import Pipeline from "../models/Pipeline.js";
import PipelineFailedNotification from "../templates/PipelineFailedNotification.js";
import config from "config";
import Handler from "./Handler.js";
import Go from "../services/go.js";

class PipelineUpdateHandler extends Handler {
static shouldHandle(request) {
Expand All @@ -29,7 +29,7 @@ class PipelineUpdateHandler extends Handler {
testCases.forEach((tc) => {
let line;
if (tc.file) {
line = `${path.basename(tc.file)} Line: ${tc.line}\n`;
line = `${basename(tc.file)} Line: ${tc.line}\n`;
if (detail) {
line = this.processTestCaseMessages(tc.messages, line);
} else {
Expand Down Expand Up @@ -140,4 +140,4 @@ class PipelineUpdateHandler extends Handler {
}
}

module.exports = PipelineUpdateHandler;
export default PipelineUpdateHandler;
24 changes: 12 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const express = require("express");
const { App, ExpressReceiver } = require("@slack/bolt");
const config = require("config");
import { json } from "express";
import Bolt from "@slack/bolt";
import config from "config";

const PipelineUpdateHandler = require("./handlers/PipelineUpdateHandler");
const Go = require("./services/go");
const Pipeline = require("./models/Pipeline");
const AgentHandler = require("./handlers/AgentHandler");
import PipelineUpdateHandler from "./handlers/PipelineUpdateHandler.js";
import Go from "./services/go.js";
import Pipeline from "./models/Pipeline.js";
import AgentHandler from "./handlers/AgentHandler.js";

const receiver = new ExpressReceiver({
const receiver = new Bolt.ExpressReceiver({
signingSecret: config.get("slack.signingSecret"),
});

const app = new App({
const app = new Bolt.App({
token: config.get("slack.token"),
signingSecret: config.get("slack.signingSecret"),
receiver,
Expand All @@ -25,7 +25,7 @@ app.action({ callback_id: "build_response" }, async ({ action, say, ack }) => {

let msg = "Error, invalid payload";
if (action.name === "rerun") {
const result = await Go.runFailedJobs(payload.uri);
const result = await runFailedJobs(payload.uri);
msg = `${result?.message || "Error triggering build"} for ${payload.name}`;
}

Expand Down Expand Up @@ -56,7 +56,7 @@ receiver.router.get("/status", async (request, response) => {
response.send({ status: "SNITCH - OK" });
});

receiver.router.post("/api/webhooks", express.json(), async (request, response) => {
receiver.router.post("/api/webhooks", json(), async (request, response) => {
const Handler = handlers.find((Handler) => {
return Handler.shouldHandle(request);
});
Expand All @@ -70,7 +70,7 @@ receiver.router.post("/api/webhooks", express.json(), async (request, response)
response.send({ status: "OK" });
});

receiver.router.post("/api/actions", express.json(), async (request, response) => {
receiver.router.post("/api/actions", json(), async (request, response) => {
console.log(request.body);
});

Expand Down
8 changes: 4 additions & 4 deletions src/models/Model.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const _ = require("lodash");
import { get as _get, set as _set } from "lodash-es";

class Model {
constructor(attributes) {
this.attributes = attributes;
}

get(path, defaultValue) {
return _.get(this.attributes, path, defaultValue);
return _get(this.attributes, path, defaultValue);
}

getModel(path, type = Model) {
return this.get(path) || new type();
}

set(path, value) {
_.set(this.attributes, path, value);
_set(this.attributes, path, value);
return this;
}

Expand All @@ -23,4 +23,4 @@ class Model {
}
}

module.exports = Model;
export default Model;
16 changes: 8 additions & 8 deletions src/models/Pipeline.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const config = require("config");
const Model = require("./Model");
const _ = require("lodash");
const md5 = require("md5");
const jv = require("junit-viewer");
const Go = require("../services/go");
import config from "config";
import Model from "./Model.js";
import { trimEnd } from "lodash-es";
import md5 from "md5";
import * as jv from "junit-viewer";
import Go from "../services/go.js";

class Pipeline extends Model {
static STAGE_FAILED = "Failed";
Expand Down Expand Up @@ -94,7 +94,7 @@ class Pipeline extends Model {
const repoUrl = new URL(this.get("build-cause.0.material.git-configuration.url"));
repoUrl.username = "";
repoUrl.password = "";
return _.trimEnd(repoUrl.toString(), ".git");
return trimEnd(repoUrl.toString(), ".git");
}

getCommitUrl() {
Expand Down Expand Up @@ -158,4 +158,4 @@ class Pipeline extends Model {
}
}

module.exports = Pipeline;
export default Pipeline;
6 changes: 3 additions & 3 deletions src/services/go.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require("axios");
const config = require("config");
import axios from "axios";
import config from "config";

const api = axios.create({
baseURL: config.get("go.url"),
Expand Down Expand Up @@ -77,4 +77,4 @@ const Go = {
},
};

module.exports = Go;
export default Go;
22 changes: 12 additions & 10 deletions src/templates/AgentUpdatedNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ class AgentUpdatedNotification {
toJSON(text) {
const { agent } = this;
const payload = {
attachments: [{
mrkdwn_in: ["text", "pretext"],
color: this.getColor(),
title: text ?? `Something changed with agent: ${agent.host_name}`,
text: `Agent State: ${agent.agent_state} Build State: ${agent.build_state}`,
title_link: `https://sage.ci.xola.com/go/agents/${agent.uuid}/job_run_history`,
footer: `Agent UUID: ${agent.uuid}`
}]
}
attachments: [
{
mrkdwn_in: ["text", "title", "pretext"],
color: this.getColor(),
title: text ?? `Something changed with agent: ${agent.host_name}`,
text: `Agent State: ${agent.agent_state} Build State: ${agent.build_state}`,
title_link: `https://sage.ci.xola.com/go/agents/${agent.uuid}/job_run_history`,
footer: `Agent UUID: ${agent.uuid}`,
},
],
};

return payload;
}
}

module.exports = AgentUpdatedNotification;
export default AgentUpdatedNotification;
5 changes: 2 additions & 3 deletions src/templates/PipelineFailedNotification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const Pipeline = require("../models/Pipeline");
const Go = require("../services/go");
import Pipeline from "../models/Pipeline.js";

class PipelineFailedNotification {
/**
Expand Down Expand Up @@ -142,4 +141,4 @@ class PipelineFailedNotification {
}
}

module.exports = PipelineFailedNotification;
export default PipelineFailedNotification;

0 comments on commit 255945f

Please sign in to comment.