Skip to content

Commit cb9ee43

Browse files
committed
perf: skip Jira client init when no keys are present
extract_jira_tickets() built the Jira client before checking whether the text contained any Jira keys. Since add_jira_tickets() runs for every provider, a configured-but-unreferenced Jira would construct a client (and log an init failure if misconfigured) on every keyless PR. Extract keys first and return early when there are none, so the client is only built when there is something to fetch. Co-Authored-By: Claude
1 parent 3d539bb commit cb9ee43

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

pr_agent/tools/ticket_pr_compliance_check.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ def extract_jira_tickets(text, max_characters=MAX_TICKET_CHARACTERS):
8282
"""
8383
Find Jira ticket keys in the given text and fetch their content. Returns a list of
8484
ticket dicts in the same shape used by the rest of the ticket-analysis flow. Returns
85-
an empty list when Jira is not configured or no keys are found.
85+
an empty list when no keys are found or when Jira is not configured.
8686
"""
87+
# Look for keys before building a client: most PRs have none, and building the
88+
# client first would do needless work (and log a noisy init failure if Jira is
89+
# misconfigured) even when there is nothing to fetch.
90+
keys = find_jira_tickets(text or "")
91+
if not keys:
92+
return []
93+
8794
jira_client = _get_jira_client()
8895
if jira_client is None:
8996
return []
@@ -93,7 +100,6 @@ def extract_jira_tickets(text, max_characters=MAX_TICKET_CHARACTERS):
93100
# instance-specific (e.g. "customfield_10127"), so it must be configured; empty
94101
# means no requirements are extracted.
95102
requirements_field = get_settings().get("JIRA.JIRA_REQUIREMENTS_FIELD", "") or ""
96-
keys = find_jira_tickets(text or "")
97103
if len(keys) > MAX_TICKETS:
98104
get_logger().info(f"Too many Jira tickets found: {len(keys)}; limiting to {MAX_TICKETS}")
99105
keys = keys[:MAX_TICKETS]

tests/unittest/test_jira_ticket_extraction.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ def test_returns_empty_when_not_configured(self):
110110
self._disable_jira()
111111
assert extract_jira_tickets("bugfix/abc-123-x") == []
112112

113+
def test_no_client_built_when_no_keys(self):
114+
"""No Jira keys in the text -> return early without constructing a client, so a
115+
keyless PR pays no client-init cost (or noisy init-failure log)."""
116+
self._configure_jira()
117+
with patch("pr_agent.tools.ticket_pr_compliance_check.Jira") as jira_cls:
118+
result = extract_jira_tickets("nothing ticket-like here")
119+
assert result == []
120+
jira_cls.assert_not_called()
121+
113122
def test_fetches_lowercase_branch_key(self):
114123
"""The whole point: a lowercased branch key is detected and fetched as upper."""
115124
self._configure_jira()

0 commit comments

Comments
 (0)