65.9K
CodeProject 正在变化。 阅读更多。
Home

Visual Studio Team Foundation Service 敏捷 Bug 记录器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.73/5 (3投票s)

2016年6月28日

CPOL

2分钟阅读

viewsIcon

16818

downloadIcon

82

这是一个简单的桌面应用程序,它使用 Visual Studio online API 在您的 AGILE 工作流程中创建和添加缺陷。

引言

我们已经开始使用 Visual Studio Team Foundation Services 来管理我们的 Agile 工作负载,但我希望确保测试人员有一个简单快捷的缺陷/功能报告方式。

在调查中,有很多基于 Web 的应用程序,专注于网站日志记录。我们开发桌面应用程序,这些解决方案对我们的需求来说不够好。

因此,我创建了一个新的简单桌面应用程序,它使用 Visual Studio Team Foundation Services APIs 来创建缺陷、添加附件和备注。

这让我很好地了解了如何使用 Visual Studio Team Foundation Services APIs,我想分享我所学到的知识。

Using the Code

该解决方案使用 HTTPClientJsonConvert.SerializeObject 来进行 JSON 调用。
将数据类对象从 Data 类转换为 HTTPContent,然后可以由 JSON 使用。

  public static System.Net.Http.HttpContent ConvertDataClassToJason(object dataClass)
        {
            string wiPostDataString = JsonConvert.SerializeObject(dataClass);

            return  new System.Net.Http.StringContent
               (wiPostDataString, Encoding.UTF8, "application/json-patch+json");
        }

下面的方法使用 HTTPClient 进行 Post 或 Patch 调用,并使用 APIs 获取结果。

Post 示例

public static async void Post(string url, System.Net.Http.HttpContent wiPostDataContent, 
     string returnType, JSONReturnCallBack callBack)
        {
           string responseString = String.Empty;
           try
           {
                using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add
                    (new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
 
                  
                    client.DefaultRequestHeaders.Authorization = 
                                 new AuthenticationHeaderValue
                                 ("Basic",Convert.ToBase64String
                                 (System.Text.ASCIIEncoding.ASCII.GetBytes
                                 (string.Format("{0}:{1}", username, password))));


                      using (System.Net.Http.HttpResponseMessage response = 
                      client.PostAsync(url, wiPostDataContent).Result)
                      {
                            response.EnsureSuccessStatusCode();
                            string ResponseContent = await response.Content.ReadAsStringAsync();

                            responseString = ResponseContent;
                      }
                }
            }
            catch(Exception ex)
            {
                            Console.WriteLine(ex.ToString());
                            Console.ReadLine();
            }
           callBack(responseString,returnType);
        }

Patch 调用

这需要在您的 Visual Studio 中添加一个库。您可以使用 NuGet 来执行此操作。
https://nuget.net.cn/packages/Microsoft.VisualStudio.Services.Client

Code snippet to be added

创建工作项

下面的示例将引导您完成以下步骤

  1. 添加新的工作项
  2. 附加文件
    1. 上传文件
    2. 附加到工作项

创建新项目

这将向您展示如何在您的 VSO 帐户的项目中创建简单的项目。

创建参数

创建项目时,您需要标题、描述和标签。

   private static void createInitailItemPostObject()
        {                                           
            AddUpdateProp("/fields/System.Title", newTaskItem.Title);
            AddUpdateProp("/fields/System.Tags", newTaskItem.Tags);
            AddUpdateProp("/fields/System.Description", newTaskItem.Description);             
        }

        private static void AddUpdateProp( string field, string value)
        {            
            DataObjectsProject.VSOJasonWorkItemPostData wiPostData = new DataObjectsProject.VSOJasonWorkItemPostData();
            wiPostData.op = "add";
            wiPostData.path = field;
            wiPostData.value = value;
            wiPostDataArr.Add(wiPostData);           
        }

用于保存参数的数据类

   public class VSOJasonWorkItemPostData
    {
        public string op;
        public string path;
        public string value;
    }
发送项目

