forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/tools/gitlab (langgenius#10407)
- Loading branch information
Showing
7 changed files
with
320 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
api/core/tools/provider/builtin/gitlab/tools/gitlab_mergerequests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import urllib.parse | ||
from typing import Any, Union | ||
|
||
import requests | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class GitlabMergeRequestsTool(BuiltinTool): | ||
def _invoke( | ||
self, user_id: str, tool_parameters: dict[str, Any] | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
repository = tool_parameters.get("repository", "") | ||
branch = tool_parameters.get("branch", "") | ||
start_time = tool_parameters.get("start_time", "") | ||
end_time = tool_parameters.get("end_time", "") | ||
state = tool_parameters.get("state", "opened") # Default to "opened" | ||
|
||
if not repository: | ||
return self.create_text_message("Repository is required") | ||
|
||
access_token = self.runtime.credentials.get("access_tokens") | ||
site_url = self.runtime.credentials.get("site_url") | ||
|
||
if not access_token: | ||
return self.create_text_message("Gitlab API Access Tokens is required.") | ||
if not site_url: | ||
site_url = "https://gitlab.com" | ||
|
||
# Get merge requests | ||
result = self.get_merge_requests(site_url, access_token, repository, branch, start_time, end_time, state) | ||
|
||
return [self.create_json_message(item) for item in result] | ||
|
||
def get_merge_requests( | ||
self, site_url: str, access_token: str, repository: str, branch: str, start_time: str, end_time: str, state: str | ||
) -> list[dict[str, Any]]: | ||
domain = site_url | ||
headers = {"PRIVATE-TOKEN": access_token} | ||
results = [] | ||
|
||
try: | ||
# URL encode the repository path | ||
encoded_repository = urllib.parse.quote(repository, safe="") | ||
merge_requests_url = f"{domain}/api/v4/projects/{encoded_repository}/merge_requests" | ||
params = {"state": state} | ||
|
||
# Add time filters if provided | ||
if start_time: | ||
params["created_after"] = start_time | ||
if end_time: | ||
params["created_before"] = end_time | ||
|
||
response = requests.get(merge_requests_url, headers=headers, params=params) | ||
response.raise_for_status() | ||
merge_requests = response.json() | ||
|
||
for mr in merge_requests: | ||
# Filter by target branch | ||
if branch and mr["target_branch"] != branch: | ||
continue | ||
|
||
results.append( | ||
{ | ||
"id": mr["id"], | ||
"title": mr["title"], | ||
"author": mr["author"]["name"], | ||
"web_url": mr["web_url"], | ||
"target_branch": mr["target_branch"], | ||
"created_at": mr["created_at"], | ||
"state": mr["state"], | ||
} | ||
) | ||
except requests.RequestException as e: | ||
print(f"Error fetching merge requests from GitLab: {e}") | ||
|
||
return results |
77 changes: 77 additions & 0 deletions
77
api/core/tools/provider/builtin/gitlab/tools/gitlab_mergerequests.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
identity: | ||
name: gitlab_mergerequests | ||
author: Leo.Wang | ||
label: | ||
en_US: GitLab Merge Requests | ||
zh_Hans: GitLab 合并请求查询 | ||
description: | ||
human: | ||
en_US: A tool for query GitLab merge requests, Input should be a exists reposity or branch. | ||
zh_Hans: 一个用于查询 GitLab 代码合并请求的工具,输入的内容应该是一个已存在的仓库名或者分支。 | ||
llm: A tool for query GitLab merge requests, Input should be a exists reposity or branch. | ||
parameters: | ||
- name: repository | ||
type: string | ||
required: false | ||
label: | ||
en_US: repository | ||
zh_Hans: 仓库路径 | ||
human_description: | ||
en_US: repository | ||
zh_Hans: 仓库路径,以namespace/project_name的形式。 | ||
llm_description: Repository path for GitLab, like namespace/project_name. | ||
form: llm | ||
- name: branch | ||
type: string | ||
required: false | ||
label: | ||
en_US: branch | ||
zh_Hans: 分支名 | ||
human_description: | ||
en_US: branch | ||
zh_Hans: 分支名 | ||
llm_description: branch for GitLab | ||
form: llm | ||
- name: start_time | ||
type: string | ||
required: false | ||
label: | ||
en_US: start_time | ||
zh_Hans: 开始时间 | ||
human_description: | ||
en_US: start_time | ||
zh_Hans: 开始时间 | ||
llm_description: Start time for GitLab | ||
form: llm | ||
- name: end_time | ||
type: string | ||
required: false | ||
label: | ||
en_US: end_time | ||
zh_Hans: 结束时间 | ||
human_description: | ||
en_US: end_time | ||
zh_Hans: 结束时间 | ||
llm_description: End time for GitLab | ||
form: llm | ||
- name: state | ||
type: select | ||
required: false | ||
options: | ||
- value: opened | ||
label: | ||
en_US: opened | ||
zh_Hans: 打开 | ||
- value: closed | ||
label: | ||
en_US: closed | ||
zh_Hans: 关闭 | ||
default: opened | ||
label: | ||
en_US: state | ||
zh_Hans: 变更状态 | ||
human_description: | ||
en_US: state | ||
zh_Hans: 变更状态 | ||
llm_description: Merge request state type for GitLab | ||
form: llm |
Oops, something went wrong.