Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. #r "Newtonsoft.Json"
  2. #r "System.Configuration"
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Http;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using Microsoft.TeamFoundation.Build.WebApi;
  16. using Microsoft.VisualStudio.Services.Common;
  17. using Newtonsoft.Json;
  18. using Newtonsoft.Json.Linq;
  19.  
  20. public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
  21. {
  22. string jsonContent = await req.Content.ReadAsStringAsync();
  23.  
  24. // Deserialize Json
  25. var jsonObject = JsonConvert.DeserializeObject<VSTSWebHook>(jsonContent);
  26.  
  27. // From ApplicationSettings
  28. var vstsUser = ConfigurationManager.AppSettings["VstsUser"];
  29. var vstsPassword = ConfigurationManager.AppSettings["VstsPassword"];
  30.  
  31. // Create VSTS Connection from Json Value
  32. var project = new Guid(jsonObject.resource.definition.project.id);
  33. var definitionId = jsonObject.resource.definition.id;
  34. var result = jsonObject.resource.result;
  35. var buildId = jsonObject.resource.id;
  36. var build = new BuildHttpClient(new Uri("https://<YOUR TENANT>.VisualStudio.com/DefaultCollection/"), new VssBasicCredential(vstsUser, vstsPassword));
  37.  
  38. // Exception will happen at here.
  39. // Obrain definition from VSTS
  40. var buildDefinition = await build.GetDefinitionAsync(project, definitionId);
  41.  
  42. return req.CreateResponse(HttpStatusCode.OK, new {
  43. body = $"Test Complete.",
  44. });
  45. }
  46.  
  47. public class VSTSWebHook
  48. {
  49. public string subscriptionId { get; set; }
  50. public int notificationId { get; set; }
  51. public string id { get; set; }
  52. public string eventType { get; set; }
  53. public string publisherId { get; set; }
  54. public Message message { get; set; }
  55. public Detailedmessage detailedMessage { get; set; }
  56. public Resource resource { get; set; }
  57. public string resourceVersion { get; set; }
  58. public Resourcecontainers resourceContainers { get; set; }
  59. public DateTime createdDate { get; set; }
  60. }
  61.  
  62. public class Message
  63. {
  64. public string text { get; set; }
  65. public string html { get; set; }
  66. public string markdown { get; set; }
  67. }
  68.  
  69. public class Detailedmessage
  70. {
  71. public string text { get; set; }
  72. public string html { get; set; }
  73. public string markdown { get; set; }
  74. }
  75.  
  76. public class Resource
  77. {
  78. public int id { get; set; }
  79. public string status { get; set; }
  80. public string result { get; set; }
  81. public DateTime queueTime { get; set; }
  82. public DateTime startTime { get; set; }
  83. public DateTime finishTime { get; set; }
  84. public string url { get; set; }
  85. public Definition definition { get; set; }
  86. public string uri { get; set; }
  87. public string sourceBranch { get; set; }
  88. public string sourceVersion { get; set; }
  89. public Queue queue { get; set; }
  90. public string priority { get; set; }
  91. public string reason { get; set; }
  92. public Requestedfor requestedFor { get; set; }
  93. public Requestedby requestedBy { get; set; }
  94. public DateTime lastChangedDate { get; set; }
  95. public Lastchangedby lastChangedBy { get; set; }
  96. public Orchestrationplan orchestrationPlan { get; set; }
  97. public Logs logs { get; set; }
  98. public Repository repository { get; set; }
  99. }
  100.  
  101. public class Definition
  102. {
  103. public string type { get; set; }
  104. public int revision { get; set; }
  105. public int id { get; set; }
  106. public string name { get; set; }
  107. public string url { get; set; }
  108. public Project project { get; set; }
  109. }
  110.  
  111. public class Project
  112. {
  113. public string id { get; set; }
  114. public string name { get; set; }
  115. public string url { get; set; }
  116. public string state { get; set; }
  117. }
  118.  
  119. public class Queue
  120. {
  121. public object pool { get; set; }
  122. public int id { get; set; }
  123. public string name { get; set; }
  124. }
  125.  
  126. public class Requestedfor
  127. {
  128. public string id { get; set; }
  129. public string displayName { get; set; }
  130. public string uniqueName { get; set; }
  131. public string url { get; set; }
  132. public string imageUrl { get; set; }
  133. }
  134.  
  135. public class Requestedby
  136. {
  137. public string id { get; set; }
  138. public string displayName { get; set; }
  139. public string uniqueName { get; set; }
  140. public string url { get; set; }
  141. public string imageUrl { get; set; }
  142. public bool isContainer { get; set; }
  143. }
  144.  
  145. public class Lastchangedby
  146. {
  147. public string id { get; set; }
  148. public string displayName { get; set; }
  149. public string uniqueName { get; set; }
  150. public string url { get; set; }
  151. public string imageUrl { get; set; }
  152. public bool isContainer { get; set; }
  153. }
  154.  
  155. public class Orchestrationplan
  156. {
  157. public string planId { get; set; }
  158. }
  159.  
  160. public class Logs
  161. {
  162. public int id { get; set; }
  163. public string type { get; set; }
  164. public string url { get; set; }
  165. }
  166.  
  167. public class Repository
  168. {
  169. public string id { get; set; }
  170. public string type { get; set; }
  171. public object clean { get; set; }
  172. public bool checkoutSubmodules { get; set; }
  173. }
  174.  
  175. public class Resourcecontainers
  176. {
  177. public Collection collection { get; set; }
  178. public Account account { get; set; }
  179. public Project1 project { get; set; }
  180. }
  181.  
  182. public class Collection
  183. {
  184. public string id { get; set; }
  185. }
  186.  
  187. public class Account
  188. {
  189. public string id { get; set; }
  190. }
  191.  
  192. public class Project1
  193. {
  194. public string id { get; set; }
  195. }
  196.  
  197. {
  198. "frameworks": {
  199. "net46":{
  200. "dependencies": {
  201. "Microsoft.TeamFoundationServer.Client" : "14.89.0",
  202. "Microsoft.VisualStudio.Services.Client" : "14.89.0"
  203. }
  204. }
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement