2014-06-13 80 views
1

我試圖在兩個工作區中同時創建一個TAG。拉力賽:創建標籤失敗

我遍歷我們的訂閱工作區並查詢標籤以查看標籤是否已經存在。

如果標籤不存在,我創建它。

但它只在我們以後的工作區創建TAG,並且從不在較老的區域創建TAG。

任何想法我可能做錯了什麼?

static void Main(string[] args) 
    { 
     string id = "TEST"; 

     RallyRestApi restApi = new RallyRestApi("[email protected]", "mypassword", "https://rally1.rallydev.com", "v2.0"); 

     DynamicJsonObject sub = restApi.GetSubscription("Workspaces"); 

     Request wRequest = new Request(sub["Workspaces"]); 

     Rally.RestApi.Response.QueryResult queryResult = restApi.Query(wRequest); 

     foreach (var result in queryResult.Results) 
     { 
      var workspaceReference = result["_ref"]; 

      Request tagRequest = new Request("tag") 
      { 
       Workspace = workspaceReference, 
       Fetch = new List<string>() { "Name", "ObjectID" }, 
       Query = new Query("Name", Query.Operator.Equals, id) 
      }; 

      QueryResult tagResult = restApi.Query(tagRequest); 

      if (tagResult.TotalResultCount == 0) 
      { 
       DynamicJsonObject newTag = new DynamicJsonObject(); 
       newTag["Name"] = id; 

       CreateResult createResult = restApi.Create(workspaceReference, "Tag", newTag); 

       Console.Out.WriteLine("TAG " + id + " not found, creating"); 
       Console.Out.WriteLine(createResult.Reference); 
      } 
      else 
      { 
       Console.Out.WriteLine("TAG " + id + " found"); 
       Console.Out.WriteLine(tagResult.Results.First()["_ref"]); 
      } 
     } 

     Console.ReadKey(); 
    } 

當我運行此,我總是得到下面的結果提前

TAG TEST not found, creating 
https://rally1.rallydev.com/slm/webservice/v2.0/tag/19735777148 
TAG TEST found 
https://rally1.rallydev.com/slm/webservice/v2.0/tag/19735777148 

感謝這個

回答

0

任何幫助,而不是通過引用到工作區中創建:

newTag["Name"] = id; 
CreateResult createResult = restApi.Create(workspaceReference, "Tag", newTag); 

設置這樣的工作區:

newTag["Name"] = tagName; 
newTag["Workspace"] = workspaceReference; 
CreateResult createResult = restApi.Create("Tag", newTag); 

這是一個完整的例子。我首先過濾掉所有者訂閱的工作區集合,但其餘代碼與您的代碼類似:

namespace aRESTcreateTags 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string tagName = "T3$T"; 

      RallyRestApi restApi = new RallyRestApi("[email protected]", "secret", "https://rally1.rallydev.com", "v2.0"); 

      //get current user 
      DynamicJsonObject user = new DynamicJsonObject(); 
      user = restApi.GetCurrentUser(); 
      Console.Out.WriteLine("owner " + user["_ref"]); 


      //find workspaces owned by this user 
      //equivalent to this endpoint: https://rally1.rallydev.com/slm/webservice/v2.0/subscription/1154643/workspaces?query=(Owner%20=%20%22/user/12345%22) 

      DynamicJsonObject sub = restApi.GetSubscription("Workspaces"); 

      Request wRequest = new Request(sub["Workspaces"]); 
      wRequest.Query = new Query("Owner", Query.Operator.Equals, user["_ref"]); 
      QueryResult wResult = restApi.Query(wRequest); 
      foreach (var workspace in wResult.Results) 
      { 
       //Console.Out.WriteLine(workspace["_refObjectName"] + " : " + workspace["_ref"]); 
       Request tagRequest = new Request("tag"); 
       tagRequest.Query = new Query("Name", Query.Operator.Equals, tagName); 
       QueryResult tagResult = restApi.Query(tagRequest); 
       if (tagResult.TotalResultCount == 0) 
       { 
        Console.Out.WriteLine("TAG " + tagName + " not found, creating"); 
        DynamicJsonObject newTag = new DynamicJsonObject(); 
        newTag["Name"] = tagName; 
        newTag["Workspace"] = workspace["_ref"]; 
        CreateResult createResult = restApi.Create("Tag", newTag); 
        Console.Out.WriteLine(createResult.Reference + " created in workspace " + workspace["_refObjectName"]); 
       } 
       else 
       { 
        Console.Out.WriteLine("TAG " + tagName + " found in " + workspace["_refObjectName"]); 
        Console.Out.WriteLine(tagResult.Results.First()["_refObjectName"] + " " + tagResult.Results.First()["_ref"]); 
       } 

      } 

     } 
    } 
}