Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit a signal when a ticket is updated #1169

Merged
merged 16 commits into from Apr 24, 2024

Conversation

samsplunks
Copy link
Contributor

@samsplunks samsplunks commented Apr 16, 2024

I have a use case where I need to follow the modifications of the tickets.

I unfortunately cannot hook to the post_save signal of the FollowUp model because at this time, the TicketChange and Ticket models are not updated yet with new data.

Someone added the notify_followup_webhooks function to update_ticket for a similar purpose.

In this PR, I propose to have the update_ticket function emit a signal when its process is done.
This way, anyone can hook post processing workflows outside of helpdesk.

Not to have duplicate code logic, I move the notify_followup_webhooks outside of update_ticket and have it listen to the ticket_update_done signal the same way someone would do it outside of helpdesk.

@samsplunks
Copy link
Contributor Author

Also added the signal to email.py, this way notify_followup_webhooks is only triggered through signal.

Copy link
Collaborator

@uhurusurfa uhurusurfa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - can you add a basic unit test to f=verify the webhook feature?

@timthelion
Copy link
Collaborator

I already included tests for webhooks 🙂https://github.com/django-helpdesk/django-helpdesk/blob/main/helpdesk/tests/test_webhooks.py

There is a second webhook that is triggered on ticket creation. It would be nice to go all the way and wire that through the signals as well.

@samsplunks
Copy link
Contributor Author

Yes, I used the test_webhooks to verify compliance, thanks Timothy!

I knew you were going to ask. ;)

@samsplunks
Copy link
Contributor Author

samsplunks commented Apr 17, 2024

The new ticket webhook is activated with a specific role, where a signal should be emitted no matter what.
And I don't really understand the relation between new_ticket_cc and the webhook activation, could you explain it?

        if "new_ticket_cc" in roles:
            send_new_ticket_webhook(self)

Anyway, what I would do is place the signal close to the new ticket creation, which I think is the forms.TicketForm.save() method before self._send_messages (also for PublicTicketForm):
https://github.com/django-helpdesk/django-helpdesk/blob/main/helpdesk/forms.py#L421
https://github.com/django-helpdesk/django-helpdesk/blob/main/helpdesk/forms.py#L510

And use ticket._state.adding to check whether the ticket was created or modified.
The signal would be emitted at every ticket modification but the webhook would be activated at ticket creation.

Does that sound OK to you?

--

Looks like I don't need the created parameter because TicketForm is only used for the CreateTicketView.

@samsplunks
Copy link
Contributor Author

Looks good to me.

Note that the send_new_ticket_webhook is now disconnected from the new_ticket_cc role.

@timthelion
Copy link
Collaborator

I put it in the "new_ticket_cc" location because the tickets are not created with the form when they are processed in email.py if I recall correctly.

@samsplunks
Copy link
Contributor Author

Thank you.
I had the same problem with email.py and fixed it by adding the signal to it.

helpdesk/signals.py Show resolved Hide resolved
docs/webhooks.rst Outdated Show resolved Hide resolved
Co-authored-by: Benbb96 <Benbb96@users.noreply.github.com>
@timthelion
Copy link
Collaborator

@uhurusurfa I think this can be merged now.

@uhurusurfa uhurusurfa merged commit 1ddb2a2 into django-helpdesk:main Apr 24, 2024
8 checks passed
@samsplunks samsplunks deleted the signal branch April 25, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants