Search in sources :

Example 1 with GitProject

use of com.epam.pipeline.entity.git.GitProject in project cloud-pipeline by epam.

the class GitManagerTest method shouldReturnTrueWhenProjectExists.

@Test
public void shouldReturnTrueWhenProjectExists() {
    final GitProject project = new GitProject();
    project.setRepoUrl(gitHost.withNamespace(ROOT_USER_NAME).withProject(REPOSITORY_NAME).asString());
    givenThat(get(urlPathEqualTo(api())).willReturn(okJson(with(project))));
    final boolean projectExists = gitManager.checkProjectExists(REPOSITORY_NAME);
    assertTrue(projectExists);
}
Also used : GitProject(com.epam.pipeline.entity.git.GitProject) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Example 2 with GitProject

use of com.epam.pipeline.entity.git.GitProject in project cloud-pipeline by epam.

the class GitlabClient method createRepo.

private GitProject createRepo(String repoName, String description) throws UnexpectedResponseStatusException, URISyntaxException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(TOKEN_HEADER, token);
    Map<String, String> parameters = new HashMap<>();
    parameters.put("name", repoName);
    if (!StringUtils.isEmpty(description)) {
        parameters.put("description", description);
    }
    parameters.put("visibility", PUBLIC_VISIBILITY);
    HttpEntity entity = new HttpEntity<>(parameters, headers);
    URI uri = new URI(String.format(GIT_POST_PROJECT_URL, gitHost));
    LOGGER.trace("Creating repo {}, URL: {}, token: {}", repoName, uri, token);
    ResponseEntity<GitProject> response = new RestTemplate().exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<GitProject>() {
    });
    if (response.getStatusCode() == HttpStatus.CREATED) {
        return response.getBody();
    } else {
        throw new UnexpectedResponseStatusException(response.getStatusCode());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitProject(com.epam.pipeline.entity.git.GitProject) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI)

Example 3 with GitProject

use of com.epam.pipeline.entity.git.GitProject in project cloud-pipeline by epam.

the class GitlabClient method projectExists.

public boolean projectExists(String name) throws GitClientException {
    String project = convertPipeNameToProject(name);
    try {
        URI uri = new URI(String.format(GIT_PROJECT_URL, gitHost, makeProjectId(adminName, project)));
        ResponseEntity<GitProject> response = new RestTemplate().exchange(uri, HttpMethod.GET, getAuthHeaders(), new ParameterizedTypeReference<GitProject>() {
        });
        return response.getStatusCode() == HttpStatus.OK;
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
            return false;
        }
        throw new UnexpectedResponseStatusException(e.getStatusCode());
    } catch (URISyntaxException | UnsupportedEncodingException e) {
        throw new GitClientException(e.getMessage(), e);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitProject(com.epam.pipeline.entity.git.GitProject) RestTemplate(org.springframework.web.client.RestTemplate) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 4 with GitProject

use of com.epam.pipeline.entity.git.GitProject in project cloud-pipeline by epam.

the class GitlabClient method createGitProject.

private GitProject createGitProject(Template template, String description, String repoName, boolean indexingEnabled, String hookUrl) throws UnexpectedResponseStatusException, URISyntaxException, IOException {
    GitProject project = createRepo(repoName, description);
    if (indexingEnabled) {
        addProjectHook(String.valueOf(project.getId()), hookUrl);
    }
    uploadFolder(template, repoName, project);
    try {
        boolean fileExists = getFileContents(project.getId().toString(), DEFAULT_README, DEFAULT_BRANCH) != null;
        if (!fileExists) {
            createFile(project, DEFAULT_README, README_DEFAULT_CONTENTS);
        }
    } catch (HttpClientErrorException e) {
        createFile(project, DEFAULT_README, README_DEFAULT_CONTENTS);
    } catch (GitClientException exception) {
        LOGGER.debug(exception.getMessage(), exception);
    }
    return project;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) GitProject(com.epam.pipeline.entity.git.GitProject) GitClientException(com.epam.pipeline.exception.git.GitClientException)

Aggregations

GitProject (com.epam.pipeline.entity.git.GitProject)4 GitClientException (com.epam.pipeline.exception.git.GitClientException)2 UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)2 URI (java.net.URI)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 RestTemplate (org.springframework.web.client.RestTemplate)2 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1