It's all about the answers!

Ask a question

[SCM]How do I add a link to an already registered SCM file to changeset?


Kodac Hasubo (364) | asked Mar 27 '22, 6:36 a.m.

 (I am posting a fileto SCM by  EWM SDK or the Plain Java API)

When I registered a new file in SCM, I was able to create a link to the changeset and work item.

If you already have a file registered in SCM,
I want to create a link to the files present in that SCM in the changeset.
* I would like to create these links in TeamPlatform.

How do I add a link to an already registered file to my changeset?

Below is a program that creates links to changesets and work items when you register a new file in SCM.

-------------------------------------------------------------------------------------------------

IChangeSetHandle changeSetHandle = workspaceConne.
createChangeSet(component, props.getProperty(prpChangesetComment), true, monitor);


IConfiguration configuration = workspaceConne.configuration(component);

File archiveFile = new File(strZipFilePath);

FileInputStream fileInputStream = new FileInputStream(archiveFile);


Charset charset = Charset.forName(props.getProperty(prpZipFileInputEncode));

ZipInputStream zipInStream = new ZipInputStream(fileInputStream, charset);
IFolder parentFolder = findOrCreateFolderWithParents(
targetFile.getParentFile(),
configuration,
monitor,
workspaceConne,
changeSetHandle
);

IFileItem file = getFile(targetFile, parentFolder, configuration, monitor);


if (file == null) {

file = createFileItem(targetFile.getName(), zipEntry, parentFolder);

}

ByteArrayOutputStream contents = copyFileData(zipInStream);

try {
IFileContentManager contentManager = FileSystemCore.getContentManager(teamRepository);

IFileContent storedzipContent = contentManager.storeContent(
IFileContent.ENCODING_UTF_8,
FileLineDelimiter.LINE_DELIMITER_PLATFORM,
new VersionedContentManagerByteArrayInputStreamPovider(
contents.toByteArray()), null, monitor);

if (!storedzipContent.sameContent(file.getContent())) {
IFileItem fileWorkingCopy = (IFileItem) file.getWorkingCopy();

fileWorkingCopy.setContent(storedzipContent);
Collection<ISaveOp> col = (Collection<ISaveOp>) Collections.singletonList(workspaceConne.
configurationOpFactory().save(fileWorkingCopy));

workspaceConne.commit(changeSetHandle, col, monitor);


addWorkItemLink(workItemId, teamRepository, monitor, changeSetHandle, workspaceConne);
}
} catch (Exception e) {

System.out.println(e.toString());

e.printStackTrace();

} finally {
contents.close();
}
}
-------------------------------------------------------------------------------------------------
private static IFolder findOrCreateFolderWithParents(
File folder, IConfiguration configuration,
IProgressMonitor monitor,
IWorkspaceConnection workspaceConne, IChangeSetHandle changeSetHandle)
throws TeamRepositoryException {

IFolder parent = null;

String folderName = folder.getName();

String parentName = folder.getParent();
if (parentName == null) {
parent = configuration.completeRootFolder(monitor);

} else {
parent = findOrCreateFolderWithParents(new File(parentName), configuration, monitor, workspaceConne, changeSetHandle);

}

IFolder found = getFolder(folderName, parent, configuration, monitor);

if (found == null) {
found = createFolder(folderName, parent, changeSetHandle, monitor, workspaceConne);
}

return found;
}
private static IFileItem createFileItem(String name, ZipEntry zipEntry,
IFolder parentFolder) throws TeamRepositoryException {

IFileItem aFile = (IFileItem) IFileItem.ITEM_TYPE.createItem();

aFile.setParent(parentFolder);

aFile.setName(name);

aFile.setContentType(IFileItem.CONTENT_TYPE_TEXT);

aFile.setFileTimestamp(new Date(zipEntry.getTime()));

return aFile;
}
-------------------------------------------------------------------------------------------------

public void addWorkItemLink(int workItemId, ITeamRepository teamRepository,
IProgressMonitor monitor, IChangeSetHandle changeSetHandle,
IWorkspaceConnection workSpaceConne) throws TeamRepositoryException {

IWorkItemCommon common = (IWorkItemCommon) teamRepository.getClientLibrary(IWorkItemCommon.class);

IWorkItem workItem = common.findWorkItemById(workItemId, IWorkItem.SMALL_PROFILE, monitor);

if (workItem == null) {
System.out.println(props.getProperty(prpLogNotFindWorkitem));
return;
}

IWorkItemHandle[] workItemHandles = new IWorkItemHandle[1];
workItemHandles[0] = (IWorkItemHandle) workItem.getItemHandle();
IWorkspaceHandle wsHandle = (IWorkspaceHandle) workSpaceConne.getResolvedWorkspace().getItemHandle();

IFileSystemWorkItemManager fsWorkItemManager = (IFileSystemWorkItemManager) teamRepository.
getClientLibrary(IFileSystemWorkItemManager.class);

fsWorkItemManager.createLink(wsHandle, changeSetHandle, workItemHandles, monitor);
}
-------------------------------------------------------------------------------------------------

Accepted answer


permanent link
Ralph Schoon (63.1k33645) | answered Mar 28 '22, 6:06 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited Mar 28 '22, 8:30 a.m.

 For all I know, there is no concept such as registering a file to SCM or linking a file to a work item. As far as I am aware there is only a capability to link a change set to a work item or a change request. The first one is a special link type that is aware of the work item and its characteristics. The second is a special link type for OSLC relationships, that does not assume the work item nature. This link could be set up to any OSLC provider that is correctly configured.



The IFileSystemWorkItemManager API you use should work with the existing change set as well. You have to find the relevant change set and get at least its handle. I can not provide how to do that.

Kodac Hasubo selected this answer as the correct answer

One other answer



permanent link
Kodac Hasubo (364) | answered Apr 02 '22, 8:58 p.m.
edited Apr 02 '22, 9:01 p.m.
workItem002 and Scm_FileItem_A
If I create a change set link that ties, I think that first, search for ChangeSet_A and add workitem002 to the ChangeSet_A. thank you.

  ChangeSetLink


Comments
Ralph Schoon commented Apr 04 '22, 2:52 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

This statement does not make any sense to me. 

Your answer


Register or to post your answer.