From 65e47ff5e6d28fa5cc3ee4cb9a61a402945acd05 Mon Sep 17 00:00:00 2001 From: Laura Liberda Date: Wed, 24 Feb 2021 01:33:32 +0100 Subject: [PATCH] patch hooks for commits --- src/cli/replicate.ts | 6 +++++- src/copykitku.ts | 32 ++++++++++++++++++++++++++------ src/types.ts | 1 + 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cli/replicate.ts b/src/cli/replicate.ts index 6139665..4bacc3b 100644 --- a/src/cli/replicate.ts +++ b/src/cli/replicate.ts @@ -25,6 +25,9 @@ export default class Replicate extends Command { description: 'branch to which the MR should target', default: 'master', // TODO: check in the repository instead of taking a wild guess }), + patchHook: flags.string({ + description: 'node.js file to patch the patches', + }), }; static args = [ @@ -46,7 +49,7 @@ export default class Replicate extends Command { const proj = getProjectConfig(); - const { destBranch, doNotCommit, remote, targetBranch } = { ...proj, ...flags }; + const { destBranch, doNotCommit, remote, targetBranch, patchHook } = { ...proj, ...flags }; const { source, dest } = { ...proj, @@ -91,6 +94,7 @@ export default class Replicate extends Command { const repl = await kitku.replicateMergeRequest(sourceMR, destRepo, { destBranch, doNotCommit, + patchHook, remote, targetBranch, }); diff --git a/src/copykitku.ts b/src/copykitku.ts index 53ccdf0..92c8cfd 100644 --- a/src/copykitku.ts +++ b/src/copykitku.ts @@ -62,15 +62,20 @@ export default class Copykitku { destBranch?: string; doNotCommit?: boolean | null; doNotPush?: boolean | null; + patchHook?: string | null; remote: string; targetBranch: string; }, ) { const destBranch = opts.destBranch || `${sourceMR.repo.owner.username}/${sourceMR.repo.name}/mr-${sourceMR.id}`; - const { doNotCommit, doNotPush, remote, targetBranch } = opts; + const { doNotCommit, doNotPush, remote, targetBranch, patchHook } = opts; - await this.replicateCommits(sourceMR.commits, destination, { destBranch, doNotCommit }); + await this.replicateCommits(sourceMR.commits, destination, { + destBranch, + doNotCommit, + patchHook, + }); if (doNotCommit !== true && doNotPush !== true) { const git = simpleGit({ baseDir: process.cwd() }); @@ -91,11 +96,21 @@ export default class Copykitku { public async replicateCommits( sourceCommit: Commit | Commit[], destination: RepoManager, - opts: { destBranch: string; doNotCommit?: boolean | null }, + opts: { + destBranch: string; + doNotCommit?: boolean | null; + patchHook?: string | null; + }, ) { - const { destBranch, doNotCommit } = opts; + const { destBranch, doNotCommit, patchHook } = opts; const commits = Array.isArray(sourceCommit) ? sourceCommit : [sourceCommit]; + const patchHookCall = patchHook + ? (require(path.join(process.cwd(), ...patchHook.split('/'))) as ( + patchContent: string, + ) => string | Promise) + : null; + // saving patch files to /tmp or equivalent const tmpPath = fs.mkdtempSync(path.join(os.tmpdir(), 'copykitku-')); const patchFiles = await Promise.all( @@ -108,9 +123,14 @@ export default class Copykitku { tmpPath, `${index.toString().padStart(4, '0')}-${commit.id}.patch`, ); - const content = await commit.patchContent(); - await fs.writeFile(filename, content); commit.patchFile = filename; + let content = await commit.patchContent(); + + if (patchHookCall) { + content = await patchHookCall(content); + } + + await fs.writeFile(filename, content); const [, from] = /^From: ([^\n]+)$/im.exec(content) || [, null]; const [, date] = /^Date: ([^\n]+)$/im.exec(content) || [, null]; diff --git a/src/types.ts b/src/types.ts index 8fe3bbe..419999d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -164,4 +164,5 @@ export interface Actor { export type CopykitkuProjectConfig = Partial<{ /** destination repository url (like "https://git.sakamoto.pl/laudompat/copykitku") */ dest: string; + patchHook: string; }>;