1
0
Fork 0

feat(resolve): implement flag rules for issue labels

This commit is contained in:
BZ Libby 2020-08-15 16:06:26 -05:00 committed by Sean Sube
parent 838628c126
commit bba4f5206a
10 changed files with 124 additions and 16 deletions

View File

@ -16,7 +16,7 @@
| Function | Description | | Function | Description |
| --- | --- | | --- | --- |
| [resolveLabels(options)](./cautious-journey.resolvelabels.md) | Resolve the desired set of labels, given a starting set and the flags/states to be applied. | | [resolveLabels(options)](./cautious-journey.resolvelabels.md) | Resolve the desired set of labels, given a starting set and the flags/states to be applied. |
| [syncIssues(options)](./cautious-journey.syncissues.md) | | | [syncIssues(options)](./cautious-journey.syncissues.md) | goes through each issue in the project resolves labels if there are changes and no errors, then update the issue |
| [syncLabels(options)](./cautious-journey.synclabels.md) | | | [syncLabels(options)](./cautious-journey.synclabels.md) | |
## Interfaces ## Interfaces

View File

@ -4,10 +4,12 @@
## syncIssues() function ## syncIssues() function
goes through each issue in the project resolves labels if there are changes and no errors, then update the issue
<b>Signature:</b> <b>Signature:</b>
```typescript ```typescript
export declare function syncIssues(options: SyncOptions): Promise<unknown>; export declare function syncIssueLabels(options: SyncOptions): Promise<unknown>;
``` ```
## Parameters ## Parameters

View File

@ -19,5 +19,5 @@ export interface SyncOptions
| [logger](./cautious-journey.syncoptions.logger.md) | Logger | | | [logger](./cautious-journey.syncoptions.logger.md) | Logger | |
| [project](./cautious-journey.syncoptions.project.md) | string | | | [project](./cautious-journey.syncoptions.project.md) | string | |
| [remote](./cautious-journey.syncoptions.remote.md) | [Remote](./cautious-journey.remote.md) | | | [remote](./cautious-journey.syncoptions.remote.md) | [Remote](./cautious-journey.remote.md) | |
| [states](./cautious-journey.syncoptions.states.md) | Array&lt;[StateLabel](./cautious-journey.statelabel.md)<!-- -->&gt; | | | [states](./cautious-journey.syncoptions.states.md) | Array&lt;[StateLabel](./cautious-journey.statelabel.md)<!-- -->&gt; | States from project config. |

View File

@ -4,6 +4,8 @@
## SyncOptions.states property ## SyncOptions.states property
States from project config.
<b>Signature:</b> <b>Signature:</b>
```typescript ```typescript

View File

@ -5,7 +5,7 @@ export { Remote, RemoteOptions } from './remote';
export { GithubRemote } from './remote/github'; export { GithubRemote } from './remote/github';
export { GitlabRemote } from './remote/gitlab'; export { GitlabRemote } from './remote/gitlab';
export { ResolveInput, ResolveResult, resolveLabels } from './resolve'; export { ResolveInput, ResolveResult, resolveLabels } from './resolve';
export { syncIssues, syncLabels, SyncOptions } from './sync'; export { syncIssueLabels as syncIssues, syncLabels, SyncOptions } from './sync';
const STATUS_ERROR = 1; const STATUS_ERROR = 1;

View File

