use of io.fabric8.repo.git.CreateRepositoryDTO in project fabric8 by fabric8io.
the class BuildConfigHelper method importNewGitProject.
public static CreateGitProjectResults importNewGitProject(KubernetesClient kubernetesClient, UserDetails userDetails, File basedir, String namespace, String projectName, String origin, String message, boolean apply, boolean useLocalGitAddress) throws GitAPIException, JsonProcessingException {
GitUtils.disableSslCertificateChecks();
InitCommand initCommand = Git.init();
initCommand.setDirectory(basedir);
Git git = initCommand.call();
LOG.info("Initialised an empty git configuration repo at {}", basedir.getAbsolutePath());
PersonIdent personIdent = userDetails.createPersonIdent();
String user = userDetails.getUser();
String address = userDetails.getAddress();
String internalAddress = userDetails.getInternalAddress();
String branch = userDetails.getBranch();
// lets create the repository
GitRepoClient repoClient = userDetails.createRepoClient();
CreateRepositoryDTO createRepository = new CreateRepositoryDTO();
createRepository.setName(projectName);
String fullName = null;
RepositoryDTO repository = repoClient.createRepository(createRepository);
if (repository != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Got repository: " + toJson(repository));
}
fullName = repository.getFullName();
}
if (Strings.isNullOrBlank(fullName)) {
fullName = user + "/" + projectName;
}
String htmlUrl = URLUtils.pathJoin(resolveToRoot(address), user, projectName);
String localCloneUrl = URLUtils.pathJoin(resolveToRoot(internalAddress), user, projectName + ".git");
String cloneUrl = htmlUrl + ".git";
String defaultCloneUrl = cloneUrl;
// lets default to using the local git clone URL
if (useLocalGitAddress && Strings.isNotBlank(internalAddress)) {
defaultCloneUrl = localCloneUrl;
}
// now lets import the code and publish
GitUtils.configureBranch(git, branch, origin, defaultCloneUrl);
GitUtils.addDummyFileToEmptyFolders(basedir);
LOG.info("About to git commit and push to: " + defaultCloneUrl + " and remote name " + origin);
GitUtils.doAddCommitAndPushFiles(git, userDetails, personIdent, branch, origin, message, true);
Map<String, String> annotations = new HashMap<>();
annotations.put(Annotations.Builds.GIT_CLONE_URL, cloneUrl);
annotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localCloneUrl);
BuildConfig buildConfig;
if (apply) {
buildConfig = createAndApplyBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
} else {
buildConfig = createBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
}
return new CreateGitProjectResults(buildConfig, fullName, htmlUrl, localCloneUrl, cloneUrl);
}
use of io.fabric8.repo.git.CreateRepositoryDTO in project fabric8 by fabric8io.
the class DevOpsConnector method createGerritRepo.
protected void createGerritRepo(String repoName, String gerritUser, String gerritPwd, String gerritGitInitialCommit, String gerritGitRepoDescription) throws Exception {
// lets add defaults if not env vars
if (Strings.isNullOrBlank(gerritUser)) {
gerritUser = "admin";
}
if (Strings.isNullOrBlank(gerritPwd)) {
gerritPwd = "secret";
}
log.info("A Gerrit git repo will be created for this name : " + repoName);
String gerritAddress = KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GERRIT, namespace, "http", true);
log.info("Found gerrit address: " + gerritAddress + " for namespace: " + namespace + " on Kubernetes address: " + kubernetes.getMasterUrl());
if (Strings.isNullOrBlank(gerritAddress)) {
throw new Exception("No address for service " + ServiceNames.GERRIT + " in namespace: " + namespace + " on Kubernetes address: " + kubernetes.getMasterUrl());
}
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpClient httpclientPost = HttpClients.createDefault();
String GERRIT_URL = gerritAddress + "/a/projects/" + repoName;
HttpGet httpget = new HttpGet(GERRIT_URL);
System.out.println("Requesting : " + httpget.getURI());
try {
// Initial request without credentials returns "HTTP/1.1 401 Unauthorized"
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
// Get current current "WWW-Authenticate" header from response
// WWW-Authenticate:Digest realm="My Test Realm", qop="auth",
// nonce="cdcf6cbe6ee17ae0790ed399935997e8", opaque="ae40d7c8ca6a35af15460d352be5e71c"
Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
System.out.println("authHeader = " + authHeader);
DigestScheme digestScheme = new DigestScheme();
// Parse realm, nonce sent by server.
digestScheme.processChallenge(authHeader);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(gerritUser, gerritPwd);
httpget.addHeader(digestScheme.authenticate(creds, httpget, null));
HttpPost httpPost = new HttpPost(GERRIT_URL);
httpPost.addHeader(digestScheme.authenticate(creds, httpPost, null));
httpPost.addHeader("Content-Type", "application/json");
CreateRepositoryDTO createRepoDTO = new CreateRepositoryDTO();
createRepoDTO.setDescription(gerritGitRepoDescription);
createRepoDTO.setName(repoName);
createRepoDTO.setCreate_empty_commit(Boolean.valueOf(gerritGitInitialCommit));
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(createRepoDTO);
HttpEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclientPost.execute(httpPost, responseHandler);
System.out.println("responseBody : " + responseBody);
}
} catch (MalformedChallengeException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (ConnectException e) {
System.out.println("Gerrit Server is not responding");
} catch (HttpResponseException e) {
System.out.println("Response from Gerrit Server : " + e.getMessage());
throw new Exception("Repository " + repoName + " already exists !");
} finally {
httpclient.close();
httpclientPost.close();
}
}
Aggregations