Search in sources :

Example 6 with GitRepositoryEntry

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

the class GitManager method renameFolder.

private GitCommitEntry renameFolder(Pipeline pipeline, String folder, String newFolderName, String lastCommitId, String commitMessage) throws GitClientException {
    if (commitMessage == null) {
        commitMessage = String.format("Renaming update %s to %s", folder, newFolderName);
    }
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FILE_WAS_UPDATED, folder));
    Assert.isTrue(folderExists(pipeline, folder), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FOLDER_NOT_FOUND, folder));
    Assert.isTrue(!folderExists(pipeline, newFolderName), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FOLDER_ALREADY_EXISTS, newFolderName));
    final GitlabClient gitlabClient = getGitlabClientForPipeline(pipeline);
    final List<GitRepositoryEntry> allFiles = gitlabClient.getRepositoryContents(folder, GIT_MASTER_REPOSITORY, true);
    final GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    gitPushCommitEntry.setCommitMessage(commitMessage);
    for (GitRepositoryEntry file : allFiles) {
        if (file.getType().equalsIgnoreCase("tree")) {
            continue;
        }
        prepareFileForRenaming(file.getPath().replaceFirst(folder, newFolderName), file.getPath(), gitPushCommitEntry, gitlabClient);
    }
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry)

Example 7 with GitRepositoryEntry

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

the class PipelineCodeHandlerTest method setup.

@BeforeEach
void setup() {
    pipelineCodeHandler = new PipelineCodeHandler(INDEX_PREFIX, INDEX_NAME, apiClient, new ElasticIndexService(), FILE_INDEX_PATHS, objectMapper, pipelineLoader, new PipelineCodeMapper(), "master");
    expectedPipelineEvent = new PipelineEvent();
    expectedPipelineEvent.setEventType(EventType.INSERT);
    expectedPipelineEvent.setObjectType(PipelineEvent.ObjectType.PIPELINE);
    expectedPipelineEvent.setObjectId(1L);
    expectedPipelineEvent.setCreatedDate(LocalDateTime.now());
    expectedPipelineEvent.setData("{\"tag\": {\"type\": \"string\", \"value\": \"admin\"}}");
    pipeline = new Pipeline();
    pipeline.setId(1L);
    pipeline.setName(TEST_NAME);
    pipeline.setCreatedDate(DateUtils.now());
    pipeline.setParentFolderId(2L);
    pipeline.setDescription(TEST_DESCRIPTION);
    pipeline.setRepository(TEST_REPO);
    pipeline.setTemplateId(TEST_TEMPLATE);
    Revision revision = new Revision();
    revision.setName(TEST_VERSION);
    PipelineDoc pipelineDoc = PipelineDoc.builder().pipeline(pipeline).revisions(Collections.singletonList(revision)).build();
    gitPushEventData = new GitEventData();
    gitPushEventData.setGitEventType(GitEventType.push);
    gitPushEventData.setPaths(Collections.singletonList(FILE_INDEX_PATHS));
    gitPushEventData.setVersion(VERSION);
    gitTagEventData = new GitEventData();
    gitTagEventData.setGitEventType(GitEventType.tag_push);
    gitTagEventData.setPaths(Collections.singletonList(FILE_INDEX_PATHS));
    gitTagEventData.setVersion(VERSION);
    gitRepositoryEntry = new GitRepositoryEntry();
    gitRepositoryEntry.setId("1");
    gitRepositoryEntry.setName(TEST_NAME);
    gitRepositoryEntry.setPath(FOLDER_INDEX_PATHS);
    gitRepositoryEntry.setType("blob");
    container = EntityContainer.<PipelineDoc>builder().entity(pipelineDoc).owner(USER).metadata(METADATA).permissions(PERMISSIONS_CONTAINER).build();
}
Also used : GitEventData(com.epam.pipeline.elasticsearchagent.model.git.GitEventData) PipelineCodeMapper(com.epam.pipeline.elasticsearchagent.service.impl.converter.pipeline.PipelineCodeMapper) PipelineEvent(com.epam.pipeline.elasticsearchagent.model.PipelineEvent) Revision(com.epam.pipeline.entity.pipeline.Revision) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) PipelineDoc(com.epam.pipeline.elasticsearchagent.model.PipelineDoc) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with GitRepositoryEntry

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

the class GitlabClient method createFile.

private void createFile(GitProject project, String path, String content) throws URISyntaxException, UnexpectedResponseStatusException, UnsupportedEncodingException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(TOKEN_HEADER, token);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("file_path", path);
    parameters.put("branch_name", DEFAULT_BRANCH);
    parameters.put("commit_message", "New pipeline initial commit");
    parameters.put("content", content);
    String url = addUrlParameters(String.format(GIT_POST_FILE_URL, gitHost, project.getId().toString(), URLEncoder.encode(path, Charset.defaultCharset().displayName())), parameters);
    URI uri = new URI(url);
    HttpEntity entity = new HttpEntity(headers);
    ResponseEntity<GitRepositoryEntry> response = new RestTemplate().exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<GitRepositoryEntry>() {
    });
    if (response.getStatusCode() != HttpStatus.CREATED) {
        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) RestTemplate(org.springframework.web.client.RestTemplate) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) URI(java.net.URI)

Example 9 with GitRepositoryEntry

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

the class GitlabClient method addProjectHook.

