Skip to content

Commit

Permalink
crud operations updated
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Aug 12, 2024
1 parent 7f54a43 commit aa8b9cf
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
36 changes: 30 additions & 6 deletions create_file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { listRepoRemote } = require("./list_repo");
const { readFileSync } = require("fs");
const { loadFile } = require("./read_file");

async function addFile(owner, repoName, destPath, content, message) {
async function addFile(owner, repoName, destPath, content, message, sha) {
const { Octokit } = await import("@octokit/rest");

const octakit = new Octokit({ auth: process.env.GITHUB_TOKEN });
Expand All @@ -18,22 +19,45 @@ async function addFile(owner, repoName, destPath, content, message) {
message: message,
committer: { name: "DARSAN", email: "[email protected]" },
author: { name: "DARSAN", email: "[email protected]" },
sha: sha,
});
}

async function main() {
const groupedRepolists = await listRepoRemote();

const content = readFileSync("consistent-desc.yaml", {
encoding: "base64",
const cresteemRME = readFileSync("crm-rme.md", {
encoding: "utf8",
});

const destPath = ".github/workflows/consistent-desc.yaml";
const commitMsg = "End-User meta WF added";
const darsanRME = readFileSync("d-rme.md", {
encoding: "utf8",
});

const destPath = "README.md";
const commitMsg = "Readme template appended";

Object.keys(groupedRepolists).forEach((username) => {
groupedRepolists[username].forEach((repoName) => {
addFile(username, repoName, destPath, content, commitMsg);
loadFile(username, repoName, destPath)
.then(([oldContent, sha]) => {
const newContent = username === "cresteem" ? cresteemRME : darsanRME;

const finalContent =
newContent +
"\n\n" +
Buffer.from(oldContent, "base64").toString("utf8");

addFile(
username,
repoName,
destPath,
Buffer.from(finalContent, "utf8").toString("base64"),
commitMsg,
sha
);
})
.catch(console.error);
});
});
}
Expand Down
19 changes: 19 additions & 0 deletions delete_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
async function delFile(owner, repoName, filePath, latestSha) {
const { Octokit } = await import("@octokit/rest");

const octakit = new Octokit({ auth: process.env.GITHUB_TOKEN });

const {
repos: { deleteFile },
} = octakit.rest;

await deleteFile({
owner: owner,
repo: repoName,
path: filePath,
message: filePath + " deleted",
sha: latestSha,
});
}

module.exports = { delFile };
25 changes: 25 additions & 0 deletions read_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
async function loadFile(owner, repoName, filePath) {
const { Octokit } = await import("@octokit/rest");

const octakit = new Octokit({ auth: process.env.GITHUB_TOKEN });

const {
repos: { getContent },
} = octakit.rest;

try {
const {
data: { content, sha },
} = await getContent({
owner: owner,
repo: repoName,
path: filePath,
});

return [content, sha];
} catch {
return [Buffer.from("").toString("base64"), null];
}
}

module.exports = { loadFile };

0 comments on commit aa8b9cf

Please sign in to comment.