在TFS中通过程序动态创建Bug并感知Bug解决状态

  为便于跟踪问题解决情况,预警引擎产生的比较严重的预警日志,需要在TFS中登记Bug,通过TFS的状态流转,利用TFS Bug的Web挂钩功能,动态感知Bug解决状态,从而跟踪预警问题的解决状态,

  整体解决方案如下:

 

微软提供了很好的文档支持:

https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#create-bug

创建Bug示例代码如下:

public void CreateBug()
{
   string _userName = "userName";
   string _userPwd = "userPwd";
   string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", _userName , _userPwd)));

   Object[] patchDocument = new Object[4];

   patchDocument[0] = new { op = "add", path = "/fields/System.Title", value = "Authorization Errors" };
   patchDocument[1] = new { op = "add", path = "/fields/Microsoft.VSTS.TCM.ReproSteps", value = "ReproSteps" };
   patchDocument[2] = new { op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "1" };
   patchDocument[3] = new { op = "add", path = "/fields/Microsoft.VSTS.Common.Severity", value = "2 - High" };

   //use the httpclient
   using (var client = new HttpClient())
   {
       //set our headers
       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"); 

       var method = new HttpMethod("PATCH");
       var request = new HttpRequestMessage(method, "TFS Bug URL") { Content = patchValue };
       var response = client.SendAsync(request).Result;

       //if the response is successfull, set the result to the workitem object
       if (response.IsSuccessStatusCode)
       {
           var result = response.Content.ReadAsStringAsync().Result;
       }
   }
}

返回的result是一个Json串,形如:

{
    "id": 58141,
    "rev": 1,
    "fields": {
        "System.AreaPath": "Bugs",
        "System.TeamProject": "Bugs",
        "System.IterationPath": "Bugs",
        "System.WorkItemType": "Bug",
        "System.State": "新建",
        "System.Reason": "新建",
        "System.AssignedTo": "用户 <TELD\\user>",
        "System.CreatedDate": "",
        "System.CreatedBy": "用户 <TELD\\user>",
        "System.ChangedDate": "",
        "System.ChangedBy": "用户 <TELD\\user>",
        "System.Title": "测试新增",
        "System.BoardColumn": "新建",
        "System.BoardColumnDone": false,
        "Microsoft.VSTS.Common.StateChangeDate": "",
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.Common.Severity": "3 - 中",
        "Microsoft.VSTS.Common.ValueArea": "业务",
        "Teld.Bug.Source": "预警引入",
        "Teld.Bug.DetectionMode": "集成测试",
        "Teld.Bug.ifCausedByModification": "",
        "Teld.Bug.Type": "程序错误",
        "Teld.Bug.IfAgreeResultState": "初始",
        "Teld.Bug.Client": "WebPC",
        "Teld.Bug.FunctionMenu": "预警中心--预警日志",
        "WEF_794F3BA7F8DF460D97A964BF78CEC582_Kanban.Column": "新建",
        "WEF_794F3BA7F8DF460D97A964BF78CEC582_Kanban.Column.Done": false,
        "Microsoft.VSTS.TCM.ReproSteps": "重现步骤"
    },
    "_links": {
        "self": {
            "href": ""
        },
        "workItemUpdates": {
            "href": ""
        },
        "workItemRevisions": {
            "href": ""
        },
        "workItemHistory": {
            "href": ""
        },
        "html": {
            "href": ""
        },
        "workItemType": {
            "href": ""
        },
        "fields": {
            "href": ""
        }
    },
    "url": ""
}

 以前只用过Http协议中的Get、Post、Put等,这个Patch方法,还是第一次用。

 

posted @ 2017-03-14 18:35  静若清池  阅读(1560)  评论(0编辑  收藏  举报