It's all about the answers!

Ask a question

How to Edit Jenkins URL on Build Engine Using Java API


Jim Bomhdia (9256) | asked May 13 '14, 3:25 p.m.
I want to write a script to clone a build engine except the new build engine will point to a different Jenkins instance. When I open the Build Engine in Eclipse I have a Hudson.Jenkins tab next to the Overview tab. In the Hudson/Jenkins tab there is a text field labeled Hudson/Jenkins URL:. I have used the API to retrieve the build engine using code like this:

IBuildEngineQueryModel queryModel = IBuildEngineQueryModel.ROOT;
IItemQuery query = IItemQuery.FACTORY.newInstance(queryModel);
IPredicate supportsDef = queryModel.supportedBuildDefinitions().itemId()._eq(query.newUUIDArg());
query.filter(supportsDef);

IItemQueryPage queryPage = null;
try {
queryPage = buildClient.queryItems(query, new Object[] { buildDefinition.getItemId() }, 10, myMonitor);
} catch (Exception e) {
e.printStackTrace();
}

I'm using Eclipse Client 4.0.6. I would like to have some code that would work like this:

IBuildEngine buildEngine = getBuildEngine();
Object[] extensions = buildEngine.getExtensions();
for (int i = 0; i < extensions.length; i++) {
if (extensions[i] instanceof JenkinsExtension) {
((JenkinsExtension)extensions[i]).setJenkinsURL("http://127.0.0.1");
}
}

I've been searching the Java API but have had no luck determining how to gain access to that text field. Is this possible? Can you provide any hint?

Accepted answer


permanent link
Scott Cowan (966310) | answered May 14 '14, 10:23 a.m.
JAZZ DEVELOPER
Hi Jim,

You should be able to do something like this,

        import com.ibm.rational.connector.hudson.internal.common.HudsonConfigurationElement;
        ...
        IBuildEngine copy = buildEngine.copyEngine();
        copy.setId("Copy of " + buildEngine.getId());
        copy.setConfigurationProperty(
                HudsonConfigurationElement.ENGINE_ELEMENT_ID,
                HudsonConfigurationElement.PROPERTY_HUDSON_URL, JENKINS_URL);
        buildClient.save(copy, null);

Hope that helps,
Scott
Jim Bomhdia selected this answer as the correct answer

Comments
Jim Bomhdia commented May 14 '14, 3:36 p.m.

I wasn't able to fit all the information in the comment box here so I clicked "answer my own question". Although, I still don't really have a solution.


Jim Bomhdia commented May 15 '14, 8:25 a.m.

This worked great thanks. I'll try to post a complete code below. 

3 other answers



permanent link
Scott Cowan (966310) | answered May 14 '14, 4:31 p.m.
JAZZ DEVELOPER
It sounds like your getBuildEngine() isn't fetching the complete item.  You could try,

        buildEngine = (IBuildEngine) getTeamRepository().itemManager()
                .fetchCompleteItem(buildEngine, IItemManager.REFRESH, null);

Comments
sam detweiler commented May 14 '14, 4:33 p.m. | edited May 14 '14, 4:34 p.m.

right.. After you UPDATE (save) the buildengine object , I think the in storage working copy is no longer any good, so you have to reload it to get the latest. 


I have to do this when I do my project migration app. I update the project area piece at a time, and have to refresh between each save


permanent link
Jim Bomhdia (9256) | answered May 14 '14, 3:35 p.m.
That is probably the answer I was looking for but I have not yet been able to test it. I had an IllegalStateException which went away when I put the operation into an Eclipse Job. Then I had an Immutable exception thing. I think I was able to work around that using getWorkingCopy or copyEngine. But now the IllegalStateException is back and I can't make sense of it. Some posts say it needs to run in another thread. That hasn't helped me. Also, the IBuildEngine reports no configurationElements exist.

Here is the code I ran to get these results:

