Search in sources :

Example 1 with SCMRepositoryLinks

use of com.hp.octane.integrations.dto.scm.SCMRepositoryLinks in project octane-ci-java-sdk by MicroFocus.

the class PullRequestAndBranchServiceImpl method syncBranchesToOctane.

@Override
public BranchSyncResult syncBranchesToOctane(FetchHandler fetcherHandler, BranchFetchParameters fp, Long workspaceId, CommitUserIdPicker idPicker, Consumer<String> logConsumer) throws IOException {
    // update ssh url
    String baseUrl = fetcherHandler.getRepoApiPath(fp.getRepoUrl());
    logConsumer.accept(fetcherHandler.getClass().getSimpleName() + " handler, Base url : " + baseUrl);
    SCMRepositoryLinks links = fetcherHandler.pingRepository(baseUrl, logConsumer);
    fp.setRepoUrlSsh(links.getSshUrl());
    if (fp.isUseSSHFormat()) {
        logConsumer.accept("Repo ssh format url : " + fp.getRepoUrlSsh());
    }
    String repoUrlForOctane = fp.isUseSSHFormat() ? fp.getRepoUrlSsh() : fp.getRepoUrl();
    // LOAD FROM CACHE
    boolean supportCaching = fetcherHandler instanceof GithubV3FetchHandler;
    Map<String, Long> sha2DateMapCache = null;
    if (supportCaching) {
        sha2DateMapCache = loadBranchCommitsFromCache(repoUrlForOctane, logConsumer);
    }
    // FETCH FROM CI SERVER
    List<Branch> ciServerBranches = fetcherHandler.fetchBranches(fp, sha2DateMapCache, logConsumer);
    Map<String, Branch> ciServerBranchMap = ciServerBranches.stream().collect(Collectors.toMap(Branch::getName, Function.identity()));
    // SAVE TO  CACHE
    if (supportCaching) {
        saveBranchCommitsToCache(repoUrlForOctane, logConsumer, sha2DateMapCache, ciServerBranches);
    }
    // GET BRANCHES FROM OCTANE
    String repoShortName = FetchUtils.getRepoShortName(fp.getRepoUrl());
    List<Entity> roots = getRepositoryRoots(repoUrlForOctane, workspaceId);
    List<Entity> octaneBranches = null;
    String rootId;
    if (roots != null && !roots.isEmpty()) {
        rootId = roots.get(0).getId();
        octaneBranches = getRepositoryBranches(rootId, workspaceId, false);
        logConsumer.accept("Found repository root with id " + rootId);
    } else {
        Entity createdRoot = createRepositoryRoot(repoUrlForOctane, repoShortName, workspaceId);
        rootId = createdRoot.getId();
        logConsumer.accept("Repository root is created with id " + rootId);
    }
    if (octaneBranches == null) {
        octaneBranches = Collections.emptyList();
    }
    List<Pattern> filterPatterns = FetchUtils.buildPatterns(fp.getFilter());
    Map<String, List<Entity>> octaneBranchMap = octaneBranches.stream().filter(br -> FetchUtils.isBranchMatch(filterPatterns, br.getName())).collect(groupingBy(b -> b.getName()));
    logConsumer.accept("Found " + octaneBranches.size() + " branches in ALM Octane related to defined filter.");
    // GENERATE UPDATES
    String finalRootId = rootId;
    BranchSyncResult result = new BranchSyncResult();
    // DELETED
    octaneBranchMap.entrySet().stream().filter(entry -> !ciServerBranchMap.containsKey(entry.getKey())).map(e -> e.getValue()).flatMap(Collection::stream).map(e -> dtoFactory.newDTO(Branch.class).setOctaneId(e.getId()).setName(e.getName())).forEach(b -> result.getDeleted().add(b));
    // NEW AND UPDATES
    ciServerBranches.forEach(ciBranch -> {
        if (ciBranch.isPartial()) {
            // SKIP if branch is partial (it can happen because of rate limitations or because branch is merged to master or not active for long time)
            return;
        }
        List<Entity> octaneBranchList = octaneBranchMap.get(ciBranch.getName());
        if (octaneBranchList == null) {
            // not exist in octane, check if to add
            long diff = System.currentTimeMillis() - ciBranch.getLastCommitTime();
            long diffDays = TimeUnit.MILLISECONDS.toDays(diff);
            if (diffDays < fp.getActiveBranchDays()) {
                result.getCreated().add(ciBranch);
            }
        } else {
            // check for update
            octaneBranchList.forEach(octaneBranch -> {
                if (!ciBranch.getLastCommitSHA().equals(octaneBranch.getField(EntityConstants.ScmRepository.LAST_COMMIT_SHA_FIELD)) || !ciBranch.getIsMerged().equals(octaneBranch.getField(EntityConstants.ScmRepository.IS_MERGED_FIELD))) {
                    ciBranch.setOctaneId(octaneBranch.getId());
                    result.getUpdated().add(ciBranch);
                }
            });
        }
    });
    // SEND TO OCTANE
    if (!result.getDeleted().isEmpty()) {
        List<Entity> toDelete = result.getDeleted().stream().map(b -> buildOctaneBranchForUpdateAsDeleted(b)).collect(Collectors.toList());
        entitiesService.updateEntities(workspaceId, EntityConstants.ScmRepository.COLLECTION_NAME, toDelete);
        logConsumer.accept("Deleted branches : " + toDelete.size());
    }
    if (!result.getUpdated().isEmpty()) {
        List<Entity> toUpdate = result.getUpdated().stream().map(b -> buildOctaneBranchForUpdate(b, idPicker)).collect(Collectors.toList());
        entitiesService.updateEntities(workspaceId, EntityConstants.ScmRepository.COLLECTION_NAME, toUpdate);
        logConsumer.accept("Updated branches : " + toUpdate.size());
    }
    if (!result.getCreated().isEmpty()) {
        List<Entity> toCreate = result.getCreated().stream().map(b -> buildOctaneBranchForCreate(finalRootId, b, idPicker)).collect(Collectors.toList());
        try {
            entitiesService.postEntities(workspaceId, EntityConstants.ScmRepository.COLLECTION_NAME, toCreate);
            logConsumer.accept("New branches : " + toCreate.size());
        } catch (OctaneBulkException bulkException) {
            logConsumer.accept(String.format("New branches created: %s, failed to create %s branches", (toCreate.size() - bulkException.getData().getErrors().size()), bulkException.getData().getErrors().size()));
            // handling previously deleted branches. (new branches were created with the name that already exist in Octane but set as deleted)
            boolean hasDuplicatedException = bulkException.getData().getErrors().stream().filter(ex -> EntityConstants.Errors.DUPLICATE_ERROR_CODE.equals(ex.getErrorCode())).findAny().isPresent();
            Map<String, Entity> deletedBranchesInOctane = !hasDuplicatedException ? Collections.emptyMap() : getRepositoryBranches(rootId, workspaceId, true).stream().collect(Collectors.toMap(e -> e.getStringValue(EntityConstants.ScmRepository.NAME_FIELD), Function.identity()));
            // try to update duplicates
            List<Entity> deletedBranchesToUpdate = new ArrayList<>();
            bulkException.getData().getErrors().forEach(ex -> {
                // if post was done with only one entity - index will be null
                int index = ex.getIndex() == null ? 0 : ex.getIndex();
                Branch branch = result.getCreated().get(index);
                if (EntityConstants.Errors.DUPLICATE_ERROR_CODE.equals(ex.getErrorCode())) {
                    Entity octaneEntity = deletedBranchesInOctane.get(branch.getName());
                    if (octaneEntity != null) {
                        branch.setOctaneId(octaneEntity.getId());
                        deletedBranchesToUpdate.add(buildOctaneBranchForUpdate(branch, idPicker).setField(EntityConstants.ScmRepository.IS_DELETED_FIELD, false));
                    } else {
                        logConsumer.accept("Failed to create/update branch : " + branch.getName());
                    }
                } else {
                    logConsumer.accept(String.format("Failed to create branch %s : %s ", branch.getName(), ex.getDescriptionTranslated()));
                }
            });
            if (!deletedBranchesToUpdate.isEmpty()) {
                entitiesService.updateEntities(workspaceId, EntityConstants.ScmRepository.COLLECTION_NAME, deletedBranchesToUpdate);
                logConsumer.accept("New branches that appear as deleted in ALM OCtane : " + deletedBranchesToUpdate.size());
            }
        }
    }
    if (result.getDeleted().isEmpty() && result.getUpdated().isEmpty() && result.getCreated().isEmpty()) {
        logConsumer.accept("No changes are found.");
    }
    return result;
}
Also used : GithubV3FetchHandler(com.hp.octane.integrations.services.pullrequestsandbranches.github.GithubV3FetchHandler) EntitiesService(com.hp.octane.integrations.services.entities.EntitiesService) java.util(java.util) Entity(com.hp.octane.integrations.dto.entities.Entity) PullRequest(com.hp.octane.integrations.dto.scm.PullRequest) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HttpStatus(org.apache.http.HttpStatus) CIPluginSDKUtils(com.hp.octane.integrations.utils.CIPluginSDKUtils) Function(java.util.function.Function) OctaneSDK(com.hp.octane.integrations.OctaneSDK) DTOFactory(com.hp.octane.integrations.dto.DTOFactory) ListUtils(org.apache.commons.collections4.ListUtils) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) JavaType(com.fasterxml.jackson.databind.JavaType) TypeReference(com.fasterxml.jackson.core.type.TypeReference) GithubV3FetchHandler(com.hp.octane.integrations.services.pullrequestsandbranches.github.GithubV3FetchHandler) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) com.hp.octane.integrations.services.pullrequestsandbranches.factory(com.hp.octane.integrations.services.pullrequestsandbranches.factory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(com.hp.octane.integrations.dto.connectivity.HttpMethod) ContentType(org.apache.http.entity.ContentType) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) RestService(com.hp.octane.integrations.services.rest.RestService) IOException(java.io.IOException) QueryHelper(com.hp.octane.integrations.services.entities.QueryHelper) Collectors(java.util.stream.Collectors) File(java.io.File) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) SCMType(com.hp.octane.integrations.dto.scm.SCMType) EntityConstants(com.hp.octane.integrations.dto.entities.EntityConstants) Logger(org.apache.logging.log4j.Logger) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) OctaneBulkException(com.hp.octane.integrations.exceptions.OctaneBulkException) SdkStringUtils(com.hp.octane.integrations.utils.SdkStringUtils) Pattern(java.util.regex.Pattern) ResourceNotFoundException(com.hp.octane.integrations.exceptions.ResourceNotFoundException) LogManager(org.apache.logging.log4j.LogManager) Branch(com.hp.octane.integrations.dto.scm.Branch) Entity(com.hp.octane.integrations.dto.entities.Entity) Pattern(java.util.regex.Pattern) OctaneBulkException(com.hp.octane.integrations.exceptions.OctaneBulkException) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) Branch(com.hp.octane.integrations.dto.scm.Branch)

