I want to add webhook to issues in jira using API but parent clause is not supported

Username November 27, 2024

In below code parent clause in jql_filter is not supported even though it works in jira account when i try to add webhook manually, why?

 

    @staticmethod
    def add_issue_webhook(project_key, issue_key):
        user_id = session.get('user_id')
        if not user_id:
            return ('Not authenticated'), 401

        user_token = UserToken.query.filter_by(user_id=user_id).first()
        if not user_token:
            return ('No tokens found for the authenticated user'), 400

        access_token = user_token.access_token

        headers = {
            'Authorization': f'Bearer {access_token}',
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

        # Get cloud_id
        resources_response = requests.get(resources_url, headers=headers)

        if resources_response.status_code != 200:
            return ('Failed to fetch accessible resources.', resources_response.text), resources_response.status_code

        try:
            resources = resources_response.json()
        except ValueError:
            return ('Failed to parse resources response as JSON', resources_response.text), resources_response.status_code

        if not resources:
            return ('No accessible resources found for the user.'), 404

        cloud_id = resources[0].get('id')
        if not cloud_id:
            return ('Could not determine cloud_id from accessible resources.'), 400

        base_api_url = f"https://api.atlassian.com/ex/jira/{cloud_id}/rest/api/2"

        # Validate the project and issue
        project_url = f"{base_api_url}/project/{project_key}"
        project_response = requests.get(project_url, headers=headers)

        if project_response.status_code != 200:
            return ('Invalid project key. No project found with the given key'), 404

        # Validate the specific issue
        issue_url = f"{base_api_url}/issue/{issue_key}"
        issue_response = requests.get(issue_url, headers=headers)

        if issue_response.status_code != 200:
            return ('Invalid issue key. No issue found with the given key in the project'), 404

        # Check if a webhook already exists for this target and user
        existing_webhook = WebhookInfo.query.filter_by(
            user_id=user_id,
            target_id=project_key,
            target_issue_key=issue_key
        ).first()
        if existing_webhook:
            return ('Webhook already exists for the given project and issue'), 400

        # Create JQL filter specific to the issue
        jql_filter = f'parent = "{issue_key}"'

        webhook_url = Config.WEBHOOK_URL
        # Prepare the payload
        data = {
            "url": webhook_url,
            "webhooks":[
                {
                "events": [
                    "jira:issue_created",
                    "jira:issue_updated"
                ],
                "jqlFilter": jql_filter,
                "enabled": True
                }
            ]
        }
        create_webhook_url = f"{base_api_url}/webhook"
       
        # Send the request to create the webhook
        response = requests.post(create_webhook_url, headers=headers, json=data)
       
        if response.status_code not in [200, 201]:
            try:
                error_details = response.json()
            except ValueError:
                error_details = response.text
            return ('Failed to create webhook', error_details), response.status_code

        try:
            response_data = response.json()
        except ValueError:
            return ('Failed to parse webhook creation response as JSON', response.text), response.status_code

        webhook_results = response_data.get('webhookRegistrationResult', [])
        print(response_data)
        if webhook_results:
            webhook_id = webhook_results[0].get('createdWebhookId')
        else:
            webhook_id = None

        if not webhook_id:
            return ('No webhook id returned'), 500

        # Store webhook information in the database
        webhook_info = WebhookInfo(
            user_id=user_id,
            target_id=project_key,
            webhook_id=webhook_id,
            target_type='issue',
            target_issue_key=issue_key
        )
        db.session.add(webhook_info)
        db.session.commit()
        logging.info(f"WebhookInfo added for issue: {webhook_info}")
        return ('Webhook created successfully for specific issue', webhook_id), 201

1 answer

0 votes
Jim Knepley - ReleaseTEAM
Atlassian Partner
November 27, 2024

Welcome to the community, @Username 

I see some conflicting API references online. You might try something like this example and see if it's better:

{
"name": "my first webhook via rest",
"description": "description of my first webhook",
"url": "https://www.example.com/webhooks",
"events": [
"jira:issue_created",
"jira:issue_updated"
],
"filters": {
"issue-related-events-section": "Project = JRA AND resolution = Fixed"
},
"excludeBody" : false,
"secret" : "G8j4166a5OkXRD4WbqV3"
}

Note the "filters" dictionary instead of the "jqlFilter" string. Both forms are referenced in the documentation.

 

ref:  https://developer.atlassian.com/cloud/jira/platform/webhooks/

Username November 27, 2024

actually I am using OAuth2.0 app and for that, i need jql_filter else it shows error [{'errors': ['Empty JQL search not supported']}]},

I am trying to add webhook to specific issue inside project then whenever child issue is created in that issue i want that event to trigger webhook and currently i am using 

 

jql_filter = f'project = "{project_key}" AND issuekey = "{issue_key}"'
but in this webhook is triggered when selected issue is updated but doesnt when any child issue is created, is there a way to get triggers when child issue are created too

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events