{
if ( checkProtocol )
{
if ( !urlValidator.validate( metadataUrl ) )
{
ContinuumProjectBuildingResult res = new ContinuumProjectBuildingResult();
res.addError( ContinuumProjectBuildingResult.ERROR_PROTOCOL_NOT_ALLOWED );
return res;
}
}
Map<String, Object> context = new HashMap<String, Object>();
CreateProjectsFromMetadataAction.setProjectBuilderId( context, projectBuilderId );
CreateProjectsFromMetadataAction.setUrl( context, metadataUrl );
CreateProjectsFromMetadataAction.setLoadRecursiveProject( context, loadRecursiveProjects );
StoreProjectAction.setUseScmCredentialsCache( context, useCredentialsCache );
AbstractContinuumAction.setWorkingDirectory( context, getWorkingDirectory() );
// CreateProjectsFromMetadataAction will check null and use default
if ( buildDefinitionTemplateId > 0 )
{
try
{
AbstractContinuumAction.setBuildDefinitionTemplate( context,
buildDefinitionService.getBuildDefinitionTemplate(
buildDefinitionTemplateId ) );
}
catch ( BuildDefinitionServiceException e )
{
throw new ContinuumException( e.getMessage(), e );
}
}
// ----------------------------------------------------------------------
// Create the projects from the URL
// ----------------------------------------------------------------------
executeAction( "create-projects-from-metadata", context );
ContinuumProjectBuildingResult result = CreateProjectsFromMetadataAction.getProjectBuildingResult( context );
if ( log.isInfoEnabled() )
{
if ( result.getProjects() != null )
{
log.info( "Created " + result.getProjects().size() + " projects." );
}
if ( result.getProjectGroups() != null )
{
log.info( "Created " + result.getProjectGroups().size() + " project groups." );
}
log.info( result.getErrors().size() + " errors." );
// ----------------------------------------------------------------------
// Look for any errors.
// ----------------------------------------------------------------------
if ( result.hasErrors() )
{
log.info( result.getErrors().size() + " errors during project add: " );
log.info( result.getErrorsAsString() );
return result;
}
}
// ----------------------------------------------------------------------
// Save any new project groups that we've found. Currently all projects
// will go into the first project group in the list.
// ----------------------------------------------------------------------
if ( result.getProjectGroups().size() != 1 )
{
throw new ContinuumException( "The project building result has to contain exactly one project group." );
}
ProjectGroup projectGroup = result.getProjectGroups().iterator().next();
boolean projectGroupCreation = false;
try
{
if ( projectGroupId == -1 )
{
try
{
projectGroup = projectGroupDao.getProjectGroupByGroupId( projectGroup.getGroupId() );
projectGroupId = projectGroup.getId();
log.info( "Using existing project group with the group id: '" + projectGroup.getGroupId() + "'." );
}
catch ( ContinuumObjectNotFoundException e )
{
log.info( "Creating project group with the group id: '" + projectGroup.getGroupId() + "'." );
Map<String, Object> pgContext = new HashMap<String, Object>();
AbstractContinuumAction.setWorkingDirectory( pgContext, getWorkingDirectory() );
AbstractContinuumAction.setUnvalidatedProjectGroup( pgContext, projectGroup );
executeAction( "validate-project-group", pgContext );
executeAction( "store-project-group", pgContext );
projectGroupId = AbstractContinuumAction.getProjectGroupId( pgContext );
projectGroupCreation = true;
}
}
projectGroup = projectGroupDao.getProjectGroupWithBuildDetailsByProjectGroupId( projectGroupId );
String url = CreateProjectsFromMetadataAction.getUrl( context );
List<ProjectScmRoot> scmRoots = getProjectScmRootByProjectGroup( projectGroup.getId() );
boolean found = false;
for ( ProjectScmRoot scmRoot : scmRoots )
{
if ( url.startsWith( scmRoot.getScmRootAddress() ) )
{
found = true;
break;
}
}
if ( !found )
{
createProjectScmRoot( projectGroup, url );
}
/* add the project group loaded from database, which has more info, like id */
result.getProjectGroups().remove( 0 );
result.getProjectGroups().add( projectGroup );
}
catch ( ContinuumStoreException e )
{
throw new ContinuumException( "Error while querying for project group.", e );
}
// ----------------------------------------------------------------------
// Save all the projects if recursive mode asked
// TODO: Validate all the projects before saving them
// ----------------------------------------------------------------------
List<Project> projects = result.getProjects();
String scmUserName = null;
String scmPassword = null;
for ( Project project : projects )
{
checkForDuplicateProjectInGroup( projectGroup, project, result );
if ( result.hasErrors() )
{
log.info( result.getErrors().size() + " errors during project add: " );
log.info( result.getErrorsAsString() );
return result;
}
project.setScmUseCache( useCredentialsCache );