Search in sources :

Example 1 with SCMData

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

the class EventListener method getCIEvents.

private List<CIEvent> getCIEvents(JSONObject event) {
    List<CIEvent> events = new ArrayList<>();
    CIEventType eventType = getEventType(event);
    Integer buildCiId = getEventTargetObjectId(event);
    Object duration = getDuration(event);
    Long startTime = getStartTime(event, duration);
    SCMData scmData = null;
    boolean isScmNull = true;
    if (isPipelineEvent(event)) {
        if (eventType == CIEventType.STARTED) {
            scmData = getScmData(event);
            isScmNull = scmData == null;
        } else {
            String GITLAB_BLANK_SHA = "0000000000000000000000000000000000000000";
            isScmNull = event.getJSONObject("object_attributes").getString("before_sha").equals(GITLAB_BLANK_SHA);
        }
    }
    events.add(dtoFactory.newDTO(CIEvent.class).setProjectDisplayName(getCiDisplayName(event)).setEventType(eventType).setBuildCiId(buildCiId.toString()).setNumber(buildCiId.toString()).setProject(getCiFullName(event)).setResult(eventType == CIEventType.STARTED || eventType == CIEventType.DELETED ? null : convertCiBuildResult(getStatus(event))).setStartTime(startTime).setEstimatedDuration(null).setDuration(calculateDuration(eventType, duration)).setScmData(null).setCauses(getCauses(event, isScmNull)).setPhaseType(isPipelineEvent(event) ? PhaseType.POST : PhaseType.INTERNAL));
    if (scmData != null) {
        events.add(dtoFactory.newDTO(CIEvent.class).setProjectDisplayName(getCiDisplayName(event)).setEventType(CIEventType.SCM).setBuildCiId(buildCiId.toString()).setNumber(null).setProject(getCiFullName(event)).setResult(null).setStartTime(null).setEstimatedDuration(null).setDuration(null).setScmData(scmData).setCauses(getCauses(event, false)).setPhaseType(null));
    }
    return events;
}
Also used : SCMData(com.hp.octane.integrations.dto.scm.SCMData) CIEvent(com.hp.octane.integrations.dto.events.CIEvent) CIEventType(com.hp.octane.integrations.dto.events.CIEventType) JSONObject(org.json.JSONObject)

Example 2 with SCMData

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

SCMData (com.hp.octane.integrations.dto.scm.SCMData)2 CIEvent (com.hp.octane.integrations.dto.events.CIEvent)1 CIEventType (com.hp.octane.integrations.dto.events.CIEventType)1 SCMChange (com.hp.octane.integrations.dto.scm.SCMChange)1 SCMCommit (com.hp.octane.integrations.dto.scm.SCMCommit)1 SCMRepository (com.hp.octane.integrations.dto.scm.SCMRepository)1 GitLabApiException (org.gitlab4j.api.GitLabApiException)1 JSONObject (org.json.JSONObject)1