Example 2 with SCMRepositoryLinks

use of com.hp.octane.integrations.dto.scm.SCMRepositoryLinks in project octane-ci-java-sdk by MicroFocus.

the class BitbucketServerFetchHandler method fetchPullRequests.

@Override
public List<com.hp.octane.integrations.dto.scm.PullRequest> fetchPullRequests(PullRequestFetchParameters parameters, CommitUserIdPicker commitUserIdPicker, Consumer<String> logConsumer) throws IOException {
    List<com.hp.octane.integrations.dto.scm.PullRequest> result = new ArrayList<>();
    String baseUrl = getRepoApiPath(parameters.getRepoUrl());
    logConsumer.accept("BitbucketServerRestHandler, Base url : " + baseUrl);
    SCMRepositoryLinks links = pingRepository(baseUrl, logConsumer);
    parameters.setRepoUrlSsh(links.getSshUrl());
    if (parameters.isUseSSHFormat()) {
        logConsumer.accept("Repo ssh format url : " + parameters.getRepoUrlSsh());
    }
    String pullRequestsUrl = baseUrl + "/pull-requests?state=ALL";
    logConsumer.accept("Pull requests url : " + pullRequestsUrl);
    List<PullRequest> pullRequests = getPagedEntities(pullRequestsUrl, PullRequest.class, parameters.getPageSize(), parameters.getMaxPRsToFetch(), parameters.getMinUpdateTime());
    List<Pattern> sourcePatterns = FetchUtils.buildPatterns(parameters.getSourceBranchFilter());
    List<Pattern> targetPatterns = FetchUtils.buildPatterns(parameters.getTargetBranchFilter());
    List<PullRequest> filteredPullRequests = pullRequests.stream().filter(pr -> FetchUtils.isBranchMatch(sourcePatterns, pr.getFromRef().getDisplayId()) && FetchUtils.isBranchMatch(targetPatterns, pr.getToRef().getDisplayId())).collect(Collectors.toList());
    logConsumer.accept(String.format("Received %d pull-requests, while %d are matching source/target filters", pullRequests.size(), filteredPullRequests.size()));
    if (!filteredPullRequests.isEmpty()) {
        logConsumer.accept("Fetching commits ...");
        int counter = 0;
        for (PullRequest pr : filteredPullRequests) {
            String url = baseUrl + "/pull-requests/" + pr.getId() + "/commits";
            List<Commit> commits = Collections.emptyList();
            try {
                commits = getPagedEntities(url, Commit.class, parameters.getPageSize(), parameters.getMaxCommitsToFetch(), parameters.getMinUpdateTime());
            } catch (Exception e) {
                // this issue was raised by customer : after merging PR with squash, branch was deleted and get commit of PR - returned with 404
                // Request to '.../pull-requests/259/commits?&limit=30&start=0' is ended with result 404 : Commit 'a44a0c2fc' does not exist in repository '...'.
                logConsumer.accept(String.format("Failed to fetch commits for PR %s : %s", pr.getId(), e.getMessage()));
            }
            List<com.hp.octane.integrations.dto.scm.SCMCommit> dtoCommits = new ArrayList<>();
            for (Commit commit : commits) {
                com.hp.octane.integrations.dto.scm.SCMCommit dtoCommit = dtoFactory.newDTO(com.hp.octane.integrations.dto.scm.SCMCommit.class).setRevId(commit.getId()).setComment(commit.getMessage()).setUser(getUserName(commit.getCommitter().getEmailAddress(), commit.getCommitter().getName())).setUserEmail(commit.getCommitter().getEmailAddress()).setTime(commit.getCommitterTimestamp()).setParentRevId(commit.getParents().get(0).getId());
                dtoCommits.add(dtoCommit);
            }
            SCMRepository sourceRepository = buildScmRepository(parameters.isUseSSHFormat(), pr.getFromRef());
            SCMRepository targetRepository = buildScmRepository(parameters.isUseSSHFormat(), pr.getToRef());
            boolean isMerged = PullRequest.MERGED_STATE.equals(pr.getState());
            String userId = getUserName(commitUserIdPicker, pr.getAuthor().getUser().getEmailAddress(), pr.getAuthor().getUser().getName());
            com.hp.octane.integrations.dto.scm.PullRequest dtoPullRequest = dtoFactory.newDTO(com.hp.octane.integrations.dto.scm.PullRequest.class).setId(pr.getId()).setTitle(pr.getTitle()).setDescription(pr.getDescription()).setState(pr.getState()).setCreatedTime(pr.getCreatedDate()).setUpdatedTime(pr.getUpdatedTime()).setAuthorName(userId).setAuthorEmail(pr.getAuthor().getUser().getEmailAddress()).setClosedTime(pr.getClosedDate()).setSelfUrl(pr.getLinks().getSelf().get(0).getHref()).setSourceRepository(sourceRepository).setTargetRepository(targetRepository).setCommits(dtoCommits).setMergedTime(isMerged ? pr.getClosedDate() : null).setIsMerged(isMerged);
            result.add(dtoPullRequest);
            if (counter > 0 && counter % 40 == 0) {
                logConsumer.accept("Fetching commits " + counter * 100 / filteredPullRequests.size() + "%");
            }
            counter++;
        }
        logConsumer.accept("Fetching commits is done");
        logConsumer.accept("Pull requests are ready");
    } else {
        logConsumer.accept("No new/updated PR is found.");
    }
    return result;
}
Also used : java.util(java.util) com.hp.octane.integrations.services.pullrequestsandbranches.factory(com.hp.octane.integrations.services.pullrequestsandbranches.factory) HttpMethod(com.hp.octane.integrations.dto.connectivity.HttpMethod) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository) HttpStatus(org.apache.http.HttpStatus) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SCMType(com.hp.octane.integrations.dto.scm.SCMType) com.hp.octane.integrations.services.pullrequestsandbranches.bitbucketserver.pojo(com.hp.octane.integrations.services.pullrequestsandbranches.bitbucketserver.pojo) Stream(java.util.stream.Stream) AuthenticationStrategy(com.hp.octane.integrations.services.pullrequestsandbranches.rest.authentication.AuthenticationStrategy) DTOFactory(com.hp.octane.integrations.dto.DTOFactory) Pattern(java.util.regex.Pattern) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) Pattern(java.util.regex.Pattern) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository)

