Search in sources :

Example 1 with Counter

use of com.bbva.arq.devops.ae.mirrorgate.collectors.jira.support.Counter in project mirrorgate-jira-stories-collector by BBVA.

the class JiraIssuesServiceImpl method getById.

public Pageable<IssueDTO> getById(List<Long> ids) {
    final StringBuilder sb = new StringBuilder(200);
    final Counter counter = new Counter();
    return (() -> {
        int firstItem = counter.get();
        if (counter.get() >= ids.size()) {
            return new ArrayList<>();
        }
        for (int i = 0; i < PAGE_SIZE && counter.get() < ids.size(); counter.inc(), i++) {
            if (i > 0) {
                sb.append(',');
            }
            sb.append(ids.get(counter.get()));
        }
        String query = String.format(ISSUES_BY_ID_QUERY_PATTERN, sb.toString());
        sb.delete(0, sb.length());
        LOGGER.info("-> Running Jira Query: " + query);
        try {
            Promise<SearchResult> results = client.searchJql(query);
            return StreamSupport.stream(results.claim().getIssues().spliterator(), false).map(getIssueMapper()).collect(Collectors.toList());
        } catch (RestClientException e) {
            LOGGER.warn("Exception", e);
            int statusCode = e.getStatusCode().isPresent() ? e.getStatusCode().get() : 0;
            if (statusCode == 401) {
                LOGGER.error("Error 401 connecting to JIRA server, your credentials are probably wrong. Note: Ensure you are using JIRA user name not your email address.");
                throw e;
            } else if (statusCode == 400) {
                if (ids.size() == 1) {
                    return new ArrayList<>();
                } else {
                    LOGGER.warn("Error 400 - Some issues where not found {}, keep on", ids);
                    LOGGER.warn(e.getMessage());
                    //Falling back to per issue invocation if one was not found... Why Jira o why...
                    List<IssueDTO> result = new ArrayList<>(PAGE_SIZE);
                    for (int i = firstItem; i < counter.get(); i++) {
                        result.addAll(getById(Arrays.asList(ids.get(i))).nextPage());
                    }
                    return result;
                }
            } else {
                LOGGER.error("No result was available from Jira unexpectedly - defaulting to blank response. The reason for this fault is the following:" + e.getCause());
                throw e;
            }
        }
    });
}
Also used : Counter(com.bbva.arq.devops.ae.mirrorgate.collectors.jira.support.Counter) ArrayList(java.util.ArrayList) RestClientException(com.atlassian.jira.rest.client.api.RestClientException) IssueDTO(com.bbva.arq.devops.ae.mirrorgate.core.dto.IssueDTO) SearchResult(com.atlassian.jira.rest.client.api.domain.SearchResult)

Example 2 with Counter

use of com.bbva.arq.devops.ae.mirrorgate.collectors.jira.support.Counter in project mirrorgate-jira-stories-collector by BBVA.

the class JiraIssuesServiceImpl method getRecentIssues.

@Override
public Pageable<IssueDTO> getRecentIssues() {
    final Counter page = new Counter(PAGE_SIZE);
    String query = String.format(ISSUES_QUERY_PATTERN, collectorStatusService.getLastExecutionDate().toString("yyyy-MM-dd HH:mm"), issueTypes);
    return (() -> {
        Promise<SearchResult> results = client.searchJql(query, PAGE_SIZE, page.inc(), null);
        return StreamSupport.stream(results.claim().getIssues().spliterator(), false).map(getIssueMapper()).collect(Collectors.toList());
    });
}
Also used : Counter(com.bbva.arq.devops.ae.mirrorgate.collectors.jira.support.Counter) SearchResult(com.atlassian.jira.rest.client.api.domain.SearchResult)

Aggregations

SearchResult (com.atlassian.jira.rest.client.api.domain.SearchResult)2 Counter (com.bbva.arq.devops.ae.mirrorgate.collectors.jira.support.Counter)2 RestClientException (com.atlassian.jira.rest.client.api.RestClientException)1 IssueDTO (com.bbva.arq.devops.ae.mirrorgate.core.dto.IssueDTO)1 ArrayList (java.util.ArrayList)1