From 109fd6a633a83b6dd360e131d3721fb7bde5624b Mon Sep 17 00:00:00 2001 From: BradBot_1 Date: Thu, 23 Feb 2023 20:49:38 +0000 Subject: [PATCH] FEAT: optional inserting message into readme --- src/Forward/Recipient.ts | 1 + src/Git/GitManager.ts | 11 ++++++----- src/index.ts | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Forward/Recipient.ts b/src/Forward/Recipient.ts index c853d1e..a6a7480 100644 --- a/src/Forward/Recipient.ts +++ b/src/Forward/Recipient.ts @@ -18,6 +18,7 @@ import { GitAuthor } from '../Git/GitAuthor'; export class Recipient { public readonly humanName: string; + public insertToReadme: boolean = false; public readonly url:string; public authorMap: Map = new Map(); diff --git a/src/Git/GitManager.ts b/src/Git/GitManager.ts index befeb21..4a3b2b0 100644 --- a/src/Git/GitManager.ts +++ b/src/Git/GitManager.ts @@ -18,7 +18,7 @@ import { simpleGit as Git } from 'simple-git'; import { GitAuthor } from './GitAuthor'; import { Recipient } from '../Forward/Recipient'; import { execSync } from 'child_process'; -import { writeFileSync } from 'fs' +import { writeFileSync, existsSync, readFileSync } from 'fs' export async function cloneRepo(repo: string, out: string = __dirname): Promise { try { @@ -83,8 +83,9 @@ export async function changeCommitAuthors(recipient: Recipient, out: string = __ } } -export async function handleForward(forward: Forward):Promise { - const git = Git().env('GIT_SSH_COMMAND', 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'); - await git.clone(forward.origin); - git.log() +export async function insertToReadme(projectName: string, projectLink: string, out: string = __dirname): Promise { + const git = Git(out); + var data: string = `> This was cloned from [${projectName}](${projectLink})`; + if (existsSync(out + "README.md")) data = data + readFileSync(out + "README.md").toString(); + writeFileSync(out + "README.md", data); } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 217df89..5a0a780 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ import { Forward } from './Forward/Forward'; import { createForward, getForwardByWebhook } from './Forward/ForwardManager'; import { createServer } from './Server/Server'; import { env, cwd } from 'process'; -import { cloneRepo, changeCommitAuthors, push } from './Git/GitManager'; +import { cloneRepo, changeCommitAuthors, push, insertToReadme } from './Git/GitManager'; import { rmSync, existsSync, readFileSync } from 'fs' var dataToLoad: string; @@ -56,6 +56,7 @@ for (const forwardData of parsedData) { continue; } const recipient = forward.createRecipient(recipientData.url, recipientData.humanName); + if (recipient.hasOwnProperty("modifyReadme")) recipient.insertToReadme = !!recipientData.modifyReadme; if (!recipientData.hasOwnProperty("authors")) { console.error("No authors provided for recipient: " + recipient.humanName||recipient.url); continue; @@ -95,6 +96,7 @@ createServer(parseInt(env["PORT"]||"3000"), async (webhookId: string, webhookDat for (const sendTo of forward.getRecipients()) { await cloneRepo(forward.origin, outputDir); await changeCommitAuthors(sendTo, outputDir); + if (sendTo.insertToReadme) await insertToReadme(webhookData.repository.full_name, webhookData.repository.html_url, outputDir); await push(sendTo.url, outputDir); rmSync(outputDir, { recursive: true,