Example 3 with SCMRepositoryLinks

use of com.hp.octane.integrations.dto.scm.SCMRepositoryLinks in project octane-ci-java-sdk by MicroFocus.

the class FetchHandlerTests method githubServerParseLinks.

@Test
public void githubServerParseLinks() throws JsonProcessingException {
    String body = "{\"id\":237845737,\"node_id\":\"MDEwOlJlcG9zaXRvcnkyMzc4NDU3Mzc=\",\"name\":\"trial\",\"full_name\":\"radislavB/trial\",\"private\":false,\"owner\":{\"login\":\"radislavB\",\"id\":20180777,\"node_id\":\"MDQ6VXNlcjIwMTgwNzc3\",\"avatar_url\":\"https://avatars.githubusercontent.com/u/20180777?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/radislavB\",\"html_url\":\"https://github.com/radislavB\",\"followers_url\":\"https://api.github.com/users/radislavB/followers\",\"following_url\":\"https://api.github.com/users/radislavB/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/radislavB/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/radislavB/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/radislavB/subscriptions\",\"organizations_url\":\"https://api.github.com/users/radislavB/orgs\",\"repos_url\":\"https://api.github.com/users/radislavB/repos\",\"events_url\":\"https://api.github.com/users/radislavB/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/radislavB/received_events\",\"type\":\"User\",\"site_admin\":false},\"html_url\":\"https://github.com/radislavB/trial\",\"description\":\"trial\",\"fork\":false,\"url\":\"https://api.github.com/repos/radislavB/trial\",\"forks_url\":\"https://api.github.com/repos/radislavB/trial/forks\",\"keys_url\":\"https://api.github.com/repos/radislavB/trial/keys{/key_id}\",\"collaborators_url\":\"https://api.github.com/repos/radislavB/trial/collaborators{/collaborator}\",\"teams_url\":\"https://api.github.com/repos/radislavB/trial/teams\",\"hooks_url\":\"https://api.github.com/repos/radislavB/trial/hooks\",\"issue_events_url\":\"https://api.github.com/repos/radislavB/trial/issues/events{/number}\",\"events_url\":\"https://api.github.com/repos/radislavB/trial/events\",\"assignees_url\":\"https://api.github.com/repos/radislavB/trial/assignees{/user}\",\"branches_url\":\"https://api.github.com/repos/radislavB/trial/branches{/branch}\",\"tags_url\":\"https://api.github.com/repos/radislavB/trial/tags\",\"blobs_url\":\"https://api.github.com/repos/radislavB/trial/git/blobs{/sha}\",\"git_tags_url\":\"https://api.github.com/repos/radislavB/trial/git/tags{/sha}\",\"git_refs_url\":\"https://api.github.com/repos/radislavB/trial/git/refs{/sha}\",\"trees_url\":\"https://api.github.com/repos/radislavB/trial/git/trees{/sha}\",\"statuses_url\":\"https://api.github.com/repos/radislavB/trial/statuses/{sha}\",\"languages_url\":\"https://api.github.com/repos/radislavB/trial/languages\",\"stargazers_url\":\"https://api.github.com/repos/radislavB/trial/stargazers\",\"contributors_url\":\"https://api.github.com/repos/radislavB/trial/contributors\",\"subscribers_url\":\"https://api.github.com/repos/radislavB/trial/subscribers\",\"subscription_url\":\"https://api.github.com/repos/radislavB/trial/subscription\",\"commits_url\":\"https://api.github.com/repos/radislavB/trial/commits{/sha}\",\"git_commits_url\":\"https://api.github.com/repos/radislavB/trial/git/commits{/sha}\",\"comments_url\":\"https://api.github.com/repos/radislavB/trial/comments{/number}\",\"issue_comment_url\":\"https://api.github.com/repos/radislavB/trial/issues/comments{/number}\",\"contents_url\":\"https://api.github.com/repos/radislavB/trial/contents/{+path}\",\"compare_url\":\"https://api.github.com/repos/radislavB/trial/compare/{base}...{head}\",\"merges_url\":\"https://api.github.com/repos/radislavB/trial/merges\",\"archive_url\":\"https://api.github.com/repos/radislavB/trial/{archive_format}{/ref}\",\"downloads_url\":\"https://api.github.com/repos/radislavB/trial/downloads\",\"issues_url\":\"https://api.github.com/repos/radislavB/trial/issues{/number}\",\"pulls_url\":\"https://api.github.com/repos/radislavB/trial/pulls{/number}\",\"milestones_url\":\"https://api.github.com/repos/radislavB/trial/milestones{/number}\",\"notifications_url\":\"https://api.github.com/repos/radislavB/trial/notifications{?since,all,participating}\",\"labels_url\":\"https://api.github.com/repos/radislavB/trial/labels{/name}\",\"releases_url\":\"https://api.github.com/repos/radislavB/trial/releases{/id}\",\"deployments_url\":\"https://api.github.com/repos/radislavB/trial/deployments\",\"created_at\":\"2020-02-02T22:21:33Z\",\"updated_at\":\"2021-03-04T12:02:08Z\",\"pushed_at\":\"2021-03-04T12:02:06Z\",\"git_url\":\"git://github.com/radislavB/trial.git\",\"ssh_url\":\"git@github.com:radislavB/trial.git\",\"clone_url\":\"https://github.com/radislavB/trial.git\",\"svn_url\":\"https://github.com/radislavB/trial\",\"homepage\":null,\"size\":45,\"stargazers_count\":0,\"watchers_count\":0,\"language\":null,\"has_issues\":true,\"has_projects\":true,\"has_downloads\":true,\"has_wiki\":true,\"has_pages\":false,\"forks_count\":0,\"mirror_url\":null,\"archived\":false,\"disabled\":false,\"open_issues_count\":4,\"license\":null,\"forks\":0,\"open_issues\":4,\"watchers\":0,\"default_branch\":\"master\",\"permissions\":{\"admin\":true,\"push\":true,\"pull\":true},\"temp_clone_token\":\"\",\"allow_squash_merge\":true,\"allow_merge_commit\":true,\"allow_rebase_merge\":true,\"delete_branch_on_merge\":false,\"network_count\":0,\"subscribers_count\":1}";
    GithubServerFetchHandler handler = new GithubServerFetchHandler(new NoCredentialsStrategy());
    SCMRepositoryLinks links = handler.parseSCMRepositoryLinks(body);
    Assert.assertEquals("git@github.com:radislavB/trial.git", links.getSshUrl());
    Assert.assertEquals("https://github.com/radislavB/trial.git", links.getHttpUrl());
}
Also used : NoCredentialsStrategy(com.hp.octane.integrations.services.pullrequestsandbranches.rest.authentication.NoCredentialsStrategy) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) GithubServerFetchHandler(com.hp.octane.integrations.services.pullrequestsandbranches.github.GithubServerFetchHandler) Test(org.junit.Test)

