Search in sources :

Example 6 with SCMRepositoryLinks

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

the class GithubV3FetchHandler 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());
    String apiUrl = getApiPath(parameters.getRepoUrl());
    logConsumer.accept(this.getClass().getSimpleName() + " handler, Base url : " + baseUrl);
    SCMRepositoryLinks links = pingRepository(baseUrl, logConsumer);
    parameters.setRepoUrlSsh(links.getSshUrl());
    if (parameters.isUseSSHFormat()) {
        logConsumer.accept("Repo ssh format url : " + parameters.getRepoUrlSsh());
    }
    getRateLimitationInfo(apiUrl, logConsumer);
    String pullRequestsUrl = baseUrl + "/pulls?state=all";
    logConsumer.accept("Pull requests url : " + pullRequestsUrl);
    // prs are returned in asc order by id , therefore we need to get all before filtering , therefore page size equals to max total
    List<PullRequest> pullRequests = getPagedEntities(pullRequestsUrl, PullRequest.class, parameters.getMaxPRsToFetch(), 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.getHead().getRef()) && FetchUtils.isBranchMatch(targetPatterns, pr.getBase().getRef())).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()) {
        // users
        Set<String> userUrls = filteredPullRequests.stream().map(PullRequest::getUser).map(PullRequestUser::getUrl).collect(Collectors.toSet());
        logConsumer.accept("Fetching PR owners information ...");
        int counter = 0;
        Map<String, User> login2User = new HashMap<>();
        for (String url : userUrls) {
            User user = getEntity(url, User.class);
            login2User.put(user.getLogin(), user);
            if (counter > 0 && counter % 10 == 0) {
                logConsumer.accept("Fetching PR owners information " + counter * 100 / userUrls.size() + "%");
            }
            counter++;
        }
        Set<String> usersWithoutMails = login2User.values().stream().filter(u -> u.getEmail() == null).map(u -> u.getLogin()).collect(Collectors.toSet());
        if (!usersWithoutMails.isEmpty()) {
            logConsumer.accept("Note : Some users doesn't have defined public email in their profile. For such users, SCM user will contain their login name:  " + usersWithoutMails);
        }
        logConsumer.accept("Fetching PR owners information is done");
        logConsumer.accept("Fetching commits ...");
        counter = 0;
        for (PullRequest pr : filteredPullRequests) {
            // commits are returned in asc order by update time , therefore we need to get all before filtering , therefore page size equals to max total
            List<Commit> commits = getPagedEntities(pr.getCommitsUrl(), Commit.class, parameters.getMaxCommitsToFetch(), parameters.getMaxCommitsToFetch(), parameters.getMinUpdateTime());
            // commits
            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.getSha()).setComment(commit.getCommit().getMessage()).setUser(getUserName(commit.getCommit().getCommitter().getEmail(), commit.getCommit().getCommitter().getName())).setUserEmail(commit.getCommit().getCommitter().getEmail()).setTime(FetchUtils.convertISO8601DateStringToLong(commit.getCommit().getCommitter().getDate())).setParentRevId(commit.getParents().get(0).getSha());
                dtoCommits.add(dtoCommit);
            }
            SCMRepository sourceRepository = buildScmRepository(parameters.isUseSSHFormat(), pr.getHead());
            SCMRepository targetRepository = buildScmRepository(parameters.isUseSSHFormat(), pr.getBase());
            User prAuthor = login2User.get(pr.getUser().getLogin());
            String userId = getUserName(commitUserIdPicker, prAuthor.getEmail(), prAuthor.getLogin());
            com.hp.octane.integrations.dto.scm.PullRequest dtoPullRequest = dtoFactory.newDTO(com.hp.octane.integrations.dto.scm.PullRequest.class).setId(Integer.toString(pr.getNumber())).setTitle(pr.getTitle()).setDescription(pr.getBody()).setState(pr.getState()).setCreatedTime(FetchUtils.convertISO8601DateStringToLong(pr.getCreatedAt())).setUpdatedTime(FetchUtils.convertISO8601DateStringToLong(pr.getUpdatedAt())).setMergedTime(FetchUtils.convertISO8601DateStringToLong(pr.getMergedAt())).setIsMerged(pr.getMergedAt() != null).setAuthorName(userId).setAuthorEmail(prAuthor.getEmail()).setClosedTime(FetchUtils.convertISO8601DateStringToLong(pr.getClosedAt())).setSelfUrl(pr.getHtmlUrl()).setSourceRepository(sourceRepository).setTargetRepository(targetRepository).setCommits(dtoCommits);
            result.add(dtoPullRequest);
            if (counter > 0 && counter % 25 == 0) {
                logConsumer.accept("Fetching commits " + counter * 100 / filteredPullRequests.size() + "%");
            }
            counter++;
        }
        logConsumer.accept("Fetching commits is done");
        getRateLimitationInfo(apiUrl, logConsumer);
        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) com.hp.octane.integrations.services.pullrequestsandbranches.github.pojo(com.hp.octane.integrations.services.pullrequestsandbranches.github.pojo) HttpStatus(org.apache.http.HttpStatus) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) IOException(java.io.IOException) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) SCMType(com.hp.octane.integrations.dto.scm.SCMType) 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) ResourceNotFoundException(com.hp.octane.integrations.exceptions.ResourceNotFoundException) Pattern(java.util.regex.Pattern) SCMRepositoryLinks(com.hp.octane.integrations.dto.scm.SCMRepositoryLinks) SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository)

Example 7 with SCMRepositoryLinks

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

the class GithubV3FetchHandler method parseSCMRepositoryLinks.

@Override
public SCMRepositoryLinks parseSCMRepositoryLinks(String responseBody) throws JsonProcessingException {
    Repo repo = com.hp.octane.integrations.services.pullrequestsandbranches.github.JsonConverter.convert(responseBody, Repo.class);
    SCMRepositoryLinks links = dtoFactory.newDTO(SCMRepositoryLinks.class).setHttpUrl(repo.getClone_url()).setSshUrl(repo.getSsh_url());
    return links;
}
Also used : 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