copykitku/src/cli/replicate.ts

41 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-02-01 11:44:18 +01:00
import { Command, flags } from '@oclif/command';
import assert from 'assert';
2021-02-14 22:24:01 +01:00
import Copykitku from '../copykitku';
2021-02-01 11:44:18 +01:00
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');
2021-02-14 22:24:01 +01:00
const cc = new Copykitku();
2021-02-01 11:44:18 +01:00
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);
2021-02-14 22:24:01 +01:00
sourceEntity.content += `\n\nReplicated from ${sourceEntity.url} with [Copykitku](https://git.sakamoto.pl/laudompat/copykitku)`;
2021-02-01 11:44:18 +01:00
const replicatedEntity = await destRepo.replicateIssue(sourceEntity);
2021-02-01 13:59:57 +01:00
console.log(`Replicated successfully: ${replicatedEntity.url}`);
2021-02-01 11:44:18 +01:00
}
}