generated from darsan-in/Template-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
|
@@ -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); | ||
}); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |