2015年4月1日 星期三

[ RTC Client Lib ] SC - How to get ChangeSet(All files changes) from IWorkItem item by Program

Source From Here 
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
repository is an ITeamRepository
mgr is an IItemManager (repository.itemManager())
workItem is the IWorkItem for which we want changesets
items holds the set of associated changeset links for the workitem

Below is the code snippet: 
  1. IClientLibraryContext ctx = (IClientLibraryContext)repository;  
  2. IQueryService svc = (IQueryService)ctx.getServiceInterface(IQueryService.class);  
  3. AuditableLinkQueryModel alqr = AuditableLinkQueryModel.ROOT;  
  4. IItemQuery iq = IItemQuery.FACTORY.newInstance(alqr);  
  5.   
  6. iq.filter(alqr.name()._eq("com.ibm.team.filesystem.workitems.change_set").  
  7.   _and( alqr.targetRef().referencedItem().itemId()._eq(iq.newUUIDArg())));  
  8.   
  9. IItemQueryPage itemQueryPage = svc.queryItems(  
  10.   iq,  
  11.   new Object[] { workItem.getItemId() },   
  12.   IQueryService.ITEM_QUERY_MAX_PAGE_SIZE);  
  13.   
  14. List items = mgr.fetchCompleteItems(itemQueryPage.getItemHandles(),   
  15.                                     mgr.DEFAULT, null);  
  16. Set changedFilesAndFolders = new TreeSet();  
  17. for (IChangeSet cs: changeSets) {  
  18.    ...  
  19. }  
You would still need to list files within each change set of items. you have some idea to list files in: 
"How to get an IConfiguration from IChangeSet?"
"how get Full path of the File from changeset Using API?"

