From fbd3f73e136a74247fa42f215e66ffc4151ac534 Mon Sep 17 00:00:00 2001 From: Bo Date: Tue, 29 Jan 2019 20:43:00 -0800 Subject: [PATCH] Add more ClientSampleMethod --- .../Build/BuildsSample.cs | 101 ++++++++++- .../WorkItemTracking/FieldsSample.cs | 40 +++++ .../WorkItemTracking/WorkItemsSample.cs | 108 ++++++++++- .../WorkItemTrackingProcess/ProcessHelper.cs | 14 +- .../ProcessesSample.cs | 169 ++++++++++++++++-- 5 files changed, 413 insertions(+), 19 deletions(-) diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Build/BuildsSample.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Build/BuildsSample.cs index 499bce4..aef00e3 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Build/BuildsSample.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/Build/BuildsSample.cs @@ -1,4 +1,5 @@ using Microsoft.TeamFoundation.Build.WebApi; +using Microsoft.TeamServices.Samples.Client.Build; using Microsoft.VisualStudio.Services.WebApi; using System; using System.Collections.Generic; @@ -10,9 +11,8 @@ namespace Microsoft.TeamServices.Samples.Client.Build { [ClientSample(BuildResourceIds.AreaName, BuildResourceIds.BuildsResource)] public class BuildsSample : ClientSample - { + { [ClientSampleMethod] - public IEnumerable ListBuildDefinitions() { string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name; @@ -28,14 +28,14 @@ namespace Microsoft.TeamServices.Samples.Client.Build do { IPagedList buildDefinitionsPage = buildClient.GetDefinitionsAsync2( - project: projectName, + project: projectName, continuationToken: continuationToken).Result; buildDefinitions.AddRange(buildDefinitionsPage); continuationToken = buildDefinitionsPage.ContinuationToken; } while (!String.IsNullOrEmpty(continuationToken)); - + // Show the build definitions foreach (BuildDefinitionReference definition in buildDefinitions) { @@ -44,5 +44,98 @@ namespace Microsoft.TeamServices.Samples.Client.Build return buildDefinitions; } + + [ClientSampleMethod] + public BuildArtifact CreateArtifact() + { + BuildArtifact result = new BuildArtifact(); + // Get a build client instance + VssConnection connection = Context.Connection; + BuildHttpClient buildClient = connection.GetClient(); + + ArtifactResource newArtifactResource = new ArtifactResource() + { + Type = "new ArtifactResource type", + Data = "new ArtifactResource Data", + Properties = null, + Url = "new Url", + DownloadUrl = "new DownloadUrl", + DownloadTicket = "new downloadticket" + }; + + + BuildArtifact newArtifact = new BuildArtifact() + { + Id = 900, + Name = "New test artifact", + Resource = newArtifactResource + }; + + BuildArtifact newArtifact2 = new BuildArtifact() + { + Id = 901, + Name = "New test artifact 2", + Resource = newArtifactResource + }; + + try + { + result = buildClient.CreateArtifactAsync(newArtifact, 1).Result; + result = buildClient.CreateArtifactAsync(newArtifact2, 1).Result; + + Console.WriteLine("success"); + Console.WriteLine("{0}", newArtifact.Name); + } + catch(Exception ex) + { + Console.WriteLine("failed"); + Console.WriteLine("Error creating artifact: " + ex.InnerException.Message); + } + + + return result; + } + + [ClientSampleMethod] + public List GetAllArtifacts() + { + // Get a build client instance + VssConnection connection = Context.Connection; + BuildHttpClient buildClient = connection.GetClient(); + + List artifactsList = buildClient.GetArtifactsAsync(1).Result; + + return artifactsList; + } + + [ClientSampleMethod] + public BuildArtifact GetArtifact() + { + VssConnection connection = Context.Connection; + BuildHttpClient buildClient = connection.GetClient(); + + BuildArtifact result = buildClient.GetArtifactAsync("Test Project", 1, "New test artifact").Result; + + return result; + } + + [ClientSampleMethod] + public void GetFile() + { + System.Diagnostics.Debugger.Launch(); + VssConnection connection = Context.Connection; + BuildHttpClient buildClient = connection.GetClient(); + + //int buildId, string artifactName, string fileId, string fileName + try + { + var result = buildClient.GetFileAsync(1, "New test artifact", "1", "test file").Result; + Console.WriteLine("Get file successed"); + } + catch(Exception e) + { + Console.WriteLine("Get File failed: " + e.Message); + } + } } } diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/FieldsSample.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/FieldsSample.cs index b1c2b64..19a27f1 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/FieldsSample.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/FieldsSample.cs @@ -18,6 +18,46 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTracking [ClientSample(WitConstants.WorkItemTrackingWebConstants.RestAreaName, WitConstants.WorkItemTrackingRestResources.Fields)] public class FieldsSample : ClientSample { + [ClientSampleMethod] + public WorkItemField CreateWorkItemField() + { + WorkItemField result = new WorkItemField(); + + WorkItemField newWorkItemField = new WorkItemField() + { + Name = "New Work Item Field", + ReferenceName = "SupportedOperations.GreaterThanEquals", + Description = null, + Type = FieldType.String, + Usage = FieldUsage.WorkItem, + ReadOnly = false, + CanSortBy = true, + IsQueryable = true, + SupportedOperations = new List() + { + new WorkItemFieldOperation(){ ReferenceName="SupportedOperations.Equals", Name="="} + }, + IsIdentity = true, + IsPicklist = false, + IsPicklistSuggested = false + + }; + + VssConnection connection = Context.Connection; + WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient(); + + try + { + result = workItemTrackingClient.CreateFieldAsync(newWorkItemField).Result; + Console.WriteLine("Work Item Field Created."); + } + catch + { + Console.WriteLine("Work Item Field Failed."); + } + + return result; + } [ClientSampleMethod] public WorkItemField GetFieldDetails() diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/WorkItemsSample.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/WorkItemsSample.cs index 2a4ef6f..21a6b32 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/WorkItemsSample.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTracking/WorkItemsSample.cs @@ -1,4 +1,5 @@ using Microsoft.TeamFoundation.Core.WebApi; +using Microsoft.TeamFoundation.Core.WebApi.Types; using Microsoft.TeamFoundation.SourceControl.WebApi; using Microsoft.TeamFoundation.WorkItemTracking.WebApi; using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models; @@ -997,6 +998,111 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTracking WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, workItemReference.Id).Result; } } - + + [ClientSampleMethod] + public WorkItem GetWorkItemTemplate() + { + VssConnection connection = Context.Connection; + WorkItemTrackingHttpClient client = connection.GetClient(); + + WorkItem result = client.GetWorkItemTemplateAsync("Test Project", "Bug").Result; + + return result; + } + + [ClientSampleMethod] + public WorkItemTemplate CreateTemplate() + { + VssConnection connection = Context.Connection; + WorkItemTrackingHttpClient client = connection.GetClient(); + + Dictionary field = new Dictionary + { + { "System.State", "New" } + }; + + WorkItemTemplate template = new WorkItemTemplate() + { + Name = "Test Template", + Description = "", + WorkItemTypeName = "Feature", + Fields = field + }; + TeamContext teamContext = new TeamContext("Test Project", "Test Project Team"); + WorkItemTemplate result = null; + + try + { + result = client.CreateTemplateAsync(template, teamContext).Result; + Console.WriteLine("Create Work Item Template Successed."); + } + catch (Exception e) + { + Console.WriteLine("Create Work Item Template Failed:" + e.Message); + } + + return result; + } + + [ClientSampleMethod] + public List ListTemplates() + { + TeamContext teamContext = new TeamContext("Test Project", "Test Project Team"); + VssConnection connection = Context.Connection; + WorkItemTrackingHttpClient client = connection.GetClient(); + + List result = client.GetTemplatesAsync(teamContext).Result; + + return result; + } + + [ClientSampleMethod] + public WorkItem GetTemplate() + { + VssConnection connection = Context.Connection; + WorkItemTrackingHttpClient client = connection.GetClient(); + + WorkItem result = client.GetWorkItemTemplateAsync("Test Project", "Feature").Result; + + return result; + } + + /*To make this work, first run create template. Then comment out create template and replace the templateID*/ + [ClientSampleMethod] + public WorkItemTemplate ReplaceTemplate() + { + VssConnection connection = Context.Connection; + WorkItemTrackingHttpClient client = connection.GetClient(); + + Guid templateID = new Guid("d35d406c-32fa-4316-ba6e-76bb564beaaa"); /*Replace template ID from get template*/ + Dictionary field = new Dictionary + { + { "System.State", "Old" } + }; + + WorkItemTemplate template = new WorkItemTemplate() + { + Name = "Test Template", + Description = "", + WorkItemTypeName = "Feature", + Fields = field + }; + TeamContext teamContext = new TeamContext("Test Project", "Test Project Team"); + WorkItemTemplate result = null; + + try + { + result = client.ReplaceTemplateAsync(template, teamContext, templateID).Result; + Console.WriteLine("Replace template successed."); + } + catch(Exception e) + { + result = null; + Console.WriteLine("Replace template failed: " + e.Message); + } + + return result; + } + } } diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessHelper.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessHelper.cs index 99bba6f..fd429ae 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessHelper.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessHelper.cs @@ -8,7 +8,7 @@ using System.Collections.Generic; namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess { public static class ProcessHelper - { + { public static Page getPage(FormLayout layout, string pageName) { List pages = layout.Pages as List; @@ -18,12 +18,12 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess } public static Section getSection(FormLayout layout, string pageName, string sectionName) - { + { Page page = getPage(layout, pageName); - + List
sections = page.Sections as List
; Section section = sections.Find(x => x.Id == sectionName); - + return section; } @@ -37,4 +37,10 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess return group; } } + static class OutOfBoxProcessTemplateTypeIds + { + public static readonly Guid Agile = new Guid("ADCC42AB-9882-485E-A3ED-7678F01F66BC"); + public static readonly Guid Scrum = new Guid("6B724908-EF14-45CF-84F8-768B5384DA45"); + public static readonly Guid Cmmi = new Guid("27450541-8E31-4150-9947-DC59F998FC01"); + } } diff --git a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessesSample.cs b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessesSample.cs index e696eb5..ee2e874 100644 --- a/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessesSample.cs +++ b/ClientLibrary/Snippets/Microsoft.TeamServices.Samples.Client/WorkItemTrackingProcess/ProcessesSample.cs @@ -130,6 +130,20 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess return processInfo; } + [ClientSampleMethod] + public ProcessInfo Process_GetById() + { + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + //extra step to get the process by name + //you should not have to do this + var processInfo = client.GetProcessByItsIdAsync(OutOfBoxProcessTemplateTypeIds.Agile).Result; + + return processInfo; + } + [ClientSampleMethod] public List WorkItemTypes_List() { @@ -151,6 +165,69 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess return list; } + [ClientSampleMethod] + public List WorkItemTypes_List_Expand_State() + { + //get process id stored in cache so we don't have to load it each time + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + Console.Write("Getting list of work item types for '" + processId.ToString() + "'..."); + List list = client.GetProcessWorkItemTypesAsync(processId, expand:GetWorkItemTypeExpand.States).Result; + Console.WriteLine("success"); + + foreach (var item in list) + { + Console.WriteLine("{0} : {1}", item.Name, item.ReferenceName); + } + + return list; + } + + [ClientSampleMethod] + public List WorkItemTypes_List_Expand_Behaviors() + { + //get process id stored in cache so we don't have to load it each time + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + Console.Write("Getting list of work item types for '" + processId.ToString() + "'..."); + List list = client.GetProcessWorkItemTypesAsync(processId, expand: GetWorkItemTypeExpand.Behaviors).Result; + Console.WriteLine("success"); + + foreach (var item in list) + { + Console.WriteLine("{0} : {1}", item.Name, item.ReferenceName); + } + + return list; + } + + [ClientSampleMethod] + public List WorkItemTypes_List_Expand_Layout() + { + //get process id stored in cache so we don't have to load it each time + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + Console.Write("Getting list of work item types for '" + processId.ToString() + "'..."); + List list = client.GetProcessWorkItemTypesAsync(processId, expand: GetWorkItemTypeExpand.Layout).Result; + Console.WriteLine("success"); + + foreach (var item in list) + { + Console.WriteLine("{0} : {1}", item.Name, item.ReferenceName); + } + + return list; + } + [ClientSampleMethod] public ProcessWorkItemType WorkItemTypes_Create() { @@ -158,6 +235,7 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess System.Guid processId = Context.GetValue("$processId"); ProcessWorkItemType processWorkItemType = null; + ProcessWorkItemType creatProcessWorkItemTypeResult = null; CreateProcessWorkItemTypeRequest createWorkItemType = new CreateProcessWorkItemTypeRequest() { @@ -186,7 +264,7 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess try { //create new work item type - processWorkItemType = client.CreateProcessWorkItemTypeAsync(createWorkItemType, processId).Result; + creatProcessWorkItemTypeResult = client.CreateProcessWorkItemTypeAsync(createWorkItemType, processId).Result; Console.WriteLine("success"); Console.WriteLine("{0} : {1}", processWorkItemType.Name, processWorkItemType.ReferenceName); @@ -205,6 +283,25 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess Context.SetValue("$newWorkItemType", processWorkItemType); + return creatProcessWorkItemTypeResult; + } + + [ClientSampleMethod] + public ProcessWorkItemType WorkItemType_Get() + { + ProcessWorkItemType processWorkItemType = null; + + //get process id stored in cache so we don't have to load it each time + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + //load the process by id + processWorkItemType = client.GetProcessWorkItemTypeAsync(processId, _witRefName).Result; + + Console.WriteLine("Getting work item type for " + _refName); + return processWorkItemType; } @@ -313,28 +410,28 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess [ClientSampleMethod] public PickListMetadata Field_CreatePicklist() - { + { List pickListMetadata = null; PickList picklist = null; string pickListName = "colorsPicklist"; VssConnection connection = Context.Connection; WorkItemTrackingProcessHttpClient client = connection.GetClient(); - + Console.Write("Searching to see if picklist '{0}' exists....", pickListName); - + pickListMetadata = client.GetListsMetadataAsync().Result; PickListMetadata item = pickListMetadata.Find(x => x.Name == pickListName); - + if (item != null) { Context.SetValue("$picklistId", item.Id); Console.WriteLine("picklist found"); - + return item; } else - { + { Console.WriteLine("picklist not found"); Console.Write("Creating new picklist...."); @@ -349,16 +446,16 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess { Name = pickListName, Items = list, - Type = "String", + Type = "String", IsSuggested = false - }; + }; PickList result = client.CreateListAsync(picklist).Result; Context.SetValue("$picklistId", result.Id); Console.WriteLine("done"); return result; - } + } } [ClientSampleMethod] @@ -415,6 +512,7 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess return field; } + [ClientSampleMethod] public ProcessWorkItemTypeField Field_AddFieldToWorkItemType() { @@ -460,5 +558,56 @@ namespace Microsoft.TeamServices.Samples.Client.WorkItemTrackingProcess return processWorkItemTypeField; } } + + [ClientSampleMethod] + public List Field_GetAllWorkItemTypeFieldsAsync() + { + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + //get the list of fields on the work item item + Console.Write("Loading list of fields on the work item and checking to see if field '{0}' already exists...", _fieldRefName); + + List list = client.GetAllWorkItemTypeFieldsAsync(processId, _witRefName).Result; + + return list; + } + + [ClientSampleMethod] + public ProcessWorkItemTypeField Field_GetWorkItemTypeField() + { + ProcessWorkItemTypeField processWorkItemTypeField = null; + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + processWorkItemTypeField = client.GetWorkItemTypeFieldAsync(processId, _witRefName, _fieldRefName).Result; + + return processWorkItemTypeField; + } + + [ClientSampleMethod] + public ProcessWorkItemTypeField Field_UpdateWorkItemTypeField() + { + UpdateProcessWorkItemTypeFieldRequest newfieldRequest = new UpdateProcessWorkItemTypeFieldRequest() + { + ReadOnly = false, + Required = false, + DefaultValue = "Blue", + AllowGroups = false + }; + + System.Guid processId = Context.GetValue("$processId"); + + VssConnection connection = Context.Connection; + WorkItemTrackingProcessHttpClient client = connection.GetClient(); + + ProcessWorkItemTypeField processWorkItemTypeField = client.UpdateWorkItemTypeFieldAsync(newfieldRequest, processId, _witRefName, _fieldRefName).Result; + + return processWorkItemTypeField; + } } }