Search in sources :

Example 1 with SCMChange

use of com.hp.octane.integrations.dto.scm.SCMChange 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)

Aggregations

SCMChange (com.hp.octane.integrations.dto.scm.SCMChange)1 SCMCommit (com.hp.octane.integrations.dto.scm.SCMCommit)1 SCMData (com.hp.octane.integrations.dto.scm.SCMData)1 SCMRepository (com.hp.octane.integrations.dto.scm.SCMRepository)1 GitLabApiException (org.gitlab4j.api.GitLabApiException)1