Question
How to get ChangeSet(All files changes) from IWorkItem item by Program. I want a list contains all the files that got change for a RTC. how can I achieve this?
How-To
You can see if this thread helps: (from the client side)
Below is the code snippet:
- IClientLibraryContext ctx = (IClientLibraryContext)repository;
- IQueryService svc = (IQueryService)ctx.getServiceInterface(IQueryService.class);
- AuditableLinkQueryModel alqr = AuditableLinkQueryModel.ROOT;
- IItemQuery iq = IItemQuery.FACTORY.newInstance(alqr);
- iq.filter(alqr.name()._eq("com.ibm.team.filesystem.workitems.change_set").
- _and( alqr.targetRef().referencedItem().itemId()._eq(iq.newUUIDArg())));
- IItemQueryPage itemQueryPage = svc.queryItems(
- iq,
- new Object[] { workItem.getItemId() },
- IQueryService.ITEM_QUERY_MAX_PAGE_SIZE);
- List
items = mgr.fetchCompleteItems(itemQueryPage.getItemHandles(), - mgr.DEFAULT, null);
- Set
changedFilesAndFolders = new TreeSet (); - for (IChangeSet cs: changeSets) {
- ...
- }
Other sources:
Below is the full sample code which will retrieve change set from workitem and show the file name(s) of change set as well:
- RetrieveChangeSetFromWorkItem.java
- package test.pub;
- import org.eclipse.core.runtime.IProgressMonitor;
- import org.eclipse.core.runtime.NullProgressMonitor;
- import com.ibm.team.repository.client.ITeamRepository;
- import com.ibm.team.repository.client.TeamPlatform;
- import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
- import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
- import com.ibm.team.repository.common.TeamRepositoryException;
- import java.util.List;
- import com.ibm.team.build.client.ITeamBuildClient;
- import com.ibm.team.filesystem.common.IFileItem;
- import com.ibm.team.filesystem.common.IFileItemHandle;
- import com.ibm.team.links.client.ILinkManager;
- import com.ibm.team.links.common.IItemReference;
- import com.ibm.team.links.common.ILink;
- import com.ibm.team.links.common.ILinkCollection;
- import com.ibm.team.links.common.ILinkQueryPage;
- import com.ibm.team.links.common.factory.IReferenceFactory;
- import com.ibm.team.process.client.IProcessClientService;
- import com.ibm.team.repository.client.IItemManager;
- import com.ibm.team.repository.client.TeamPlatform;
- import com.ibm.team.repository.common.TeamRepositoryException;
- import com.ibm.team.scm.client.IVersionableManager;
- import com.ibm.team.scm.client.SCMPlatform;
- import com.ibm.team.scm.common.IChangeSet;
- import com.ibm.team.scm.common.IChangeSetHandle;
- import com.ibm.team.scm.common.IVersionableHandle;
- import com.ibm.team.scm.common.internal.Change;
- import com.ibm.team.workitem.client.IAuditableClient;
- import com.ibm.team.workitem.client.IWorkItemClient;
- import com.ibm.team.workitem.common.model.IWorkItem;
- import com.ibm.team.workitem.common.model.WorkItemLinkTypes;
- public class RetrieveChangeSetFromWorkItem{
- public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();
- public static ITeamRepository TeamRepository = null;
- public static String repositoryURI = "...";
- public static String userId = "...";
- public static String password = "...";
- public static void Start(String wi) throws Exception {
- IProcessClientService processClient = (IProcessClientService) TeamRepository
- .getClientLibrary(IProcessClientService.class);
- IAuditableClient auditableClient = (IAuditableClient) TeamRepository
- .getClientLibrary(IAuditableClient.class);
- IWorkItemClient workItemClient = (IWorkItemClient) TeamRepository
- .getClientLibrary(IWorkItemClient.class);
- ITeamBuildClient buildClient = (ITeamBuildClient) TeamRepository
- .getClientLibrary(ITeamBuildClient.class);
- ILinkManager linkManager = (ILinkManager) TeamRepository
- .getClientLibrary(ILinkManager.class);
- IVersionableManager vm = SCMPlatform.getWorkspaceManager(TeamRepository).versionableManager();
- IWorkItem task = workItemClient.findWorkItemById(Integer.parseInt(wi),
- IWorkItem.FULL_PROFILE, null);
- IItemReference linkTarget = IReferenceFactory.INSTANCE
- .createReferenceToItem(task);
- // Get alist of all change sets
- ILinkQueryPage changeSets = linkManager.findLinksByTarget(
- WorkItemLinkTypes.CHANGE_SET, linkTarget,
- new NullProgressMonitor());
- System.out.printf("\t[Info] Change Set Of WorkItem(%s):\n", wi);
- ILinkCollection linkCollection = changeSets.getLinks();
- for (ILink iLink : linkCollection) {
- IChangeSetHandle changeSetHandle = (IChangeSetHandle) iLink
- .getSourceRef().resolve();
- IChangeSet changeset = (IChangeSet) TeamRepository.itemManager()
- .fetchCompleteItem(changeSetHandle, IItemManager.DEFAULT, null);
- System.out.printf("\t%s\n", changeset.getComment());
- List changes = changeset.changes();
- for(Object change:changes)
- {
- Change c = (Change) change;
- IVersionableHandle after = c.afterState();
- if(after!=null && after instanceof IFileItemHandle)
- {
- IFileItem fileItem = (IFileItem) vm.fetchCompleteState(after, null);
- System.out.printf("\t\t%s\n", fileItem.getName());
- }
- }
- }
- }
- /**
- * How to get ChangeSet(All files changes) from IWorkItem item by Program
- * http://stackoverflow.com/questions/24628644/how-to-get-changesetall-files-changes-from-iworkitem-item-by-program
- * https://jazz.net/forum/questions/128294/want-to-read-all-changesets-associated-with-a-work-item
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception{
- String workItemID = "68418";
- TeamPlatform.startup();
- try {
- Login();
- Start(workItemID);
- } catch (TeamRepositoryException e) {
- System.out.println("Unable to login: " + e.getMessage());
- } finally {
- TeamPlatform.shutdown();
- }
- }
- private static class LoginHandler implements ILoginHandler, ILoginInfo {
- private String fUserId;
- private String fPassword;
- private LoginHandler(String userId, String password) {
- fUserId = userId;
- fPassword = password;
- }
- public String getUserId() {
- return fUserId;
- }
- public String getPassword() {
- return fPassword;
- }
- public ILoginInfo challenge(ITeamRepository repository) {
- return this;
- }
- }
- public static void Login() throws TeamRepositoryException {
- ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
- teamRepository.registerLoginHandler(new LoginHandler(userId, password));
- teamRepository.login(MyProgressMonitor);
- TeamRepository = teamRepository;
- }
- }
沒有留言:
張貼留言