2020-08-13 23:44:13 +00:00
|
|
|
import { doesExist, mustExist } from '@apextoaster/js-utils';
|
2020-08-14 04:07:47 +00:00
|
|
|
import { Logger } from 'noicejs';
|
2020-08-15 19:19:57 +00:00
|
|
|
import { prng } from 'seedrandom';
|
2020-08-13 01:01:48 +00:00
|
|
|
|
2020-08-15 17:38:33 +00:00
|
|
|
import { FlagLabel, getLabelColor, getLabelNames, getValueName, StateLabel } from './labels';
|
2020-08-13 01:40:28 +00:00
|
|
|
import { LabelUpdate, Remote } from './remote';
|
2020-08-14 03:36:30 +00:00
|
|
|
import { resolveLabels } from './resolve';
|
2020-08-15 23:50:39 +00:00
|
|
|
import { defaultTo, defaultUntil, compareItems } from './utils';
|
2020-08-12 01:39:53 +00:00
|
|
|
|
|
|
|
export interface SyncOptions {
|
2020-08-15 21:06:26 +00:00
|
|
|
/**
|
|
|
|
*/
|
2020-08-15 15:12:41 +00:00
|
|
|
colors: Array<string>;
|
2020-08-15 21:06:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
2020-08-13 00:33:53 +00:00
|
|
|
flags: Array<FlagLabel>;
|
2020-08-15 21:06:26 +00:00
|
|
|
|
2020-08-14 04:07:47 +00:00
|
|
|
logger: Logger;
|
2020-08-12 01:39:53 +00:00
|
|
|
project: string;
|
2020-08-15 19:19:57 +00:00
|
|
|
random: prng;
|
2020-08-12 01:39:53 +00:00
|
|
|
remote: Remote;
|
2020-08-15 21:06:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* States from project config.
|
|
|
|
*/
|
2020-08-13 00:33:53 +00:00
|
|
|
states: Array<StateLabel>;
|
2020-08-12 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 21:06:26 +00:00
|
|
|
/**
|
2020-08-15 21:10:23 +00:00
|
|
|
* goes through and resolves each issue in the project.
|
|
|
|
* if there are changes and no errors, then updates the issue.
|
2020-08-15 21:06:26 +00:00
|
|
|
*/
|
|
|
|
export async function syncIssueLabels(options: SyncOptions): Promise<unknown> {
|
2020-08-12 23:21:35 +00:00
|
|
|
const issues = await options.remote.listIssues({
|
|
|
|
project: options.project,
|
|
|
|
});
|
2020-08-12 01:39:53 +00:00
|
|
|
|
|
|
|
for (const issue of issues) {
|
2020-08-15 21:06:26 +00:00
|
|
|
options.logger.info({ issue }, 'project issue');
|
2020-08-14 03:36:30 +00:00
|
|
|
|
2020-08-15 21:06:26 +00:00
|
|
|
const { changes, errors, labels } = resolveLabels({
|
2020-08-14 03:36:30 +00:00
|
|
|
flags: options.flags,
|
|
|
|
labels: issue.labels,
|
|
|
|
states: options.states,
|
|
|
|
});
|
|
|
|
|
2020-08-15 23:50:39 +00:00
|
|
|
options.logger.debug({ changes, errors, issue, labels }, 'resolved labels');
|
|
|
|
|
2020-08-14 03:36:30 +00:00
|
|
|
// TODO: prompt user to update this particular issue
|
2020-08-16 22:07:07 +00:00
|
|
|
const sameLabels = compareItems(issue.labels, labels) || changes.length === 0;
|
|
|
|
if (sameLabels === false && errors.length === 0) {
|
2020-08-17 13:32:43 +00:00
|
|
|
options.logger.info({ changes, errors, issue, labels }, 'updating issue');
|
2020-08-14 03:36:30 +00:00
|
|
|
await options.remote.updateIssue({
|
|
|
|
...issue,
|
2020-08-15 21:06:26 +00:00
|
|
|
labels,
|
2020-08-14 03:36:30 +00:00
|
|
|
});
|
2020-08-16 22:07:07 +00:00
|
|
|
await options.remote.createComment({
|
|
|
|
...issue,
|
|
|
|
changes,
|
|
|
|
errors,
|
|
|
|
});
|
2020-08-14 03:36:30 +00:00
|
|
|
}
|
2020-08-12 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:41:48 +00:00
|
|
|
export async function syncProjectLabels(options: SyncOptions): Promise<unknown> {
|
2020-08-12 23:21:35 +00:00
|
|
|
const labels = await options.remote.listLabels({
|
|
|
|
project: options.project,
|
|
|
|
});
|
2020-08-12 01:39:53 +00:00
|
|
|
|
2020-08-13 01:40:28 +00:00
|
|
|
const present = new Set(labels.map((l) => l.name));
|
|
|
|
const desired = getLabelNames(options.flags, options.states);
|
|
|
|
const combined = new Set([...desired, ...present]);
|
2020-08-13 00:24:05 +00:00
|
|
|
|
2020-08-13 01:40:28 +00:00
|
|
|
for (const label of combined) {
|
|
|
|
const exists = present.has(label);
|
|
|
|
const expected = desired.has(label);
|
2020-08-14 04:07:47 +00:00
|
|
|
|
|
|
|
options.logger.info({
|
|
|
|
exists,
|
|
|
|
expected,
|
|
|
|
label,
|
|
|
|
}, 'label');
|
2020-08-13 00:24:05 +00:00
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
if (expected) {
|
2020-08-13 01:40:28 +00:00
|
|
|
const data = mustExist(labels.find((l) => l.name === label));
|
|
|
|
await syncSingleLabel(options, data);
|
2020-08-13 00:24:05 +00:00
|
|
|
} else {
|
2020-08-14 04:07:47 +00:00
|
|
|
options.logger.warn({ label }, 'remove label');
|
2020-08-13 23:44:13 +00:00
|
|
|
await options.remote.deleteLabel({
|
|
|
|
name: label,
|
|
|
|
project: options.project,
|
|
|
|
});
|
2020-08-13 00:24:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (expected) {
|
2020-08-14 04:07:47 +00:00
|
|
|
options.logger.info({ label }, 'create label');
|
2020-08-13 23:44:13 +00:00
|
|
|
await createLabel(options, label);
|
2020-08-13 00:24:05 +00:00
|
|
|
} else {
|
|
|
|
// skip
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-08-13 01:01:48 +00:00
|
|
|
|
2020-08-13 23:44:13 +00:00
|
|
|
export async function createLabel(options: SyncOptions, name: string) {
|
|
|
|
const flag = options.flags.find((it) => name === it.name);
|
|
|
|
if (doesExist(flag)) {
|
|
|
|
await options.remote.createLabel({
|
2020-08-15 19:19:57 +00:00
|
|
|
color: getLabelColor(options.colors, options.random, flag),
|
2020-08-13 23:44:13 +00:00
|
|
|
desc: mustExist(flag.desc),
|
|
|
|
name,
|
|
|
|
project: options.project,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = options.states.find((it) => name.startsWith(it.name));
|
|
|
|
if (doesExist(state)) {
|
2020-08-15 17:38:33 +00:00
|
|
|
const value = state.values.find((it) => getValueName(state, it) === name);
|
2020-08-13 23:44:13 +00:00
|
|
|
if (doesExist(value)) {
|
|
|
|
await options.remote.createLabel({
|
2020-08-15 19:19:57 +00:00
|
|
|
color: getLabelColor(options.colors, options.random, state, value),
|
2020-08-15 15:12:41 +00:00
|
|
|
desc: defaultUntil(value.desc, state.desc, ''),
|
2020-08-15 17:38:33 +00:00
|
|
|
name: getValueName(state, value),
|
2020-08-13 23:44:13 +00:00
|
|
|
project: options.project,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-08-13 01:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:34:28 +00:00
|
|
|
export async function syncLabelDiff(options: SyncOptions, oldLabel: LabelUpdate, newLabel: LabelUpdate) {
|
2020-08-13 01:01:48 +00:00
|
|
|
const dirty =
|
2020-08-15 21:34:28 +00:00
|
|
|
oldLabel.color !== mustExist(newLabel.color) ||
|
|
|
|
oldLabel.desc !== mustExist(newLabel.desc);
|
2020-08-13 01:01:48 +00:00
|
|
|
|
|
|
|
if (dirty) {
|
2020-08-13 01:40:28 +00:00
|
|
|
const body = {
|
2020-08-15 21:34:28 +00:00
|
|
|
color: defaultTo(newLabel.color, oldLabel.color),
|
|
|
|
desc: defaultTo(newLabel.desc, oldLabel.desc),
|
|
|
|
name: oldLabel.name,
|
2020-08-13 01:40:28 +00:00
|
|
|
project: options.project,
|
|
|
|
};
|
|
|
|
|
2020-08-15 23:50:39 +00:00
|
|
|
options.logger.debug({ body, newLabel, oldLabel }, 'update label');
|
2020-08-13 01:40:28 +00:00
|
|
|
|
|
|
|
const resp = await options.remote.updateLabel(body);
|
|
|
|
|
2020-08-14 04:07:47 +00:00
|
|
|
options.logger.debug({ resp }, 'update resp');
|
2020-08-13 01:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate): Promise<void> {
|
|
|
|
const flag = options.flags.find((it) => label.name === it.name);
|
|
|
|
if (doesExist(flag)) {
|
2020-08-15 21:34:28 +00:00
|
|
|
const color = getLabelColor(options.colors, options.random, flag);
|
2020-08-13 01:40:28 +00:00
|
|
|
await syncLabelDiff(options, label, {
|
2020-08-15 21:34:28 +00:00
|
|
|
color,
|
2020-08-13 01:40:28 +00:00
|
|
|
desc: defaultTo(flag.desc, label.desc),
|
|
|
|
name: flag.name,
|
|
|
|
project: options.project,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = options.states.find((it) => label.name.startsWith(it.name));
|
|
|
|
if (doesExist(state)) {
|
2020-08-15 17:38:33 +00:00
|
|
|
const value = state.values.find((it) => getValueName(state, it) === label.name);
|
2020-08-13 01:40:28 +00:00
|
|
|
if (doesExist(value)) {
|
2020-08-15 21:34:28 +00:00
|
|
|
const color = mustExist(getLabelColor(options.colors, options.random, state, value));
|
2020-08-13 01:40:28 +00:00
|
|
|
await syncLabelDiff(options, label, {
|
2020-08-15 21:34:28 +00:00
|
|
|
color,
|
2020-08-13 01:40:28 +00:00
|
|
|
desc: defaultTo(value.desc, label.desc),
|
2020-08-15 17:38:33 +00:00
|
|
|
name: getValueName(state, value),
|
2020-08-13 01:40:28 +00:00
|
|
|
project: options.project,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-08-13 01:01:48 +00:00
|
|
|
}
|
|
|
|
}
|