Reputation: 21
I cannot find the syntax to add a tag value to a User Story via C#.
Using the latest Rally API binaries, 2.0 Beta.
Closest example I can find is via Java ( How to add Tags to a TestCase in Rally using Rally's JAVA API? ), which looks helpful, but from the example I'll need access to JSONArray and JSONElement but my C# Deployment uses DynamicJSONObject
via the RestAPI as per the Rally examples, but neither JSONArray
nor DynamicJSONArray
(nor also JSONElement
) are exposed via the RestAPI Reference.
I Could enable DynamicJSONArray
access by adding Microsoft's System.Web.Helpers
reference, but the Microsoft DynamicJSonArray
reference contends with the RestAPI variant...
I Could use the full name to qualify the restapi variants to get around this but I don't really want to start down this road with no clear view if it will lead to something functional.If I pass a Microsoft DyamicJSONArray
into the Rally DyamicJSONObject
, I'm dubious anything functional will come of it.
Does anyone have any C# Code doing something as simple as creating a tag and assiging it to a user story or test case?
Upvotes: 2
Views: 1865
Reputation: 21
//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"]
}
}
}
Upvotes: 0
Reputation:
Here's a simple example that updates the Tag Collection on a Story both with an Existing Tag and a newly-created Tag:
namespace RestExample_UpdateStoryTags
{
class Program
{
static void Main(string[] args)
{
//Initialize the REST API
RallyRestApi restApi;
String rallyUserName = "[email protected]";
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();
}
}
}
Upvotes: 1