private async _importWITStates()

in src/Common/ProcessImporter.ts [372:438]


    private async _importWITStates(witStateEntry: IWITStates, payload: IProcessPayload) {
        let targetWITStates: WITProcessDefinitionsInterfaces.WorkItemStateResultModel[];
        try {
            targetWITStates = await Engine.Task(
                () => this._witProcessApi.getStateDefinitions(payload.process.typeId, witStateEntry.workItemTypeRefName),
                `Get states on target process for work item type '${witStateEntry.workItemTypeRefName}'`);
            if (!targetWITStates || targetWITStates.length <= 0) {
                throw new ImportError(`Failed to get states definitions from work item type '${witStateEntry.workItemTypeRefName}' on target account, server returned empty result.`)
            }
        }
        catch (error) {
            Utility.handleKnownError(error);
            throw new ImportError(`Failed to get states definitions from work item type '${witStateEntry.workItemTypeRefName}' on target account, see logs for details.`)
        }

        for (const sourceState of witStateEntry.states) {
            try {
                const existingStates: WITProcessDefinitionsInterfaces.WorkItemStateResultModel[] = targetWITStates.filter(targetState => sourceState.name === targetState.name);
                if (existingStates.length === 0) {  // does not exist on target
                    const createdState = await Engine.Task(
                        () => this._witProcessDefinitionApi.createStateDefinition(Utility.toCreateOrUpdateStateDefinition(sourceState), payload.process.typeId, witStateEntry.workItemTypeRefName),
                        `Create state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type`);
                    if (!createdState || !createdState.id) {
                        throw new ImportError(`Unable to create state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type, server returned empty result or id.`);
                    }
                }
                else {
                    if (sourceState.hidden) { // if state exists on target, only update if hidden 
                        const hiddenState = await Engine.Task(
                            () => this._witProcessDefinitionApi.hideStateDefinition({ hidden: true }, payload.process.typeId, witStateEntry.workItemTypeRefName, existingStates[0].id),
                            `Hide state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type`);
                        if (!hiddenState || hiddenState.name !== sourceState.name || !hiddenState.hidden) {
                            throw new ImportError(`Unable to hide state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type, server returned empty result, id or state is not hidden.`);
                        }
                    }

                    const existingState = existingStates[0];
                    if (sourceState.color !== existingState.color || sourceState.stateCategory !== existingState.stateCategory || sourceState.name !== existingState.name) {
                        // Inherited state can be edited in custom work item types.
                        const updatedState = await Engine.Task(
                            () => this._witProcessDefinitionApi.updateStateDefinition(Utility.toCreateOrUpdateStateDefinition(sourceState), payload.process.typeId, witStateEntry.workItemTypeRefName, existingState.id),
                            `Update state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type`);
                        if (!updatedState || updatedState.name !== sourceState.name) {
                            throw new ImportError(`Unable to update state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type, server returned empty result, id or state is not hidden.`);
                        }
                    }
                }
            }
            catch (error) {
                Utility.handleKnownError(error);
                throw new ImportError(`Unable to create/hide/update state '${sourceState.name}' in '${witStateEntry.workItemTypeRefName}' work item type, see logs for details`);
            }
        }

        for (const targetState of targetWITStates) {
            const sourceStateMatchingTarget: WITProcessDefinitionsInterfaces.WorkItemStateResultModel[] = witStateEntry.states.filter(sourceState => sourceState.name === targetState.name);
            if (sourceStateMatchingTarget.length === 0) {
                try {
                    await Engine.Task(() => this._witProcessDefinitionApi.deleteStateDefinition(payload.process.typeId, witStateEntry.workItemTypeRefName, targetState.id),
                        `Delete state '${targetState.name}' in '${witStateEntry.workItemTypeRefName}' work item type`);
                }
                catch (error) {
                    throw new ImportError(`Unable to delete state '${targetState.name}' in '${witStateEntry.workItemTypeRefName}' work item type, see logs for details`);
                }
            }
        }
    }