private async Task Synchronize()
        {
            try
            {
                // Lets first start by taking inventory of what we have in source and target, in parallel.
                await Task.WhenAll(
                    BuildModel(SourceModel, Source, true),
                    BuildModel(TargetModel, Target, false));

                Log.LogInformation("Source and target data models established.");

                Log.LogInformation("Synchronizing organization level fields.");
                // We've got all the target and sources data.. let's start syncronizing
                await SourceFields.Values.ParallelForEachAsync(Math.Max(1, _Options.MaxDegreeOfParallelism), async (sourceField) =>
                {
                    if (sourceField.ReferenceName.StartsWith("System.") || sourceField.ReferenceName.StartsWith("Microsoft."))
                    {
                        return;
                    }
                    WorkItemField targetField = null;
                    if (!TargetFields.ContainsKey(sourceField.ReferenceName))
                    {
                        await SyncDefinitionType <WorkItemField>(TargetFields, sourceField, targetField);
                    }
                });

                await SourceModel.ProcessDefinitions.Values.ParallelForEachAsync(Math.Max(1, _Options.MaxDegreeOfParallelism), SyncProcess);
            }
            catch (Exception ex)
            {
                Log.LogError(ex, "Failed to synchronize processes.");
                throw ex;
            }
        }
Beispiel #2
0
        public WorkItemField CreateWorkItemField()
        {
            WorkItemField newlyCreatedWorkItemField = new WorkItemField();

            WorkItemField newWorkItemField = new WorkItemField()
            {
                Name        = "New Work Item Field",
                Description = "New work item filed for testing",
                Type        = FieldType.String
            };

            VssConnection connection = Context.Connection;
            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                newlyCreatedWorkItemField = workItemTrackingClient.CreateFieldAsync(newWorkItemField).Result;
                Context.SetValue <string>("$newlyCreatedWorkItemFieldName", newlyCreatedWorkItemField.Name);
                Console.WriteLine("Work Item Field Create Succeeded.");
            }
            catch
            {
                Console.WriteLine("Work Item Field Create Failed.");
            }


            return(newlyCreatedWorkItemField);
        }
        private static ProcessWorkItemTypeField AddFieldToWorkItemType(VssConnection connection, WorkItemField field, Process process, string workItemTypeRefName)
        {
            // if field does not exist add field to process
            WorkItemField workItemField = null;

            try
            {
                workItemField = GetField(connection, field.Name);
                ConsoleLogger.Log($"Field {field.Name} already exists.");
            }
            catch (Exception e)
            {
            }

            if (workItemField == null)
            {
                workItemField = new WorkItemField()
                {
                    Name = field.Name,
                    Type = field.Type
                };

                ConsoleLogger.Log($"Field {field.Name} does not exist. Creating the field..");

                CreateField(connection, workItemField, process);
                workItemField = GetField(connection, field.Name);
            }

            ConsoleLogger.Log($"Adding field to the work item type");
            return(AddFieldToWorkItemType(connection, workItemField, workItemTypeRefName, process));
        }
        private string ValidateString(WorkItemField field)
        {
            if (field.Type == Constants.FieldTypePlainText)
            {
                return(ValidateString(field.Value));
            }

            return(field.Value);
        }
