Skip to content

Commit

Permalink
/backup upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 27, 2023
1 parent ea5e4cf commit 6805dbb
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 107 deletions.
2 changes: 1 addition & 1 deletion commands/bot/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const addDeleteButton = require("@helpers/addDeleteButton");
const settings = require("@resources/settings.json");
const strings = require("@resources/strings.json");

const { default: axios } = require("axios");
const axios = require("axios").default;

const { EmbedBuilder, SlashCommandBuilder } = require("discord.js");

Expand Down
25 changes: 19 additions & 6 deletions commands/submission/backup.js → commands/developer/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,34 @@ module.exports = {

await interaction.deferReply({ ephemeral: true });

const succeeded = await saveDB(
const { successfulPushes, failedPushes, commit } = await saveDB(
interaction.client,
`Manual backup executed by: ${interaction.user.displayName}`,
);

if (!succeeded) return warnUser(interaction, strings.command.backup_failed, true);
const url = `https://github.com/${settings.backup.git.org}/${settings.backup.git.repo}/${
commit ? `commit/${commit}` : `tree/${settings.backup.git.branch}`
}`;

return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setTitle("Database successfully backed up!")
.setURL(
`https://github.com/${settings.backup.git.org}/${settings.backup.git.repo}/tree/${settings.backup.git.branch}`,
.setTitle("Database backed up!")
.setURL(url)
.setDescription(
failedPushes.length
? "*Check developer logs for potential failure reasons!*"
: undefined,
)
.setColor(settings.colors.blue),
.addFields(
{ name: "Successful", value: successfulPushes.join("\n"), inline: true },
{ name: "Failed", value: failedPushes.join("\n"), inline: true },
)
.setColor(
successfulPushes.length > failedPushes.length
? settings.colors.blue
: settings.colors.red,
),
],
});
},
Expand Down
28 changes: 0 additions & 28 deletions commands/developer/hotfix.js

This file was deleted.

2 changes: 1 addition & 1 deletion functions/fetchSettings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { writeFile } = require("fs/promises");
const { join } = require("path");
const FETCH_SETTINGS = process.env.FETCH_SETTINGS.toLowerCase() == "true";
const { default: axios } = require("axios");
const axios = require("axios").default;

/**
* Download and apply remote settings file
Expand Down
83 changes: 27 additions & 56 deletions functions/pushToGitHub.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* ORIGINAL LINK: (Typescript)
* ORIGINAL LINK: (TypeScript)
* https://dev.to/lucis/how-to-push-files-programatically-to-a-repository-using-octokit-with-typescript-1nj0
*
*/

const glob = require("globby");
Expand All @@ -17,34 +16,21 @@ const { readFileSync } = require("fs");
* @param {string} branch branch name
* @param {string} commitMessage
* @param {string} localPath
* @returns {Promise<string>} sent commit SHA
*/
module.exports = async function pushToGitHub(org, repo, branch, commitMessage, localPath) {
const octo = new Octokit({
auth: process.env.GIT_TOKEN,
});

// Upload files to repo:
await uploadToRepo(octo, localPath, org, repo, branch, commitMessage);
};

/**
* Upload files to repository
* @param {Octokit} octo
* @param {string} coursePath path from where file are uploaded
* @param {string} org GitHub organisation
* @param {string} repo GitHub repository of the organisation
* @param {string} branch GitHub branch of the repository
* @param {string} commitMessage Message of commit
*/
const uploadToRepo = async (octo, coursePath, org, repo, branch, commitMessage) => {
const currentCommit = await getCurrentCommit(octo, org, repo, branch);
const filesPaths = await glob(coursePath);
const filesPaths = await glob(localPath);
if (!filesPaths) return;

// suspected problem on createBlobForFile which fails and give undefined
const filesBlobs = await Promise.all(filesPaths.map(createBlobForFile(octo, org, repo)));
const pathsForBlobs = filesPaths.map((fullPath) =>
normalize(relative(coursePath, fullPath)).replace(/\\/g, "/"),
normalize(relative(localPath, fullPath)).replace(/\\/g, "/"),
);
const newTree = await createNewTree(
octo,
Expand All @@ -54,6 +40,7 @@ const uploadToRepo = async (octo, coursePath, org, repo, branch, commitMessage)
pathsForBlobs,
currentCommit.treeSha,
);

const newCommit = await createNewCommit(
octo,
org,
Expand All @@ -62,7 +49,9 @@ const uploadToRepo = async (octo, coursePath, org, repo, branch, commitMessage)
newTree.sha,
currentCommit.commitSha,
);

await setBranchToCommit(octo, org, repo, branch, newCommit.sha);
return newCommit.sha;
};

