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

Commit

Permalink
Removed remote and seperated discord messages
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBusyBiscuit committed Jan 21, 2021
1 parent f6970e6 commit cb7c24b
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 366 deletions.
73 changes: 36 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
{
"name": "maven-builds-server",
"version": "2.1.2",
"description": "A simple Builds Server for all my Maven Projects",
"main": "src/remote.js",
"scripts": {
"remote": "node ./src/remote.js",
"iteration": "node ./src/single-run.js",
"regen": "node ./src/force-update.js",
"test": "nyc --reporter=lcov --reporter=text-lcov mocha test/*.js --reporter mocha-multi-reporters --reporter-options configFile=resources/mocha.json",
"sonar": "node ./src/sonar.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/TheBusyBiscuit/builds.git"
},
"author": "TheBusyBiscuit",
"license": "MIT",
"bugs": {
"url": "https://github.com/TheBusyBiscuit/builds/issues"
},
"homepage": "https://github.com/TheBusyBiscuit/builds#readme",
"dependencies": {
"child-process-promise": "^2.2.1",
"discord.js": "^11.5.1",
"lodash": "^4.17.14",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"xml-library": "0.2.0"
},
"devDependencies": {
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"mocha": "8.2.1",
"mocha-multi-reporters": "1.5.1",
"mocha-sonarqube-reporter": "1.0.2",
"nyc": "15.1.0"
}
"name": "maven-builds-server",
"version": "2.1.2",
"description": "A simple Builds Server for all my Maven Projects",
"main": "src/single-run.js",
"scripts": {
"iteration": "node ./src/single-run.js",
"regen": "node ./src/force-update.js",
"test": "nyc --reporter=lcov --reporter=text-lcov mocha test/*.js --reporter mocha-multi-reporters --reporter-options configFile=resources/mocha.json",
"sonar": "node ./src/sonar.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/TheBusyBiscuit/builds.git"
},
"author": "TheBusyBiscuit",
"license": "MIT",
"bugs": {
"url": "https://github.com/TheBusyBiscuit/builds/issues"
},
"homepage": "https://github.com/TheBusyBiscuit/builds#readme",
"dependencies": {
"child-process-promise": "^2.2.1",
"discord.js": "^11.5.1",
"lodash": "^4.17.14",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"xml-library": "0.2.0"
},
"devDependencies": {
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"mocha": "8.2.1",
"mocha-multi-reporters": "1.5.1",
"mocha-sonarqube-reporter": "1.0.2",
"nyc": "15.1.0"
}
}
40 changes: 40 additions & 0 deletions resources/discord-messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"success": [
"Is it a bird?\nIs it a plane?\nNo, it's Build #<id> of <repo>",
"Wow, that actually worked?\nI mean, of course, yes, sure.",
"It'sa me, Build #<id> of <repo>",
"<repo> has been updated to Build #<id>",
"<user> really outdid themselves!\nBuild #<id> of <repo> was a success!",
"<repo> #<id> compiled successfully!\n<user> must be so proud!",
"Oh cool another build of <repo>...",
"Look!\nA new Build of <repo> just dropped from the sky!",
"Did someone order a large <repo> with some extra <user> on top?",
"Ding Dong!\nBuild #<id> of <repo> is here!",
"Knock Knock!\nWho's there?\nBuild #<id>!\nBuild #<id> who?\n\nBuild #<id> of <repo>!",
"Now entering... Build #<id> of <repo>",
"A new challenger approached!\nBuild #<id> of <repo>",
"A wild <repo> has appeared!",
"Whooooooooooooosh\n\nA <repo> build flew by!",
"Pssst...\nYou want some <repo>?\nHere's a fresh build!",
"Build <id> of <repo> has just graduated!\nIt is now ready for your servers!",
"<user> just grew another <repo> Tree in his garden.\nLook! There is already a juicy build #<id> hanging beneath it!",
"Another fresh oven-baked piece of <repo> is ready!\nBe careful not to burn yourself!",
"Did someone say <repo>?\nNo?\nWell, here you go anyway..."
],
"failure": [
"Oh no!\nWhat has <user> done?\n<repo> did not compile!",
"Well, <user>...\nThat did not work out, did it?",
"<repo> failed to compile! No Build #<id> for you :(",
"A new build of <repo> is...\nOh damn it, it broke...",
"Build #<id> of <repo> fell into the void.",
"Build #<id> of <repo> failed to compile.\nPress 'F' to pay respect.",
"Another one bites the dust!\nIt was <repo> this time...",
"Uh oh, <repo> got snapped out of existance",
"Someone ate Build <id> of <repo>\n<user>, was it you?",
"<user>, there is a problem! *over*",
"Mission failed. We'll get 'em next time, <user>",
"How... Just how?",
"<repo>, I believed in you...",
"<repo> believed it could fly.\n\nSoon after... it hit the ground."
]
}
12 changes: 7 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
const lodash = require("lodash/object");