private GitRepositoryEntry addProjectHook(String projectId, String hookUrl) throws UnsupportedEncodingException, UnexpectedResponseStatusException, URISyntaxException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(TOKEN_HEADER, token);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("url", hookUrl);
    parameters.put("push_events", true);
    parameters.put("push_events_branch_filter", DEFAULT_BRANCH);
    parameters.put("tag_push_events", true);
    parameters.put("enable_ssl_verification", false);
    String url = addUrlParameters(String.format(GITLAB_PROJECT_HOOKS, gitHost, projectId), parameters);
    URI uri = new URI(url);
    HttpEntity entity = new HttpEntity(headers);
    ResponseEntity<GitRepositoryEntry> response = new RestTemplate().exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<GitRepositoryEntry>() {
    });
    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) RestTemplate(org.springframework.web.client.RestTemplate) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) URI(java.net.URI)

Example 10 with GitRepositoryEntry

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

the class GitManagerTest method shouldFetchPipelineDocs.

@Test
public void shouldFetchPipelineDocs() throws GitClientException {
    final Pipeline pipeline = testingPipeline();
    final GitRepositoryEntry bla = new GitRepositoryEntry();
    bla.setName(README_FILE);
    bla.setType(BLOB_TYPE);
    final List<GitRepositoryEntry> tree = singletonList(bla);
    givenThat(get(urlPathEqualTo(api(REPOSITORY_TREE))).withQueryParam(REF_NAME, equalTo(TEST_REVISION)).withQueryParam(PATH, equalTo(DOCS + "/")).withQueryParam(RECURSIVE, equalTo(String.valueOf(false))).willReturn(okJson(with(tree))));
    final GitTagEntry tag = new GitTagEntry();
    givenThat(get(urlPathEqualTo(api(REPOSITORY_TAGS + "/" + TEST_REVISION))).willReturn(okJson(with(tag))));
    final List<GitRepositoryEntry> repoEntries = gitManager.getPipelineDocs(pipeline.getId(), TEST_REVISION);
    final boolean noEntries = repoEntries.isEmpty();
    final boolean docsOnly = repoEntries.stream().filter(e -> !e.getName().startsWith(".")).allMatch(e -> e.getName().endsWith(".md"));
    assertFalse(noEntries);
    assertTrue(docsOnly);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PipelineManager(com.epam.pipeline.manager.pipeline.PipelineManager) SneakyThrows(lombok.SneakyThrows) Date(java.util.Date) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) ZonedDateTime(java.time.ZonedDateTime) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) WireMock.notFound(com.github.tomakehurst.wiremock.client.WireMock.notFound) Collections.singletonList(java.util.Collections.singletonList) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) Matchers.eq(org.mockito.Matchers.eq) After(org.junit.After) PipelineSourceItemVO(com.epam.pipeline.controller.vo.PipelineSourceItemVO) WireMock.givenThat(com.github.tomakehurst.wiremock.client.WireMock.givenThat) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Path(java.nio.file.Path) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) SpyBean(org.springframework.boot.test.mock.mockito.SpyBean) GitProject(com.epam.pipeline.entity.git.GitProject) UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) GIT_MASTER_REPOSITORY(com.epam.pipeline.manager.git.GitManager.GIT_MASTER_REPOSITORY) Matchers.any(org.mockito.Matchers.any) Base64(java.util.Base64) List(java.util.List) PipelineSourceItemsVO(com.epam.pipeline.controller.vo.PipelineSourceItemsVO) Whitebox(org.mockito.internal.util.reflection.Whitebox) GitRepositoryUrl(com.epam.pipeline.entity.git.GitRepositoryUrl) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertFalse(org.junit.Assert.assertFalse) UTC(java.time.ZoneOffset.UTC) CmdExecutor(com.epam.pipeline.manager.CmdExecutor) Comparator.reverseOrder(java.util.Comparator.reverseOrder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GitTagEntry(com.epam.pipeline.entity.git.GitTagEntry) GitClientException(com.epam.pipeline.exception.git.GitClientException) Mock(org.mockito.Mock) CoreMatchers.not(org.hamcrest.CoreMatchers.not) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) WireMock.okJson(com.github.tomakehurst.wiremock.client.WireMock.okJson) CONTENT_TYPE(com.google.common.net.HttpHeaders.CONTENT_TYPE) Matchers.anyString(org.mockito.Matchers.anyString) Value(org.springframework.beans.factory.annotation.Value) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) WireMock.urlPathEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) GitFile(com.epam.pipeline.entity.git.GitFile) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) File(java.io.File) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) Ignore(org.junit.Ignore) WireMock.created(com.github.tomakehurst.wiremock.client.WireMock.created) Paths(java.nio.file.Paths) Revision(com.epam.pipeline.entity.pipeline.Revision) DRAFT_PREFIX(com.epam.pipeline.manager.git.GitManager.DRAFT_PREFIX) Collections(java.util.Collections) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) GitTagEntry(com.epam.pipeline.entity.git.GitTagEntry) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Aggregations

GitRepositoryEntry (com.epam.pipeline.entity.git.GitRepositoryEntry)11 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)6 GitCommitEntry (com.epam.pipeline.entity.git.GitCommitEntry)4 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)4 Test (org.junit.Test)4 PipelineSourceItemVO (com.epam.pipeline.controller.vo.PipelineSourceItemVO)3 GitFile (com.epam.pipeline.entity.git.GitFile)3 GitPushCommitEntry (com.epam.pipeline.entity.git.GitPushCommitEntry)2 Revision (com.epam.pipeline.entity.pipeline.Revision)2 GitClientException (com.epam.pipeline.exception.git.GitClientException)2 UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 RestTemplate (org.springframework.web.client.RestTemplate)2 PipelineSourceItemsVO (com.epam.pipeline.controller.vo.PipelineSourceItemsVO)1 UploadFileMetadata (com.epam.pipeline.controller.vo.UploadFileMetadata)1 PipelineDoc (com.epam.pipeline.elasticsearchagent.model.PipelineDoc)1