@Inject
private TestService testService;
public JobResult createOrCloneRepository( final String jobId,
final RepositoryRequest repository ) {
JobResult result = new JobResult();
result.setJobId( jobId );
if ( repository.getRequestType() == null || "".equals( repository.getRequestType() )
|| !( "new".equals( repository.getRequestType() ) || ( "clone".equals( repository.getRequestType() ) ) ) ) {
result.setStatus( JobStatus.BAD_REQUEST );
result.setResult( "Repository request type can only be new or clone." );
return result;
}
final String scheme = "git";
if ( "new".equals( repository.getRequestType() ) ) {
if ( repository.getName() == null || "".equals( repository.getName() ) ) {
result.setStatus( JobStatus.BAD_REQUEST );
result.setResult( "Repository name must be provided" );
return result;
}
// username and password are optional
final Map<String, Object> env = new HashMap<String, Object>( 3 );
if ( repository.getUserName() != null && !"".equals( repository.getUserName() ) ) {
env.put( "username", repository.getUserName() );
}
if ( repository.getPassword() != null && !"".equals( repository.getPassword() ) ) {
env.put( "crypt:password", repository.getPassword() );
}
env.put( "init", true );
org.guvnor.structure.repositories.Repository newlyCreatedRepo = repositoryService.createRepository( scheme, repository.getName(), env );
if ( newlyCreatedRepo != null ) {
result.setStatus( JobStatus.SUCCESS );
result.setResult( "Alias: " + newlyCreatedRepo.getAlias() + ", Scheme: " + newlyCreatedRepo.getScheme() + ", Uri: " + newlyCreatedRepo.getUri() );
} else {
result.setStatus( JobStatus.FAIL );
}
} else if ( "clone".equals( repository.getRequestType() ) ) {
if ( repository.getName() == null || "".equals( repository.getName() ) || repository.getGitURL() == null
|| "".equals( repository.getGitURL() ) ) {
result.setStatus( JobStatus.BAD_REQUEST );
result.setResult( "Repository name and GitURL must be provided" );
}
// username and password are optional
final Map<String, Object> env = new HashMap<String, Object>( 3 );
if ( repository.getUserName() != null && !"".equals( repository.getUserName() ) ) {
env.put( "username", repository.getUserName() );
}
if ( repository.getPassword() != null && !"".equals( repository.getPassword() ) ) {
env.put( "crypt:password", repository.getPassword() );
}
env.put( "origin", repository.getGitURL() );
org.guvnor.structure.repositories.Repository newlyCreatedRepo = repositoryService.createRepository( scheme, repository.getName(), env );
if ( newlyCreatedRepo != null ) {
result.setStatus( JobStatus.SUCCESS );
result.setResult( "Alias: " + newlyCreatedRepo.getAlias() + ", Scheme: " + newlyCreatedRepo.getScheme() + ", Uri: " + newlyCreatedRepo.getUri() );
} else {
result.setStatus( JobStatus.FAIL );
}
}
return result;
}