1
0
Fork 0

fix(sync): handle negative random indices

This commit is contained in:
ssube 2020-08-15 16:34:28 -05:00 committed by Sean Sube
parent 6229e2b46f
commit 686cb834bc
3 changed files with 19 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { LogLevel } from 'noicejs'; import { LogLevel, NullLogger } from 'noicejs';
import { FlagLabel, StateLabel } from '../labels'; import { FlagLabel, StateLabel } from '../labels';
import { RemoteOptions } from '../remote'; import { RemoteOptions } from '../remote';
@ -46,12 +46,18 @@ export interface ConfigData {
*/ */
export function initConfig(): ConfigData { export function initConfig(): ConfigData {
return { return {
logger: {
level: LogLevel.Info,
name: '',
},
projects: [{ projects: [{
colors: [], colors: [],
flags: [], flags: [],
name: '', name: '',
remote: { remote: {
data: {}, data: {},
dryrun: true,
logger: NullLogger.global,
type: '', type: '',
}, },
states: [], states: [],

View File

@ -130,20 +130,20 @@ export async function createLabel(options: SyncOptions, name: string) {
} }
} }
export async function syncLabelDiff(options: SyncOptions, current: LabelUpdate, expected: LabelUpdate) { export async function syncLabelDiff(options: SyncOptions, oldLabel: LabelUpdate, newLabel: LabelUpdate) {
const dirty = const dirty =
current.color !== expected.color || oldLabel.color !== mustExist(newLabel.color) ||
current.desc !== expected.desc; oldLabel.desc !== mustExist(newLabel.desc);
if (dirty) { if (dirty) {
const body = { const body = {
color: defaultTo(expected.color, current.color), color: defaultTo(newLabel.color, oldLabel.color),
desc: defaultTo(expected.desc, current.desc), desc: defaultTo(newLabel.desc, oldLabel.desc),
name: current.name, name: oldLabel.name,
project: options.project, project: options.project,
}; };
options.logger.debug({ body, current, expected }, 'update label'); options.logger.debug({ body, newLabel, oldLabel, options }, 'update label');
const resp = await options.remote.updateLabel(body); const resp = await options.remote.updateLabel(body);
@ -154,8 +154,9 @@ export async function syncLabelDiff(options: SyncOptions, current: LabelUpdate,
export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate): Promise<void> { export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate): Promise<void> {
const flag = options.flags.find((it) => label.name === it.name); const flag = options.flags.find((it) => label.name === it.name);
if (doesExist(flag)) { if (doesExist(flag)) {
const color = getLabelColor(options.colors, options.random, flag);
await syncLabelDiff(options, label, { await syncLabelDiff(options, label, {
color: getLabelColor(options.colors, options.random, flag), color,
desc: defaultTo(flag.desc, label.desc), desc: defaultTo(flag.desc, label.desc),
name: flag.name, name: flag.name,
project: options.project, project: options.project,
@ -168,8 +169,9 @@ export async function syncSingleLabel(options: SyncOptions, label: LabelUpdate):
if (doesExist(state)) { if (doesExist(state)) {
const value = state.values.find((it) => getValueName(state, it) === label.name); const value = state.values.find((it) => getValueName(state, it) === label.name);
if (doesExist(value)) { if (doesExist(value)) {
const color = mustExist(getLabelColor(options.colors, options.random, state, value));
await syncLabelDiff(options, label, { await syncLabelDiff(options, label, {
color: getLabelColor(options.colors, options.random, state, value), color,
desc: defaultTo(value.desc, label.desc), desc: defaultTo(value.desc, label.desc),
name: getValueName(state, value), name: getValueName(state, value),
project: options.project, project: options.project,

View File

@ -15,6 +15,6 @@ export function defaultUntil<T>(...items: Array<Optional<T>>): T {
} }
export function randomItem<T>(items: Array<T>, source: prng): T { export function randomItem<T>(items: Array<T>, source: prng): T {
const idx = source.int32() % items.length; const idx = Math.abs(source.int32()) % items.length;
return items[idx]; return items[idx];
} }