Skip to content

Commit

Permalink
AddLinkToWorkItem, UpdateBugByPassingRules
Browse files Browse the repository at this point in the history
  • Loading branch information
danhellem committed Sep 15, 2016
1 parent 346dc15 commit 7dea279
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<Reference Include="System" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\VSTSRestApiSamples\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3-beta1\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
Expand Down
30 changes: 30 additions & 0 deletions VSTSRestApiSamples.UnitTests/WorkItemTracking/SamplesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ public void WorkItemTracking_Samples_UpdateBug_Success()
samples = null;
}

[TestMethod, TestCategory("REST API")]
public void WorkItemTracking_Samples_AddLinkToBug_Success()
{
//arrange
Samples samples = new Samples(_configuration);

//act
var response = samples.AddLinkToBug();

//assert
Assert.AreEqual("success", response);

samples = null;
}

[TestMethod, TestCategory("REST API")]
public void WorkItemTracking_Samples_UpdateBugByPassingRules_Success()
{
//arrange
Samples samples = new Samples(_configuration);

//act
var response = samples.UpdateBugByPassingRules();

//assert
Assert.AreEqual("success", response);

samples = null;
}

[TestMethod, TestCategory("REST API")]
public void WorkItemTracking_Samples_AddCommentToBug_Success()
{
Expand Down
17 changes: 17 additions & 0 deletions VSTSRestApiSamples.UnitTests/WorkItemTracking/WorkItemsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public void WorkItemTracking_WorkItems_UpdateWorkItemWithByPassRules_Success()
request = null;
}

[TestMethod, TestCategory("REST API")]
public void WorkItemTracking_WorkItems_AddLink_Success()
{
//arrange
WorkItems request = new WorkItems(_configuration);

string[] arr = _configuration.WorkItemIds.Split(',');

//act
WorkItemPatchResponse.WorkItem response = request.AddLink(arr[0].ToString(), arr[1].ToString());

//assert
Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode);

request = null;
}

[TestMethod, TestCategory("REST API")]
public void WorkItemTracking_WorkItems_AddAndUpdateWorkItemTags_Success()
{
Expand Down
2 changes: 1 addition & 1 deletion VSTSRestApiSamples.UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3-beta1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
</packages>
12 changes: 12 additions & 0 deletions VSTSRestApiSamples/ViewModels/WorkItemTracking/WorkItemPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@ public class Field
public string path { get; set; }
public object value { get; set; }
}

public class Value
{
public string rel { get; set; }
public string url { get; set; }
public Attributes attributes { get; set; }
}

public class Attributes
{
public string comment { get; set; }
}
}
}
6 changes: 3 additions & 3 deletions VSTSRestApiSamples/VstsRestApiSamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3-beta1\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
Expand Down
81 changes: 81 additions & 0 deletions VSTSRestApiSamples/WorkItemTracking/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,87 @@ public string UpdateBug()
}
}

public string UpdateBugByPassingRules()
{
string _id = _configuration.WorkItemId;

Object[] patchDocument = new Object[1];

//change some values on a few fields
patchDocument[0] = new { op = "add", path = "/fields/System.AssignedTo", value = "Invalid User" };

using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

//serialize the fields array into a json string
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json"); //mediaType needs to be application/json-patch+json for a patch call

//set the httpmethod to Patch
var method = new HttpMethod("PATCH");

//send the request
var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/workitems/" + _id + "?bypassRules=true&api-version=2.2") { Content = patchValue };
var response = client.SendAsync(request).Result;

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}

return "success";
}
}

public string AddLinkToBug()
{
string _id = _configuration.WorkItemId;
string _linkToId = _configuration.WorkItemIds.Split(',')[0];

Object[] patchDocument = new Object[1];

//change some values on a few fields
patchDocument[0] = new {
op = "add",
path = "/relations/-",
value = new
{
rel = "System.LinkTypes.Dependency-forward",
url = _configuration.UriString + "/_apis/wit/workitems/" + _linkToId,
attributes = new
{
comment = "Making a new link for the dependency"
}
}
};

using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

//serialize the fields array into a json string
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json"); //mediaType needs to be application/json-patch+json for a patch call

//set the httpmethod to Patch
var method = new HttpMethod("PATCH");

//send the request
var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/workitems/" + _id + "?api-version=2.2") { Content = patchValue };
var response = client.SendAsync(request).Result;

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}