Example 4 with SCMRepositoryLinks

use of com.hp.octane.integrations.dto.scm.SCMRepositoryLinks in project octane-ci-java-sdk by MicroFocus.

the class FetchHandlerTests method bitbucketParseLinks.

@Test
public void bitbucketParseLinks() throws JsonProcessingException {
    String body = "{\"slug\":\"simple-tests\",\"id\":1,\"name\":\"simple-tests\",\"hierarchyId\":\"3652cee12ecd25817b22\",\"scmId\":\"git\",\"state\":\"AVAILABLE\",\"statusMessage\":\"Available\",\"forkable\":true,\"project\":{\"key\":\"TES\",\"id\":2,\"name\":\"tests\",\"public\":false,\"type\":\"NORMAL\",\"links\":{\"self\":[{\"href\":\"http://myd-hvm02624.swinfra.net:7990/projects/TES\"}]}},\"public\":true,\"links\":{\"clone\":[{\"href\":\"ssh://git@myd-hvm02624.swinfra.net:7999/tes/simple-tests.git\",\"name\":\"ssh\"},{\"href\":\"http://myd-hvm02624.swinfra.net:7990/scm/tes/simple-tests.git\",\"name\":\"http\"}],\"self\":[{\"href\":\"http://myd-hvm02624.swinfra.net:7990/projects/TES/repos/simple-tests/browse\"}]}}";
    BitbucketServerFetchHandler handler = new BitbucketServerFetchHandler(new NoCredentialsStrategy());
    SCMRepositoryLinks links = handler.parseSCMRepositoryLinks(body);
    Assert.assertEquals("ssh://git@myd-hvm02624.swinfra.net:7999/tes/simple-tests.git", links.getSshUrl());
    Assert.assertEquals("http://myd-hvm02624.swinfra.net:7990/scm/tes/simple-tests.git", links.getHttpUrl());
}
Also used : BitbucketServerFetchHandler(com.hp.octane.integrations.services.pullrequestsandbranches.bitbucketserver.BitbucketServerFetchHandler) NoCredentialsStrategy(com.hp.octane.integrations.services.pullrequestsandbranches.rest.authentication.NoCredentialsStrategy) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) Test(org.junit.Test)

