feat(resolve): implement flag rules for issue labels
This commit is contained in:
parent
838628c126
commit
bba4f5206a
|
@ -16,7 +16,7 @@
|
|||
| 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. |
|
||||
| [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) | |
|
||||
|
||||
## Interfaces
|
||||
|
|
|
@ -4,10 +4,12 @@
|
|||
|
||||
## 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>
|
||||
|
||||
```typescript
|
||||
export declare function syncIssues(options: SyncOptions): Promise<unknown>;
|
||||
export declare function syncIssueLabels(options: SyncOptions): Promise<unknown>;
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
|
|
@ -19,5 +19,5 @@ export interface SyncOptions
|
|||
| [logger](./cautious-journey.syncoptions.logger.md) | Logger | |
|
||||
| [project](./cautious-journey.syncoptions.project.md) | string | |
|
||||
| [remote](./cautious-journey.syncoptions.remote.md) | [Remote](./cautious-journey.remote.md) | |
|
||||
| [states](./cautious-journey.syncoptions.states.md) | Array<[StateLabel](./cautious-journey.statelabel.md)<!-- -->> | |
|
||||
| [states](./cautious-journey.syncoptions.states.md) | Array<[StateLabel](./cautious-journey.statelabel.md)<!-- -->> | States from project config. |
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
## SyncOptions.states property
|
||||
|
||||
States from project config.
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```typescript
|
||||
|
|
|
@ -5,7 +5,7 @@ export { Remote, RemoteOptions } from './remote';
|
|||
export { GithubRemote } from './remote/github';
|
||||
export { GitlabRemote } from './remote/gitlab';
|
||||
export { ResolveInput, ResolveResult, resolveLabels } from './resolve';
|
||||
export { syncIssues, syncLabels, SyncOptions } from './sync';
|
||||
export { syncIssueLabels as syncIssues, syncLabels, SyncOptions } from './sync';
|
||||
|
||||
const STATUS_ERROR = 1;
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ export interface BaseLabel {
|
|||
color?: string;
|
||||
desc?: string;
|
||||
priority: number;
|
||||
requires: Array<unknown>;
|
||||
requires: Array<LabelRef>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,7 +8,7 @@ import { ConfigData } from './config';
|
|||
import { Commands, createParser } from './config/args';
|
||||
import { BunyanLogger } from './logger/bunyan';
|
||||
import { GithubRemote } from './remote/github';
|
||||
import { syncIssues, syncLabels, SyncOptions } from './sync';
|
||||
import { syncIssueLabels, syncLabels, SyncOptions } from './sync';
|
||||
import { VERSION_INFO } from './version';
|
||||
|
||||
export { FlagLabel, StateLabel } from './labels';
|
||||
|
@ -16,7 +16,7 @@ export { Remote, RemoteOptions } from './remote';
|
|||
export { GithubRemote } from './remote/github';
|
||||
export { GitlabRemote } from './remote/gitlab';
|
||||
export { resolveLabels } from './resolve';
|
||||
export { syncIssues, syncLabels } from './sync';
|
||||
export { syncIssueLabels as syncIssues, syncLabels } from './sync';
|
||||
|
||||
const SLICE_ARGS = 2;
|
||||
|
||||
|
@ -83,7 +83,7 @@ export async function main(argv: Array<string>): Promise<number> {
|
|||
};
|
||||
switch (mode) {
|
||||
case Commands.ISSUES:
|
||||
await syncIssues(options);
|
||||
await syncIssueLabels(options);
|
||||
break;
|
||||
case Commands.LABELS:
|
||||
await syncLabels(options);
|
||||
|
|
|
@ -57,6 +57,7 @@ export interface ResolveResult {
|
|||
* Resolve the desired set of labels, given a starting set and the flags/states to be
|
||||
* applied.
|
||||
*/
|
||||
/* eslint-disable-next-line sonarjs/cognitive-complexity */
|
||||
export function resolveLabels(options: ResolveInput): ResolveResult {
|
||||
const activeLabels = new Set(options.labels);
|
||||
const changes: Array<ChangeRecord> = [];
|
||||
|
@ -65,9 +66,30 @@ export function resolveLabels(options: ResolveInput): ResolveResult {
|
|||
const sortedFlags = prioritySort(options.flags);
|
||||
for (const flag of sortedFlags) {
|
||||
const { name } = flag;
|
||||
|
||||
if (activeLabels.has(name)) {
|
||||
// TODO: check removes
|
||||
// TODO: check requires
|
||||
// TODO: check requires rule
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
27
src/sync.ts
27
src/sync.ts
|
@ -7,34 +7,49 @@ import { resolveLabels } from './resolve';
|
|||
import { defaultTo, defaultUntil } from './utils';
|
||||
|
||||
export interface SyncOptions {
|
||||
/**
|
||||
*/
|
||||
colors: Array<string>;
|
||||
|
||||
/**
|
||||
*/
|
||||
flags: Array<FlagLabel>;
|
||||
|
||||
logger: Logger;
|
||||
project: string;
|
||||
remote: Remote;
|
||||
|
||||
/**
|
||||
* States from project config.
|
||||
*/
|
||||
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({
|
||||
project: options.project,
|
||||
});
|
||||
|
||||
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,
|
||||
labels: issue.labels,
|
||||
states: options.states,
|
||||
});
|
||||
|
||||
// TODO: prompt user to update this particular issue
|
||||
if (resolution.changes.length > 0 && resolution.errors.length === 0) {
|
||||
options.logger.info({ issue, resolution }, 'updating issue');
|
||||
if (changes.length > 0 && errors.length === 0) {
|
||||
options.logger.info({ issue, labels }, 'updating issue');
|
||||
await options.remote.updateIssue({
|
||||
...issue,
|
||||
labels: resolution.labels,
|
||||
labels,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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']);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue