Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 10 additions & 128 deletions src/exporter.js → src/exporting/baseExporter.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,12 @@
import minimist from "minimist";

import fs from "fs";
import { WebhookClient } from "discord.js";
import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
import { Octokit } from "@octokit/core";
import beautify from "json-beautify";

const argv = minimist(process.argv.slice(2));

const discordUsername = argv.discordUsername || "Screeps Performance Server";
const avatarURL =
argv.discordAvatarUrl ||
"https://user-images.githubusercontent.com/48334001/189509241-7f04fe66-a5bc-4791-ada7-4f948794ceb0.png";
const githubOwner = argv.githubOwner || "The-International-Screeps-Bot";
const githubRepo = argv.githubRepo || "The-International-Open-Source";
export default class Exporter {
static discordMessage = null;

static githubCommit = null;

static webhookClient = null;

static octokit = null;

static commit = null;

static commitName = "localhost";

static customConstructor() {
if (process.env.GITHUB_EVENT_PATH) {
const file = fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8");
this.githubCommit = JSON.parse(file);

const object = this.githubCommit;
if (object.commits) {
this.commitName = object.commits[object.commits.length - 1].message;
}
}
}

static async sendDiscordWebHook(content, isPeriodic = true) {
if (!argv.discordWebHookUrl) return;

if (!this.webhookClient)
this.webhookClient = new WebhookClient({ url: argv.discordWebHookUrl });

try {
const data = {
username: discordUsername + (isPeriodic ? " - Periodic " : " - Final"),
avatarURL,
content,
};

if (isPeriodic) {
if (!this.discordMessage)
this.discordMessage = await this.webhookClient.send(data);
else {
data.content = `${this.discordMessage.content}\n${content}`;
this.discordMessage.content = data.content;
await this.webhookClient.editMessage(this.discordMessage.id, data);
}
} else {
await this.webhookClient.send(data);
}
} catch (error) {
console.log("Failed to send discord webhook", error);
}
}

static async sendGithubComment(content, isPeriodic = true) {
if (
!argv.githubAuth ||
!this.githubCommit ||
!argv.githubOwner ||
!argv.githubRepo
) {
return;
}

if (!this.octokit) {
this.octokit = new Octokit({
auth: argv.githubAuth,
});
}

const baseComment = "**BOT COMMENT!**\n\n";
const url =
isPeriodic && this.githubCommit
? "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}"
: "POST /repos/{owner}/{repo}/issues/{issue_number}/comments";
let body = "";
if (!isPeriodic) {
body = baseComment + content;
} else if (this.githubCommit) {
body = `${this.githubCommit.data.body}\n${content}`;
} else {
body = baseComment + content;
}

try {
const response = await this.octokit.request(url, {
owner: githubOwner,
repo: githubRepo,
issue_number: this.githubCommit.number,
body,
comment_id: this.githubCommit ? this.githubCommit.data.id : null,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});

if (isPeriodic && !this.githubCommit) this.githubCommit = response;
} catch (error) {
console.log("Failed to send github comment", error);
}
}

static getLoggerFile() {
export default class BaseExporter {
getLoggerFile() {

Check failure on line 9 in src/exporting/baseExporter.js

View workflow job for this annotation

GitHub Actions / Run linters

Expected 'this' to be used by class method 'getLoggerFile'
if (fs.existsSync("./logs/logListener.log", "utf8")) {
const fileText = fs.readFileSync("./logs/logListener.log", "utf8");
const fileLineCount = fileText.split("\n").length;
Expand All @@ -125,7 +15,7 @@
return { success: false, text: "No log dump file found" };
}

static getHistory() {
getHistory() {

Check failure on line 18 in src/exporting/baseExporter.js

View workflow job for this annotation

GitHub Actions / Run linters

Expected 'this' to be used by class method 'getHistory'
if (fs.existsSync("./logs/history.log", "utf8")) {
const fileText = fs.readFileSync("./logs/history.log", "utf8");
const fileLineCount = fileText.split("\n").length;
Expand All @@ -134,7 +24,7 @@
return { success: false, text: "No history file found" };
}

static async createPasteBinUrl(content) {
async createPasteBinUrl(content) {
const key = argv.pasteBinUrlDevKey;
if (!key) return "No key provided";

Expand All @@ -155,7 +45,7 @@
}
}

static generatePeriodicMessage(gameTime, milestones, startTime) {
generatePeriodicMessage(gameTime, milestones, startTime) {
let message = "";
if (!this.discordMessage) {
message = `**Performance Test Results**\n**Commit:** ${this.commitName}\n`;
Expand All @@ -173,7 +63,7 @@
return message;
}

static generateFinalMessage(
generateFinalMessage(
milestones,
fails,
status,
Expand All @@ -187,13 +77,13 @@

const loggerText = loggerFile.success
? `**Filtered Logs:** ${loggerFile.pasteBinUrl} (${
loggerFile.lineCount - 1

Check failure on line 80 in src/exporting/baseExporter.js

View workflow job for this annotation

GitHub Actions / Run linters

Insert `··`
} logs)\n`

Check failure on line 81 in src/exporting/baseExporter.js

View workflow job for this annotation

GitHub Actions / Run linters

Insert `··`
: "**Filtered Logs:** No log dump file found\n";
const historyText = historyFile.success
? `**Milestone History:** ${historyFile.pasteBinUrl} (${
historyFile.lineCount - 1

Check failure on line 85 in src/exporting/baseExporter.js

View workflow job for this annotation

GitHub Actions / Run linters

Insert `··`
} logs)\n`

Check failure on line 86 in src/exporting/baseExporter.js

View workflow job for this annotation

GitHub Actions / Run linters

Insert `··`
: "**Milestone History:** No history file found\n";

return (
Expand Down Expand Up @@ -221,17 +111,11 @@
);
}

static async sendPeriodicResult(gameTime, milestone, startTime) {
const message = this.generatePeriodicMessage(
gameTime,
milestone,
startTime
);
await this.sendDiscordWebHook(message);
await this.sendGithubComment(message);
getPeriodicResultMessage(gameTime, milestone, startTime) {
return this.generatePeriodicMessage(gameTime, milestone, startTime);
}

static async sendFinalResult(
async getFinalResultMessage(
milestones,
fails,
status,
Expand All @@ -241,7 +125,7 @@
const logs = this.getLoggerFile();
const history = this.getHistory();

const message = this.generateFinalMessage(
return this.generateFinalMessage(
milestones,
fails,
status,
Expand All @@ -258,7 +142,5 @@
success: history.success,
}
);
await this.sendDiscordWebHook(message, false);
await this.sendGithubComment(message, false);
}
}
49 changes: 49 additions & 0 deletions src/exporting/discordWebhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import minimist from "minimist";

Check failure on line 1 in src/exporting/discordWebhook.js

View workflow job for this annotation

GitHub Actions / Run linters

Expected linebreaks to be 'LF' but found 'CRLF'
import { WebhookClient } from "discord.js";

Check failure on line 2 in src/exporting/discordWebhook.js

View workflow job for this annotation

GitHub Actions / Run linters

Expected linebreaks to be 'LF' but found 'CRLF'
import BaseExporter from "./baseExporter.js";

Check failure on line 3 in src/exporting/discordWebhook.js

View workflow job for this annotation

GitHub Actions / Run linters

Expected linebreaks to be 'LF' but found 'CRLF'

Check failure on line 4 in src/exporting/discordWebhook.js

View workflow job for this annotation

GitHub Actions / Run linters

Insert `␍`
const argv = minimist(process.argv.slice(2));
const { discordUsername } = argv;
const avatarURL = argv.discordAvatarUrl;

export default class DiscordWebhookExporter extends BaseExporter {
webhookClient = null;

discordMessage = null;

constructor() {
super();
}

isEnabled() {
if (!argv.discordWebHookUrl) return false;
return true;
}

async send(content, isPeriodic = true) {
if (!this.webhookClient)
this.webhookClient = new WebhookClient({ url: argv.discordWebHookUrl });

try {
const data = {
username: discordUsername + (isPeriodic ? " - Periodic " : " - Final"),
avatarURL,
content,
};

if (isPeriodic) {
if (!this.discordMessage)
this.discordMessage = await this.webhookClient.send(data);
else {
data.content = `${this.discordMessage.content}\n${content}`;
this.discordMessage.content = data.content;
await this.webhookClient.editMessage(this.discordMessage.id, data);
}
} else {
await this.webhookClient.send(data);
}
} catch (error) {
console.log("Failed to send discord webhook", error);
}
}
}
77 changes: 77 additions & 0 deletions src/exporting/githubComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import minimist from "minimist";
import fs from "fs";
import { Octokit } from "@octokit/core";
import BaseExporter from "./baseExporter.js";

const argv = minimist(process.argv.slice(2));
const {githubOwner} = argv;
const {githubRepo} = argv;

export default class GithubCommentExporter extends BaseExporter {
githubCommit = null;

commitName = "none";

octokit = null;

constructor() {
super();

if (process.env.GITHUB_EVENT_PATH) {
const file = fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8");
this.githubCommit = JSON.parse(file);

const object = this.githubCommit;
if (object.commits) {
this.commitName = object.commits[object.commits.length - 1].message;
}
}
}

isEnabled() {
return !(
!argv.githubAuth ||
!this.githubCommit ||
!argv.githubOwner ||
!argv.githubRepo
}

async send(content, isPeriodic = true) {
if (!this.octokit) {
this.octokit = new Octokit({
auth: argv.githubAuth,
});
}

const baseComment = "**BOT COMMENT!**\n\n";
const url =
isPeriodic && this.githubCommit
? "PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}"
: "POST /repos/{owner}/{repo}/issues/{issue_number}/comments";
let body = "";
if (!isPeriodic) {
body = baseComment + content;
} else if (this.githubCommit) {
body = `${this.githubCommit.data.body}\n${content}`;
} else {
body = baseComment + content;
}

try {
const response = await this.octokit.request(url, {
owner: githubOwner,
repo: githubRepo,
issue_number: this.githubCommit.number,
body,
comment_id: this.githubCommit ? this.githubCommit.data.id : null,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});

if (isPeriodic && !this.githubCommit) this.githubCommit = response;
} catch (error) {
console.log("Failed to send github comment", error);
}
}
}
34 changes: 34 additions & 0 deletions src/exporting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import GithubCommentExporter from "./githubComment.js";
import DiscordWebhookExporter from "./discordWebhook.js";

export default class Exporter {
githubCommentExporter = new GithubCommentExporter();

discordWebhookExporter = new DiscordWebhookExporter();

async sendFinalResult(milestones, fails, status, lastTickNumber, startTime) {
if (this.githubCommentExporter.isEnabled()) {
const message = await this.githubCommentExporter.getFinalResultMessage(milestones, fails, status, lastTickNumber, startTime);
);
await this.githubCommentExporter.send(message, false);
}
if (this.discordWebhookExporter.isEnabled()) {
const message = await this.discordWebhookExporter.getFinalResultMessage(milestones, fails, status, lastTickNumber, startTime);
);
await this.discordWebhookExporter.send(message, false);
}
}

async sendPeriodicResult(gameTime, milestone, startTime) {
if (this.githubCommentExporter.isEnabled()) {
const message = this.githubCommentExporter.getPeriodicResultMessage(gameTime, milestone, startTime);
);
await this.githubCommentExporter.send(message, true);
}
if (this.discordWebhookExporter.isEnabled()) {
const message = this.discordWebhookExporter.getPeriodicResultMessage(gameTime, milestone, startTime);
);
await this.discordWebhookExporter.send(message, true);
}
}
}
Loading
Loading