How do I retrieve the change set (diff from previous build result) from a given build result object? From the RTC web page, There is a link "Show Change":
when click on it, I can have the corresponding change set diffed from the previous build result. That way, I can analyze the change set to narrow down the problem space while the test cases broken.
Sample Code:
Below sample code will do actions:
- package test.pub;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- import org.eclipse.core.runtime.IProgressMonitor;
- import org.eclipse.core.runtime.NullProgressMonitor;
- import com.ibm.team.build.client.ITeamBuildClient;
- import com.ibm.team.build.common.ScmConstants;
- import com.ibm.team.build.common.model.BuildState;
- import com.ibm.team.build.common.model.BuildStatus;
- import com.ibm.team.build.common.model.IBuildDefinition;
- import com.ibm.team.build.common.model.IBuildResult;
- import com.ibm.team.build.common.model.IBuildResultContribution;
- import com.ibm.team.build.common.model.IBuildResultHandle;
- import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel.IBuildResultQueryModel;
- import com.ibm.team.links.client.ILinkManager;
- import com.ibm.team.links.common.ILink;
- import com.ibm.team.links.common.ILinkHandle;
- import com.ibm.team.process.client.IProcessClientService;
- import com.ibm.team.repository.client.IItemManager;
- 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.IItemHandle;
- import com.ibm.team.repository.common.TeamRepositoryException;
- import com.ibm.team.repository.common.UUID;
- import com.ibm.team.repository.common.model.FetchResult;
- import com.ibm.team.repository.common.query.IItemQuery;
- import com.ibm.team.repository.common.query.IItemQueryPage;
- import com.ibm.team.repository.common.query.ast.IPredicate;
- import com.ibm.team.repository.common.service.IQueryService;
- import com.ibm.team.scm.client.IBaselineConnection;
- import com.ibm.team.scm.client.IWorkspaceManager;
- import com.ibm.team.scm.client.SCMPlatform;
- import com.ibm.team.scm.common.IBaseline;
- import com.ibm.team.scm.common.IBaselineHandle;
- import com.ibm.team.scm.common.IBaselineSet;
- import com.ibm.team.scm.common.IBaselineSetHandle;
- import com.ibm.team.scm.common.IChangeSet;
- import com.ibm.team.scm.common.IChangeSetHandle;
- import com.ibm.team.scm.common.dto.IChangeHistorySyncReport;
- import com.ibm.team.scm.common.dto.IChangeSetLinkSummary;
- import com.ibm.team.scm.common.links.ChangeSetLinks;
- import com.ibm.team.scm.common.links.ILinkConstants;
- import com.ibm.team.scm.common.providers.ProviderFactory;
- 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.IWorkItemHandle;
- public class RetrieveChangeSetFromBuildResult{
- public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();
- public static ITeamRepository TeamRepository = null;
- public static String BuildDefinitionID="xxx";
- public static String repositoryURI = "xxx";
- public static String userId = "xxx";
- public static String password = "xxx";
- public static String CHANGE_SET_TYPE = "com.ibm.team.filesystem.workitems.change_set";
- 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 Start(String buildDftID) 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);
- // 1) Fetch Build Definition
- IBuildResultHandle handle = (IBuildResultHandle) IBuildResult.ITEM_TYPE
- .createItemHandle(UUID.valueOf(buildDftID), null);
- ArrayList
handleList = new ArrayList (); - handleList.add(handle);
- FetchResult fetchResult = (FetchResult) TeamRepository.itemManager()
- .fetchCompleteItemsPermissionAware(handleList,
- IItemManager.REFRESH, null);
- IBuildDefinition definition = null;
- if (fetchResult.hasNotFoundItems()) {
- System.err.printf("\t[Error] Fail in retrieving build definition...");
- return;
- } else {
- for (Object result : fetchResult.getRetrievedItems()) {
- // System.out.printf("\t[Test] Retrieved item:\n%s\n", result);
- if (result instanceof IBuildResult) {
- IBuildResult buildResult = (IBuildResult) result;
- //System.out.printf("\t\tBuild Result=%s\n", buildResult);
- } else if (result instanceof IBuildDefinition) {
- //System.out.printf("\t\tBuild Definition=%s\n", result);
- definition = (IBuildDefinition) result;
- }
- }
- }
- if (definition == null) {
- System.err.printf("\t[Error] Fail in retrieving build definition!\n");
- return;
- }
- // 2) Query Build Result
- IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;
- IItemQuery query = IItemQuery.FACTORY
- .newInstance(buildResultQueryModel);
- IPredicate predicate = (buildResultQueryModel.buildDefinition()
- ._eq(query.newItemHandleArg()))._and(
- buildResultQueryModel.personalBuild()._isFalse())._and(
- buildResultQueryModel.buildState()._eq(
- BuildState.COMPLETED.name()));
- query.filter(predicate);
- query.orderByAsc(buildResultQueryModel.buildStartTime());
- IItemQueryPage queryPage = buildClient.queryItems(query,
- new Object[] { definition.getItemId() },
- IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, MyProgressMonitor);
- // print out the labels of the retrieved builds
- String[] properties = new String[] { IBuildResult.PROPERTY_LABEL,
- IBuildResult.PROPERTY_BUILD_STATUS,
- IBuildResult.PROPERTY_BUILD_REQUESTS,
- IBuildResult.PROPERTY_SUMMARY,
- IBuildResult.PROPERTY_BUILD_RESULT_CONTRIBUTIONS};
- List buildResults = TeamRepository.itemManager().fetchPartialItems(
- queryPage.getItemHandles(), IItemManager.DEFAULT,
- Arrays.asList(properties), MyProgressMonitor);
- IBaselineConnection pbaseLineConnection=null;
- Date preBaselineDate=null;
- if(buildResults.size()==0) System.err.printf("\t[Warn] No build result exist!\n");
- for (Object result : buildResults) {
- IBuildResult buildResult = (IBuildResult) result;
- if (buildResult.getStatus().equals(BuildStatus.ERROR)
- || buildResult.getStatus().equals(BuildStatus.OK)) {
- System.out.printf("\t[Info] Build Result='%s'...\n",
- buildResult.getLabel());
- IBuildResultContribution[] contribs = buildClient
- .getBuildResultContributions(
- buildResult,
- ScmConstants.EXTENDED_DATA_TYPE_ID_BUILD_SNAPSHOT,
- null);
- for (IBuildResultContribution c : contribs) {
- IItemHandle item = c.getExtendedContribution();
- IBaselineSetHandle baseLineHandle = (IBaselineSetHandle) item;
- IBaselineSet snapshot = (IBaselineSet) TeamRepository
- .itemManager().fetchCompleteItem(baseLineHandle,
- IItemManager.DEFAULT, null);
- List
baseLines = snapshot.getBaselines(); - Date latestDate = null;
- IBaseline latestLine = null;
- for (IBaselineHandle lineHandle : baseLines) {
- IBaseline line = (IBaseline) TeamRepository
- .itemManager().fetchCompleteItem(lineHandle,
- IItemManager.DEFAULT, null);
- if (latestDate == null) {
- latestDate = line.getCreationDate();
- latestLine = line;
- } else {
- if (line.getCreationDate().compareTo(latestDate) > 0) {
- latestDate = line.getCreationDate();
- latestLine = line;
- }
- }
- }
- System.out.printf("\t%s (%s/%d)\n", latestLine.getComment(), latestLine.getCreationDate(), latestLine.getId());
- if (latestLine != null) {
- IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(TeamRepository);
- IBaselineConnection baseLineConnection = wm.getBaselineConnection(latestLine, null);
- if (pbaseLineConnection != null && latestDate.compareTo(preBaselineDate)!=0)
- {
- IWorkspaceManager workspaceManager = com.ibm.team.scm.client.SCMPlatform.getWorkspaceManager(TeamRepository);
- IChangeHistorySyncReport changeHistory = pbaseLineConnection
- .compareTo(
- baseLineConnection.getBaseline(),
- null);
- List
incomingChangeSetHandles = changeHistory.incomingChangeSets(); - List
changeSetLink = workspaceManager - .getChangeSetLinkSummary(
- incomingChangeSetHandles, null);
- System.out.printf("\t========== Change Set Start (%d) ==========\n", changeSetLink.size());
- for (IChangeSetLinkSummary summary : changeSetLink) {
- List
linkHandles = summary.getLinks(); - for (ILinkHandle linkHandle : linkHandles) {
- ILink link = (ILink) TeamRepository
- .itemManager().fetchCompleteItem(
- linkHandle,
- IItemManager.DEFAULT, null);
- if (CHANGE_SET_TYPE.equals(link.getLinkTypeId()))
- {
- IChangeSetHandle changeSetHandle = (IChangeSetHandle) link.getSourceRef().resolve();
- IChangeSet changeset = (IChangeSet) TeamRepository.itemManager()
- .fetchCompleteItem(changeSetHandle, IItemManager.DEFAULT,null);
- System.out.printf("\t\t%s (%s)\n", changeset.getComment(), changeset.getItemId().getUuidValue());
- List
links = ChangeSetLinks - .findLinks(
- (ProviderFactory) TeamRepository
- .getClientLibrary(ProviderFactory.class),
- changeSetHandle,
- new String[] { ILinkConstants.CHANGESET_WORKITEM_LINKTYPE_ID },
- MyProgressMonitor);
- for (final ILink lk : links) {
- final Object resolved = lk
- .getTargetRef().resolve();
- if (resolved instanceof IWorkItemHandle) {
- IWorkItem workItem = (IWorkItem) TeamRepository
- .itemManager()
- .fetchCompleteItem(
- (IWorkItemHandle)resolved,
- IItemManager.DEFAULT,
- null);
- System.out.printf("\t\t\tWorkItemID=%d:%s\n", workItem.getId(), workItem.getHTMLSummary().getPlainText());
- }
- }
- }
- else
- {
- //System.out.printf("\t\t%s\n", link.getLinkTypeId());
- }
- }
- }
- System.out.printf("\t========== Change Set End ==========\n");
- pbaseLineConnection=baseLineConnection;
- preBaselineDate=latestDate;
- }
- else {
- pbaseLineConnection=baseLineConnection;
- preBaselineDate=latestDate;
- }
- }
- }
- }
- }
- }
- /**
- * Login RTC System.
- * @throws TeamRepositoryException
- */
- public static void Login() throws TeamRepositoryException {
- ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
- teamRepository.registerLoginHandler(new LoginHandler(userId, password));
- teamRepository.login(MyProgressMonitor);
- TeamRepository = teamRepository;
- }
- /**
- * Retrieve change set based on given IBuildResult object
- * http://www.ibm.com/developerworks/cn/rational/r-cn-rtcbuildwithplainjavaapi/
- * https://github.com/jenkinsci/teamconcert-plugin/blob/master/com.ibm.team.build.hjplugin-rtc/src/test/java/com/ibm/team/build/internal/hjplugin/rtc/tests/BuildConnectionTests.java
- * http://stackoverflow.com/questions/24628644/how-to-get-changesetall-files-changes-from-iworkitem-item-by-program
- * https://jazz.net/forum/questions/153345/how-to-get-associated-work-items-from-a-change-set-using-rtc-sdk
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception{
- TeamPlatform.startup();
- try {
- Login();
- Start(BuildDefinitionID);
- } catch (TeamRepositoryException e) {
- System.out.println("Unable to login: " + e.getMessage());
- } finally {
- TeamPlatform.shutdown();
- }
- }
- }
* 通过 Plain Java API 增强 Rational Team Concert 的自动化构建
* Stackoverflow - How to get ChangeSet(All files cha...from IWorkItem item by Program
* Jazz forum - How to get Associated Work Items from a Change Set using RTC SDK?
沒有留言:
張貼留言