コード例 #1
0
        public async Task Prepare(IValidationContext context)
        {
            Logger.LogInformation("Reading the work item types from the source and target accounts");

            // When reading the work item, we need the type field for this validator
            context.RequestedFields.Add(FieldNames.WorkItemType);

            try
            {
                // We need all fields to validate the field types
                var sourceFields = (await WorkItemTrackingHelpers.GetFields(context.SourceClient.WorkItemTrackingHttpClient)).ToDictionary(key => key.ReferenceName);
                context.SourceFields = new ConcurrentDictionary <string, WorkItemField>(sourceFields, StringComparer.OrdinalIgnoreCase);
            }
            catch (Exception e)
            {
                throw new ValidationException("Unable to read fields on the source", e);
            }

            try
            {
                // We need all fields to validate the field types
                var targetFields = (await WorkItemTrackingHelpers.GetFields(context.TargetClient.WorkItemTrackingHttpClient)).ToDictionary(key => key.ReferenceName);
                context.TargetFields   = new ConcurrentDictionary <string, WorkItemField>(targetFields, StringComparer.OrdinalIgnoreCase);
                context.IdentityFields = new HashSet <string>(targetFields.Where(f => f.Value.IsIdentity).Select(f => f.Key), StringComparer.OrdinalIgnoreCase);
            }
            catch (Exception e)
            {
                throw new ValidationException("Unable to read fields on the target", e);
            }

            // handle condition of activated/not activated:
            ValidateFieldsMapping(context);

            try
            {
                var workItemTypes = await WorkItemTrackingHelpers.GetWorkItemTypes(context.SourceClient.WorkItemTrackingHttpClient, context.Config.SourceConnection.Project);

                foreach (var workItemType in workItemTypes)
                {
                    context.SourceTypesAndFields[workItemType.Name] = new HashSet <string>(workItemType.Fields.Select(f => f.ReferenceName), StringComparer.OrdinalIgnoreCase);
                }
            }
            catch (Exception e)
            {
                throw new ValidationException("Unable to read work item types on the source", e);
            }

            try
            {
                var workItemTypes = await WorkItemTrackingHelpers.GetWorkItemTypes(context.TargetClient.WorkItemTrackingHttpClient, context.Config.TargetConnection.Project);

                foreach (var workItemType in workItemTypes)
                {
                    context.TargetTypesAndFields[workItemType.Name] = new HashSet <string>(workItemType.Fields.Select(f => f.ReferenceName), StringComparer.OrdinalIgnoreCase);
                }
            }
            catch (Exception e)
            {
                throw new ValidationException("Unable to read work item types on the target", e);
            }

            // A very edge case, but we should fail this scenario.
            if (context.SourceTypesAndFields.Count == 0 || context.TargetTypesAndFields.Count == 0)
            {
                throw new ValidationException("Source or target does not have any work item types");
            }
        }