const defaultConfig = {
Expand Down Expand Up @@ -29,7 +30,7 @@ const defaultConfig = {
}
}

module.exports = (file) => {
module.exports = file => {
let cfg = {};

if (file != 'null' && process.env.JSON_CONFIG) {
Expand All @@ -39,8 +40,7 @@ module.exports = (file) => {
try {
// Sadly this has to be a sync-process, otherwise a Promise would be more appropriate here
cfg = JSON.parse(fs.readFileSync(file, 'UTF-8'));
}
catch(err) {}
} catch (err) { }

cfg = lodash.defaultsDeep(cfg, defaultConfig);

Expand All @@ -52,6 +52,8 @@ module.exports = (file) => {
};

function structure(json) {
let messages = JSON.stringify(fs.readFileSync(path.resolve(__dirname, "../resources/discord-messages.json"), 'UTF-8'));

return {
server: {
getPort: () => json.server.port,
Expand All @@ -64,8 +66,8 @@ function structure(json) {
isEnabled: () => json.discord.enabled,
getID: () => json.discord.id,
getToken: () => json.discord.token,
getMessages: (success) => {
return success ? json.discord.messages.success: json.discord.messages.failure;
getMessages: success => {
return success ? messages.success : messages.failure;
}
},
sonar: {
Expand Down
45 changes: 24 additions & 21 deletions src/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ const lodash = require("lodash/collection");
const projects = require('../src/projects.js');

module.exports = (cfg) => {
var config = cfg;
var config = cfg;

var webhook = {
send: () => Promise.resolve()
};

if (config) {
if (config.isEnabled()) webhook = new Discord.WebhookClient(config.getID(), config.getToken());
}
else config = {
isEnabled: () => true,
getMessages: (success) => {
return success ? "This Build was a success!": "This Build was a failure!";
}
}
if (config.isEnabled()) {
webhook = new Discord.WebhookClient(config.getID(), config.getToken());
}
} else config = {
isEnabled: () => true,
getMessages: (success) => {
return success ? "This Build was a success!" : "This Build was a failure!";
}
}

return {
/**
Expand All @@ -31,11 +32,11 @@ module.exports = (cfg) => {
*/
sendUpdate: (job) => sendUpdate(webhook, job, config),

/**
* This method returns the discord config used by this instance
*
* @return {Object} Config
*/
/**
* This method returns the discord config used by this instance
*
* @return {Object} Config
*/
getConfig: () => config
}
};
Expand All @@ -51,23 +52,25 @@ module.exports = (cfg) => {
*/
function sendUpdate(webhook, job, cfg) {
return new Promise((resolve, reject) => {
if (!cfg.isEnabled()) resolve();
if (!cfg.isEnabled()) {
resolve();
}

if (!projects.isValid(job, true)) {
reject("Invalid Job");
return;
}

var message = lodash.sample(cfg.getMessages(job.success));
let message = lodash.sample(cfg.getMessages(job.success));
message = message.replace(/<user>/g, job.author).replace(/<repo>/g, job.repo).replace(/<branch>/g, job.branch).replace(/<id>/g, job.id);

webhook.send(
new Discord.RichEmbed()
.setTitle(job.author + "/" + job.repo + ":" + job.branch + " ( #" + job.id + " )")
.setColor(job.success ? 0X00FF00: 0XFF0000)
.setDescription(message)
.setURL("https://thebusybiscuit.github.io/builds/" + job.directory + "#" + job.id)
.setTimestamp(Date.now())
.setTitle(job.author + "/" + job.repo + ":" + job.branch + " ( #" + job.id + " )")
.setColor(job.success ? 0X00FF00 : 0XFF0000)
.setDescription(message)
.setURL("https://thebusybiscuit.github.io/builds/" + job.directory + "#" + job.id)
.setTimestamp(Date.now())
).then(resolve, reject);
});
}
42 changes: 20 additions & 22 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ module.exports = (cfg) => {
exists: (job) => exists(job, cfg),

/**
* This method returns the discord config used by this instance
*
* @return {Object} Config
*/
* This method returns the discord config used by this instance
*
* @return {Object} Config
*/
getConfig: () => cfg,

clone,
Expand Down Expand Up @@ -140,8 +140,7 @@ function getJSON(job, cfg, logging, endpoint, resolve, reject) {

if (json.documentation_url) {
reject("Missing License file");
}
else {
} else {
resolve(json);
}
}, reject);
Expand Down Expand Up @@ -188,16 +187,15 @@ function hasUpdate(job, timestamp) {

if (FileSystem.existsSync(file)) {
fs.readFile(file, "utf8")
.then((data) => {
if (!data) resolve(0); // Pretend like there is an Update if no local builds exist
var json = JSON.parse(data);

if (!json.latest) resolve(0); // Pretend like there is an Update if no local builds exist
else if (timestamp > json[json.latest].timestamp) resolve(json.latest);
else reject();
}, () => resolve(0))
}
else resolve(0); // Pretend like there is an Update if no local builds exist
.then((data) => {
if (!data) resolve(0); // Pretend like there is an Update if no local builds exist
var json = JSON.parse(data);

if (!json.latest) resolve(0); // Pretend like there is an Update if no local builds exist
else if (timestamp > json[json.latest].timestamp) resolve(json.latest);
else reject();
}, () => resolve(0))
} else resolve(0); // Pretend like there is an Update if no local builds exist
});
}

Expand Down Expand Up @@ -238,8 +236,8 @@ function clone(job, commit, logging) {
"--hard",
commit
], {
cwd: path.resolve(__dirname, "../" + job.directory + "/files")
});
cwd: path.resolve(__dirname, "../" + job.directory + "/files")
});

refresh.childProcess.stdout.on('data', (data) => log(logging, "-> " + data));
refresh.childProcess.stderr.on('data', (data) => log(logging, "-> " + data));
Expand Down Expand Up @@ -283,7 +281,7 @@ function pushChanges(job, logging) {
var commit = process.spawn("git", [
"commit",
"-m",
(job.success ? "Successfully compiled: ": "Failed to compile: ") + job.author + "/" + job.repo + ":" + job.branch + " (" + job.id + ")"
(job.success ? "Successfully compiled: " : "Failed to compile: ") + job.author + "/" + job.repo + ":" + job.branch + " (" + job.id + ")"
]);

commit.childProcess.stdout.on('data', (data) => log(logging, "-> " + data));
Expand Down Expand Up @@ -317,9 +315,9 @@ function pushChanges(job, logging) {
function getURL(job, cfg, endpoint) {
var url = "https://api.github.com/repos/" + job.author + "/" + job.repo + endpoint;
var headers = {
"Accept": "application/vnd.github.v3+json",
"User-Agent": "The Busy Biscuit's Repository Compiler",
"Time-Zone": "UTC"
"Accept": "application/vnd.github.v3+json",
"User-Agent": "The Busy Biscuit's Repository Compiler",
"Time-Zone": "UTC"
};

if (cfg.getToken()) {
Expand Down
7 changes: 5 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
module.exports = (logging, str, direct) => {
if (logging) {
if (direct) process.stdout.write(str);
else console.log(str);
if (direct) {
process.stdout.write(str);
} else {
console.log(str);
}
}
}
Loading

0 comments on commit cb7c24b

Please sign in to comment.