DEV Community

Cover image for Create Jira Ticket on Prefect Task Failure
Falk
Falk

Posted on

Create Jira Ticket on Prefect Task Failure

Prefect offers a great UI to monitor and manage your Prefect runs. And there is nothing better than opening your laptop in the morning and seeing all green when you look at it. However, that's unfortunately not always the case and, even though this depends on your organization's workflows, usually you will have to create a bug ticket in Jira before attempting to resolve any issues in your runs. Let's automate this by integrating the jira_notifier in our tasks.

Prerequisites

First, we'll need to create a Jira API token. You can do this by following this URL, logging in, and then click the Create API token button. Write down the token and store it somewhere safe, we'll need it in a second.

Image description

Next we'll have to pass the token we just created, the Jira user and the Jira server URL on to Prefect. One easy way to do this is to add the following to your Prefect configuration file (~/.prefect/config.toml):

[context.secrets]
JIRASECRETS.JIRATOKEN = "thetokenyoucreated"
JIRASECRETS.JIRAUSER = "xxx@example.com"
JIRASECRETS.JIRASERVER = "https://xxx.atlassian.net"
Enter fullscreen mode Exit fullscreen mode

Configure the Prefect Task

Now we can add the jira_notifier to our Prefect task:

from prefect import Task
from prefect.utilities.notifications.jira_notification import jira_notifier
from prefect.engine.state import Failed

@task(
    name="jira_example_task",
    state_handlers=[
        jira_notifier(
            only_states=[Failed],
            options={"project": "DE", "issuetype": {"name": "Bug"}},
        )
    ],
)
def add(x, y):
    return x + y
Enter fullscreen mode Exit fullscreen mode

This example task will create a Jira Bug ticket in the DE project when the run fails.
One thing to keep in mind is that the issue type you're selecting must be available in the project you're creating the issue in. You can also change the state to, for example, Retrying to create a Jira Task ticket instead. An overview of the various states is available here.

The created ticket will look like this:
Image description

Top comments (0)