How to match any issue with JQL in Bitbucket JIRA pre hook?

3 min read 06-10-2024
How to match any issue with JQL in Bitbucket JIRA pre hook?


Matching Any Issue with JQL in Bitbucket JIRA Pre-Hook: A Comprehensive Guide

Problem: You want to use a JIRA Pre-Hook in Bitbucket to perform an action based on any issue mentioned in a commit message. However, you're unsure how to construct a JQL query that effectively matches any possible issue key.

Rephrased: Imagine you want to automatically update a JIRA issue whenever a commit message mentions it. You want a way to catch all possible issue keys, no matter how they're formatted. This article will show you how to achieve this using JQL in your Bitbucket JIRA Pre-Hook.

Scenario: You're working on a project where developers are expected to link their commits to JIRA issues using the standard JIRA key format (e.g., "ABC-123"). You want to automate the process of adding a comment to the relevant JIRA issue when a commit message mentions its key.

Original Code (Example):

def pre_receive(repo, changes):
  for change in changes:
    commit_message = change.get("message")
    # Find all JIRA issue keys in the commit message
    issue_keys = re.findall(r"(?i)\b[A-Z]+-\d+\b", commit_message)
    for issue_key in issue_keys:
      # Use JQL to find the issue
      issue = jira.issue(issue_key)
      # Add a comment to the issue
      issue.add_comment("Commit message: " + commit_message)

This code uses a regular expression to identify potential issue keys but lacks a robust solution for accurately catching all possible issue formats.

Analysis and Clarification:

The key to solving this lies in understanding JQL's capabilities. While regular expressions are useful for finding patterns in strings, JQL is designed specifically for querying JIRA issues.

Key Insight: Instead of attempting to extract all possible issue keys from the commit message, you can use JQL to search for issues based on the entire commit message.

Optimized Solution:

def pre_receive(repo, changes):
  for change in changes:
    commit_message = change.get("message")
    # Use JQL to search for issues based on the commit message
    jql = f"text ~ \"{commit_message}\""
    issues = jira.search_issues(jql)
    for issue in issues:
      # Add a comment to the issue
      issue.add_comment("Commit message: " + commit_message)

Explanation:

  • JQL Query: The jql variable constructs a JQL query using the text ~ operator. This operator searches for issues where the "text" field (which includes the issue's summary, description, and comments) contains the commit message.
  • JIRA Search: The jira.search_issues function executes the JQL query and returns a list of matching issues.
  • Comment Addition: The code then iterates through the found issues and adds a comment to each one, linking the commit message.

Advantages of this approach:

  • Flexibility: This approach is more robust than relying solely on regular expressions, as it captures all issue keys regardless of their format.
  • Accuracy: JQL is specifically designed for JIRA issue querying, ensuring more reliable results.
  • Simplicity: The code is cleaner and easier to understand compared to using multiple regular expressions.

Additional Value:

  • Error Handling: You can enhance the code by adding error handling to gracefully handle situations where the JIRA search fails or no issues are found.
  • Custom Actions: You can modify the code to perform various actions based on the matching issue, such as adding a custom field or changing the issue's status.

Resources:

By implementing this solution, you can leverage the power of JQL to streamline your workflow and automatically connect your code changes to JIRA issues, making your development process more efficient and collaborative.