1
0
Fork 0

feat(remote/github): implement label create, delete

This commit is contained in:
ssube 2020-08-13 18:44:13 -05:00 committed by Sean Sube
parent c7581df13a
commit abd7ac5295
11 changed files with 106 additions and 26 deletions

View File

@ -7,8 +7,15 @@
<b>Signature:</b>
```typescript
createLabel(): Promise<LabelUpdate>;
createLabel(options: LabelUpdate): Promise<LabelUpdate>;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | LabelUpdate | |
<b>Returns:</b>
Promise&lt;LabelUpdate&gt;

View File

@ -7,9 +7,16 @@
<b>Signature:</b>
```typescript
deleteLabel(): Promise<LabelUpdate>;
deleteLabel(options: LabelQuery): Promise<LabelQuery>;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | LabelQuery | |
<b>Returns:</b>
Promise&lt;LabelUpdate&gt;
Promise&lt;LabelQuery&gt;

View File

@ -32,8 +32,8 @@ export declare class GithubRemote implements Remote
| --- | --- | --- |
| [connect()](./cautious-journey.githubremote.connect.md) | | |
| [createComment()](./cautious-journey.githubremote.createcomment.md) | | |
| [createLabel()](./cautious-journey.githubremote.createlabel.md) | | |
| [deleteLabel()](./cautious-journey.githubremote.deletelabel.md) | | |
| [createLabel(options)](./cautious-journey.githubremote.createlabel.md) | | |
| [deleteLabel(options)](./cautious-journey.githubremote.deletelabel.md) | | |
| [getIssue()](./cautious-journey.githubremote.getissue.md) | | |
| [getLabel()](./cautious-journey.githubremote.getlabel.md) | | |
| [listIssues(options)](./cautious-journey.githubremote.listissues.md) | | |

View File

@ -9,14 +9,14 @@ Create a new label.
<b>Signature:</b>
```typescript
createLabel(options: LabelQuery): Promise<LabelUpdate>;
createLabel(options: LabelUpdate): Promise<LabelUpdate>;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| options | LabelQuery | |
| options | LabelUpdate | |
<b>Returns:</b>

View File

@ -9,7 +9,7 @@ Delete an existing label.
<b>Signature:</b>
```typescript
deleteLabel(options: LabelQuery): Promise<LabelUpdate>;
deleteLabel(options: LabelQuery): Promise<LabelQuery>;
```
## Parameters
@ -20,5 +20,5 @@ deleteLabel(options: LabelQuery): Promise<LabelUpdate>;
<b>Returns:</b>
Promise&lt;LabelUpdate&gt;
Promise&lt;LabelQuery&gt;

View File

@ -88,7 +88,7 @@
},
"husky": {
"hooks": {
"pre-commit": "make ci"
"pre-commit": "make"
}
},
"dependencies": {

View File

@ -87,3 +87,11 @@ export function getLabelNames(flags: Array<FlagLabel>, states: Array<StateLabel>
return new Set(labels);
}
export function splitName(name: string): Array<string> {
return name.split('/');
}
export function valueName(state: StateLabel, value: StateValue): string {
return `${state.name}/${value.name}`;
}

View File

@ -2,7 +2,7 @@ import { mustExist, NotImplementedError } from '@apextoaster/js-utils';
import { createAppAuth } from '@octokit/auth-app';
import { Octokit } from '@octokit/rest';
import { IssueUpdate, LabelUpdate, ProjectQuery, Remote, RemoteOptions } from '.';
import { IssueUpdate, LabelQuery, LabelUpdate, ProjectQuery, Remote, RemoteOptions } from '.';
/**
* Github/Octokit API implementation of the `Remote` contract.
@ -41,12 +41,34 @@ export class GithubRemote implements Remote {
throw new NotImplementedError();
}
public async createLabel(): Promise<LabelUpdate> {
throw new NotImplementedError();
public async createLabel(options: LabelUpdate): Promise<LabelUpdate> {
const path = await this.splitProject(options.project);
// 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,
});
return options;
}
public async deleteLabel(): Promise<LabelUpdate> {
throw new NotImplementedError();
public async deleteLabel(options: LabelQuery): Promise<LabelQuery> {
const path = await this.splitProject(options.project);
// TODO: check if the label is in use
await mustExist(this.request).issues.deleteLabel({
name: options.name,
owner: path.owner,
repo: path.repo,
});
return options;
}
public async getIssue(): Promise<Array<IssueUpdate>> {

View File

@ -41,12 +41,12 @@ export interface Remote {
/**
* Create a new label.
*/
createLabel(options: LabelQuery): Promise<LabelUpdate>;
createLabel(options: LabelUpdate): Promise<LabelUpdate>;
/**
* Delete an existing label.
*/
deleteLabel(options: LabelQuery): Promise<LabelUpdate>;
deleteLabel(options: LabelQuery): Promise<LabelQuery>;
/**
* Get details of a single issue.

View File

@ -1,7 +1,8 @@
import { doesExist, mustExist, Optional } from '@apextoaster/js-utils';
import { doesExist, mustExist } from '@apextoaster/js-utils';
import { FlagLabel, getLabelNames, StateLabel } from './labels';
import { FlagLabel, getLabelNames, StateLabel, valueName } from './labels';
import { LabelUpdate, Remote } from './remote';
import { defaultTo } from './utils';
// TODO: turn this back on/remove the disable pragma
/* eslint-disable no-console */
@ -45,10 +46,15 @@ export async function syncLabels(options: SyncOptions): Promise<unknown> {
await syncSingleLabel(options, data);
} else {
console.log('remove label:', label);
await options.remote.deleteLabel({
name: label,
project: options.project,
});
}
} else {
if (expected) {
console.log('create label:', label);
await createLabel(options, label);
} else {
// skip
}
@ -58,11 +64,32 @@ export async function syncLabels(options: SyncOptions): Promise<unknown> {
return undefined;
}
function defaultTo<T>(a: Optional<T>, b: T): T {
if (doesExist(a)) {
return a;
} else {
return b;
export async function createLabel(options: SyncOptions, name: string) {
const flag = options.flags.find((it) => name === it.name);
if (doesExist(flag)) {
await options.remote.createLabel({
color: mustExist(flag.color),
desc: mustExist(flag.desc),
name,
project: options.project,
});
return;
}
const state = options.states.find((it) => name.startsWith(it.name));
if (doesExist(state)) {
const value = state.values.find((it) => valueName(state, it) === name);
if (doesExist(value)) {
await options.remote.createLabel({
color: defaultTo(defaultTo(value.color, state.color), ''),
desc: defaultTo(defaultTo(value.desc, state.desc), ''),
name: valueName(state, value),
project: options.project,
});
return;
}
}
}
@ -102,12 +129,12 @@ export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate):
const state = options.states.find((it) => label.name.startsWith(it.name));
if (doesExist(state)) {
const value = state.values.find((it) => `${state.name}/${it.name}` === label.name);
const value = state.values.find((it) => valueName(state, it) === label.name);
if (doesExist(value)) {
await syncLabelDiff(options, label, {
color: defaultTo(value.color, label.color),
desc: defaultTo(value.desc, label.desc),
name: `${state.name}/${value.name}`,
name: valueName(state, value),
project: options.project,
});

9
src/utils.ts Normal file
View File

@ -0,0 +1,9 @@
import { doesExist, Optional } from '@apextoaster/js-utils';
export function defaultTo<T>(a: Optional<T>, b: T): T {
if (doesExist(a)) {
return a;
} else {
return b;
}
}