copykitku/src/cli/replicate.ts
2021-02-15 00:36:13 +01:00

41 lines
1.6 KiB
TypeScript

import { Command, flags } from '@oclif/command';
import assert from 'assert';
import Copykitku from '../copykitku';
import { ENTITY_TYPE } from '../types';
import { parsePath } from '../utils';
export default class Replicate extends Command {
static description = 'Replicate issues/MRs between repositories';
static flags = {
help: flags.help({ char: 'h' }),
source: flags.string({ char: 's', required: true }),
dest: flags.string({ char: 'd', required: true }),
};
static args = [];
async run() {
const { flags } = this.parse(Replicate);
const sourcePath = parsePath(flags.source);
const destPath = parsePath(flags.dest);
assert(sourcePath.entity === ENTITY_TYPE.ISSUE, 'Only issues are supported now');
const cc = new Copykitku();
await cc.initialize();
const sourceVendor = cc.vendorManagers.find((v) => v.vendor.domain === sourcePath.domain);
assert(sourceVendor, 'Source vendor not found in config');
const destVendor = cc.vendorManagers.find((v) => v.vendor.domain === destPath.domain);
assert(destVendor, 'Destination vendor not found in config');
const sourceRepo = await sourceVendor.getRepo(sourcePath.path);
const destRepo = await destVendor.getRepo(destPath.path);
const sourceEntity = await sourceRepo.getIssue(sourcePath.entityID);
sourceEntity.content += `\n\nReplicated from ${sourceEntity.url} with [Copykitku](https://git.sakamoto.pl/laudompat/copykitku)`;
const replicatedEntity = await destRepo.replicateIssue(sourceEntity);
console.log(`Replicated successfully: ${replicatedEntity.url}`);
}
}