return "success";
}
}

public string AddCommentToBug()
{
string _id = _configuration.WorkItemId;
Expand Down
48 changes: 48 additions & 0 deletions VSTSRestApiSamples/WorkItemTracking/WorkItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,54 @@ public BatchOfWorkItemRevisionsResponse.WorkItemRevisions GetBatchOfWorkItemRevi
}
}

public WorkItemPatchResponse.WorkItem AddLink(string id, string linkToId)
{
WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
WorkItemPatch.Field[] fields = new WorkItemPatch.Field[1];

//change some values on a few fields
fields[0] = new WorkItemPatch.Field()
{
op = "add",
path = "/relations/-",
value = new WorkItemPatch.Value()
{
rel = "System.LinkTypes.Dependency-forward",
url = _configuration.UriString + "/_apis/wit/workitems/" + linkToId,
attributes = new WorkItemPatch.Attributes()
{
comment = "Making a new link for the dependency"
}
}
};

using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

//serialize the fields array into a json string
var patchValue = new StringContent(JsonConvert.SerializeObject(fields), Encoding.UTF8, "application/json-patch+json"); //mediaType needs to be application/json-patch+json for a patch call

//set the httpmethod to Patch
var method = new HttpMethod("PATCH");

//send the request
var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2") { Content = patchValue };
var response = client.SendAsync(request).Result;

if (response.IsSuccessStatusCode)
{
viewModel = response.Content.ReadAsAsync<WorkItemPatchResponse.WorkItem>().Result;
}

viewModel.HttpStatusCode = response.StatusCode;

return viewModel;
}
}

/// <summary>
/// manage tags on a work item
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion VSTSRestApiSamples/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
4 changes: 2 additions & 2 deletions VSTSRestApiSamples/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3-beta1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
</packages>
25 changes: 25 additions & 0 deletions VstsClientLibrariesSamples.Tests/WorkItemTracking/SampleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ public void WorkItemTracking_Sample_UpdateBug_Success()
Assert.AreEqual("success", result);
}

[TestMethod, TestCategory("Client Libraries")]
public void WorkItemTracking_Sample_UpdateBugUsingByPassRules_Success()
{
//arrange
Sample sample = new Sample(_configuration);

//act
var result = sample.UpdateBugUsingByPassRules();

Assert.AreEqual("success", result);
}

[TestMethod, TestCategory("Client Libraries")]
public void WorkItemTracking_Samples_AddLinkToBug_Success()
{
//arrange
Sample sample = new Sample(_configuration);

//act
var result = sample.AddLinkToBug();

//assert
Assert.AreEqual("success", result);
}

[TestMethod, TestCategory("Client Libraries")]
public void WorkItemTracking_Sample_AddCommentsToBug_Success()
{
Expand Down
13 changes: 13 additions & 0 deletions VstsClientLibrariesSamples.Tests/WorkItemTracking/WorkItemsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public void WorkItemTracking_WorkItems_GetWorkItem_Success()
Assert.AreEqual("success", result);
}

[TestMethod, TestCategory("Client Libraries")]
public void WorkItemTracking_WorkItems_AddLink_Success()
{
//arrange
WorkItems workItems = new WorkItems(_configuration);

string[] arr = _configuration.WorkItemIds.Split(',');

//act
var result = workItems.AddLink(Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]));

Assert.AreEqual("success", result);
}

}
}
4 changes: 2 additions & 2 deletions VstsClientLibrariesSamples.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<add key="appsetting.project" value="" />
<add key="appsetting.query" value="Shared Queries/Current Iteration/Open User Stories" />
<add key="appsetting.identity" value="" />
<add key="appsetting.workitemids" value=""/>
<add key="appsetting.workitemid" value=""/>
<add key="appsetting.workitemids" value="" />
<add key="appsetting.workitemid" value="" />
</appSettings>
</configuration>
Loading

0 comments on commit 7dea279

Please sign in to comment.