Skip to content

Commit

Permalink
feat(remote/github): add dry run option
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube authored and bzlibby committed Aug 14, 2020
1 parent 0f41020 commit 4460c98
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 44 deletions.
11 changes: 11 additions & 0 deletions docs/api/cautious-journey.githubremote.forreal.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/api/cautious-journey.githubremote.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/api/cautious-journey.remoteoptions.data.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions docs/api/cautious-journey.remoteoptions.dryrun.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions docs/api/cautious-journey.remoteoptions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/api/cautious-journey.remoteoptions.type.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/config/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Parser<TData> {

export interface ParsedArgs {
config: string;
dryrun: boolean;
remote: string;
}

Expand All @@ -38,6 +39,12 @@ export function createParser(modeset: Modeback): Parser<ParsedArgs> {
demand: true,
type: 'string',
},
dryrun: {
alias: ['d'],
default: true,
demand: false,
type: 'boolean',
},
remote: {
alias: ['r'],
demand: true,
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export async function main(argv: Array<string>): Promise<number> {
});

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
Expand Down
104 changes: 63 additions & 41 deletions src/remote/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -81,33 +88,39 @@ export class GithubRemote implements Remote {

public async listIssues(options: ProjectQuery): Promise<Array<IssueUpdate>> {
const path = await this.splitProject(options.project);
const repo = await mustExist(this.request).issues.listForRepo(path);

const issues: Array<IssueUpdate> = [];
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;
}

public async listLabels(options: ProjectQuery): Promise<Array<LabelUpdate>> {
const path = await this.splitProject(options.project);
const repo = await mustExist(this.request).issues.listLabelsForRepo(path);

const labels: Array<LabelUpdate> = [];
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;
Expand All @@ -119,19 +132,28 @@ export class GithubRemote implements Remote {

public async updateLabel(options: LabelUpdate): Promise<LabelUpdate> {
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;
}
}
12 changes: 12 additions & 0 deletions src/remote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;

/**
* If set, do not make any real changes.
*/
dryrun: boolean;

/**
* Remote class/type name.
*/
type: string;
}

Expand Down

0 comments on commit 4460c98

Please sign in to comment.