From 4460c98e9b53726716dd1ac4308cd801f7027fb8 Mon Sep 17 00:00:00 2001 From: ssube Date: Thu, 13 Aug 2020 19:31:51 -0500 Subject: [PATCH] feat(remote/github): add dry run option --- .../cautious-journey.githubremote.forreal.md | 11 ++ docs/api/cautious-journey.githubremote.md | 1 + .../cautious-journey.remoteoptions.data.md | 2 + .../cautious-journey.remoteoptions.dryrun.md | 13 +++ docs/api/cautious-journey.remoteoptions.md | 5 +- .../cautious-journey.remoteoptions.type.md | 2 + src/config/args.ts | 7 ++ src/main.ts | 5 +- src/remote/github.ts | 104 +++++++++++------- src/remote/index.ts | 12 ++ 10 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 docs/api/cautious-journey.githubremote.forreal.md create mode 100644 docs/api/cautious-journey.remoteoptions.dryrun.md diff --git a/docs/api/cautious-journey.githubremote.forreal.md b/docs/api/cautious-journey.githubremote.forreal.md new file mode 100644 index 0000000..eb5bcc5 --- /dev/null +++ b/docs/api/cautious-journey.githubremote.forreal.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [cautious-journey](./cautious-journey.md) > [GithubRemote](./cautious-journey.githubremote.md) > [forReal](./cautious-journey.githubremote.forreal.md) + +## GithubRemote.forReal property + +Signature: + +```typescript +protected get forReal(): boolean; +``` diff --git a/docs/api/cautious-journey.githubremote.md b/docs/api/cautious-journey.githubremote.md index 15ce5d4..6ed8e90 100644 --- a/docs/api/cautious-journey.githubremote.md +++ b/docs/api/cautious-journey.githubremote.md @@ -23,6 +23,7 @@ export declare class GithubRemote implements Remote | Property | Modifiers | Type | Description | | --- | --- | --- | --- | +| [forReal](./cautious-journey.githubremote.forreal.md) | | boolean | | | [options](./cautious-journey.githubremote.options.md) | | [RemoteOptions](./cautious-journey.remoteoptions.md) | | | [request](./cautious-journey.githubremote.request.md) | | Octokit | | diff --git a/docs/api/cautious-journey.remoteoptions.data.md b/docs/api/cautious-journey.remoteoptions.data.md index 19082c8..2662cd8 100644 --- a/docs/api/cautious-journey.remoteoptions.data.md +++ b/docs/api/cautious-journey.remoteoptions.data.md @@ -4,6 +4,8 @@ ## RemoteOptions.data property +Arbitrary key-value data for this remote, usually credentials and base URLs. + Signature: ```typescript diff --git a/docs/api/cautious-journey.remoteoptions.dryrun.md b/docs/api/cautious-journey.remoteoptions.dryrun.md new file mode 100644 index 0000000..720b761 --- /dev/null +++ b/docs/api/cautious-journey.remoteoptions.dryrun.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [cautious-journey](./cautious-journey.md) > [RemoteOptions](./cautious-journey.remoteoptions.md) > [dryrun](./cautious-journey.remoteoptions.dryrun.md) + +## RemoteOptions.dryrun property + +If set, do not make any real changes. + +Signature: + +```typescript +dryrun: boolean; +``` diff --git a/docs/api/cautious-journey.remoteoptions.md b/docs/api/cautious-journey.remoteoptions.md index ebbdd8f..1a14cdd 100644 --- a/docs/api/cautious-journey.remoteoptions.md +++ b/docs/api/cautious-journey.remoteoptions.md @@ -14,6 +14,7 @@ export interface RemoteOptions | Property | Type | Description | | --- | --- | --- | -| [data](./cautious-journey.remoteoptions.data.md) | Record<string, string> | | -| [type](./cautious-journey.remoteoptions.type.md) | string | | +| [data](./cautious-journey.remoteoptions.data.md) | Record<string, string> | Arbitrary key-value data for this remote, usually credentials and base URLs. | +| [dryrun](./cautious-journey.remoteoptions.dryrun.md) | boolean | If set, do not make any real changes. | +| [type](./cautious-journey.remoteoptions.type.md) | string | Remote class/type name. | diff --git a/docs/api/cautious-journey.remoteoptions.type.md b/docs/api/cautious-journey.remoteoptions.type.md index f857c40..4d56aee 100644 --- a/docs/api/cautious-journey.remoteoptions.type.md +++ b/docs/api/cautious-journey.remoteoptions.type.md @@ -4,6 +4,8 @@ ## RemoteOptions.type property +Remote class/type name. + Signature: ```typescript diff --git a/src/config/args.ts b/src/config/args.ts index 9599b25..ae7b7b6 100644 --- a/src/config/args.ts +++ b/src/config/args.ts @@ -14,6 +14,7 @@ interface Parser { export interface ParsedArgs { config: string; + dryrun: boolean; remote: string; } @@ -38,6 +39,12 @@ export function createParser(modeset: Modeback): Parser { demand: true, type: 'string', }, + dryrun: { + alias: ['d'], + default: true, + demand: false, + type: 'boolean', + }, remote: { alias: ['r'], demand: true, diff --git a/src/main.ts b/src/main.ts index 150c55c..dac3477 100644 --- a/src/main.ts +++ b/src/main.ts @@ -57,7 +57,10 @@ export async function main(argv: Array): Promise { }); for (const project of config.projects) { - const remote = new GithubRemote(project.remote); + const remote = new GithubRemote({ + ...project.remote, + dryrun: args.dryrun, + }); await remote.connect(); // mode switch diff --git a/src/remote/github.ts b/src/remote/github.ts index 86b7fa0..4c0f790 100644 --- a/src/remote/github.ts +++ b/src/remote/github.ts @@ -13,6 +13,9 @@ export class GithubRemote implements Remote { constructor(options: RemoteOptions) { this.options = options; + + /* eslint-disable-next-line */ + console.log('options:', options); } public async connect() { @@ -46,13 +49,15 @@ export class GithubRemote implements Remote { // TODO: check if the label already exists - await mustExist(this.request).issues.createLabel({ - color: options.color, - description: options.desc, - name: options.name, - owner: path.owner, - repo: path.repo, - }); + if (this.forReal) { + await mustExist(this.request).issues.createLabel({ + color: options.color, + description: options.desc, + name: options.name, + owner: path.owner, + repo: path.repo, + }); + } return options; } @@ -62,11 +67,13 @@ export class GithubRemote implements Remote { // TODO: check if the label is in use - await mustExist(this.request).issues.deleteLabel({ - name: options.name, - owner: path.owner, - repo: path.repo, - }); + if (this.forReal) { + await mustExist(this.request).issues.deleteLabel({ + name: options.name, + owner: path.owner, + repo: path.repo, + }); + } return options; } @@ -81,16 +88,19 @@ export class GithubRemote implements Remote { public async listIssues(options: ProjectQuery): Promise> { const path = await this.splitProject(options.project); - const repo = await mustExist(this.request).issues.listForRepo(path); const issues: Array = []; - for (const issue of repo.data) { - issues.push({ - issue: issue.id.toString(10), - labels: issue.labels.map((l) => l.name), - name: issue.title, - project: options.project, - }); + + if (this.forReal) { + const repo = await mustExist(this.request).issues.listForRepo(path); + for (const issue of repo.data) { + issues.push({ + issue: issue.id.toString(10), + labels: issue.labels.map((l) => l.name), + name: issue.title, + project: options.project, + }); + } } return issues; @@ -98,16 +108,19 @@ export class GithubRemote implements Remote { public async listLabels(options: ProjectQuery): Promise> { const path = await this.splitProject(options.project); - const repo = await mustExist(this.request).issues.listLabelsForRepo(path); const labels: Array = []; - for (const label of repo.data) { - labels.push({ - color: label.color, - desc: label.description, - name: label.name, - project: options.project, - }); + + if (this.forReal) { + const repo = await mustExist(this.request).issues.listLabelsForRepo(path); + for (const label of repo.data) { + labels.push({ + color: label.color, + desc: label.description, + name: label.name, + project: options.project, + }); + } } return labels; @@ -119,19 +132,28 @@ export class GithubRemote implements Remote { public async updateLabel(options: LabelUpdate): Promise { const path = await this.splitProject(options.project); - const data = await mustExist(this.request).issues.updateLabel({ - color: options.color, - description: options.desc, - name: options.name, - owner: path.owner, - repo: path.repo, - }); - return { - color: data.data.color, - desc: data.data.description, - name: data.data.name, - project: options.project, - }; + if (this.forReal) { + const data = await mustExist(this.request).issues.updateLabel({ + color: options.color, + description: options.desc, + name: options.name, + owner: path.owner, + repo: path.repo, + }); + + return { + color: data.data.color, + desc: data.data.description, + name: data.data.name, + project: options.project, + }; + } else { + return options; + } + } + + protected get forReal(): boolean { + return !this.options.dryrun; } } diff --git a/src/remote/index.ts b/src/remote/index.ts index 0760ba0..7dba3b2 100644 --- a/src/remote/index.ts +++ b/src/remote/index.ts @@ -25,7 +25,19 @@ export interface LabelUpdate extends LabelQuery { } export interface RemoteOptions { + /** + * Arbitrary key-value data for this remote, usually credentials and base URLs. + */ data: Record; + + /** + * If set, do not make any real changes. + */ + dryrun: boolean; + + /** + * Remote class/type name. + */ type: string; }