Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KeesCBakker committed Aug 29, 2023
1 parent 03d7d92 commit f524af5
Show file tree
Hide file tree
Showing 8 changed files with 1,676 additions and 298 deletions.
12 changes: 0 additions & 12 deletions index.coffee

This file was deleted.

31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const fs = require('fs');
const path = require('path');

module.exports = function(robot, scripts) {
const scriptsPath = path.resolve(__dirname, 'src', 'scripts');
return fs.exists(scriptsPath, function(exists) {
if (exists) {
return (() => {
const result = [];
for (var script of Array.from(fs.readdirSync(scriptsPath))) {
if ((scripts != null) && !Array.from(scripts).includes('*')) {
if (Array.from(scripts).includes(script)) { result.push(robot.loadFile(scriptsPath, script)); } else {
result.push(undefined);
}
} else {
result.push(robot.loadFile(scriptsPath, script));
}
}
return result;
})();
}
});
};
149 changes: 0 additions & 149 deletions src/pagerduty.coffee

This file was deleted.

195 changes: 195 additions & 0 deletions src/pagerduty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const HttpClient = require('scoped-http-client');

const pagerDutyApiKey = process.env.HUBOT_PAGERDUTY_API_KEY;
const pagerDutySubdomain = process.env.HUBOT_PAGERDUTY_SUBDOMAIN;
const pagerDutyBaseUrl = 'https://api.pagerduty.com';
const pagerDutyServices = process.env.HUBOT_PAGERDUTY_SERVICES;
const pagerDutyFromEmail = process.env.HUBOT_PAGERDUTY_FROM_EMAIL;
let pagerNoop = process.env.HUBOT_PAGERDUTY_NOOP;
if ((pagerNoop === 'false') || (pagerNoop === 'off')) { pagerNoop = false; }

class PagerDutyError extends Error {}
module.exports = {
http(path) {
return HttpClient.create(`${pagerDutyBaseUrl}${path}`)
.headers({
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': `Token token=${pagerDutyApiKey}`,
'From': pagerDutyFromEmail
});
},

missingEnvironmentForApi(msg) {
let missingAnything = false;
if (pagerDutyFromEmail == null) {
msg.send("PagerDuty From is missing: Ensure that HUBOT_PAGERDUTY_FROM_EMAIL is set.");
missingAnything |= true;
}
if (pagerDutyApiKey == null) {
msg.send("PagerDuty API Key is missing: Ensure that HUBOT_PAGERDUTY_API_KEY is set.");
missingAnything |= true;
}
return missingAnything;
},

get(url, query, cb) {
if (typeof(query) === 'function') {
cb = query;
query = {};
}

if ((pagerDutyServices != null) && url.match(/\/incidents/)) {
query['service_id'] = pagerDutyServices;
}

return this.http(url)
.query(query)
.get()(function(err, res, body) {
if (err != null) {
cb(err);
return;
}
let json_body = null;
switch (res.statusCode) {
case 200: json_body = JSON.parse(body); break;
default:
cb(new PagerDutyError(`${res.statusCode} back from ${url}`));
}

return cb(null, json_body);
});
},

put(url, data, cb) {
if (pagerNoop) {
console.log(`Would have PUT ${url}: ${inspect(data)}`);
return;
}

const json = JSON.stringify(data);
return this.http(url)
.header('content-type', 'application/json')
.put(json)(function(err, res, body) {
if (err != null) {
callback(err);
return;
}

let json_body = null;
switch (res.statusCode) {
case 200: json_body = JSON.parse(body); break;
default:
if (body != null) {
return cb(new PagerDutyError(`${res.statusCode} back from ${url} with body: ${body}`));
} else {
return cb(new PagerDutyError(`${res.statusCode} back from ${url}`));
}
}
return cb(null, json_body);
});
},

post(url, data, cb) {
if (pagerNoop) {
console.log(`Would have POST ${url}: ${inspect(data)}`);
return;
}

const json = JSON.stringify(data);
return this.http(url)
.header('content-type', 'application/json')
.post(json)(function(err, res, body) {
if (err != null) {
return cb(err);
}

let json_body = null;
switch (res.statusCode) {
case 201: json_body = JSON.parse(body); break;
default:
return cb(new PagerDutyError(`${res.statusCode} back from ${url}`));
}
return cb(null, json_body);
});
},

delete(url, cb) {
if (pagerNoop) {
console.log(`Would have DELETE ${url}`);
return;
}

const auth = `Token token=${pagerDutyApiKey}`;
return this.http(url)
.header("content-length",0)
.delete()(function(err, res, body) {
let value;
if (err != null) {
return cb(err);
}
const json_body = null;
switch (res.statusCode) {
case 204: case 200:
value = true;
break;
default:
console.log(res.statusCode);
console.log(body);
value = false;
}
return cb(null, value);
});
},

getIncident(incident_key, cb) {
const query =
{incident_key};

return this.get("/incidents", query, function(err, json) {
if (err != null) {
cb(err);
return;
}
return cb(null, json.incidents);
});
},

getIncidents(status, cb) {
const query = {
sort_by: 'incident_number:asc',
'statuses[]': status.split(',')
};

return this.get("/incidents", query, function(err, json) {
if (err != null) {
cb(err);
return;
}
return cb(null, json.incidents);
});
},

getSchedules(query, cb) {
if (typeof(query) === 'function') {
cb = query;
query = {};
}

return this.get("/schedules", query, function(err, json) {
if (err != null) {
cb(err);
return;
}

return cb(null, json.schedules);
});
},

subdomain: pagerDutySubdomain
};
Loading

0 comments on commit f524af5

Please sign in to comment.