diff --git a/exporters/failure/collector_jira.py b/exporters/failure/collector_jira.py index 263e902d2..a8315be08 100755 --- a/exporters/failure/collector_jira.py +++ b/exporters/failure/collector_jira.py @@ -25,6 +25,10 @@ import pelorus from failure.collector_base import AbstractFailureCollector, TrackerIssue +JIRA_SEARCH_RESULTS = 50 # One query limit, exporter will query +# multiple times. Do not exceed 100 as +# JIRA won't return more. + class JiraFailureCollector(AbstractFailureCollector): """ @@ -60,16 +64,32 @@ def _connect_to_jira(self) -> JIRA: def search_issues(self): jira = self._connect_to_jira() - # TODO FIXME This may need to be modified to suit needs and have a time period. - query_string = "type=bug and priority=highest" - if self.projects is not None: - query_string = query_string + " and project in (" + self.projects + ")" - jira_issues = jira.search_issues(query_string) critical_issues = [] try: - jira_issues = jira.search_issues(query_string) + # TODO FIXME This may need to be modified to suit needs and have a time period. + query_string = "type=bug and priority=highest" + query_result_fields = "summary,labels,created,resolutiondate" + + if self.projects is not None: + query_string = query_string + " AND project in (" + self.projects + ")" + + jira_issues = [] + start_at = 0 + while True: + jira_search_results = jira.search_issues( + query_string, + startAt=start_at, + maxResults=JIRA_SEARCH_RESULTS, + fields=query_result_fields, + ) + jira_issues += jira_search_results.iterable + start_at += JIRA_SEARCH_RESULTS + logging.info("Getting jira results: %s" % start_at) + if start_at >= jira_search_results.total: + break + for issue in jira_issues: logging.debug(issue) logging.debug( diff --git a/exporters/tests/test_failure_exporter.py b/exporters/tests/test_failure_exporter.py index ae58edeeb..6c378fc47 100644 --- a/exporters/tests/test_failure_exporter.py +++ b/exporters/tests/test_failure_exporter.py @@ -16,9 +16,11 @@ # from typing import cast + import pytest from jira.exceptions import JIRAError from prometheus_client.core import REGISTRY + from failure.app import TrackerFactory from failure.collector_jira import JiraFailureCollector @@ -80,6 +82,7 @@ def test_jira_prometheus_register( ): def mock_search_issues(self): return [] + monkeypatch.setattr(JiraFailureCollector, "search_issues", mock_search_issues) collector = JiraFailureCollector( user=username, apikey=apikey, server=server, projects=None @@ -100,7 +103,7 @@ def mock_search_issues(self): ) def test_jira_exception(server, username, apikey, monkeypatch: pytest.MonkeyPatch): class FakeJira(object): - def search_issues(self, issues): + def search_issues(self, issues, startAt=0, maxResults=50, fields=None): raise JIRAError(status_code=400, text="Fake search error") def mock_jira_connect(self):