Hello, We are trying to create a Jira ticket when...
# ask-community
j
Hello, We are trying to create a Jira ticket whenever a failure occurs in Prefect Flow/Task. For this we are trying to import some modules and we are getting module not found error for the below.
Copy code
from prefect.engine.state import FAIL
from prefect.tasks.control_flow import ifelse
from prefect.utilities.tasks import defaults_from_attrs
environment details Prefect Version: 2.8.7 Do we need to install any additional packages?
Below is the sample code
Copy code
from prefect import Task
from jira import JIRA
from prefect import Flow, task
from prefect.engine.state import FAIL
from prefect.tasks.control_flow import ifelse
from prefect.utilities.tasks import defaults_from_attrs


class JiraTask(Task):
    def __init__(self, url, username, password, project, issue_type, summary, description, **kwargs):
        self.url = url
        self.username = username
        self.password = password
        self.project = project
        self.issue_type = issue_type
        self.summary = summary
        self.description = description
        super().__init__(**kwargs)

    @defaults_from_attrs("url", "username", "password", "project", "issue_type", "summary", "description")
    def run(self, task, **kwargs):
        if task.is_failed():
            jira = JIRA(server=self.url, basic_auth=(self.username, self.password))
            issue_dict = {
                "project": {"key": self.project},
                "summary": self.summary,
                "description": self.description,
                "issuetype": {"name": self.issue_type},
            }
            jira.create_issue(fields=issue_dict)
        return None


jira_task = JiraTask(
    url="<https://jira4project.atlassian.net>",
    username="username",
    password="password",
    project="project",
    issue_type="Bug",
    summary="A task in my flow failed",
    description="Task {task_name} in flow {flow_name} failed with error: {error_message}",
)


@task
def task1():
    # Some task code here
    raise FAIL("Task 1 failed")


@task
def task2():
    # Some task code here
    pass


with Flow("Jira-Flow") as flow:
    res1 = task1()
    res2 = ifelse(res1, task2, jira_task, upstream_tasks=[res1])

flow.run()
z
Hey! it looks like you’re using Prefect 1 syntax but you said you’re using Prefect 2?
j
Hi @Zanie, Yes I am using Prefect 2, I was under an impression that this is the generic syntax. Can you please help me with the reference documents which I can follow for Prefect 2. My use case is to create a Jira ticket whenever a Task/Flow fails in Prefect. Thanks
z
Here’s the Prefect 2 documentation https://docs.prefect.io/tutorials/first-steps/
j
Thanks @Zanie, this information helps. Will get back in case of any issues