@ -32,7 +32,7 @@ export interface BaseLabel {
color?: string; color?: string;
desc?: string; desc?: string;
priority: number; priority: number;
requires: Array<unknown>; requires: Array<LabelRef>;
} }
/** /**

View File

@ -8,7 +8,7 @@ import { ConfigData } from './config';
import { Commands, createParser } from './config/args'; import { Commands, createParser } from './config/args';
import { BunyanLogger } from './logger/bunyan'; import { BunyanLogger } from './logger/bunyan';
import { GithubRemote } from './remote/github'; import { GithubRemote } from './remote/github';
import { syncIssues, syncLabels, SyncOptions } from './sync'; import { syncIssueLabels, syncLabels, SyncOptions } from './sync';
import { VERSION_INFO } from './version'; import { VERSION_INFO } from './version';
export { FlagLabel, StateLabel } from './labels'; export { FlagLabel, StateLabel } from './labels';
@ -16,7 +16,7 @@ export { Remote, RemoteOptions } from './remote';
export { GithubRemote } from './remote/github'; export { GithubRemote } from './remote/github';
export { GitlabRemote } from './remote/gitlab'; export { GitlabRemote } from './remote/gitlab';
export { resolveLabels } from './resolve'; export { resolveLabels } from './resolve';
export { syncIssues, syncLabels } from './sync'; export { syncIssueLabels as syncIssues, syncLabels } from './sync';
const SLICE_ARGS = 2; const SLICE_ARGS = 2;
@ -83,7 +83,7 @@ export async function main(argv: Array<string>): Promise<number> {
}; };
switch (mode) { switch (mode) {
case Commands.ISSUES: case Commands.ISSUES:
await syncIssues(options); await syncIssueLabels(options);
break; break;
case Commands.LABELS: case Commands.LABELS:
await syncLabels(options); await syncLabels(options);

View File

@ -57,6 +57,7 @@ export interface ResolveResult {
* Resolve the desired set of labels, given a starting set and the flags/states to be * Resolve the desired set of labels, given a starting set and the flags/states to be
* applied. * applied.
*/ */
/* eslint-disable-next-line sonarjs/cognitive-complexity */
export function resolveLabels(options: ResolveInput): ResolveResult { export function resolveLabels(options: ResolveInput): ResolveResult {
const activeLabels = new Set(options.labels); const activeLabels = new Set(options.labels);
const changes: Array<ChangeRecord> = []; const changes: Array<ChangeRecord> = [];
@ -65,9 +66,30 @@ export function resolveLabels(options: ResolveInput): ResolveResult {
const sortedFlags = prioritySort(options.flags); const sortedFlags = prioritySort(options.flags);
for (const flag of sortedFlags) { for (const flag of sortedFlags) {
const { name } = flag; const { name } = flag;
if (activeLabels.has(name)) { if (activeLabels.has(name)) {
// TODO: check removes // TODO: check requires rule
// TODO: check requires let removed = false;
for (const requiredLabel of flag.requires) {
if (!activeLabels.has(requiredLabel.name)) {
activeLabels.delete(name);
removed = true;
}
}
if (removed) {
break;
}
// TODO: check adds rule
for (const addedLabel of flag.adds) {
activeLabels.add(addedLabel.name);
}
// TODO: check removes rule
for (const removedLabel of flag.removes) {
activeLabels.delete(removedLabel.name);
}
} }
} }

View File

@ -7,34 +7,49 @@ import { resolveLabels } from './resolve';
import { defaultTo, defaultUntil } from './utils'; import { defaultTo, defaultUntil } from './utils';
export interface SyncOptions { export interface SyncOptions {
/**
*/
colors: Array<string>; colors: Array<string>;
/**
*/
flags: Array<FlagLabel>; flags: Array<FlagLabel>;
logger: Logger; logger: Logger;
project: string; project: string;
remote: Remote; remote: Remote;
/**
* States from project config.
*/
states: Array<StateLabel>; states: Array<StateLabel>;
} }
export async function syncIssues(options: SyncOptions): Promise<unknown> { /**
* goes through each issue in the project
* resolves labels
* if there are changes and no errors, then update the issue
*/
export async function syncIssueLabels(options: SyncOptions): Promise<unknown> {
const issues = await options.remote.listIssues({ const issues = await options.remote.listIssues({
project: options.project, project: options.project,
}); });
for (const issue of issues) { for (const issue of issues) {
options.logger.info({ issue }, 'issue'); options.logger.info({ issue }, 'project issue');
const resolution = resolveLabels({ const { changes, errors, labels } = resolveLabels({
flags: options.flags, flags: options.flags,
labels: issue.labels, labels: issue.labels,
states: options.states, states: options.states,
}); });
// TODO: prompt user to update this particular issue // TODO: prompt user to update this particular issue
if (resolution.changes.length > 0 && resolution.errors.length === 0) { if (changes.length > 0 && errors.length === 0) {
options.logger.info({ issue, resolution }, 'updating issue'); options.logger.info({ issue, labels }, 'updating issue');
await options.remote.updateIssue({ await options.remote.updateIssue({
...issue, ...issue,
labels: resolution.labels, labels,
}); });
} }
} }

View File

@ -0,0 +1,67 @@
import { expect } from 'chai';
import { resolveLabels } from '../../src/resolve';
const TEST_LABELS = ['foo', 'bar'];
describe('resolve labels', () => {
describe('flags with missing requires', () => {
it('should be removed when required label is missing', () => {
const result = resolveLabels({
flags: [{
adds: [],
name: 'hambone',
priority: 1,
removes: [],
requires: [{
name: 'it needs a name',
}],
}],
labels: ['hambone'],
states: [],
});
expect(result.labels).to.deep.equal([]);
});
});
describe('flags with add rules', () => {
it('should add the labels', () => {
const result = resolveLabels({
flags: [{
adds: [{
name: 'linda',
}],
name: 'bob',
priority: 1,
removes: [],
requires: [],
}],
labels: ['bob'],
states: [],
});
expect(result.labels).to.deep.equal(['bob', 'linda']);
});
});
describe('flags with remove rules', () => {
it('should remove labels', () => {
const result = resolveLabels({
flags: [{
adds: [],
name: 'bob',
priority: 1,
removes: [{
name: 'hugo',
}],
requires: [],
}],
labels: ['bob', 'hugo'],
states: [],
});
expect(result.labels).to.deep.equal(['bob']);
});
});
});