Beispiel #5
0
        public WorkItemField Field_CreatePicklistField()
        {
            //get process id stored in cache so we don't have to load it each time
            System.Guid processId  = Context.GetValue <Guid>("$processId");
            System.Guid picklistId = Context.GetValue <Guid>("$picklistId");

            VssConnection connection          = Context.Connection;
            WorkItemTrackingHttpClient client = connection.GetClient <WorkItemTrackingHttpClient>();
            WorkItemField field = null;

            Console.Write("Searching to see if field '{0}' exists....", _fieldRefName);

            try
            {
                field = client.GetFieldAsync(_fieldRefName).Result;
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message.Contains("TF51535: Cannot find field"))
                {
                    field = null;
                }
            }

            if (field == null)
            {
                Console.WriteLine("field not found");
                Console.Write("Creating new picklist field and setting it to former picklistId....");

                field = new WorkItemField()
                {
                    ReferenceName = _fieldRefName,
                    Name          = "Colors",
                    Description   = "My new field",
                    Type          = TeamFoundation.WorkItemTracking.WebApi.Models.FieldType.String,
                    IsPicklist    = true,
                    PicklistId    = picklistId,
                    Usage         = FieldUsage.WorkItem,
                    ReadOnly      = false,
                    IsIdentity    = false,
                    IsQueryable   = true
                };

                WorkItemField newField = client.CreateFieldAsync(field).Result;

                Console.WriteLine("Done");
                return(newField);
            }

            Console.WriteLine("field found");
            return(field);
        }
        public static WorkItemField GetField(VssConnection connection, string refname)
        {
            WorkItemTrackingHttpClient client = connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                WorkItemField field = client.GetFieldAsync(refname).Result;

                return(field);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #7
0
        public WorkItemField GetFieldDetails()
        {
            string fieldName = "System.Title";

            VssConnection connection = Context.Connection;
            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>();

            WorkItemField workitemField = workItemTrackingClient.GetFieldAsync(fieldName).Result;

            Console.WriteLine("Name: " + workitemField.Name);
            Console.WriteLine("Ref name: " + workitemField.ReferenceName);
            Console.WriteLine("Read only? " + workitemField.ReadOnly);

            return(workitemField);
        }
        public static WorkItemField AddField(VssConnection connection, string refname, string name, string type)
        {
            WorkItemTrackingHttpClient client = connection.GetClient <WorkItemTrackingHttpClient>();

            Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.FieldType fieldType = SetFieldType(type);

            WorkItemField workItemField = new WorkItemField()
            {
                Name          = name,
                ReferenceName = refname,
                Type          = fieldType
            };

            var result = client.CreateFieldAsync(workItemField).Result;

            return(result);
        }
        private WorkItemFields CreateFields(string version, IReadOnlyCollection <WorkItem> releasedWorkItems)
        {
            var titleField   = new WorkItemField("Title", $"Release {version}");
            var versionField = new WorkItemField("Custom.Version", version);

            var releaseLog       = _releaseLogFactory.CreateReleaseLog(version, releasedWorkItems);
            var descriptionField = new WorkItemField("Description", releaseLog);

            var entries = new List <WorkItemField>
            {
                titleField,
                descriptionField,
                versionField
            };

            return(new WorkItemFields(entries));
        }
        private bool CompareField(IValidationContext context, WorkItemField source, WorkItemField target)
        {
            var matches = true;

            if (source.Type != target.Type)
            {
                matches = false;
                Logger.LogWarning(LogDestination.File, $"Target: Field {source.ReferenceName} of type {source.Type} is not of the same type {target.Type}");
            }

            if (matches)
            {
                context.ValidatedFields.Add(source.ReferenceName);
            }
            else
            {
                context.SkippedFields.Add(source.ReferenceName);
            }

            return(matches);
        }
        public static bool TryGetValueIgnoringCase(this ConcurrentDictionary <string, WorkItemField> dictionary, string desiredKeyOfAnyCase, out WorkItemField value)
        {
            var key = GetKeyIgnoringCase(dictionary, desiredKeyOfAnyCase);

            if (key != null)
            {
                return(dictionary.TryGetValue(key, out value));
            }
            else
            {
                value = null;
                return(false);
            }
        }
        static void Main(string[] args)
        {
            var result = Parser.Default.ParseArguments <Options>(args);

            string accountUrl  = null;
            string projectName = null;

            result.WithParsed((options) =>
            {
                accountUrl  = options.AccountUrl;
                projectName = options.ProjectName;
            });

            result.WithNotParsed((e) =>
            {
                ConsoleLogger.LogError("Usage: WorkItemCustomizationSample.exe -a yourAccountUrl -p yourPojectName -c yourAreaPathName -g yourGroupName", true);
            });

            ConsoleLogger.Log("You might see a login screen if you have never signed in to your account using this app.");

            VssConnection connection = new VssConnection(new Uri(accountUrl), new VssClientCredentials());

            string workItemTypeName = "Task";
            string fieldName        = "StringField";

            // todo add sample for picklist field

            ConsoleLogger.Log("Getting team project");
            // Get the team project
            TeamProject project = GetProject(connection, projectName);

            ConsoleLogger.Log($"Getting process for team project {project.Name}");
            Process process = GetProcess(connection, project);

            if (process.Type != ProcessType.Inherited)
            {
                ConsoleLogger.LogError("The process is not an inherited process.", true);
            }

            List <WorkItemTypeModel> workItemTypes = GetProcessWorkItemTypes(connection, process);

            if (!TryGetWorkItemType(workItemTypes, workItemTypeName, out WorkItemTypeModel workItemType))
            {
                ConsoleLogger.LogError("The work item type does not exist.", true);
            }

            string systemTypeRefName  = null;
            string derivedTypeRefName = null;

            if (workItemType.Class == WorkItemTypeClass.Derived)
            {
                systemTypeRefName  = workItemType.Inherits;
                derivedTypeRefName = workItemType.Id;
            }
            else
            {
                systemTypeRefName = workItemType.Id;
            }

            // since the derived type doesnt exists in the process. Lets add one.
            if (string.IsNullOrEmpty(derivedTypeRefName))
            {
                ConsoleLogger.Log("Derived work item type does not exit. Creating a new derived work item type");
                ProcessWorkItemType type = CreateWorkItemType(connection, process, workItemType);

                derivedTypeRefName = type.ReferenceName;
            }

            WorkItemField field = new WorkItemField()
            {
                Name = fieldName,
                Type = Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.FieldType.String
            };

            // add the field to the derived type
            var processWorkItemTypeField = AddFieldToWorkItemType(connection, field, process, derivedTypeRefName);

            ConsoleLogger.Log("Adding field as a control to the layout");
            // add the field as a control on the layout
            AddFieldToWorkItemTypeLayout(connection, process, processWorkItemTypeField, derivedTypeRefName);

            ConsoleLogger.LogSuccess("Field was successfully added to the work item type and layout");
        }
        private static ProcessWorkItemTypeField AddFieldToWorkItemType(VssConnection connection, WorkItemField workItemField, string workItemTypeRefName, Process process)
        {
            var request = new AddProcessWorkItemTypeFieldRequest()
            {
                ReferenceName = workItemField.ReferenceName
            };

            var processClient = connection.GetClient <WorkItemTrackingProcessHttpClient>();

            return(processClient.AddFieldToWorkItemTypeAsync(request, process.Id, workItemTypeRefName).Result);
        }
        private static WorkItemField CreateField(VssConnection connection, WorkItemField field, Process process)
        {
            var workClient = connection.GetClient <WorkItemTrackingHttpClient>();

            return(workClient.CreateFieldAsync(field).Result);
        }
Beispiel #15
0
        public static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                ShowUsage();
                return(0);
            }

            args = SetArgumentsFromConfig(args);

            string org, pat, process, project, refname, name, type, action;
            string witrefname, targetprocess;

            try
            {
                CheckArguments(args, out org, out pat, out project, out refname, out name, out type, out action, out process, out witrefname, out targetprocess);

                Uri baseUri = new Uri(org);

                VssCredentials clientCredentials = new VssCredentials(new VssBasicCredential("username", pat));
                VssConnection  vssConnection     = new VssConnection(baseUri, clientCredentials);

                if (action == "clonewit")
                {
                    Console.WriteLine("Start Validation...");

                    bool val = CloneWitAndProcessValidation(vssConnection, process, targetprocess, witrefname);

                    if (!val)
                    {
                        return(0);
                    }
                }

                //action out all fields
                if (action == "listallfields")
                {
                    var fields = Repos.Fields.GetAllFields(vssConnection);

                    var table = new ConsoleTable("Name", "Reference Name", "Type");

                    foreach (WorkItemField field in fields)
                    {
                        table.AddRow(field.Name, field.ReferenceName, field.Type);
                    }

                    table.Write();
                    Console.WriteLine();

                    return(0);
                }

                //get one field by refname and me all of the processes that field is in
                if (action == "getfield" && (!String.IsNullOrEmpty(refname)))
                {
                    List <ProcessInfo>         processList = Repos.Process.GetProcesses(vssConnection);
                    List <ProcessWorkItemType> witList;

                    var table = new ConsoleTable("Process", "Work Item Type", "Field Name", "Field Reference Name");

                    foreach (var processInfo in processList)
                    {
                        witList = Repos.Process.GetWorkItemTypes(vssConnection, processInfo.TypeId);

                        foreach (var witItem in witList)
                        {
                            ProcessWorkItemTypeField witField = Repos.Process.GetField(vssConnection, processInfo.TypeId, witItem.ReferenceName, refname);

                            if (witField != null)
                            {
                                table.AddRow(processInfo.Name, witItem.Name, witField.Name, witField.ReferenceName);
                            }

                            witField = null;
                        }
                    }

                    table.Write();
                    Console.WriteLine();
                }

                if (action == "getfieldforprojects" && (!String.IsNullOrEmpty(refname)))
                {
                    Console.WriteLine("Getting list of projects and work item types...");
                    Console.WriteLine();

                    var table = new ConsoleTable("Project", "Work Item Type", "Field Reference Name", "Field Name");
                    WorkItemTypeFieldWithReferences field;

                    List <TeamProjectReference> projectList = Repos.Projects.GetAllProjects(vssConnection);
                    List <WorkItemType>         witList     = null;

                    foreach (TeamProjectReference projectItem in projectList)
                    {
                        witList = Repos.WorkItemTypes.GetWorkItemTypesForProject(vssConnection, projectItem.Name);

                        foreach (WorkItemType witItem in witList)
                        {
                            field = Repos.Fields.GetFieldForWorkItemType(vssConnection, projectItem.Name, witItem.ReferenceName, refname);

                            if (field != null)
                            {
                                table.AddRow(projectItem.Name, witItem.ReferenceName, field.ReferenceName, field.Name);
                            }

                            field = null;
                        }
                    }

                    table.Write();
                    Console.WriteLine();

                    field   = null;
                    table   = null;
                    witList = null;
                }

                if (action == "searchfields")
                {
                    var fields = Repos.Fields.SearchFields(vssConnection, name, type);

                    if (fields.Count == 0)
                    {
                        Console.WriteLine("No fields found for name: '" + name + "' or type: '" + type + "'");
                        return(0);
                    }

                    var table = new ConsoleTable("Name", "Reference Name", "Type");

                    foreach (WorkItemField field in fields)
                    {
                        table.AddRow(field.Name, field.ReferenceName, field.Type);
                    }

                    table.Write();
                    Console.WriteLine();

                    return(0);
                }

                //add new field to the organization
                if (action == "addfield")
                {
                    //check to see if the type is a legit type
                    int pos = Array.IndexOf(Repos.Fields.Types, type);

                    if (pos == -1)
                    {
                        var types = Repos.Fields.Types;

                        Console.WriteLine("Invalid field type value '" + type + "'");
                        Console.WriteLine();
                        Console.WriteLine("Valid field types are:");
                        Console.WriteLine();

                        foreach (string item in types)
                        {
                            Console.WriteLine(item);
                        }

                        return(0);
                    }

                    //check and make sure the field does not yet exist
                    var field = Repos.Fields.GetField(vssConnection, refname);

                    if (field != null)
                    {
                        Console.WriteLine("Field already exists");
                        Console.WriteLine();

                        var table = new ConsoleTable("Name", "Reference Name", "Type");

                        table.AddRow(field.Name, field.ReferenceName, field.Type);

                        table.Write();
                        Console.WriteLine();

                        return(0);
                    }

                    WorkItemField newField = Repos.Fields.AddField(vssConnection, refname, name, type);

                    if (newField != null)
                    {
                        Console.WriteLine("Field '" + refname + "' was successfully added");
                    }
                }

                if (action == "listfieldsforprocess")
                {
                    List <FieldsPerProcess> list = Repos.Fields.ListFieldsForProcess(vssConnection, process);

                    var table = new ConsoleTable("Work Item Type", "Name", "Reference Name", "Type");

                    foreach (FieldsPerProcess item in list)
                    {
                        List <ProcessWorkItemTypeField> fields = item.fields;

                        foreach (ProcessWorkItemTypeField field in fields)
                        {
                            table.AddRow(item.workItemType.Name, field.Name, field.ReferenceName, field.Type);
                        }
                    }

                    table.Write();
                    Console.WriteLine();

                    return(0);
                }

                vssConnection = null;
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);

                ShowUsage();
                return(-1);
            }

            return(0);
        }