Search in sources :

Example 1 with SCMRepository

use of com.hp.octane.integrations.dto.scm.SCMRepository in project octane-gitlab-service by MicroFocus.

the class EventListener method getScmData.

private SCMData getScmData(JSONObject event) {
    try {
        Integer projectId = event.getJSONObject("project").getInt("id");
        String sha = event.getJSONObject("object_attributes").getString("sha");
        String beforeSha = event.getJSONObject("object_attributes").getString("before_sha");
        CompareResults results = gitLabApi.getRepositoryApi().compare(projectId, beforeSha, sha);
        List<SCMCommit> commits = new ArrayList<>();
        results.getCommits().forEach(c -> {
            SCMCommit commit = dtoFactory.newDTO(SCMCommit.class);
            commit.setTime(c.getTimestamp() != null ? c.getTimestamp().getTime() : new Date().getTime());
            commit.setUser(c.getCommitterName());
            commit.setUserEmail(c.getCommitterEmail());
            commit.setRevId(c.getId());
            commit.setParentRevId(sha);
            commit.setComment(c.getMessage());
            try {
                List<Diff> diffs = gitLabApi.getCommitsApi().getDiff(projectId, c.getId());
                List<SCMChange> changes = new ArrayList<>();
                diffs.forEach(d -> {
                    SCMChange change = dtoFactory.newDTO(SCMChange.class);
                    change.setFile(d.getNewPath());
                    change.setType(d.getNewFile() ? "add" : d.getDeletedFile() ? "delete" : "edit");
                    changes.add(change);
                });
                commit.setChanges(changes);
            } catch (GitLabApiException e) {
                log.warn("Failed to add a commit to the SCM data", e);
            }
            commits.add(commit);
        });
        SCMRepository repo = dtoFactory.newDTO(SCMRepository.class);
        repo.setType(SCMType.GIT);
        repo.setUrl(event.getJSONObject("project").getString("git_http_url"));
        repo.setBranch(getBranchName(event));
        SCMData data = dtoFactory.newDTO(SCMData.class);
        data.setRepository(repo);
        data.setBuiltRevId(sha);
        data.setCommits(commits);
        return data;
    } catch (GitLabApiException e) {
        log.warn("Failed to return the SCM data. Returning null.");
        return null;
    }
}
Also used : GitLabApiException(org.gitlab4j.api.GitLabApiException) SCMData(com.hp.octane.integrations.dto.scm.SCMData) SCMCommit(com.hp.octane.integrations.dto.scm.SCMCommit) SCMChange(com.hp.octane.integrations.dto.scm.SCMChange) SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository)

Example 2 with SCMRepository

use of com.hp.octane.integrations.dto.scm.SCMRepository in project octane-gitlab-service by MicroFocus.

the class PullRequestHelper method convertAndSendMergeRequestToOctane.

public static void convertAndSendMergeRequestToOctane(MergeRequest mergeRequest, List<Commit> mrCommits, Map<String, List<Diff>> mrCommitDiffs, String repoUrl, String destinationWS) {
    SCMRepository sourceScmRepository = PullRequestHelper.createGitScmRepository(repoUrl, mergeRequest.getSourceBranch());
    SCMRepository targetScmRepository = PullRequestHelper.createGitScmRepository(repoUrl, mergeRequest.getTargetBranch());
    List<SCMCommit> pullRequestCommits = convertMergeRequestCommits(mrCommits, mrCommitDiffs);
    PullRequest pullRequest = PullRequestHelper.createPullRequest(mergeRequest, sourceScmRepository, targetScmRepository, pullRequestCommits);
    sendPullRequestToOctane(repoUrl, pullRequest, destinationWS);
}
Also used : SCMCommit(com.hp.octane.integrations.dto.scm.SCMCommit) PullRequest(com.hp.octane.integrations.dto.scm.PullRequest) SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository)

Example 3 with SCMRepository

use of com.hp.octane.integrations.dto.scm.SCMRepository 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 4 with SCMRepository

use of com.hp.octane.integrations.dto.scm.SCMRepository 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 5 with SCMRepository

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

the class ExecutorDTOTests method testDiscoveryInfo.

@Test
public void testDiscoveryInfo() {
    SCMRepository scm = dtoFactory.newDTO(SCMRepository.class);
    scm.setType(SCMType.GIT);
    scm.setUrl("git:bubu");
    DiscoveryInfo discInfo = dtoFactory.newDTO(DiscoveryInfo.class);
    discInfo.setExecutorId("123").setWorkspaceId("789").setForceFullDiscovery(true).setScmRepository(scm);
    String json = dtoFactory.dtoToJson(discInfo);
    Assert.assertNotNull(json);
}
Also used : SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository) Test(org.junit.Test)

Aggregations

SCMRepository (com.hp.octane.integrations.dto.scm.SCMRepository)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 DTOFactory (com.hp.octane.integrations.dto.DTOFactory)2 HttpMethod (com.hp.octane.integrations.dto.connectivity.HttpMethod)2 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)2 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)2 SCMCommit (com.hp.octane.integrations.dto.scm.SCMCommit)2 SCMRepositoryLinks (com.hp.octane.integrations.dto.scm.SCMRepositoryLinks)2 SCMType (com.hp.octane.integrations.dto.scm.SCMType)2 com.hp.octane.integrations.services.pullrequestsandbranches.factory (com.hp.octane.integrations.services.pullrequestsandbranches.factory)2 AuthenticationStrategy (com.hp.octane.integrations.services.pullrequestsandbranches.rest.authentication.AuthenticationStrategy)2 IOException (java.io.IOException)2 java.util (java.util)2 Consumer (java.util.function.Consumer)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 HttpStatus (org.apache.http.HttpStatus)2 PullRequest (com.hp.octane.integrations.dto.scm.PullRequest)1 SCMChange (com.hp.octane.integrations.dto.scm.SCMChange)1 SCMData (com.hp.octane.integrations.dto.scm.SCMData)1