mirror of
https://github.com/langgenius/dify-plugin-sdks.git
synced 2026-07-24 13:06:56 -04:00
feat(trigger): enhance GitHub trigger with multiple event selection and dynamic parameter options
- Updated `IssueCommentTrigger` and `GithubProvider` to support multiple event selections for GitHub triggers. - Introduced a new `multiple` field in the trigger parameters to allow users to select multiple events. - Implemented dynamic fetching of repository options based on OAuth credentials. - Bumped version in `manifest.yaml` to 0.0.7 to reflect these enhancements. This update improves the flexibility and usability of the GitHub trigger integration.
This commit is contained in:
@@ -36,4 +36,4 @@ resource:
|
||||
tags:
|
||||
- utilities
|
||||
type: plugin
|
||||
version: 0.0.6
|
||||
version: 0.0.7
|
||||
|
||||
@@ -8,6 +8,7 @@ from typing import Any
|
||||
import uuid
|
||||
|
||||
from dify_plugin.entities.oauth import TriggerOAuthCredentials
|
||||
from examples.github_trigger.utils.dynamic_options import fetch_repositories
|
||||
import requests
|
||||
from werkzeug import Request, Response
|
||||
|
||||
@@ -283,6 +284,9 @@ class GithubProvider(TriggerProvider):
|
||||
)
|
||||
|
||||
def _fetch_parameter_options(self, parameter: str) -> list[ParameterOption]:
|
||||
if parameter == "repository":
|
||||
return fetch_repositories(self.runtime.credentials.get("access_tokens"))
|
||||
|
||||
return [
|
||||
ParameterOption(
|
||||
value="iamjoel",
|
||||
|
||||
@@ -73,9 +73,10 @@ subscription_schema:
|
||||
zh_Hans: "GitHub 仓库,格式为 owner/repo(例如:microsoft/vscode)"
|
||||
pt_BR: "Repositório do GitHub no formato owner/repo (ex: microsoft/vscode)"
|
||||
- name: "events"
|
||||
type: "dynamic-select"
|
||||
type: "select"
|
||||
required: true
|
||||
default: "issue_comment"
|
||||
default: ["issue_comment", "issues"]
|
||||
multiple: true
|
||||
options:
|
||||
- value: "issue_comment"
|
||||
label:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from dify_plugin.entities import ParameterOption
|
||||
from examples.github_trigger.utils.dynamic_options import fetch_repositories
|
||||
from werkzeug import Request
|
||||
|
||||
from dify_plugin.entities.trigger import Event
|
||||
@@ -120,3 +122,9 @@ class IssueCommentTrigger(TriggerEvent):
|
||||
}
|
||||
|
||||
return Event(variables=variables)
|
||||
|
||||
def _fetch_parameter_options(self, parameter: str) -> list[ParameterOption]:
|
||||
if parameter == "repository":
|
||||
return fetch_repositories(self.runtime.credentials.get("access_tokens"))
|
||||
|
||||
return []
|
||||
|
||||
@@ -17,6 +17,35 @@ description:
|
||||
pt_BR: Este gatilho é ativado quando um comentário é criado, editado ou deletado em uma issue ou pull request do GitHub, fornecendo informações sobre o comentário, issue e usuário.
|
||||
|
||||
parameters:
|
||||
- name: "events"
|
||||
type: "select"
|
||||
required: true
|
||||
default: ["issue_comment", "issues"]
|
||||
multiple: true
|
||||
options:
|
||||
- value: "issue_comment"
|
||||
label:
|
||||
en_US: "Issue Comments"
|
||||
zh_Hans: "Issue 评论"
|
||||
- value: "issues"
|
||||
label:
|
||||
en_US: "Issues"
|
||||
zh_Hans: "Issues"
|
||||
- value: "pull_request"
|
||||
label:
|
||||
en_US: "Pull Requests"
|
||||
zh_Hans: "Pull Requests"
|
||||
- value: "push"
|
||||
label:
|
||||
en_US: "Push"
|
||||
zh_Hans: "推送"
|
||||
label:
|
||||
zh_Hans: "监听事件"
|
||||
en_US: "Events to Listen"
|
||||
help:
|
||||
en_US: "Select which GitHub events to subscribe to"
|
||||
pt_BR: "Selecione quais eventos do GitHub inscrever-se"
|
||||
zh_Hans: "选择要订阅的 GitHub 事件"
|
||||
- name: action_filter
|
||||
label:
|
||||
en_US: Action Filter
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
from dify_plugin.entities import I18nObject, ParameterOption
|
||||
import requests
|
||||
|
||||
|
||||
def fetch_repositories(access_token: str) -> list[ParameterOption]:
|
||||
if not access_token:
|
||||
raise ValueError("access_tokens is required")
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
"Accept": "application/vnd.github+json",
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
}
|
||||
|
||||
options: list[ParameterOption] = []
|
||||
per_page = 100
|
||||
page = 1
|
||||
|
||||
while True:
|
||||
params = {
|
||||
"per_page": per_page,
|
||||
"page": page,
|
||||
# Include all repositories the user can access
|
||||
"affiliation": "owner,collaborator,organization_member",
|
||||
"sort": "full_name",
|
||||
"direction": "asc",
|
||||
}
|
||||
|
||||
response = requests.get("https://api.github.com/user/repos", headers=headers, params=params, timeout=10)
|
||||
|
||||
if response.status_code != 200:
|
||||
try:
|
||||
err = response.json()
|
||||
message = err.get("message", str(err))
|
||||
except Exception:
|
||||
message = response.text
|
||||
raise ValueError(f"Failed to fetch repositories from GitHub: {message}")
|
||||
|
||||
repos = response.json() or []
|
||||
if not isinstance(repos, list):
|
||||
raise ValueError("Unexpected response format from GitHub API when fetching repositories")
|
||||
|
||||
for repo in repos:
|
||||
full_name = repo.get("full_name") # e.g., owner/repo
|
||||
owner = repo.get("owner") or {}
|
||||
avatar_url = owner.get("avatar_url")
|
||||
if full_name:
|
||||
options.append(
|
||||
ParameterOption(
|
||||
value=full_name,
|
||||
label=I18nObject(en_US=full_name),
|
||||
icon=avatar_url,
|
||||
)
|
||||
)
|
||||
|
||||
# pagination break condition
|
||||
if len(repos) < per_page:
|
||||
break
|
||||
|
||||
page += 1
|
||||
|
||||
return options
|
||||
Reference in New Issue
Block a user