[英]Using C# Rally RestApi to Add and Update Rally Item ChangeSets
[英]C# Rally RestAPI Add or Update A Tag
我找不到通過C#將標簽值添加到用戶故事的語法。
使用最新的Rally API二進制文件2.0 Beta。
我能找到的最接近的示例是通過Java( 如何使用Rally的JAVA API向Rally中的TestCase添加標簽? ),它看起來很有幫助,但是從該示例中,我需要訪問JSONArray和JSONElement,但我的C#部署通過以下方式使用DynamicJSONObject
根據Rally示例的RestAPI,但是JSONArray
和DynamicJSONArray
(以及JSONElement
)都沒有通過RestAPI參考公開。
我可以通過添加Microsoft的System.Web.Helpers
引用來啟用DynamicJSONArray
訪問,但是Microsoft DynamicJSonArray
引用與RestAPI變體競爭...
我可以使用全名來限定restapi變體來解決這個問題,但是我真的不想在沒有清晰視圖的情況下開始這條路,如果它會導致某些功能的話。如果我將Microsoft DyamicJSONArray
傳遞到Rally DyamicJSONObject
,我懷疑任何功能都會產生。
是否有人有C#代碼做一些簡單的事情,例如創建標簽並將其輔助到用戶案例或測試用例上?
這是一個簡單的示例,該示例同時使用現有標簽和新創建的標簽來更新故事中的標簽集合:
namespace RestExample_UpdateStoryTags
{
class Program
{
static void Main(string[] args)
{
//Initialize the REST API
RallyRestApi restApi;
String rallyUserName = "user@company.com";
String rallyPassword = "topsecret";
String rallyURL = "https://rally1.rallydev.com";
String wsapiVersion = "1.41";
String myWorkspaceName = "My Workspace";
restApi = new RallyRestApi(
rallyUserName,
rallyPassword,
rallyURL,
wsapiVersion
);
// Get a Reference to Target Workspace
Request workspaceRequest = new Request("workspace");
workspaceRequest.Fetch = new List<string>()
{
"Name",
"ObjectID"
};
workspaceRequest.Query = new Query("Name", Query.Operator.Equals, myWorkspaceName);
QueryResult workspaceQueryResults = restApi.Query(workspaceRequest);
var targetWorkspace = workspaceQueryResults.Results.First();
Console.WriteLine("Found Target Workspace: " + targetWorkspace["Name"]);
String workspaceRef = targetWorkspace["_ref"];
//Query for Target Tag
Request tagRequest = new Request("tag");
tagRequest.Fetch = new List<string>()
{
"Name",
"ObjectID"
};
// Query all Tags for a tag named "Montane"
tagRequest.Query = new Query("Name", Query.Operator.Equals, "Tundra");
QueryResult queryTagResults = restApi.Query(tagRequest);
var targetTagResult = queryTagResults.Results.First();
long tagOID = targetTagResult["ObjectID"];
DynamicJsonObject targetTag = restApi.GetByReference("tag", tagOID, "Name", "ObjectID");
// Query for User Story
// FormattedID of target story
String targetStoryFormattedID = "US5";
Request storyRequest = new Request("hierarchicalrequirement");
storyRequest.Fetch = new List<string>()
{
"Name",
"ObjectID",
"Iteration",
"FormattedID",
"Tags"
};
storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, targetStoryFormattedID);
QueryResult queryStoryResults = restApi.Query(storyRequest);
var targetUserStory = queryStoryResults.Results.First();
Console.WriteLine("Found Target User Story: " + targetUserStory["Name"]);
// Grab collection of existing Tags
var existingTags = targetUserStory["Tags"];
// Summarize Existing Tags
Console.WriteLine("Existing Tags for Story" + targetStoryFormattedID + ": ");
foreach (var tag in existingTags)
{
Console.WriteLine(tag["Name"]);
}
long targetOID = targetUserStory["ObjectID"];
// Now do update of the User Story
// Tags collection on object is expected to be a System.Collections.ArrayList
var targetTagArray = existingTags;
targetTagArray.Add(targetTag);
DynamicJsonObject toUpdate = new DynamicJsonObject();
toUpdate["Tags"] = targetTagArray;
OperationResult updateResult = restApi.Update("HierarchicalRequirement", targetOID, toUpdate);
foreach (var error in updateResult.Errors)
{
Console.WriteLine(error.ToString());
}
// Re-read target Story
DynamicJsonObject updatedStory = restApi.GetByReference(targetUserStory["_ref"], "Tags,Name");
var updatedTags = updatedStory["Tags"];
// Summarize Updated Tags
Console.WriteLine("Updated Tags for Story" + targetStoryFormattedID + ": ");
foreach (var tag in updatedTags)
{
Console.WriteLine(tag["Name"]);
}
// Create a New Tag, and add New Tag to Story
DynamicJsonObject newTag = new DynamicJsonObject();
newTag["Name"] = "Boreal";
CreateResult createResult = restApi.Create(workspaceRef, "Tag", newTag);
// Get the ref of the created Tag
String newTagRef = createResult.Reference;
// Read the Target Tag
DynamicJsonObject newTagRead = restApi.GetByReference(newTagRef, "Name");
// Add the newly-created Tag to the Story
targetTagArray.Add(newTagRead);
toUpdate["Tags"] = targetTagArray;
updateResult = restApi.Update("HierarchicalRequirement", targetOID, toUpdate);
// Re-read target Story
updatedStory = restApi.GetByReference(targetUserStory["_ref"], "Tags,Name");
updatedTags = updatedStory["Tags"];
// Summarize Updated Tags
Console.WriteLine("Updated Tags (with newly-created Tag for Story" + targetStoryFormattedID + ": ");
foreach (var tag in updatedTags)
{
Console.WriteLine(tag["Name"]);
}
Console.ReadKey();
}
}
}
//Based on Mark's code, below comes close... but updates still failing
//Maybe I need to try an earlier version?
String strTeam = "xxx";
String workspaceRef = "/workspace/4914063320";
String projectRef = "/project/7412041414";
//get user story
Request reqUserStory = new Request("hierarchicalrequirement");
reqUserStory.Fetch = new List<string>() { "ObjectID", "Release", "Iteration", "PlanEstimate", "Description", "Tags" };
reqUserStory.Query = new Query("Project", Query.Operator.Equals, projectRef)
.And(new Query("Name", Query.Operator.Equals, "mystoryname"));
QueryResult queryResultUS = restApi.Query(reqUserStory);
DynamicJsonObject myUserStory = new DynamicJsonObject();
myUserStory = queryResultUS.Results.FirstOrDefault();
if (null != myUserStory)
{
//get tags for user story
var existingTags = myUserStory["Tags"];
if (null != existingTags)
{
bool bFound = false;
var tagList = existingTags["_tagsNameArray"];
//look for tag
foreach (var tag in tagList)
{
String strValue = tag["Name"];
if (strValue == strTeam)
bFound = true;
}
if (!bFound)
{
//replace tags array
var targetTagArray = existingTags;
var tagList2 = targetTagArray["_tagsNameArray"];
tagList2.Add(targetTag);//even if i comment this out, update below fails...
targetTagArray["_tagsNameArray"] = tagList2;
DynamicJsonObject toUpdate = new DynamicJsonObject();
toUpdate["Tags"] = targetTagArray;
long oid = Convert.ToInt64(myUserStory["ObjectID"]);
opResultRelease = restApi.Update("hierarchicalrequirement", oid, toUpdate);
// Fails with Error: ["Could not read: Could not read referenced object 2"]
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.