/**
Expand Down Expand Up @@ -95,14 +84,14 @@ const getCurrentCommit = async (octo, org, repo, branch) => {
* Get file as utf8 file
* Notice that readFile's UTF8 is typed differently from Github's UTF-8
* @param {string} filePath
* @returns an utf8 file
* @returns {string} a utf8 file
*/
const getFileAsUTF8 = (filePath) => readFileSync(filePath, { encoding: "utf-8" });

/**
* Get file as binary file (used for .png files)
* @param {string} filePath
* @returns a base64 file
* @returns {string} a base64 file
*/
const getFileAsBinary = (filePath) => readFileSync(filePath, { encoding: "base64" });

Expand All @@ -111,34 +100,24 @@ const getFileAsBinary = (filePath) => readFileSync(filePath, { encoding: "base64
* @param {Octokit} octo
* @param {string} org Github organisation
* @param {string} repo Github repository of the organisation
* @returns data of the blob
* @returns {Function} data of the blob
*/
const createBlobForFile = (octo, org, repo) => async (filePath) => {
/** @type {string} */
let content;
let blobData;
let encoding = "utf-8";

if (filePath.endsWith(".png")) {
content = getFileAsBinary(filePath);
blobData = await octo.git.createBlob({
owner: org,
repo,
content,
encoding: "base64",
});
} else {
content = getFileAsUTF8(filePath);
blobData = await octo.git
.createBlob({
owner: org,
repo,
content,
encoding: "utf-8",
})
.catch((err) => {
console.error(err);
return Promise.reject(err);
});
}
encoding = "base64";
} else content = getFileAsUTF8(filePath);

const blobData = await octo.git.createBlob({
owner: org,
repo,
content,
encoding,
});

return blobData.data;
};
Expand All @@ -147,29 +126,21 @@ const createBlobForFile = (octo, org, repo) => async (filePath) => {
* @param {Octokit} octo
* @param {string} owner GitHub organisation
* @param {string} repo GitHub repository of the organisation
* @param {Blob} blobs
* @param {Blob[]} blobs
* @param {string[]} paths
* @param {string} parentTreeSha
* @returns data : {owner, repo, tree, base_tree: parentTreeSha }
*/
const createNewTree = async (
octo,
owner,
repo,
blobs = Octokit.GitCreateBlobResponse,
paths,
parentTreeSha,
) => {
const createNewTree = async (octo, owner, repo, blobs, paths, parentTreeSha) => {
const tree = blobs.map(({ sha }, index) => ({
path: paths[index],
mode: `100644`,
type: `blob`,
sha: sha,
sha,
})); //as Octokit.GitCreateTreeParamsTree()
const { data } = await octo.git.createTree({
owner: owner,
repo: repo,
tree: tree,
owner,
repo,
tree,
base_tree: parentTreeSha,
});

Expand Down
31 changes: 21 additions & 10 deletions functions/saveDB.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
const settings = require("@resources/settings.json");

const DEV = process.env.DEV.toLowerCase() == "true";
const DEBUG = process.env.DEBUG.toLowerCase() == "true";

const { mkdirSync, writeFileSync } = require("fs");

const pushToGitHub = require("@functions/pushToGitHub");
const { join } = require("path");
const { default: axios } = require("axios");
const axios = require("axios").default;
const devLogger = require("@helpers/devLogger");

/**
* push all raw api collections to github
* Push all raw api collections to github
* @author Evorp, Juknum
* @param {import("discord.js").Client} client
* @param {string} commitMessage
* @param {{org?: string, repo?: string, branch?: string}} params configure where to backup to
* @returns {Promise<boolean>} if every collection could be backed up
* @returns {Promise<{ successfulPushes: string[], failedPushes: string[], commit?: string }>}
*/
module.exports = async function saveDB(client, commitMessage = "Daily Backup", params = {}) {
if (!params.org) params.org = settings.backup.git.org;
Expand All @@ -25,7 +26,8 @@ module.exports = async function saveDB(client, commitMessage = "Daily Backup", p
const folderPath = join(process.cwd(), "json", "database");
mkdirSync(folderPath, { recursive: true });

let successfulPushes = [];
const successfulPushes = [];
const failedPushes = [];
for (const [filename, url] of Object.entries(settings.backup.urls)) {
try {
const fetched = (
Expand All @@ -44,8 +46,9 @@ module.exports = async function saveDB(client, commitMessage = "Daily Backup", p

successfulPushes.push(filename);
} catch (err) {
failedPushes.push(filename);
if (DEBUG) console.error(err?.response?.data ?? err);
else
if (!DEV)
devLogger(client, JSON.stringify(err?.response?.data ?? err), {
codeBlocks: "json",
title: `Failed to backup collection "${filename}"`,
Expand All @@ -54,11 +57,19 @@ module.exports = async function saveDB(client, commitMessage = "Daily Backup", p
}

if (DEBUG) console.log(`Downloaded database files: ${successfulPushes}`);
try {
await pushToGitHub(params.org, params.repo, params.branch, commitMessage, "./json/");
} catch {
const commit = await pushToGitHub(
params.org,
params.repo,
params.branch,
commitMessage,
"./json/",
).catch(() => {
if (DEBUG) console.log(`Branch ${params.branch} doesn't exist for repository ${params.repo}!`);
}
});

return successfulPushes.length >= Object.keys(settings.backup.urls).length;
return {
successfulPushes,
failedPushes,
commit,
};
};
2 changes: 1 addition & 1 deletion functions/submission/parseResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const devLogger = require("@helpers/devLogger");
const getPackByChannel = require("@submission/utility/getPackByChannel");

const { mkdirSync, writeFile } = require("fs");
const { default: axios } = require("axios");
const axios = require("axios").default;

/**
* @typedef MappedTexture
Expand Down
2 changes: 1 addition & 1 deletion functions/submission/submitTexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cancelSubmission = require("@submission/utility/cancelSubmission");
const getAuthors = require("@submission/utility/getAuthors");
const minecraftSorter = require("@helpers/minecraftSorter");

const { default: axios } = require("axios");
const axios = require("axios").default;

/**
* Get submission information and create embed
Expand Down
2 changes: 1 addition & 1 deletion functions/submission/utility/choiceEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
StringSelectMenuBuilder,
SelectMenuInteraction,
} = require("discord.js");
const { default: axios } = require("axios");
const axios = require("axios").default;

const DEBUG = process.env.DEBUG.toLowerCase() == "true";

Expand Down
2 changes: 1 addition & 1 deletion functions/submission/utility/generateComparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const settings = require("@resources/settings.json");
const stitch = require("@images/stitch");
const { magnifyToAttachment, magnify } = require("@images/magnify");
const { loadImage } = require("@napi-rs/canvas");
const { default: axios } = require("axios");
const axios = require("axios").default;
const minecraftSorter = require("@helpers/minecraftSorter");
const animate = require("@images/animate");
const { AttachmentBuilder } = require("discord.js");
Expand Down
2 changes: 1 addition & 1 deletion functions/submission/utility/getAuthors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { default: axios } = require("axios");
const axios = require("axios").default;

/**
* Detects co-authors from pings and curly bracket syntax in a given message
Expand Down

0 comments on commit 6805dbb

Please sign in to comment.