Other sources: 
"Setting up Rational Team Concert for API Development"
"The RTC WorkItem Client Link API – Linking to Work Items and Other Elements"

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 
  1. package test.pub;  
  2.   
  3. import org.eclipse.core.runtime.IProgressMonitor;  
  4. import org.eclipse.core.runtime.NullProgressMonitor;  
  5.   
  6. import com.ibm.team.repository.client.ITeamRepository;  
  7. import com.ibm.team.repository.client.TeamPlatform;  
  8. import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;  
  9. import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;  
  10. import com.ibm.team.repository.common.TeamRepositoryException;  
  11.   
  12. import java.util.List;  
  13.   
  14. import com.ibm.team.build.client.ITeamBuildClient;  
  15. import com.ibm.team.filesystem.common.IFileItem;  
  16. import com.ibm.team.filesystem.common.IFileItemHandle;  
  17. import com.ibm.team.links.client.ILinkManager;  
  18. import com.ibm.team.links.common.IItemReference;  
  19. import com.ibm.team.links.common.ILink;  
  20. import com.ibm.team.links.common.ILinkCollection;  
  21. import com.ibm.team.links.common.ILinkQueryPage;  
  22. import com.ibm.team.links.common.factory.IReferenceFactory;  
  23. import com.ibm.team.process.client.IProcessClientService;  
  24. import com.ibm.team.repository.client.IItemManager;  
  25. import com.ibm.team.repository.client.TeamPlatform;  
  26. import com.ibm.team.repository.common.TeamRepositoryException;  
  27. import com.ibm.team.scm.client.IVersionableManager;  
  28. import com.ibm.team.scm.client.SCMPlatform;  
  29. import com.ibm.team.scm.common.IChangeSet;  
  30. import com.ibm.team.scm.common.IChangeSetHandle;  
  31. import com.ibm.team.scm.common.IVersionableHandle;  
  32. import com.ibm.team.scm.common.internal.Change;  
  33. import com.ibm.team.workitem.client.IAuditableClient;  
  34. import com.ibm.team.workitem.client.IWorkItemClient;  
  35. import com.ibm.team.workitem.common.model.IWorkItem;  
  36. import com.ibm.team.workitem.common.model.WorkItemLinkTypes;  
  37.   
  38. public class RetrieveChangeSetFromWorkItem{  
  39.     public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();  
  40.     public static ITeamRepository TeamRepository = null;  
  41.       
  42.     public static String repositoryURI = "...";  
  43.     public static String userId = "...";  
  44.     public static String password = "...";  
  45.       
  46.     public static void Start(String wi) throws Exception {  
  47.         IProcessClientService processClient = (IProcessClientService) TeamRepository  
  48.                 .getClientLibrary(IProcessClientService.class);  
  49.         IAuditableClient auditableClient = (IAuditableClient) TeamRepository  
  50.                 .getClientLibrary(IAuditableClient.class);  
  51.         IWorkItemClient workItemClient = (IWorkItemClient) TeamRepository  
  52.                 .getClientLibrary(IWorkItemClient.class);  
  53.         ITeamBuildClient buildClient = (ITeamBuildClient) TeamRepository  
  54.                 .getClientLibrary(ITeamBuildClient.class);  
  55.         ILinkManager linkManager = (ILinkManager) TeamRepository  
  56.                 .getClientLibrary(ILinkManager.class);  
  57.         IVersionableManager vm = SCMPlatform.getWorkspaceManager(TeamRepository).versionableManager();  
  58.   
  59.         IWorkItem task = workItemClient.findWorkItemById(Integer.parseInt(wi),  
  60.                 IWorkItem.FULL_PROFILE, null);  
  61.   
  62.         IItemReference linkTarget = IReferenceFactory.INSTANCE  
  63.                 .createReferenceToItem(task);  
  64.         // Get alist of all change sets  
  65.         ILinkQueryPage changeSets = linkManager.findLinksByTarget(  
  66.                 WorkItemLinkTypes.CHANGE_SET, linkTarget,  
  67.                 new NullProgressMonitor());  
  68.         System.out.printf("\t[Info] Change Set Of WorkItem(%s):\n", wi);  
  69.         ILinkCollection linkCollection = changeSets.getLinks();  
  70.         for (ILink iLink : linkCollection) {  
  71.             IChangeSetHandle changeSetHandle = (IChangeSetHandle) iLink  
  72.                     .getSourceRef().resolve();  
  73.             IChangeSet changeset = (IChangeSet) TeamRepository.itemManager()  
  74.                     .fetchCompleteItem(changeSetHandle, IItemManager.DEFAULT, null);  
  75.             System.out.printf("\t%s\n", changeset.getComment());  
  76.             List changes = changeset.changes();  
  77.             for(Object change:changes)  
  78.             {  
  79.                 Change c = (Change) change;  
  80.                 IVersionableHandle after = c.afterState();  
  81.                 if(after!=null && after instanceof IFileItemHandle)  
  82.                 {  
  83.                     IFileItem fileItem = (IFileItem) vm.fetchCompleteState(after, null);  
  84.                     System.out.printf("\t\t%s\n", fileItem.getName());  
  85.                 }  
  86.             }  
  87.         }  
  88.     }  
  89.   
  90.     /** 
  91.      * How to get ChangeSet(All files changes) from IWorkItem item by Program 
  92.      *  http://stackoverflow.com/questions/24628644/how-to-get-changesetall-files-changes-from-iworkitem-item-by-program 
  93.      *  https://jazz.net/forum/questions/128294/want-to-read-all-changesets-associated-with-a-work-item 
  94.      * @param args 
  95.      * @throws Exception 
  96.      */  
  97.     public static void main(String[] args) throws Exception{  
  98.         String workItemID = "68418";  
  99.         TeamPlatform.startup();  
  100.         try {             
  101.             Login();  
  102.             Start(workItemID);  
  103.         } catch (TeamRepositoryException e) {  
  104.             System.out.println("Unable to login: " + e.getMessage());  
  105.         } finally {  
  106.             TeamPlatform.shutdown();  
  107.         }  
  108.     }  
  109.   
  110.     private static class LoginHandler implements ILoginHandler, ILoginInfo {  
  111.   
  112.         private String fUserId;  
  113.         private String fPassword;  
  114.   
  115.         private LoginHandler(String userId, String password) {  
  116.             fUserId = userId;  
  117.             fPassword = password;  
  118.         }  
  119.   
  120.         public String getUserId() {  
  121.             return fUserId;  
  122.         }  
  123.   
  124.         public String getPassword() {  
  125.             return fPassword;  
  126.         }  
  127.   
  128.         public ILoginInfo challenge(ITeamRepository repository) {  
  129.             return this;  
  130.         }  
  131.     }  
  132.       
  133.     public static void Login() throws TeamRepositoryException {  
  134.         ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);  
  135.         teamRepository.registerLoginHandler(new LoginHandler(userId, password));  
  136.         teamRepository.login(MyProgressMonitor);  
  137.         TeamRepository = teamRepository;  
  138.     }  
  139. }  

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...