Example 5 with SCMRepositoryLinks

use of com.hp.octane.integrations.dto.scm.SCMRepositoryLinks in project octane-ci-java-sdk by MicroFocus.

the class BitbucketServerFetchHandler method parseSCMRepositoryLinks.

@Override
public SCMRepositoryLinks parseSCMRepositoryLinks(String responseBody) throws JsonProcessingException {
    Repository repo = JsonConverter.convert(responseBody, Repository.class);
    SCMRepositoryLinks links = dtoFactory.newDTO(SCMRepositoryLinks.class);
    repo.getLinks().getClone().forEach(l -> {
        if ("ssh".equalsIgnoreCase(l.getName())) {
            links.setSshUrl(l.getHref());
        } else {
            links.setHttpUrl(l.getHref());
        }
    });
    return links;
}
Also used : SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks)

Aggregations

SCMRepositoryLinks (com.hp.octane.integrations.dto.scm.SCMRepositoryLinks)7 DTOFactory (com.hp.octane.integrations.dto.DTOFactory)3 HttpMethod (com.hp.octane.integrations.dto.connectivity.HttpMethod)3 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)3 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)3 SCMRepository (com.hp.octane.integrations.dto.scm.SCMRepository)3 SCMType (com.hp.octane.integrations.dto.scm.SCMType)3 com.hp.octane.integrations.services.pullrequestsandbranches.factory (com.hp.octane.integrations.services.pullrequestsandbranches.factory)3 IOException (java.io.IOException)3 java.util (java.util)3 Consumer (java.util.function.Consumer)3 Pattern (java.util.regex.Pattern)3 Collectors (java.util.stream.Collectors)3 HttpStatus (org.apache.http.HttpStatus)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ResourceNotFoundException (com.hp.octane.integrations.exceptions.ResourceNotFoundException)2 AuthenticationStrategy (com.hp.octane.integrations.services.pullrequestsandbranches.rest.authentication.AuthenticationStrategy)2 NoCredentialsStrategy (com.hp.octane.integrations.services.pullrequestsandbranches.rest.authentication.NoCredentialsStrategy)2 TimeUnit (java.util.concurrent.TimeUnit)2 Function (java.util.function.Function)2