Skip to content

Commit b41c845

Browse files
committed
EX-329: fix and update jira logic v5
1 parent ab702e0 commit b41c845

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

src/code_review_agent/cli.py

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,53 @@ def prioritize_changed_files(changed_files_map: Dict[str, str]) -> List[List[str
3333
tier3.append(path)
3434
return [tier1, tier2, tier3]
3535

36-
def _get_task_id_from_git_info(commit_messages: str) -> str | None:
36+
def _get_task_id_from_git_info(commit_messages) -> str | None:
3737
"""
38-
Finds a Jira task ID by searching in common CI/CD environment variables and commit messages.
39-
Prioritizes branch names over commit messages.
38+
Finds a Jira task ID by searching (in order):
39+
1. Branch name (GITHUB_HEAD_REF / BITBUCKET_BRANCH)
40+
2. Explicit BITBUCKET_COMMIT_MESSAGE (if provided)
41+
3. Supplied commit messages (list or str)
42+
4. (Fallback) last 10 commit messages (git log) if still not found
4043
"""
41-
4244
branch_name = os.environ.get("GITHUB_HEAD_REF") or os.environ.get("BITBUCKET_BRANCH", "")
4345
if branch_name:
4446
task_id = jira_client.find_task_id(branch_name)
4547
if task_id:
4648
logging.info(f"Found Jira task ID '{task_id}' in branch name.")
4749
return task_id
4850

49-
commit_text = " ".join(commit_messages)
50-
task_id = jira_client.find_task_id(commit_text)
51+
commit_msg_env = os.environ.get("BITBUCKET_COMMIT_MESSAGE", "")
52+
if commit_msg_env:
53+
task_id = jira_client.find_task_id(commit_msg_env)
54+
if task_id:
55+
logging.info("Found Jira task ID in BITBUCKET_COMMIT_MESSAGE.")
56+
return task_id
57+
58+
if isinstance(commit_messages, (list, tuple)):
59+
joined = " ".join(m for m in commit_messages if m)
60+
else:
61+
joined = str(commit_messages)
62+
63+
task_id = jira_client.find_task_id(joined)
5164
if task_id:
52-
logging.info(f"Found Jira task ID '{task_id}' in commit messages.")
65+
logging.info("Found Jira task ID in provided commit messages.")
5366
return task_id
54-
55-
logging.info("No Jira task ID found in branch name or commit messages.")
67+
68+
try:
69+
import subprocess
70+
log_output = subprocess.check_output(
71+
["git", "log", "-n", "10", "--pretty=%B"],
72+
text=True,
73+
stderr=subprocess.DEVNULL
74+
)
75+
task_id = jira_client.find_task_id(log_output)
76+
if task_id:
77+
logging.info("Found Jira task ID in last 10 commits fallback.")
78+
return task_id
79+
except Exception as e:
80+
logging.debug(f"Fallback git log scan failed: {e}")
81+
82+
logging.info("No Jira task ID found in branch name, env commit message, commit range or fallback log.")
5683
return None
5784

5885
def filter_test_files(
@@ -380,6 +407,7 @@ def assess(
380407

381408
if task_id:
382409
task_details = jira_client.get_task_details(task_id)
410+
jira_details_text = ""
383411
if task_details:
384412
jira_details_text = (
385413
f"**--- JIRA TASK CONTEXT ({task_id}) ---**\n"
@@ -390,24 +418,24 @@ def assess(
390418
logging.info(f"✅ Successfully fetched context from Jira task {task_id}.")
391419
else:
392420
logging.warning(f"Found Jira task ID '{task_id}', but could not fetch its details.")
393-
394-
logging.info("\n--- Assessing Task Relevance ---")
395-
396-
relevance = relevance_assessor.assess_relevance(
397-
jira_details=jira_details_text,
398-
commit_messages=commit_messages,
399-
diff_text=diff_text,
400-
review_summary="Code has been merged.",
401-
llm_config=load_config(repo_path).get('llm', {})
402-
)
403421

404-
if relevance:
405-
comment_body = (
422+
logging.info("\n--- Assessing Task Relevance ---")
423+
relevance = relevance_assessor.assess_relevance(
424+
jira_details=jira_details_text,
425+
commit_messages=commit_messages,
426+
diff_text=diff_text,
427+
review_summary="Code has been merged.",
428+
llm_config=load_config(repo_path).get('llm', {})
429+
)
430+
if relevance:
431+
comment_body = (
406432
f"🤖 **AI Assessment Complete for this PR**\n\n"
407433
f"/!\\ The code changes have a **{relevance.score}%** relevance score to this task.\n\n"
408434
f"**Justification:** {relevance.justification}"
409435
)
410-
jira_client.add_comment(task_id, comment_body)
436+
jira_client.add_comment(task_id, comment_body)
437+
else:
438+
logging.info("No task ID found; skipping relevance assessment.")
411439

412440

413441
def main():

src/code_review_agent/jira_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ def find_task_id(text: str) -> str | None:
1010
"""Finds a Jira-like task ID (e.g., ABC-123) in a string."""
1111
if not text:
1212
return None
13-
14-
match = re.search(r'(?<![A-Z\d-])([A-Z]{2,4}-\d+)', text, re.IGNORECASE)
13+
match = re.search(r'(?<![A-Z\d-])([A-Z][A-Z0-9]{1,9}-\d+)', text, re.IGNORECASE)
1514
return match.group(1).upper() if match else None
1615

1716
def get_task_details(task_id: str) -> dict | None:

0 commit comments

Comments
 (0)