将项目发送到 VSO 以创建工作项。此方法将返回一个 ID 以及其他一些字段。

string NewWorkItem = 
"https://[Account].visualstudio.com/DefaultCollection/[Project]/_apis/wit/workitems/$Task?api-version=1.0";

 Models.Helpers.JASONHelper.Post(newWorkItem, Models.Helpers.JASONHelper.ConvertDataClassToJason
 (wiPostDataArr), "createtask", new Models.Helpers.JASONHelper.JSONReturnCallBack(jsonReturn));         

JSON 调用返回一个字符串,下面将把字符串转换为可用的对象。

  public static VSOJasonWorkItemReturnData ConvertDataClassToWorkItemReturnDataObject(string jasonString)
        {
            var wiPostDataString = 
            JsonConvert.DeserializeObject<VSOJasonWorkItemReturnData>(jasonString);
            return wiPostDataString;
           
       }

使用的数据对象类

    public class VSOJasonWorkItemReturnData
    {
        [JsonProperty(PropertyName = "id")]
        public int Id { get; set; }
        [JsonProperty(PropertyName = "rev")]
        public int Rev { get; set; }
        [JsonProperty(PropertyName = "fields")]
        public JsonArrayAttribute  Fields { get; set; }
        [JsonProperty(PropertyName = "links")]
        public JsonArrayAttribute Links { get; set; }
        
    }

发送附件

您首先需要将附件上传到您的 Visual Studio Team Foundation Services 帐户。

 string responseString = String.Empty;
            try
            {
                using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization =
                                 new AuthenticationHeaderValue("Basic",
                                 Convert.ToBase64String
                                 (System.Text.ASCIIEncoding.ASCII.GetBytes(getConnectionDetails())));

                    FileStream files = new FileStream(filepath, FileMode.Open);
                    StreamContent streamcontent = new StreamContent(files);

                    using (System.Net.Http.HttpResponseMessage response = 
                                    client.PostAsync(SetURL(url),streamcontent).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        responseString = await response.Content.ReadAsStringAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }

附加附件

发送附件后,您需要将附件附加到所需的工作项。

 try
            {
                using (HttpClient client = new System.Net.Http.HttpClient())
                {
  StringBuilder msg = new StringBuilder();
            msg.Append("[{");
            msg.Append("\"op\": \"add\",");
            msg.Append("\"path\": \"/relations/-\",");
            msg.Append("\"value\": {");
            msg.Append("\"rel\": \"AttachedFile\",");
            msg.Append(String.Format("\"url\": \"{0}\",", attachmentUploadReturnObject.URL));
            msg.Append("\"attributes\": {");
            msg.Append(String.Format("\"comment\": \"{0}\"", comment));
            msg.Append("}");
            msg.Append("}");
            msg.Append("}]");
            wiPostDataContent = new System.Net.Http.StringContent(msg.ToString(), 
                             Encoding.UTF8, "application/json-patch+json");

                    client.DefaultRequestHeaders.Accept.Add
                           (new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue
                           ("application/json-patch+json"));

                    client.DefaultRequestHeaders.Authorization =
                                 new AuthenticationHeaderValue("Basic", Convert.ToBase64String
                                 (System.Text.ASCIIEncoding.ASCII.GetBytes(getConnectionDetails())));
                  
                    using (System.Net.Http.HttpResponseMessage response = 
                           client.PatchAsync(SetURL(url), wiPostDataContent).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string ResponseContent = await response.Content.ReadAsStringAsync();

                        responseString = ResponseContent;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }

关注点

您可以更详细地查看该应用程序

  1. 在线网站发布的版本
  2. 查看可下载文件中的代码
  3. 查看 GitHub 仓库中的代码

已发布应用程序:http://bugs.coopsmill.com
GitHub:https://github.com/chriscooper01/VSODesktop.git
API 文档:https://www.visualstudio.com/en-us/docs/integrate/api/overview

历史

  • 2016 年 6 月 28 日:初始版本
© . All rights reserved.