2020-08-16 21:41:58 +00:00
|
|
|
import { doesExist } from '@apextoaster/js-utils';
|
|
|
|
|
|
|
|
import { BaseLabel, FlagLabel, getValueName, prioritySort, StateLabel } from './labels';
|
2020-08-16 22:08:44 +00:00
|
|
|
import { defaultUntil } from './utils';
|
2020-08-12 00:14:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How a label changed.
|
|
|
|
*/
|
2020-08-15 14:31:28 +00:00
|
|
|
export enum ChangeVerb {
|
2020-08-16 21:41:58 +00:00
|
|
|
CONFLICTED = 'conflicted',
|
2020-08-12 00:14:42 +00:00
|
|
|
CREATED = 'created',
|
2020-08-16 21:41:58 +00:00
|
|
|
EXISTING = 'existing',
|
2020-08-12 00:14:42 +00:00
|
|
|
REMOVED = 'removed',
|
2020-08-12 03:19:01 +00:00
|
|
|
REQUIRED = 'required',
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Details of a label change.
|
|
|
|
*/
|
|
|
|
export interface ChangeRecord {
|
2020-08-15 14:31:28 +00:00
|
|
|
/**
|
|
|
|
* The label which caused this change.
|
|
|
|
*/
|
2020-08-12 00:14:42 +00:00
|
|
|
cause: string;
|
2020-08-15 14:31:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How the label was changed.
|
|
|
|
*/
|
|
|
|
effect: ChangeVerb;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The label being changed.
|
|
|
|
*/
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ErrorRecord {
|
|
|
|
error: Error;
|
2020-08-12 00:14:42 +00:00
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collected inputs for a resolver run.
|
|
|
|
*/
|
|
|
|
export interface ResolveInput {
|
2020-08-12 01:39:53 +00:00
|
|
|
flags: Array<FlagLabel>;
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array<string>;
|
2020-08-12 01:39:53 +00:00
|
|
|
states: Array<StateLabel>;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collected results from a resolver run.
|
|
|
|
*/
|
|
|
|
export interface ResolveResult {
|
|
|
|
changes: Array<ChangeRecord>;
|
2020-08-15 14:31:28 +00:00
|
|
|
errors: Array<ErrorRecord>;
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array<string>;
|
|
|
|
}
|
|
|
|
|
2020-08-15 14:31:28 +00:00
|
|
|
/**
|
|
|
|
* Resolve the desired set of labels, given a starting set and the flags/states to be
|
|
|
|
* applied.
|
|
|
|
*/
|
2020-08-15 21:06:26 +00:00
|
|
|
/* eslint-disable-next-line sonarjs/cognitive-complexity */
|
2020-08-12 00:14:42 +00:00
|
|
|
export function resolveLabels(options: ResolveInput): ResolveResult {
|
|
|
|
const activeLabels = new Set(options.labels);
|
2020-08-12 03:19:01 +00:00
|
|
|
const changes: Array<ChangeRecord> = [];
|
2020-08-15 14:31:28 +00:00
|
|
|
const errors: Array<ErrorRecord> = [];
|
2020-08-12 00:14:42 +00:00
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
function checkLabelRules(label: BaseLabel) {
|
|
|
|
let isRemoved = false;
|
|
|
|
if (activeLabels.has(label.name)) {
|
|
|
|
for (const requiredLabel of label.requires) {
|
2020-08-15 21:06:26 +00:00
|
|
|
if (!activeLabels.has(requiredLabel.name)) {
|
2020-08-16 21:41:58 +00:00
|
|
|
if (activeLabels.delete(label.name)) {
|
|
|
|
changes.push({
|
|
|
|
cause: requiredLabel.name,
|
|
|
|
effect: ChangeVerb.REQUIRED,
|
|
|
|
label: label.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
isRemoved = true;
|
2020-08-15 21:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
2020-08-16 21:41:58 +00:00
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
if (isRemoved) {
|
2020-08-15 22:44:40 +00:00
|
|
|
return true;
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
2020-08-15 21:06:26 +00:00
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
for (const addedLabel of label.adds) {
|
2020-08-16 21:41:58 +00:00
|
|
|
// Set.add does not return a boolean, unlike the other methods
|
|
|
|
if (!activeLabels.has(addedLabel.name)) {
|
|
|
|
activeLabels.add(addedLabel.name);
|
|
|
|
changes.push({
|
|
|
|
cause: label.name,
|
|
|
|
effect: ChangeVerb.CREATED,
|
|
|
|
label: addedLabel.name,
|
|
|
|
});
|
|
|
|
}
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
2020-08-15 21:06:26 +00:00
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
for (const removedLabel of label.removes) {
|
2020-08-16 21:41:58 +00:00
|
|
|
if (activeLabels.delete(removedLabel.name)) {
|
|
|
|
changes.push({
|
|
|
|
cause: label.name,
|
|
|
|
effect: ChangeVerb.REMOVED,
|
|
|
|
label: removedLabel.name,
|
|
|
|
});
|
|
|
|
}
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
2020-08-15 22:44:40 +00:00
|
|
|
|
|
|
|
return false;
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 22:24:16 +00:00
|
|
|
const sortedFlags = prioritySort(options.flags);
|
|
|
|
for (const flag of sortedFlags) {
|
|
|
|
checkLabelRules(flag);
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:38:33 +00:00
|
|
|
const sortedStates = prioritySort(options.states);
|
2020-08-12 00:14:42 +00:00
|
|
|
for (const state of sortedStates) {
|
2020-08-16 21:41:58 +00:00
|
|
|
let activeValue;
|
|
|
|
|
2020-08-15 17:38:33 +00:00
|
|
|
const sortedValues = prioritySort(state.values);
|
2020-08-15 14:31:28 +00:00
|
|
|
for (const value of sortedValues) {
|
2020-08-15 17:38:33 +00:00
|
|
|
const name = getValueName(state, value);
|
2020-08-12 00:14:42 +00:00
|
|
|
if (activeLabels.has(name)) {
|
2020-08-16 21:41:58 +00:00
|
|
|
if (doesExist(activeValue)) {
|
|
|
|
if (activeLabels.delete(name)) {
|
|
|
|
changes.push({
|
|
|
|
cause: name,
|
|
|
|
effect: ChangeVerb.CONFLICTED,
|
|
|
|
label: name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: combine rules, but use state/value name
|
2020-08-16 22:08:44 +00:00
|
|
|
const combinedValue: BaseLabel = {
|
|
|
|
adds: [...state.adds, ...value.adds],
|
|
|
|
name,
|
|
|
|
priority: defaultUntil(value.priority, state.priority, 0),
|
|
|
|
removes: [...state.removes, ...value.removes],
|
|
|
|
requires: [...state.requires, ...value.requires],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!checkLabelRules(combinedValue)) {
|
2020-08-15 22:44:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-08-16 21:41:58 +00:00
|
|
|
|
|
|
|
// TODO: check becomes rules
|
|
|
|
activeValue = name;
|
2020-08-15 22:24:16 +00:00
|
|
|
}
|
2020-08-12 00:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-08-12 03:19:01 +00:00
|
|
|
changes,
|
|
|
|
errors,
|
2020-08-12 00:14:42 +00:00
|
|
|
labels: Array.from(activeLabels),
|
|
|
|
};
|
|
|
|
}
|