private async Task AddTaskGroupFromTeamProject(object argument)
        {
            using (var dialog = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false))
            {
                var result = dialog.ShowDialog(Application.Current.MainWindow.GetIWin32Window());
                if (result == System.Windows.Forms.DialogResult.OK && dialog.SelectedProjects != null && dialog.SelectedProjects.Length > 0)
                {
                    var teamProjectCollection = dialog.SelectedTeamProjectCollection;
                    var teamProject           = dialog.SelectedProjects.First();
                    var taskAgentClient       = teamProjectCollection.GetClient <TaskAgentHttpClient>();
                    var taskGroups            = await taskAgentClient.GetTaskGroupsAsync(teamProject.Name);

                    if (!taskGroups.Any())
                    {
                        MessageBox.Show("The selected Team Project does not contain any task groups.", "No Task Groups", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else
                    {
                        var picker = new ItemsPickerDialog();
                        picker.ItemDisplayMemberPath = nameof(TaskGroup.Name);
                        picker.Owner          = Application.Current.MainWindow;
                        picker.Title          = "Select the task groups to add";
                        picker.SelectionMode  = SelectionMode.Multiple;
                        picker.AvailableItems = taskGroups;
                        if (picker.ShowDialog() == true)
                        {
                            foreach (var taskGroupToImport in picker.SelectedItems.Cast <TaskGroup>().ToArray())
                            {
                                this.TaskGroupsToImport.Add(taskGroupToImport);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void importFromRegisteredProcessTemplateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var dialog = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false))
                {
                    var result = dialog.ShowDialog(Application.Current.MainWindow.GetIWin32Window());
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        var projectCollection      = dialog.SelectedTeamProjectCollection;
                        var processTemplateService = projectCollection.GetService <IProcessTemplates>();
                        var registeredTemplates    = processTemplateService.TemplateHeaders().OrderBy(t => t.Rank).ToArray();

                        var processTemplatePicker = new ItemsPickerDialog();
                        processTemplatePicker.Title                 = "Please select a Process Template";
                        processTemplatePicker.SelectionMode         = SelectionMode.Single;
                        processTemplatePicker.AvailableItems        = registeredTemplates;
                        processTemplatePicker.ItemDisplayMemberPath = nameof(TemplateHeader.Name);
                        processTemplatePicker.Owner                 = this;
                        if (processTemplatePicker.ShowDialog() == true)
                        {
                            var selectedProcessTemplate = (TemplateHeader)processTemplatePicker.SelectedItem;
                            if (selectedProcessTemplate != null)
                            {
                                var downloadedTemplateZipFileName = processTemplateService.GetTemplateData(selectedProcessTemplate.TemplateId);
                                var unzipPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                                try
                                {
                                    ZipFile.ExtractToDirectory(downloadedTemplateZipFileName, unzipPath);
                                    var processTemplateXmlFile = Path.Combine(unzipPath, "ProcessTemplate.xml");
                                    if (!File.Exists(processTemplateXmlFile))
                                    {
                                        MessageBox.Show("The selected Process Template did not contain a \"ProcessTemplate.xml\" file in its root directory.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                                    }
                                    else
                                    {
                                        this.Configuration = WorkItemConfiguration.FromProcessTemplate(processTemplateXmlFile);
                                    }
                                }
                                finally
                                {
                                    File.Delete(downloadedTemplateZipFileName);
                                    Directory.Delete(unzipPath, true);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                ShowException(exc);
            }
        }
        private async Task <ICollection <BuildDefinitionTemplate> > GetBuildTemplatesAsync(TfsMajorVersion tfsVersion, bool fromBuildDefinitions)
        {
            var buildTemplates = new List <BuildDefinitionTemplate>();

            using (var dialog = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false))
            {
                var result = dialog.ShowDialog(Application.Current.MainWindow.GetIWin32Window());
                if (result == System.Windows.Forms.DialogResult.OK && dialog.SelectedProjects != null && dialog.SelectedProjects.Length > 0)
                {
                    var teamProjectCollection = dialog.SelectedTeamProjectCollection;
                    var teamProject           = dialog.SelectedProjects.First();
                    var buildServer           = teamProjectCollection.GetClient <BuildHttpClient>();

                    if (fromBuildDefinitions)
                    {
                        var buildDefinitions = new List <BuildDefinition>();
                        if (tfsVersion == TfsMajorVersion.TeamServices)
                        {
                            // Use a single API call if available.
                            var projectBuildDefinitions = await buildServer.GetFullDefinitionsAsync(project : teamProject.Name);

                            buildDefinitions.AddRange(projectBuildDefinitions);
                        }
                        else
                        {
                            // Otherwise get the base info first and then individual details.
                            var baseBuildDefinitions = await buildServer.GetDefinitionsAsync(project : teamProject.Name);

                            foreach (var baseBuildDefinition in baseBuildDefinitions.Where(b => b.Type == DefinitionType.Build))
                            {
                                var buildDefinition = await buildServer.GetDefinitionAsync(project : teamProject.Name, definitionId : baseBuildDefinition.Id);

                                buildDefinitions.Add(buildDefinition);
                            }
                        }
                        buildDefinitions = buildDefinitions.Where(b => b.Type == DefinitionType.Build).OrderBy(b => b.Name).ToList();

                        if (!buildDefinitions.Any())
                        {
                            MessageBox.Show("The selected Team Project does not contain any build definitions.", "No Build Definitions", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        else
                        {
                            foreach (var buildDefinition in buildDefinitions)
                            {
                                var template = new BuildDefinitionTemplate
                                {
                                    Id          = buildDefinition.Name.Replace(" ", ""),
                                    Name        = buildDefinition.Name,
                                    Description = "Build template based on build definition \"{0}\" in Team Project \"{1}\"".FormatCurrent(buildDefinition.Name, teamProject.Name),
                                    Template    = buildDefinition
                                };
                                buildTemplates.Add(template);
                            }
                        }
                    }
                    else
                    {
                        var buildTemplatesFromProject = await buildServer.GetTemplatesAsync(project : teamProject.Name);

                        buildTemplatesFromProject = buildTemplatesFromProject.Where(t => t.Template != null && t.Template.Project != null).OrderBy(t => t.Name).ToList();

                        if (!buildTemplatesFromProject.Any())
                        {
                            MessageBox.Show("The selected Team Project does not contain any build templates.", "No Build Templates", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        else
                        {
                            foreach (var buildTemplate in buildTemplatesFromProject)
                            {
                                buildTemplates.Add(buildTemplate);
                            }
                        }
                    }
                }
            }

            if (buildTemplates.Any())
            {
                var picker = new ItemsPickerDialog();
                picker.ItemDisplayMemberPath = nameof(BuildDefinitionTemplate.Name);
                picker.Owner          = Application.Current.MainWindow;
                picker.Title          = fromBuildDefinitions ? "Select the build definitions to import" : "Select the build templates to import";
                picker.SelectionMode  = SelectionMode.Multiple;
                picker.AvailableItems = buildTemplates;
                if (picker.ShowDialog() == true)
                {
                    return(picker.SelectedItems.Cast <BuildDefinitionTemplate>().ToArray());
                }
            }

            return(new BuildDefinitionTemplate[0]);
        }