|
| 1 | +import type { GitLabCreateBranchParams, GitLabCreateBranchResponse } from '@/tools/gitlab/types' |
| 2 | +import { getGitLabApiBase } from '@/tools/gitlab/utils' |
| 3 | +import type { ToolConfig } from '@/tools/types' |
| 4 | + |
| 5 | +export const gitlabCreateBranchTool: ToolConfig< |
| 6 | + GitLabCreateBranchParams, |
| 7 | + GitLabCreateBranchResponse |
| 8 | +> = { |
| 9 | + id: 'gitlab_create_branch', |
| 10 | + name: 'GitLab Create Branch', |
| 11 | + description: 'Create a new branch in a GitLab project repository', |
| 12 | + version: '1.0.0', |
| 13 | + |
| 14 | + params: { |
| 15 | + accessToken: { |
| 16 | + type: 'string', |
| 17 | + required: true, |
| 18 | + visibility: 'user-only', |
| 19 | + description: 'GitLab Personal Access Token', |
| 20 | + }, |
| 21 | + host: { |
| 22 | + type: 'string', |
| 23 | + required: false, |
| 24 | + visibility: 'user-only', |
| 25 | + description: 'Self-managed GitLab host (e.g. gitlab.example.com). Defaults to gitlab.com.', |
| 26 | + }, |
| 27 | + projectId: { |
| 28 | + type: 'string', |
| 29 | + required: true, |
| 30 | + visibility: 'user-or-llm', |
| 31 | + description: 'Project ID or URL-encoded path', |
| 32 | + }, |
| 33 | + branch: { |
| 34 | + type: 'string', |
| 35 | + required: true, |
| 36 | + visibility: 'user-or-llm', |
| 37 | + description: 'Name of the new branch', |
| 38 | + }, |
| 39 | + ref: { |
| 40 | + type: 'string', |
| 41 | + required: true, |
| 42 | + visibility: 'user-or-llm', |
| 43 | + description: 'Source branch/tag/SHA', |
| 44 | + }, |
| 45 | + }, |
| 46 | + |
| 47 | + request: { |
| 48 | + url: (params) => { |
| 49 | + const encodedId = encodeURIComponent(String(params.projectId).trim()) |
| 50 | + const queryParams = new URLSearchParams() |
| 51 | + queryParams.append('branch', String(params.branch)) |
| 52 | + queryParams.append('ref', String(params.ref)) |
| 53 | + return `${getGitLabApiBase(params.host)}/projects/${encodedId}/repository/branches?${queryParams.toString()}` |
| 54 | + }, |
| 55 | + method: 'POST', |
| 56 | + headers: (params) => ({ |
| 57 | + 'PRIVATE-TOKEN': params.accessToken, |
| 58 | + }), |
| 59 | + }, |
| 60 | + |
| 61 | + transformResponse: async (response) => { |
| 62 | + if (!response.ok) { |
| 63 | + const errorText = await response.text() |
| 64 | + return { |
| 65 | + success: false, |
| 66 | + error: `GitLab API error: ${response.status} ${errorText}`, |
| 67 | + output: {}, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const data = await response.json() |
| 72 | + |
| 73 | + return { |
| 74 | + success: true, |
| 75 | + output: { |
| 76 | + name: data.name ?? null, |
| 77 | + webUrl: data.web_url ?? null, |
| 78 | + protected: data.protected ?? null, |
| 79 | + commit: data.commit ?? null, |
| 80 | + }, |
| 81 | + } |
| 82 | + }, |
| 83 | + |
| 84 | + outputs: { |
| 85 | + name: { |
| 86 | + type: 'string', |
| 87 | + description: 'The created branch name', |
| 88 | + }, |
| 89 | + webUrl: { |
| 90 | + type: 'string', |
| 91 | + description: 'The web URL of the branch', |
| 92 | + }, |
| 93 | + protected: { |
| 94 | + type: 'boolean', |
| 95 | + description: 'Whether the branch is protected', |
| 96 | + }, |
| 97 | + commit: { |
| 98 | + type: 'object', |
| 99 | + description: 'The commit the branch points to', |
| 100 | + }, |
| 101 | + }, |
| 102 | +} |
0 commit comments