Files
dify-plugin-sdks/python/examples/github_trigger/triggers/pull_request_opened.py
T
Harry b182fd2668 feat(trigger): enhance GitHub trigger event handling and update version
- Refactored `TriggerInvokeResponse` to use `Mapping[str, Any]` for event representation.
- Renamed `properties` to `variables` in the `Event` class for clarity and updated the description accordingly.
- Expanded the event options in the GitHub trigger configuration to allow selection of all events.
- Removed the deprecated `issue_comment` trigger implementation and its associated files.
- Bumped version in `manifest.yaml` to 0.0.9 to reflect these changes.

These updates improve the flexibility and clarity of the GitHub trigger integration, allowing for a broader range of event handling.
2025-09-17 13:46:04 +08:00

125 lines
5.1 KiB
Python

from collections.abc import Mapping
from typing import Any
from werkzeug import Request
from dify_plugin.entities.trigger import Event
from dify_plugin.interfaces.trigger import TriggerEvent
class PullRequestOpenedTrigger(TriggerEvent):
"""
GitHub Pull Request Opened Event Trigger
This trigger handles GitHub pull request opened events and extracts relevant
information from the webhook payload to provide as variables to the workflow.
"""
def _trigger(self, request: Request, parameters: Mapping[str, Any]) -> Event:
"""
Handle GitHub pull request opened event trigger
Parameters:
- pr_filter: Filter by specific pull request number (optional)
"""
# Get the event payload
payload = request.get_json()
if not payload:
raise ValueError("No payload received")
# Verify this is an opened action
action = payload.get("action", "")
if action != "opened":
# This trigger only handles opened events
return Event(variables={})
# Extract pull request information
pull_request = payload.get("pull_request", {})
repository = payload.get("repository", {})
sender = payload.get("sender", {})
# Apply PR number filter if specified
pr_filter = parameters.get("pr_filter")
if pr_filter is not None:
pr_number = pull_request.get("number")
if pr_number != int(pr_filter):
# Skip this event if it doesn't match the PR filter
return Event(variables={})
# Extract labels
labels = [
{
"name": label.get("name", ""),
"color": label.get("color", ""),
"description": label.get("description", ""),
}
for label in pull_request.get("labels", [])
]
# Build variables for the workflow
variables = {
"pull_request": {
"number": pull_request.get("number"),
"title": pull_request.get("title", ""),
"body": pull_request.get("body", ""),
"state": pull_request.get("state", ""),
"html_url": pull_request.get("html_url", ""),
"created_at": pull_request.get("created_at", ""),
"updated_at": pull_request.get("updated_at", ""),
"draft": pull_request.get("draft", False),
"merged": pull_request.get("merged", False),
"mergeable": pull_request.get("mergeable"),
"labels": labels,
"assignees": [
{
"login": assignee.get("login", ""),
"avatar_url": assignee.get("avatar_url", ""),
"html_url": assignee.get("html_url", ""),
}
for assignee in pull_request.get("assignees", [])
],
"requested_reviewers": [
{
"login": reviewer.get("login", ""),
"avatar_url": reviewer.get("avatar_url", ""),
"html_url": reviewer.get("html_url", ""),
}
for reviewer in pull_request.get("requested_reviewers", [])
],
"author": {
"login": pull_request.get("user", {}).get("login", ""),
"avatar_url": pull_request.get("user", {}).get("avatar_url", ""),
"html_url": pull_request.get("user", {}).get("html_url", ""),
},
"head": {
"ref": pull_request.get("head", {}).get("ref", ""),
"sha": pull_request.get("head", {}).get("sha", ""),
"repo_name": pull_request.get("head", {}).get("repo", {}).get("full_name", ""),
},
"base": {
"ref": pull_request.get("base", {}).get("ref", ""),
"sha": pull_request.get("base", {}).get("sha", ""),
"repo_name": pull_request.get("base", {}).get("repo", {}).get("full_name", ""),
},
},
"repository": {
"name": repository.get("name", ""),
"full_name": repository.get("full_name", ""),
"html_url": repository.get("html_url", ""),
"description": repository.get("description", ""),
"private": repository.get("private", False),
"owner": {
"login": repository.get("owner", {}).get("login", ""),
"avatar_url": repository.get("owner", {}).get("avatar_url", ""),
"html_url": repository.get("owner", {}).get("html_url", ""),
},
},
"sender": {
"login": sender.get("login", ""),
"avatar_url": sender.get("avatar_url", ""),
"html_url": sender.get("html_url", ""),
"type": sender.get("type", ""),
},
}
return Event(variables=variables)