How to add a Comment using the RTC API.
One answer
(Answer from Simon Starkie)
This is working for us:
This is working for us:
package com.ibm.js.team.workitem.plainjava.example;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import com.ibm.team.filesystem.client.internal.rest.util.LoginUtil.LoginHandler;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IContributor;
import com.ibm.team.repository.common.ItemNotFoundException;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.workitem.common.IWorkItemCommon;
import com.ibm.team.workitem.common.model.IWorkItem;
public class WorkItemAddCommentTest {
public static void main(String[] args) {
boolean result;
TeamPlatform.startup();
try {
result = run(args);
} catch (TeamRepositoryException x) {
x.printStackTrace();
result = false;
} finally {
TeamPlatform.shutdown();
}
if (!result)
System.exit(1);
}
private static boolean run(String[] args) throws TeamRepositoryException {
boolean result = false;
if (args.length != 6) {
System.out
.println("Usage: AddComment []");
return result;
}
String repositoryURI = args[0];
String userId = args[1];
String password = args[2];
String idString = args[3];
String commentText = args[4];
String commenterID = null;
if (args.length == 6) {
commenterID = args[5];
}
IProgressMonitor monitor = new NullProgressMonitor();
ITeamRepository teamRepository = TeamPlatform
.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(monitor);
IContributor commentUser = null;
if (null != commenterID) {
try {
commentUser = teamRepository.contributorManager().fetchContributorByUserId(
commenterID, monitor);
} catch (ItemNotFoundException e) {
}
}
if (commentUser == null) {
commentUser = teamRepository.loggedInContributor();
}
// Use IWorkItemClient or IWorkItemCommon
IWorkItemCommon workItemCommon = (IWorkItemCommon) teamRepository
.getClientLibrary(IWorkItemCommon.class);
int id = new Integer(idString).intValue();
IWorkItem workItem = workItemCommon.findWorkItemById(id,
IWorkItem.SMALL_PROFILE, monitor);
if(null!=workItem){
WorkItemAddComment operation = new WorkItemAddComment(commentUser,
commentText);
operation.run(workItem, monitor);
System.out.println("Modified item " + workItem.getId() + ".");
result=true;
} else{
System.out.println("Canfind work Item " + idString + ".");
}
teamRepository.logout();
return result;
}
}
Comments
Tiago Moura
Jun 02 '15, 12:29 p.m.Is it supposed to be a question?
SImon Starkie
Jun 02 '15, 12:42 p.m.Is that supposed to be an answer ;)
Geoffrey Clemm
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER Jun 05 '15, 2:20 a.m.Taigo's comment is valid for several reasons:
- We discourage posting answers to questions that nobody has asked. There are 45,000 questions in the forum. If nobody has asked the question, probably nobody is interested in the answer (at least, not yet).
- In this case, this question has been asked ( https://jazz.net/forum/questions/25483/add-comment-to-work-item-using-api ), and this would be a good answer, but it is much more useful to post this as an answer to that question, instead of posting it as a duplicate question that users have to then browse through to find which one has a good answer.
- And if you decide to ignore the first two comments, at the very least you should post your answer as an "answer" to your question, and not embed the answer in the question field, so that the question shows up as being answered. I'll go ahead and make that edit.