patch hooks for commits

This commit is contained in:
Laura Liberda 2021-02-24 01:33:32 +01:00
parent 9cc044e6e0
commit 65e47ff5e6
3 changed files with 32 additions and 7 deletions

View file

@ -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,
});

View file

@ -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<string>)
: 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];

View file

@ -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;
}>;