org.eclipse.core.runtime.jobs.Job job = new org.eclipse.core.runtime.jobs.Job("my job") {
protected org.eclipse.core.runtime.IStatus run(IProgressMonitor monitor) {
try {
IBuildEngine newEngine = (IBuildEngine)result.getWorkingCopy();//copyEngine produces the same result
System.out.println("running job " + newEngine.getConfigurationElement(HudsonConfigurationElement.ELEMENT_ID));
System.out.println("running job " + newEngine.getConfigurationElement(HudsonConfigurationElement.PROPERTY_HUDSON_URL));
System.out.println("running job " + newEngine.getConfigurationPropertyValue(HudsonConfigurationElement.ELEMENT_ID, HudsonConfigurationElement.PROPERTY_HUDSON_URL, "beeo"));
// your longop call here
newEngine.setConfigurationProperty( 
HudsonConfigurationElement.ENGINE_ELEMENT_ID, 
HudsonConfigurationElement.PROPERTY_HUDSON_URL, "http://hcntpnv02.paychex.com:8080/"); 
System.out.println("done setting");
buildClient.save(newEngine, null); 
} catch (Exception e) {
System.out.println("awww " + e);
e.printStackTrace();
}
System.out.println("done saving");
return null;
}
};
job.schedule(); // start the job
try {
job.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("done joining");
try {
Thread.sleep(30000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("after sleep");
System.out.println("result config = " + result.getConfigurationElements());

After executing I get:

running job null (ConfigurationElement HudsonConfigurationElement.ELEMENT_ID does not exist)
running job null (ConfigurationElement HudsonConfigurationElement.PROPERTY_HUDSON_URL does not exist)
running job beeo (getConfigurationPropertyValue did not return the value)
awww java.lang.IllegalStateException  (setConfigurationProperty fails for unknown reason)
java.lang.IllegalStateException
        at com.ibm.team.build.internal.common.model.impl.BuildEngineImpl.setConf
igurationProperty(BuildEngineImpl.java:1300)
        at CreateReminder$1.run(CreateReminder.java:287)
done saving (this only prints because it is after the catch)
done joining
after sleep (sleep was added to guarentee the other thread didnt just need more time)
result config = [] (IBuildEngine reports no results from getConfigurationElements)
not shown here but getProperties also reports an empty list.

In Eclipse UI I can see the BuildEngine and its Jenkins configuration, URL included. Any ideas?

permanent link
Jim Bomhdia (9256) | answered May 15 '14, 8:28 a.m.
 IBuildEngine result = getBuildEngines(definition, buildClient, myMonitor, teamRepository);
IItemManager itemManager = teamRepository.itemManager();
final IBuildEngine completeBuildEngine = (IBuildEngine) itemManager.fetchCompleteItem(result, IItemManager.REFRESH, null);
org.eclipse.core.runtime.jobs.Job job = new org.eclipse.core.runtime.jobs.Job("my job") {
protected org.eclipse.core.runtime.IStatus run(IProgressMonitor monitor) {
try {
IBuildEngine newEngine = (IBuildEngine)completeBuildEngine.getWorkingCopy();
newEngine.setConfigurationProperty( 
HudsonConfigurationElement.ENGINE_ELEMENT_ID, 
HudsonConfigurationElement.PROPERTY_HUDSON_URL, "http://127.0.0.1/"); 
System.out.println("done setting");
buildClient.save(newEngine, null); 
} catch (Exception e) {
System.out.println("awww " + e);
e.printStackTrace();
}
System.out.println("done saving");
return null;
}
};
job.schedule(); // start the job
try {
job.join();
} catch (Exception e) {
e.printStackTrace();
}


private IBuildEngine getBuildEngines(IBuildDefinition buildDefinition, ITeamBuildClient buildClient, MyMonitor myMonitor, ITeamRepository teamRepository) {
IBuildEngine firstResult = null;
IBuildEngineQueryModel queryModel = IBuildEngineQueryModel.ROOT;
IItemQuery query = IItemQuery.FACTORY.newInstance(queryModel);
IPredicate supportsDef = queryModel.supportedBuildDefinitions().itemId()._eq(query.newUUIDArg());
query.filter(supportsDef);

IItemQueryPage queryPage = null;
try {
queryPage = buildClient.queryItems(query, new Object[] { buildDefinition.getItemId() }, 10, myMonitor);
} catch (Exception e) {
e.printStackTrace();
}

if (queryPage.getResultSize() == 0) {
System.out.println("Zero build engines found for build definition '" + buildDefinition.getItemId() + "'");
} else {

System.out.println("Found " + queryPage.getResultSize() + " build engine(s) for build definition '" + buildDefinition.getItemId() + "':");

int pageNum = 1;
while (queryPage != null) {

System.out.println("Page:" + pageNum++);

// print out a page of IDs of the retrieved build engines
String[] properties = new String[] { IBuildEngine.PROPERTY_ID };
IFetchResult fetchResult = null;
try {
fetchResult = teamRepository.itemManager().fetchPartialItemsPermissionAware(queryPage.getItemHandles(), IItemManager.DEFAULT, Arrays.asList(properties), myMonitor);
} catch (Exception e) {
e.printStackTrace();
}

List<IBuildEngine> buildEngines = fetchResult.getRetrievedItems();
for (IBuildEngine buildEngine : buildEngines) {
System.out.println(buildEngine.getId());
if (firstResult == null) {
firstResult = buildEngine;
}
}
if (queryPage.hasNext()) {
try {
queryPage = (IItemQueryPage) buildClient.fetchPage(queryPage.getToken(), queryPage.getNextStartPosition(), queryPage.getSize(), myMonitor);
} catch (Exception e) {
e.printStackTrace();
}
} else {
queryPage = null;
}

}
}
return firstResult;
}

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.