Compare commits
112 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd4a4db89c | ||
|
|
b94da1384b | ||
|
|
69e4aa00a6 | ||
|
|
3d83c18cdf | ||
|
|
41adbc460b | ||
|
|
1b806fc393 | ||
|
|
d3c4e271ef | ||
|
|
9b407fb70a | ||
|
|
60f05e5f1e | ||
|
|
df25d328d1 | ||
|
|
9ce5604e4c | ||
|
|
653781a199 | ||
|
|
67a1e1e7fe | ||
|
|
4a2935d898 | ||
|
|
6540693771 | ||
|
|
3b9236695c | ||
|
|
723a30ae23 | ||
|
|
7bbe015ea7 | ||
|
|
bb9470a259 | ||
|
|
8728c04748 | ||
|
|
4d5fa3ab2c | ||
|
|
21e4e1b42b | ||
|
|
4aacfe348a | ||
|
|
1f7392305f | ||
|
|
c3b24d65fe | ||
|
|
dcd01441c5 | ||
|
|
2dbfc62042 | ||
|
|
e851f542f5 | ||
|
|
b8f2377f47 | ||
|
|
017ca94a86 | ||
|
|
17119bcab6 | ||
|
|
8b06d7154e | ||
|
|
bdd9fb1816 | ||
|
|
058d4cd07f | ||
|
|
ba64780c2f | ||
|
|
6930c8ee30 | ||
|
|
f08720a625 | ||
|
|
f6b45fdf6e | ||
|
|
98f908d5a1 | ||
|
|
e4aa29b0f9 | ||
|
|
32eaf86426 | ||
|
|
8c028ec48b | ||
|
|
88471b5de0 | ||
|
|
e9840cf6c0 | ||
|
|
95ab3a4b73 | ||
|
|
de311344cd | ||
|
|
d7addd56c4 | ||
|
|
dc3e120e97 | ||
|
|
f33b04a3df | ||
|
|
ba07925969 | ||
|
|
5c2ff6dcb2 | ||
|
|
feaedaf604 | ||
|
|
a601d6b698 | ||
|
|
62cb6e7830 | ||
|
|
9fff996294 | ||
|
|
4c3f5149d8 | ||
|
|
eb6b5a8f92 | ||
|
|
1d9bdb5b44 | ||
|
|
093cddbcb6 | ||
|
|
5dbfe21127 | ||
|
|
b85a523983 | ||
|
|
da08718e24 | ||
|
|
44ea8969f4 | ||
|
|
94aa8dc572 | ||
|
|
05194ffc1c | ||
|
|
5c329129f8 | ||
|
|
52ccf92761 | ||
|
|
061ea86b0b | ||
|
|
f14b60fe56 | ||
|
|
94782a85b6 | ||
|
|
e94dd26b30 | ||
|
|
da49bdeb96 | ||
|
|
3f61299f72 | ||
|
|
5308fbfb2b | ||
|
|
a7061f9b64 | ||
|
|
f25cc0de8c | ||
|
|
417ef26da0 | ||
|
|
34ca5d45db | ||
|
|
796fd4682d | ||
|
|
95c036bf3a | ||
|
|
70b9ac5b80 | ||
|
|
59e699aac7 | ||
|
|
26c50d53bd | ||
|
|
7bfc596a58 | ||
|
|
966d617670 | ||
|
|
af27b685d4 | ||
|
|
fac6e1d8d1 | ||
|
|
f656c92cda | ||
|
|
af0975d93f | ||
|
|
001383142f | ||
|
|
b35919989f | ||
|
|
d0225c4c24 | ||
|
|
6993bb2b5d | ||
|
|
f1b4a208a7 | ||
|
|
d76f02a234 | ||
|
|
b2bde61882 | ||
|
|
7cfa1fa218 | ||
|
|
1fecc1df30 | ||
|
|
8dc9ed9299 | ||
|
|
1965c9830b | ||
|
|
f377f06478 | ||
|
|
02fd91da86 | ||
|
|
55f32ef4f5 | ||
|
|
c9cada1a8d | ||
|
|
a784029828 | ||
|
|
f27c4c622d | ||
|
|
df47a0c9eb | ||
|
|
e3307adbdf | ||
|
|
afada4435e | ||
|
|
6285bd2467 | ||
|
|
5bbf8e0afb | ||
|
|
0535f5bab7 |
52
.air.toml
Normal file
52
.air.toml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
root = "."
|
||||||
|
testdata_dir = "testdata"
|
||||||
|
tmp_dir = "tmp"
|
||||||
|
|
||||||
|
[build]
|
||||||
|
args_bin = ["-t", "http"]
|
||||||
|
bin = "./gitea-mcp"
|
||||||
|
cmd = "make build"
|
||||||
|
delay = 1000
|
||||||
|
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||||
|
exclude_file = []
|
||||||
|
exclude_regex = ["_test.go"]
|
||||||
|
exclude_unchanged = false
|
||||||
|
follow_symlink = false
|
||||||
|
full_bin = ""
|
||||||
|
include_dir = []
|
||||||
|
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||||
|
include_file = []
|
||||||
|
kill_delay = "0s"
|
||||||
|
log = "build-errors.log"
|
||||||
|
poll = false
|
||||||
|
poll_interval = 0
|
||||||
|
post_cmd = []
|
||||||
|
pre_cmd = []
|
||||||
|
rerun = false
|
||||||
|
rerun_delay = 500
|
||||||
|
send_interrupt = false
|
||||||
|
stop_on_error = false
|
||||||
|
|
||||||
|
[color]
|
||||||
|
app = ""
|
||||||
|
build = "yellow"
|
||||||
|
main = "magenta"
|
||||||
|
runner = "green"
|
||||||
|
watcher = "cyan"
|
||||||
|
|
||||||
|
[log]
|
||||||
|
main_only = false
|
||||||
|
silent = false
|
||||||
|
time = false
|
||||||
|
|
||||||
|
[misc]
|
||||||
|
clean_on_exit = false
|
||||||
|
|
||||||
|
[proxy]
|
||||||
|
app_port = 0
|
||||||
|
enabled = false
|
||||||
|
proxy_port = 0
|
||||||
|
|
||||||
|
[screen]
|
||||||
|
clear_on_rebuild = false
|
||||||
|
keep_scroll = true
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "Gitea MCP DevContainer",
|
"name": "Gitea MCP DevContainer",
|
||||||
"image": "mcr.microsoft.com/devcontainers/go:1.24-bookworm",
|
"image": "mcr.microsoft.com/devcontainers/go:1.24-bookworm",
|
||||||
"features": {
|
"features": {},
|
||||||
},
|
|
||||||
"customizations": {
|
"customizations": {
|
||||||
"vscode": {
|
"vscode": {
|
||||||
"settings": {},
|
"settings": {},
|
||||||
@@ -12,8 +11,9 @@
|
|||||||
"golang.go",
|
"golang.go",
|
||||||
"stylelint.vscode-stylelint",
|
"stylelint.vscode-stylelint",
|
||||||
"DavidAnson.vscode-markdownlint",
|
"DavidAnson.vscode-markdownlint",
|
||||||
"github.copilot"
|
"github.copilot",
|
||||||
|
"eamodio.gitlens"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
.dockerignore
Normal file
61
.dockerignore
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
# Git
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
.github/
|
||||||
|
.gitea/
|
||||||
|
|
||||||
|
# Docker
|
||||||
|
Dockerfile
|
||||||
|
.dockerignore
|
||||||
|
|
||||||
|
# Build artifacts
|
||||||
|
bin/
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Go specific
|
||||||
|
vendor/
|
||||||
|
go.work
|
||||||
|
|
||||||
|
# Testing
|
||||||
|
*_test.go
|
||||||
|
**/test/
|
||||||
|
**/tests/
|
||||||
|
coverage.out
|
||||||
|
coverage.html
|
||||||
|
|
||||||
|
# IDE and editor files
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
|
||||||
|
# OS specific
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
|
||||||
|
# Temporary files
|
||||||
|
tmp/
|
||||||
|
temp/
|
||||||
|
*.tmp
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Documentation
|
||||||
|
docs/
|
||||||
|
*.md
|
||||||
|
LICENSE
|
||||||
|
|
||||||
|
# Development tools
|
||||||
|
.air.toml
|
||||||
|
.golangci.yml
|
||||||
|
.goreleaser.yml
|
||||||
|
|
||||||
|
# Debug files
|
||||||
|
debug
|
||||||
|
__debug_bin
|
||||||
@@ -2,37 +2,29 @@ name: release
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags: ["*"]
|
||||||
- '*'
|
|
||||||
|
env:
|
||||||
|
GIT_SSL_NO_VERIFY: true
|
||||||
|
GOPRIVATE: git.lethalbits.com/*
|
||||||
|
GONOSUMCHECK: git.lethalbits.com/*
|
||||||
|
GOINSECURE: git.lethalbits.com/*
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
goreleaser:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- name: setup go
|
- uses: actions/setup-go@v5
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
with:
|
||||||
go-version-file: 'go.mod'
|
go-version-file: 'go.mod'
|
||||||
- name: release-build
|
- run: go install github.com/goreleaser/goreleaser/v2@latest
|
||||||
run: go build -ldflags="-s -w -X 'main.Version=${{ gitea.ref_name }}'" -o bin/mcp-gitea-${{ gitea.ref_name }}-linux-amd64
|
- run: goreleaser release --clean
|
||||||
- name: release-build-windows
|
env:
|
||||||
run: GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -X 'main.Version=${{ gitea.ref_name }}'" -o bin/mcp-gitea-${{ gitea.ref_name }}-windows-amd64.exe
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
- name: release-build-darwin
|
GORELEASER_FORCE_TOKEN: "gitea"
|
||||||
run: GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w -X 'main.Version=${{ gitea.ref_name }}'" -o bin/mcp-gitea-${{ gitea.ref_name }}-darwin-amd64
|
|
||||||
- name: release-build-arm64
|
|
||||||
run: GOARCH=arm64 go build -ldflags="-s -w -X 'main.Version=${{ gitea.ref_name }}'" -o bin/mcp-gitea-${{ gitea.ref_name }}-linux-arm64
|
|
||||||
- name: release-build-windows-arm64
|
|
||||||
run: GOOS=windows GOARCH=arm64 go build -ldflags="-s -w -X 'main.Version=${{ gitea.ref_name }}'" -o bin/mcp-gitea-${{ gitea.ref_name }}-windows-arm64.exe
|
|
||||||
- name: release-build-darwin-arm64
|
|
||||||
run: GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w -X 'main.Version=${{ gitea.ref_name }}'" -o bin/mcp-gitea-${{ gitea.ref_name }}-darwin-arm64
|
|
||||||
|
|
||||||
- name: Use Go Action
|
|
||||||
id: use-go-action
|
|
||||||
uses: https://gitea.com/actions/gitea-release-action@main
|
|
||||||
with:
|
|
||||||
files: |-
|
|
||||||
bin/**
|
|
||||||
token: '${{secrets.RELEASE_TOKEN}}'
|
|
||||||
|
|||||||
@@ -3,24 +3,23 @@ name: check-and-test
|
|||||||
on:
|
on:
|
||||||
- pull_request
|
- pull_request
|
||||||
|
|
||||||
|
env:
|
||||||
|
GIT_SSL_NO_VERIFY: true
|
||||||
|
GOPRIVATE: git.lethalbits.com/*
|
||||||
|
GONOSUMCHECK: git.lethalbits.com/*
|
||||||
|
GOINSECURE: git.lethalbits.com/*
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check-and-test:
|
check-and-test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version-file: 'go.mod'
|
go-version-file: 'go.mod'
|
||||||
|
- name: lint
|
||||||
|
run: make lint
|
||||||
- name: build
|
- name: build
|
||||||
run: |
|
run: make build
|
||||||
make build
|
- name: security-check
|
||||||
|
run: make security-check
|
||||||
govulncheck_job:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
name: Run govulncheck
|
|
||||||
steps:
|
|
||||||
- id: govulncheck
|
|
||||||
uses: golang/govulncheck-action@v1
|
|
||||||
with:
|
|
||||||
go-version-file: 'go.mod'
|
|
||||||
go-package: ./...
|
|
||||||
|
|||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,7 +1,5 @@
|
|||||||
.idea
|
.idea
|
||||||
.vscode
|
gitea-mcp-extended
|
||||||
|
gitea-mcp-extended.exe
|
||||||
gitea-mcp
|
|
||||||
gitea-mcp.exe
|
|
||||||
|
|
||||||
*.log
|
*.log
|
||||||
|
tmp
|
||||||
|
|||||||
113
.golangci.yml
Normal file
113
.golangci.yml
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
version: "2"
|
||||||
|
output:
|
||||||
|
sort-order:
|
||||||
|
- file
|
||||||
|
linters:
|
||||||
|
default: none
|
||||||
|
enable:
|
||||||
|
- bidichk
|
||||||
|
- bodyclose
|
||||||
|
- depguard
|
||||||
|
- errcheck
|
||||||
|
- forbidigo
|
||||||
|
- gocheckcompilerdirectives
|
||||||
|
- gocritic
|
||||||
|
- govet
|
||||||
|
- ineffassign
|
||||||
|
- mirror
|
||||||
|
- modernize
|
||||||
|
- nakedret
|
||||||
|
- nilnil
|
||||||
|
- nolintlint
|
||||||
|
- perfsprint
|
||||||
|
- revive
|
||||||
|
- staticcheck
|
||||||
|
- testifylint
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
- unused
|
||||||
|
- usestdlibvars
|
||||||
|
- usetesting
|
||||||
|
- wastedassign
|
||||||
|
settings:
|
||||||
|
depguard:
|
||||||
|
rules:
|
||||||
|
main:
|
||||||
|
deny:
|
||||||
|
- pkg: io/ioutil
|
||||||
|
desc: use os or io instead
|
||||||
|
- pkg: golang.org/x/exp
|
||||||
|
desc: it's experimental and unreliable
|
||||||
|
- pkg: github.com/pkg/errors
|
||||||
|
desc: use builtin errors package instead
|
||||||
|
nolintlint:
|
||||||
|
allow-unused: false
|
||||||
|
require-explanation: true
|
||||||
|
require-specific: true
|
||||||
|
gocritic:
|
||||||
|
enabled-checks:
|
||||||
|
- equalFold
|
||||||
|
disabled-checks: []
|
||||||
|
revive:
|
||||||
|
severity: error
|
||||||
|
rules:
|
||||||
|
- name: blank-imports
|
||||||
|
- name: constant-logical-expr
|
||||||
|
- name: context-as-argument
|
||||||
|
- name: context-keys-type
|
||||||
|
- name: dot-imports
|
||||||
|
- name: empty-lines
|
||||||
|
- name: error-return
|
||||||
|
- name: error-strings
|
||||||
|
- name: exported
|
||||||
|
- name: identical-branches
|
||||||
|
- name: if-return
|
||||||
|
- name: increment-decrement
|
||||||
|
- name: modifies-value-receiver
|
||||||
|
- name: package-comments
|
||||||
|
- name: redefines-builtin-id
|
||||||
|
- name: superfluous-else
|
||||||
|
- name: time-naming
|
||||||
|
- name: unexported-return
|
||||||
|
- name: var-declaration
|
||||||
|
- name: var-naming
|
||||||
|
disabled: true
|
||||||
|
staticcheck:
|
||||||
|
checks:
|
||||||
|
- all
|
||||||
|
testifylint: {}
|
||||||
|
usetesting:
|
||||||
|
os-temp-dir: true
|
||||||
|
perfsprint:
|
||||||
|
concat-loop: false
|
||||||
|
govet:
|
||||||
|
enable:
|
||||||
|
- nilness
|
||||||
|
- unusedwrite
|
||||||
|
exclusions:
|
||||||
|
generated: lax
|
||||||
|
presets:
|
||||||
|
- comments
|
||||||
|
- common-false-positives
|
||||||
|
- legacy
|
||||||
|
- std-error-handling
|
||||||
|
rules:
|
||||||
|
- linters:
|
||||||
|
- errcheck
|
||||||
|
- staticcheck
|
||||||
|
- unparam
|
||||||
|
path: _test\.go
|
||||||
|
issues:
|
||||||
|
max-issues-per-linter: 0
|
||||||
|
max-same-issues: 0
|
||||||
|
formatters:
|
||||||
|
enable:
|
||||||
|
- gofmt
|
||||||
|
- gofumpt
|
||||||
|
settings:
|
||||||
|
gofumpt:
|
||||||
|
extra-rules: true
|
||||||
|
exclusions:
|
||||||
|
generated: lax
|
||||||
|
run:
|
||||||
|
timeout: 10m
|
||||||
77
.goreleaser.yaml
Normal file
77
.goreleaser.yaml
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
main: .
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
flags:
|
||||||
|
- -trimpath
|
||||||
|
ldflags:
|
||||||
|
- -s -w
|
||||||
|
- -X main.Version={{.Version}}
|
||||||
|
|
||||||
|
archives:
|
||||||
|
- formats: tar.gz
|
||||||
|
# this name template makes the OS and Arch compatible with the results of `uname`.
|
||||||
|
name_template: >-
|
||||||
|
{{ .ProjectName }}_
|
||||||
|
{{- title .Os }}_
|
||||||
|
{{- if eq .Arch "amd64" }}x86_64
|
||||||
|
{{- else if eq .Arch "386" }}i386
|
||||||
|
{{- else }}{{ .Arch }}{{ end }}
|
||||||
|
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||||
|
# use zip for windows archives
|
||||||
|
format_overrides:
|
||||||
|
- goos: windows
|
||||||
|
formats: zip
|
||||||
|
|
||||||
|
changelog:
|
||||||
|
sort: asc
|
||||||
|
groups:
|
||||||
|
- title: Features
|
||||||
|
regexp: "^.*feat[(\\w)]*:+.*$"
|
||||||
|
order: 0
|
||||||
|
- title: "Bug fixes"
|
||||||
|
regexp: "^.*fix[(\\w)]*:+.*$"
|
||||||
|
order: 1
|
||||||
|
- title: "Enhancements"
|
||||||
|
regexp: "^.*chore[(\\w)]*:+.*$"
|
||||||
|
order: 2
|
||||||
|
- title: "Refactor"
|
||||||
|
regexp: "^.*refactor[(\\w)]*:+.*$"
|
||||||
|
order: 3
|
||||||
|
- title: "Build process updates"
|
||||||
|
regexp: ^.*?(build|ci)(\(.+\))??!?:.+$
|
||||||
|
order: 4
|
||||||
|
- title: "Documentation updates"
|
||||||
|
regexp: ^.*?docs?(\(.+\))??!?:.+$
|
||||||
|
order: 4
|
||||||
|
- title: Others
|
||||||
|
order: 999
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- "^docs:"
|
||||||
|
- "^test:"
|
||||||
|
|
||||||
|
release:
|
||||||
|
footer: >-
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Released by [GoReleaser](https://github.com/goreleaser/goreleaser).
|
||||||
|
|
||||||
|
gitea_urls:
|
||||||
|
api: https://git.lethalbits.com/api/v1
|
||||||
|
download: https://git.lethalbits.com
|
||||||
|
skip_tls_verify: true
|
||||||
|
force_token: gitea
|
||||||
39
.vscode/mcp.json
vendored
Normal file
39
.vscode/mcp.json
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
// 💡 Inputs are prompted on first server start, then stored securely by VS Code.
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"type": "promptString",
|
||||||
|
"id": "gitea-host",
|
||||||
|
"description": "Gitea Host",
|
||||||
|
"password": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "promptString",
|
||||||
|
"id": "gitea-token",
|
||||||
|
"description": "Gitea Access Token",
|
||||||
|
"password": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "promptString",
|
||||||
|
"id": "gitea-insecure",
|
||||||
|
"description": "Allow insecure connections (e.g., self-signed certificates)",
|
||||||
|
"default": "false"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"servers": {
|
||||||
|
"gitea-mcp-stdio": {
|
||||||
|
"type": "stdio",
|
||||||
|
"command": "gitea-mcp",
|
||||||
|
"args": ["-t", "stdio"],
|
||||||
|
"env": {
|
||||||
|
"GITEA_HOST": "${input:gitea-host}",
|
||||||
|
"GITEA_ACCESS_TOKEN": "${input:gitea-token}",
|
||||||
|
"GITEA_INSECURE": "${input:gitea-insecure}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"gitea-mcp-http": {
|
||||||
|
"type": "http",
|
||||||
|
"url": "http://localhost:8080/mcp",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
71
AGENTS.md
Normal file
71
AGENTS.md
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# AGENTS.md
|
||||||
|
|
||||||
|
This file provides guidance to AI coding agents when working with code in this repository.
|
||||||
|
|
||||||
|
## Development Commands
|
||||||
|
|
||||||
|
**Build**: `make build` - Build the gitea-mcp binary
|
||||||
|
**Install**: `make install` - Build and install to GOPATH/bin
|
||||||
|
**Clean**: `make clean` - Remove build artifacts
|
||||||
|
**Test**: `go test ./...` - Run all tests
|
||||||
|
**Hot reload**: `make dev` - Start development server with hot reload (requires air)
|
||||||
|
**Dependencies**: `make vendor` - Tidy and verify module dependencies
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
|
This is a **Gitea MCP (Model Context Protocol) Server** written in Go that provides MCP tools for interacting with Gitea repositories, issues, pull requests, users, and more.
|
||||||
|
|
||||||
|
**Core Components**:
|
||||||
|
|
||||||
|
- `main.go` + `cmd/cmd.go`: CLI entry point and flag parsing
|
||||||
|
- `operation/operation.go`: Main server setup and tool registration
|
||||||
|
- `pkg/tool/tool.go`: Tool registry with read/write categorization
|
||||||
|
- `operation/*/`: Individual tool modules (user, repo, issue, pull, search, wiki, etc.)
|
||||||
|
|
||||||
|
**Transport Modes**:
|
||||||
|
|
||||||
|
- **stdio** (default): Standard input/output for MCP clients
|
||||||
|
- **http**: HTTP server mode on configurable port (default 8080)
|
||||||
|
|
||||||
|
**Authentication**:
|
||||||
|
|
||||||
|
- Global token via `--token` flag or `GITEA_ACCESS_TOKEN` env var
|
||||||
|
- HTTP mode supports per-request Bearer token override in Authorization header
|
||||||
|
- Token precedence: HTTP Authorization header > CLI flag > environment variable
|
||||||
|
|
||||||
|
**Tool Organization**:
|
||||||
|
|
||||||
|
- Tools are categorized as read-only or write operations
|
||||||
|
- `--read-only` flag exposes only read tools
|
||||||
|
- Tool modules register via `Tool.RegisterRead()` and `Tool.RegisterWrite()`
|
||||||
|
|
||||||
|
**Key Configuration**:
|
||||||
|
|
||||||
|
- Default Gitea host: `https://gitea.com` (override with `--host` or `GITEA_HOST`)
|
||||||
|
- Environment variables can override CLI flags: `MCP_MODE`, `GITEA_READONLY`, `GITEA_DEBUG`, `GITEA_INSECURE`
|
||||||
|
- Logs are written to `~/.gitea-mcp/gitea-mcp.log` with rotation
|
||||||
|
|
||||||
|
## Available Tools
|
||||||
|
|
||||||
|
The server provides 40+ MCP tools covering:
|
||||||
|
|
||||||
|
- **User**: get_my_user_info, get_user_orgs, search_users
|
||||||
|
- **Repository**: create_repo, fork_repo, list_my_repos, search_repos
|
||||||
|
- **Branches/Tags**: create_branch, delete_branch, list_branches, create_tag, list_tags
|
||||||
|
- **Files**: get_file_content, create_file, update_file, delete_file, get_dir_content
|
||||||
|
- **Issues**: create_issue, list_repo_issues, create_issue_comment, edit_issue
|
||||||
|
- **Pull Requests**: create_pull_request, list_repo_pull_requests, get_pull_request_by_index
|
||||||
|
- **Releases**: create_release, list_releases, get_latest_release
|
||||||
|
- **Wiki**: create_wiki_page, update_wiki_page, list_wiki_pages
|
||||||
|
- **Search**: search_repos, search_users, search_org_teams
|
||||||
|
- **Version**: get_gitea_mcp_server_version
|
||||||
|
|
||||||
|
## Common Development Patterns
|
||||||
|
|
||||||
|
**Testing**: Use `go test ./operation -run TestFunctionName` for specific tests
|
||||||
|
|
||||||
|
**Token Context**: HTTP requests use `pkg/context.TokenContextKey` for request-scoped token access
|
||||||
|
|
||||||
|
**Flag Access**: All packages access configuration via global variables in `pkg/flag/flag.go`
|
||||||
|
|
||||||
|
**Graceful Shutdown**: HTTP mode implements graceful shutdown with 10-second timeout on SIGTERM/SIGINT
|
||||||
63
BUILDING.md
Normal file
63
BUILDING.md
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# Building gitea-mcp on Windows
|
||||||
|
|
||||||
|
This project includes PowerShell and batch scripts to build the gitea-mcp application on Windows systems.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Go 1.24 or later
|
||||||
|
- Git (for version information)
|
||||||
|
- PowerShell 5.1 or later (included with Windows 10/11)
|
||||||
|
|
||||||
|
## Build Scripts
|
||||||
|
|
||||||
|
### PowerShell Script (`build.ps1`)
|
||||||
|
|
||||||
|
The main build script that replicates all Makefile functionality:
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
# Show help
|
||||||
|
.\build.ps1 help
|
||||||
|
|
||||||
|
# Build the application
|
||||||
|
.\build.ps1 build
|
||||||
|
|
||||||
|
# Install the application
|
||||||
|
.\build.ps1 install
|
||||||
|
|
||||||
|
# Clean build artifacts
|
||||||
|
.\build.ps1 clean
|
||||||
|
|
||||||
|
# Run in development mode (hot reload)
|
||||||
|
.\build.ps1 dev
|
||||||
|
|
||||||
|
# Update vendor dependencies
|
||||||
|
.\build.ps1 vendor
|
||||||
|
```
|
||||||
|
|
||||||
|
### Batch File Wrapper (`build.bat`)
|
||||||
|
|
||||||
|
A simple wrapper to run the PowerShell script:
|
||||||
|
|
||||||
|
```cmd
|
||||||
|
# Run with default help target
|
||||||
|
build.bat
|
||||||
|
|
||||||
|
# Run specific target
|
||||||
|
build.bat build
|
||||||
|
build.bat install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Available Targets
|
||||||
|
|
||||||
|
- **help** - Print help message
|
||||||
|
- **build** - Build the application executable
|
||||||
|
- **install** - Build and install to GOPATH/bin
|
||||||
|
- **uninstall** - Remove executable from GOPATH/bin
|
||||||
|
- **clean** - Remove build artifacts
|
||||||
|
- **air** - Install air for hot reload development
|
||||||
|
- **dev** - Run with hot reload development
|
||||||
|
- **vendor** - Tidy and verify Go module dependencies
|
||||||
|
|
||||||
|
## Output
|
||||||
|
|
||||||
|
The build process creates `gitea-mcp.exe` in the project directory.
|
||||||
71
CLAUDE.md
Normal file
71
CLAUDE.md
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Development Commands
|
||||||
|
|
||||||
|
**Build**: `make build` - Build the gitea-mcp-extended binary
|
||||||
|
**Install**: `make install` - Build and install to GOPATH/bin
|
||||||
|
**Clean**: `make clean` - Remove build artifacts
|
||||||
|
**Test**: `go test ./...` - Run all tests
|
||||||
|
**Hot reload**: `make dev` - Start development server with hot reload (requires air)
|
||||||
|
**Dependencies**: `make vendor` - Tidy and verify module dependencies
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
|
This is **gitea-mcp-extended**, a Gitea MCP (Model Context Protocol) Server written in Go that provides 299+ MCP tools for interacting with Gitea repositories, issues, pull requests, users, and more. It is a fork of [gitea/gitea-mcp](https://gitea.com/gitea/gitea-mcp) with significantly expanded tool coverage.
|
||||||
|
|
||||||
|
**Core Components**:
|
||||||
|
|
||||||
|
- `main.go` + `cmd/cmd.go`: CLI entry point and flag parsing
|
||||||
|
- `operation/operation.go`: Main server setup and tool registration
|
||||||
|
- `pkg/tool/tool.go`: Tool registry with read/write categorization
|
||||||
|
- `operation/*/`: Individual tool modules (user, repo, issue, pull, search, wiki, etc.)
|
||||||
|
|
||||||
|
**Transport Modes**:
|
||||||
|
|
||||||
|
- **stdio** (default): Standard input/output for MCP clients
|
||||||
|
- **http**: HTTP server mode on configurable port (default 8080)
|
||||||
|
|
||||||
|
**Authentication**:
|
||||||
|
|
||||||
|
- Global token via `--token` flag or `GITEA_ACCESS_TOKEN` env var
|
||||||
|
- HTTP mode supports per-request Bearer token override in Authorization header
|
||||||
|
- Token precedence: HTTP Authorization header > CLI flag > environment variable
|
||||||
|
|
||||||
|
**Tool Organization**:
|
||||||
|
|
||||||
|
- Tools are categorized as read-only or write operations
|
||||||
|
- `--read-only` flag exposes only read tools
|
||||||
|
- Tool modules register via `Tool.RegisterRead()` and `Tool.RegisterWrite()`
|
||||||
|
|
||||||
|
**Key Configuration**:
|
||||||
|
|
||||||
|
- Default Gitea host: `https://gitea.com` (override with `--host` or `GITEA_HOST`)
|
||||||
|
- Environment variables can override CLI flags: `MCP_MODE`, `GITEA_READONLY`, `GITEA_DEBUG`, `GITEA_INSECURE`
|
||||||
|
- Logs are written to `~/.gitea-mcp/gitea-mcp.log` with rotation (log dir unchanged for backward compatibility)
|
||||||
|
|
||||||
|
## Available Tools
|
||||||
|
|
||||||
|
The server provides 299+ MCP tools covering:
|
||||||
|
|
||||||
|
- **User**: get_my_user_info, get_user_orgs, search_users
|
||||||
|
- **Repository**: create_repo, fork_repo, list_my_repos, search_repos
|
||||||
|
- **Branches/Tags**: create_branch, delete_branch, list_branches, create_tag, list_tags
|
||||||
|
- **Files**: get_file_content, create_file, update_file, delete_file, get_dir_content
|
||||||
|
- **Issues**: create_issue, list_repo_issues, create_issue_comment, edit_issue
|
||||||
|
- **Pull Requests**: create_pull_request, list_repo_pull_requests, get_pull_request_by_index
|
||||||
|
- **Releases**: create_release, list_releases, get_latest_release
|
||||||
|
- **Wiki**: create_wiki_page, update_wiki_page, list_wiki_pages
|
||||||
|
- **Search**: search_repos, search_users, search_org_teams
|
||||||
|
- **Version**: get_gitea_mcp_server_version
|
||||||
|
|
||||||
|
## Common Development Patterns
|
||||||
|
|
||||||
|
**Testing**: Use `go test ./operation -run TestFunctionName` for specific tests
|
||||||
|
|
||||||
|
**Token Context**: HTTP requests use `pkg/context.TokenContextKey` for request-scoped token access
|
||||||
|
|
||||||
|
**Flag Access**: All packages access configuration via global variables in `pkg/flag/flag.go`
|
||||||
|
|
||||||
|
**Graceful Shutdown**: HTTP mode implements graceful shutdown with 10-second timeout on SIGTERM/SIGINT
|
||||||
32
Dockerfile
Normal file
32
Dockerfile
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# syntax=docker/dockerfile:1.4
|
||||||
|
|
||||||
|
# Build stage
|
||||||
|
FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS builder
|
||||||
|
|
||||||
|
ARG VERSION=dev
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||||
|
go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||||
|
--mount=type=cache,target=/root/.cache/go-build \
|
||||||
|
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
|
||||||
|
go build -trimpath -ldflags="-s -w -X main.Version=${VERSION}" -o gitea-mcp-extended
|
||||||
|
|
||||||
|
# Final stage
|
||||||
|
FROM gcr.io/distroless/static-debian12:nonroot
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder --chown=nonroot:nonroot /app/gitea-mcp-extended .
|
||||||
|
|
||||||
|
USER nonroot:nonroot
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.version="${VERSION}"
|
||||||
|
|
||||||
|
CMD ["/app/gitea-mcp-extended"]
|
||||||
74
Makefile
74
Makefile
@@ -1,27 +1,77 @@
|
|||||||
GO ?= go
|
GO ?= go
|
||||||
EXECUTABLE := gitea-mcp
|
EXECUTABLE := gitea-mcp-extended
|
||||||
VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
|
VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
|
||||||
LDFLAGS := -X "main.Version=$(VERSION)"
|
LDFLAGS := -X "main.Version=$(VERSION)"
|
||||||
|
|
||||||
|
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1
|
||||||
|
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/govulncheck@v1
|
||||||
|
GOFUMPT_PACKAGE ?= mvdan.cc/gofumpt@v0.9.2
|
||||||
|
|
||||||
|
.PHONY: help
|
||||||
|
help: ## Print this help message.
|
||||||
|
@echo "Usage: make [target]"
|
||||||
|
@echo ""
|
||||||
|
@echo "Targets:"
|
||||||
|
@echo ""
|
||||||
|
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
||||||
|
|
||||||
|
.PHONY: install
|
||||||
|
install: build ## Install the application.
|
||||||
|
@echo "Installing $(EXECUTABLE)..."
|
||||||
|
@mkdir -p $(GOPATH)/bin
|
||||||
|
@cp $(EXECUTABLE) $(GOPATH)/bin/$(EXECUTABLE)
|
||||||
|
@echo "Installed $(EXECUTABLE) to $(GOPATH)/bin/$(EXECUTABLE)"
|
||||||
|
@echo "Please add $(GOPATH)/bin to your PATH if it is not already there."
|
||||||
|
|
||||||
|
.PHONY: uninstall
|
||||||
|
uninstall: ## Uninstall the application.
|
||||||
|
@echo "Uninstalling $(EXECUTABLE)..."
|
||||||
|
@rm -f $(GOPATH)/bin/$(EXECUTABLE)
|
||||||
|
@echo "Uninstalled $(EXECUTABLE) from $(GOPATH)/bin/$(EXECUTABLE)"
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean: ## Clean the build artifacts.
|
||||||
|
@echo "Cleaning up build artifacts..."
|
||||||
|
@rm -f $(EXECUTABLE)
|
||||||
|
@echo "Cleaned up $(EXECUTABLE)"
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build: ## Build the application.
|
||||||
$(GO) build -v -ldflags '-s -w $(LDFLAGS)' -o $(EXECUTABLE)
|
$(GO) build -v -ldflags '-s -w $(LDFLAGS)' -o $(EXECUTABLE)
|
||||||
|
|
||||||
## air: install air for hot reload
|
|
||||||
.PHONY: air
|
.PHONY: air
|
||||||
air:
|
air: ## Install air for hot reload.
|
||||||
@hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
@hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
$(GO) install github.com/air-verse/air@latest; \
|
$(GO) install github.com/air-verse/air@latest; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## dev: run the application with hot reload
|
|
||||||
.PHONY: dev
|
.PHONY: dev
|
||||||
dev: air
|
dev: air ## run the application with hot reload
|
||||||
air --build.cmd "make build" --build.bin ./gitea-mcp
|
air --build.cmd "make build" --build.bin ./gitea-mcp-extended
|
||||||
|
|
||||||
|
.PHONY: lint
|
||||||
|
lint: lint-go ## lint everything
|
||||||
|
|
||||||
|
.PHONY: lint-fix
|
||||||
|
lint-fix: lint-go-fix ## lint everything and fix issues
|
||||||
|
|
||||||
|
.PHONY: lint-go
|
||||||
|
lint-go: ## lint go files
|
||||||
|
$(GO) run $(GOLANGCI_LINT_PACKAGE) run
|
||||||
|
|
||||||
|
.PHONY: lint-go-fix
|
||||||
|
lint-go-fix: ## lint go files and fix issues
|
||||||
|
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --fix
|
||||||
|
|
||||||
|
.PHONY: security-check
|
||||||
|
security-check: ## run security check
|
||||||
|
$(GO) run $(GOVULNCHECK_PACKAGE) -show color ./... || true
|
||||||
|
|
||||||
|
.PHONY: tidy
|
||||||
|
tidy: ## run go mod tidy
|
||||||
|
$(eval MIN_GO_VERSION := $(shell grep -Eo '^go\s+[0-9]+\.[0-9.]+' go.mod | cut -d' ' -f2))
|
||||||
|
$(GO) mod tidy -compat=$(MIN_GO_VERSION)
|
||||||
|
|
||||||
## vendor: tidy and verify module dependencies
|
|
||||||
.PHONY: vendor
|
.PHONY: vendor
|
||||||
vendor:
|
vendor: tidy ## tidy and verify module dependencies
|
||||||
@echo 'Tidying and verifying module dependencies...'
|
$(GO) mod verify
|
||||||
go mod tidy
|
|
||||||
go mod verify
|
|
||||||
|
|||||||
242
README.md
242
README.md
@@ -1,19 +1,100 @@
|
|||||||
# Gitea MCP Server
|
# Gitea MCP Server
|
||||||
|
|
||||||
|
[繁體中文](README.zh-tw.md) | [简体中文](README.zh-cn.md)
|
||||||
|
|
||||||
**Gitea MCP Server** is an integration plugin designed to connect Gitea with Model Context Protocol (MCP) systems. This allows for seamless command execution and repository management through an MCP-compatible chat interface.
|
**Gitea MCP Server** is an integration plugin designed to connect Gitea with Model Context Protocol (MCP) systems. This allows for seamless command execution and repository management through an MCP-compatible chat interface.
|
||||||
|
|
||||||
## 🚧Installation
|
[](https://insiders.vscode.dev/redirect/mcp/install?name=gitea&inputs=[{%22id%22:%22gitea_token%22,%22type%22:%22promptString%22,%22description%22:%22Gitea%20Personal%20Access%20Token%22,%22password%22:true}]&config={%22command%22:%22docker%22,%22args%22:[%22run%22,%22-i%22,%22--rm%22,%22-e%22,%22GITEA_ACCESS_TOKEN%22,%22docker.gitea.com/gitea-mcp-server%22],%22env%22:{%22GITEA_ACCESS_TOKEN%22:%22${input:gitea_token}%22}}) [](https://insiders.vscode.dev/redirect/mcp/install?name=gitea&inputs=[{%22id%22:%22gitea_token%22,%22type%22:%22promptString%22,%22description%22:%22Gitea%20Personal%20Access%20Token%22,%22password%22:true}]&config={%22command%22:%22docker%22,%22args%22:[%22run%22,%22-i%22,%22--rm%22,%22-e%22,%22GITEA_ACCESS_TOKEN%22,%22docker.gitea.com/gitea-mcp-server%22],%22env%22:{%22GITEA_ACCESS_TOKEN%22:%22${input:gitea_token}%22}}&quality=insiders)
|
||||||
|
|
||||||
### 📥Download the official binary release
|
## Table of Contents
|
||||||
|
|
||||||
You can download the official release from [here](https://gitea.com/gitea/gitea-mcp/releases).
|
- [Gitea MCP Server](#gitea-mcp-server)
|
||||||
|
- [Table of Contents](#table-of-contents)
|
||||||
|
- [What is Gitea?](#what-is-gitea)
|
||||||
|
- [What is MCP?](#what-is-mcp)
|
||||||
|
- [🚧 Installation](#-installation)
|
||||||
|
- [Usage with Claude Code](#usage-with-claude-code)
|
||||||
|
- [Usage with VS Code](#usage-with-vs-code)
|
||||||
|
- [📥 Download the official binary release](#-download-the-official-binary-release)
|
||||||
|
- [🔧 Build from Source](#-build-from-source)
|
||||||
|
- [📁 Add to PATH](#-add-to-path)
|
||||||
|
- [🚀 Usage](#-usage)
|
||||||
|
- [✅ Available Tools](#-available-tools)
|
||||||
|
- [🐛 Debugging](#-debugging)
|
||||||
|
- [🛠 Troubleshooting](#-troubleshooting)
|
||||||
|
|
||||||
### 🔧Build from Source
|
## What is Gitea?
|
||||||
|
|
||||||
|
Gitea is a community-managed lightweight code hosting solution written in Go. It is published under the MIT license. Gitea provides Git hosting including a repository viewer, issue tracking, pull requests, and more.
|
||||||
|
|
||||||
|
## What is MCP?
|
||||||
|
|
||||||
|
Model Context Protocol (MCP) is a protocol that allows for the integration of various tools and systems through a chat interface. It enables seamless command execution and management of repositories, users, and other resources.
|
||||||
|
|
||||||
|
## 🚧 Installation
|
||||||
|
|
||||||
|
### Usage with Claude Code
|
||||||
|
|
||||||
|
This method uses `go run` and requires [Go](https://go.dev) to be installed.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add --transport stdio --scope user gitea \
|
||||||
|
--env GITEA_ACCESS_TOKEN=token \
|
||||||
|
--env GITEA_HOST=https://gitea.com \
|
||||||
|
-- go run git.lethalbits.com/lethalbits/gitea-mcp@latest -t stdio
|
||||||
|
```
|
||||||
|
|
||||||
|
### Usage with VS Code
|
||||||
|
|
||||||
|
For quick installation, use one of the one-click install buttons at the top of this README.
|
||||||
|
|
||||||
|
For manual installation, add the following JSON block to your User Settings (JSON) file in VS Code. You can do this by pressing `Ctrl + Shift + P` and typing `Preferences: Open User Settings (JSON)`.
|
||||||
|
|
||||||
|
Optionally, you can add it to a file called `.vscode/mcp.json` in your workspace. This will allow you to share the configuration with others.
|
||||||
|
|
||||||
|
> Note that the `mcp` key is not needed in the `.vscode/mcp.json` file.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcp": {
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"type": "promptString",
|
||||||
|
"id": "gitea_token",
|
||||||
|
"description": "Gitea Personal Access Token",
|
||||||
|
"password": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"servers": {
|
||||||
|
"gitea-mcp": {
|
||||||
|
"command": "docker",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"-i",
|
||||||
|
"--rm",
|
||||||
|
"-e",
|
||||||
|
"GITEA_ACCESS_TOKEN",
|
||||||
|
"docker.gitea.com/gitea-mcp-server"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
"GITEA_ACCESS_TOKEN": "${input:gitea_token}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📥 Download the official binary release
|
||||||
|
|
||||||
|
You can download the official release from [official Gitea MCP binary releases](https://git.lethalbits.com/lethalbits/gitea-mcp/releases).
|
||||||
|
|
||||||
|
### 🔧 Build from Source
|
||||||
|
|
||||||
You can download the source code by cloning the repository using Git:
|
You can download the source code by cloning the repository using Git:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://gitea.com/gitea/gitea-mcp.git
|
git clone https://git.lethalbits.com/lethalbits/gitea-mcp.git
|
||||||
```
|
```
|
||||||
|
|
||||||
Before building, make sure you have the following installed:
|
Before building, make sure you have the following installed:
|
||||||
@@ -24,18 +105,18 @@ Before building, make sure you have the following installed:
|
|||||||
Then run:
|
Then run:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make build
|
make install
|
||||||
```
|
```
|
||||||
|
|
||||||
### 📁Add to PATH
|
### 📁 Add to PATH
|
||||||
|
|
||||||
After building, copy the binary gitea-mcp to a directory included in your system's PATH. For example:
|
After installing, copy the binary gitea-mcp to a directory included in your system's PATH. For example:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cp gitea-mcp /usr/local/bin/
|
cp gitea-mcp /usr/local/bin/
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🚀Usage
|
## 🚀 Usage
|
||||||
|
|
||||||
This example is for Cursor, you can also use plugins in VSCode.
|
This example is for Cursor, you can also use plugins in VSCode.
|
||||||
To configure the MCP server for Gitea, add the following to your MCP configuration file:
|
To configure the MCP server for Gitea, add the following to your MCP configuration file:
|
||||||
@@ -48,12 +129,15 @@ To configure the MCP server for Gitea, add the following to your MCP configurati
|
|||||||
"gitea": {
|
"gitea": {
|
||||||
"command": "gitea-mcp",
|
"command": "gitea-mcp",
|
||||||
"args": [
|
"args": [
|
||||||
"-t", "stdio",
|
"-t",
|
||||||
"--host", "https://gitea.com"
|
"stdio",
|
||||||
|
"--host",
|
||||||
|
"https://gitea.com"
|
||||||
// "--token", "<your personal access token>"
|
// "--token", "<your personal access token>"
|
||||||
],
|
],
|
||||||
"env": {
|
"env": {
|
||||||
// "GITEA_HOST": "https://gitea.com",
|
// "GITEA_HOST": "https://gitea.com",
|
||||||
|
// "GITEA_INSECURE": "true",
|
||||||
"GITEA_ACCESS_TOKEN": "<your personal access token>"
|
"GITEA_ACCESS_TOKEN": "<your personal access token>"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,18 +145,23 @@ To configure the MCP server for Gitea, add the following to your MCP configurati
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- **sse mode**
|
- **http mode**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"mcpServers": {
|
"mcpServers": {
|
||||||
"gitea": {
|
"gitea": {
|
||||||
"url": "http://localhost:8080/sse"
|
"url": "http://localhost:8080/mcp",
|
||||||
|
"headers": {
|
||||||
|
"Authorization": "Bearer <your personal access token>"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Default log path**: `$HOME/.gitea-mcp/gitea-mcp.log`
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> You can provide your Gitea host and access token either as command-line arguments or environment variables.
|
> You can provide your Gitea host and access token either as command-line arguments or environment variables.
|
||||||
> Command-line arguments have the highest priority
|
> Command-line arguments have the highest priority
|
||||||
@@ -83,42 +172,113 @@ Once everything is set up, try typing the following in your MCP-compatible chatb
|
|||||||
list all my repositories
|
list all my repositories
|
||||||
```
|
```
|
||||||
|
|
||||||
## ✅Available Tools
|
## ✅ Available Tools
|
||||||
|
|
||||||
The Gitea MCP Server supports the following tools:
|
The Gitea MCP Server supports the following tools:
|
||||||
|
|
||||||
| Tool | Scope | Description |
|
| Tool | Scope | Description |
|
||||||
|:------:|:-------:|:------------:|
|
| :-------------------------------: | :----------: | :------------------------------------------------------: |
|
||||||
|get_my_user_info|User|Get the information of the authenticated user|
|
| get_my_user_info | User | Get the information of the authenticated user |
|
||||||
|create_repo|Repository|Create a new repository|
|
| get_user_orgs | User | Get organizations associated with the authenticated user |
|
||||||
|fork_repo|Repository|Fork a repository|
|
| create_repo | Repository | Create a new repository |
|
||||||
|list_my_repos|Repository|List all repositories owned by the authenticated user|
|
| fork_repo | Repository | Fork a repository |
|
||||||
|create_branch|Branch|Create a new branch|
|
| list_my_repos | Repository | List all repositories owned by the authenticated user |
|
||||||
|delete_branch|Branch|Delete a branch|
|
| create_branch | Branch | Create a new branch |
|
||||||
|list_branches|Branch|List all branches in a repository|
|
| delete_branch | Branch | Delete a branch |
|
||||||
|list_repo_commits|Commit|List all commits in a repository|
|
| list_branches | Branch | List all branches in a repository |
|
||||||
|get_file_content|File|Get the content and metadata of a file|
|
| create_release | Release | Create a new release in a repository |
|
||||||
|create_file|File|Create a new file|
|
| delete_release | Release | Delete a release from a repository |
|
||||||
|update_file|File|Update an existing file|
|
| get_release | Release | Get a release |
|
||||||
|delete_file|File|Delete a file|
|
| get_latest_release | Release | Get the latest release in a repository |
|
||||||
|get_issue_by_index|Issue|Get an issue by its index|
|
| list_releases | Release | List all releases in a repository |
|
||||||
|list_repo_issues|Issue|List all issues in a repository|
|
| create_tag | Tag | Create a new tag |
|
||||||
|create_issue|Issue|Create a new issue|
|
| delete_tag | Tag | Delete a tag |
|
||||||
|create_issue_comment|Issue|Create a comment on an issue|
|
| get_tag | Tag | Get a tag |
|
||||||
|get_pull_request_by_index|Pull Request|Get a pull request by its index|
|
| list_tags | Tag | List all tags in a repository |
|
||||||
|list_repo_pull_requests|Pull Request|List all pull requests in a repository|
|
| list_repo_commits | Commit | List all commits in a repository |
|
||||||
|create_pull_request|Pull Request|Create a new pull request|
|
| get_file_content | File | Get the content and metadata of a file |
|
||||||
|search_users|User|Search for users|
|
| get_dir_content | File | Get a list of entries in a directory |
|
||||||
|search_org_teams|Organization|Search for teams in an organization|
|
| create_file | File | Create a new file |
|
||||||
|search_repos|Repository|Search for repositories|
|
| update_file | File | Update an existing file |
|
||||||
|get_gitea_mcp_server_version|Server|Get the version of the Gitea MCP Server|
|
| delete_file | File | Delete a file |
|
||||||
|
| get_issue_by_index | Issue | Get an issue by its index |
|
||||||
|
| list_repo_issues | Issue | List all issues in a repository |
|
||||||
|
| create_issue | Issue | Create a new issue |
|
||||||
|
| create_issue_comment | Issue | Create a comment on an issue |
|
||||||
|
| edit_issue | Issue | Edit a issue |
|
||||||
|
| edit_issue_comment | Issue | Edit a comment on an issue |
|
||||||
|
| get_issue_comments_by_index | Issue | Get comments of an issue by its index |
|
||||||
|
| get_pull_request_by_index | Pull Request | Get a pull request by its index |
|
||||||
|
| get_pull_request_diff | Pull Request | Get a pull request diff |
|
||||||
|
| list_repo_pull_requests | Pull Request | List all pull requests in a repository |
|
||||||
|
| create_pull_request | Pull Request | Create a new pull request |
|
||||||
|
| create_pull_request_reviewer | Pull Request | Add reviewers to a pull request |
|
||||||
|
| delete_pull_request_reviewer | Pull Request | Remove reviewers from a pull request |
|
||||||
|
| list_pull_request_reviews | Pull Request | List all reviews for a pull request |
|
||||||
|
| get_pull_request_review | Pull Request | Get a specific review by ID |
|
||||||
|
| list_pull_request_review_comments | Pull Request | List inline comments for a review |
|
||||||
|
| create_pull_request_review | Pull Request | Create a review with optional inline comments |
|
||||||
|
| submit_pull_request_review | Pull Request | Submit a pending review |
|
||||||
|
| delete_pull_request_review | Pull Request | Delete a review |
|
||||||
|
| dismiss_pull_request_review | Pull Request | Dismiss a review with optional message |
|
||||||
|
| merge_pull_request | Pull Request | Merge a pull request |
|
||||||
|
| search_users | User | Search for users |
|
||||||
|
| search_org_teams | Organization | Search for teams in an organization |
|
||||||
|
| list_org_labels | Organization | List labels defined at organization level |
|
||||||
|
| create_org_label | Organization | Create a label in an organization |
|
||||||
|
| edit_org_label | Organization | Edit a label in an organization |
|
||||||
|
| delete_org_label | Organization | Delete a label in an organization |
|
||||||
|
| search_repos | Repository | Search for repositories |
|
||||||
|
| list_repo_action_secrets | Actions | List repository Actions secrets (metadata only) |
|
||||||
|
| upsert_repo_action_secret | Actions | Create/update (upsert) a repository Actions secret |
|
||||||
|
| delete_repo_action_secret | Actions | Delete a repository Actions secret |
|
||||||
|
| list_org_action_secrets | Actions | List organization Actions secrets (metadata only) |
|
||||||
|
| upsert_org_action_secret | Actions | Create/update (upsert) an organization Actions secret |
|
||||||
|
| delete_org_action_secret | Actions | Delete an organization Actions secret |
|
||||||
|
| list_repo_action_variables | Actions | List repository Actions variables |
|
||||||
|
| get_repo_action_variable | Actions | Get a repository Actions variable |
|
||||||
|
| create_repo_action_variable | Actions | Create a repository Actions variable |
|
||||||
|
| update_repo_action_variable | Actions | Update a repository Actions variable |
|
||||||
|
| delete_repo_action_variable | Actions | Delete a repository Actions variable |
|
||||||
|
| list_org_action_variables | Actions | List organization Actions variables |
|
||||||
|
| get_org_action_variable | Actions | Get an organization Actions variable |
|
||||||
|
| create_org_action_variable | Actions | Create an organization Actions variable |
|
||||||
|
| update_org_action_variable | Actions | Update an organization Actions variable |
|
||||||
|
| delete_org_action_variable | Actions | Delete an organization Actions variable |
|
||||||
|
| list_repo_action_workflows | Actions | List repository Actions workflows |
|
||||||
|
| get_repo_action_workflow | Actions | Get a repository Actions workflow |
|
||||||
|
| dispatch_repo_action_workflow | Actions | Trigger (dispatch) a repository Actions workflow |
|
||||||
|
| list_repo_action_runs | Actions | List repository Actions runs |
|
||||||
|
| get_repo_action_run | Actions | Get a repository Actions run |
|
||||||
|
| cancel_repo_action_run | Actions | Cancel a repository Actions run |
|
||||||
|
| rerun_repo_action_run | Actions | Rerun a repository Actions run |
|
||||||
|
| list_repo_action_jobs | Actions | List repository Actions jobs |
|
||||||
|
| list_repo_action_run_jobs | Actions | List Actions jobs for a run |
|
||||||
|
| get_repo_action_job_log_preview | Actions | Get a job log preview (tail/limited) |
|
||||||
|
| download_repo_action_job_log | Actions | Download a job log to a file |
|
||||||
|
| get_gitea_mcp_server_version | Server | Get the version of the Gitea MCP Server |
|
||||||
|
| list_wiki_pages | Wiki | List all wiki pages in a repository |
|
||||||
|
| get_wiki_page | Wiki | Get a wiki page content and metadata |
|
||||||
|
| get_wiki_revisions | Wiki | Get revisions history of a wiki page |
|
||||||
|
| create_wiki_page | Wiki | Create a new wiki page |
|
||||||
|
| update_wiki_page | Wiki | Update an existing wiki page |
|
||||||
|
| delete_wiki_page | Wiki | Delete a wiki page |
|
||||||
|
|
||||||
## 🐛Debugging
|
## 🐛 Debugging
|
||||||
|
|
||||||
To enable debug mode, add the `-d` flag when running the Gitea MCP Server with sse mode:
|
To enable debug mode, add the `-d` flag when running the Gitea MCP Server with http mode:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
./gitea-mcp -t sse [--port 8080] --token <your personal access token> -d
|
./gitea-mcp -t http [--port 8080] --token <your personal access token> -d
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 🛠 Troubleshooting
|
||||||
|
|
||||||
|
If you encounter any issues, here are some common troubleshooting steps:
|
||||||
|
|
||||||
|
1. **Check your PATH**: Ensure that the `gitea-mcp` binary is in a directory included in your system's PATH.
|
||||||
|
2. **Verify dependencies**: Make sure you have all the required dependencies installed, such as `make` and `Golang`.
|
||||||
|
3. **Review configuration**: Double-check your MCP configuration file for any errors or missing information.
|
||||||
|
4. **Consult logs**: Check the logs for any error messages or warnings that can provide more information about the issue.
|
||||||
|
|
||||||
Enjoy exploring and managing your Gitea repositories via chat!
|
Enjoy exploring and managing your Gitea repositories via chat!
|
||||||
|
|||||||
256
README.zh-cn.md
Normal file
256
README.zh-cn.md
Normal file
@@ -0,0 +1,256 @@
|
|||||||
|
# Gitea MCP 服务器
|
||||||
|
|
||||||
|
[English](README.md) | [繁體中文](README.zh-tw.md)
|
||||||
|
|
||||||
|
**Gitea MCP 服务器** 是一个集成插件,旨在将 Gitea 与 Model Context Protocol (MCP) 系统连接起来。这允许通过 MCP 兼容的聊天界面无缝执行命令和管理仓库。
|
||||||
|
|
||||||
|
[](https://insiders.vscode.dev/redirect/mcp/install?name=gitea&inputs=[{%22id%22:%22gitea_token%22,%22type%22:%22promptString%22,%22description%22:%22Gitea%20Personal%20Access%20Token%22,%22password%22:true}]&config={%22command%22:%22docker%22,%22args%22:[%22run%22,%22-i%22,%22--rm%22,%22-e%22,%22GITEA_ACCESS_TOKEN%22,%22docker.gitea.com/gitea-mcp-server%22],%22env%22:{%22GITEA_ACCESS_TOKEN%22:%22${input:gitea_token}%22}}) [](https://insiders.vscode.dev/redirect/mcp/install?name=gitea&inputs=[{%22id%22:%22gitea_token%22,%22type%22:%22promptString%22,%22description%22:%22Gitea%20Personal%20Access%20Token%22,%22password%22:true}]&config={%22command%22:%22docker%22,%22args%22:[%22run%22,%22-i%22,%22--rm%22,%22-e%22,%22GITEA_ACCESS_TOKEN%22,%22docker.gitea.com/gitea-mcp-server%22],%22env%22:{%22GITEA_ACCESS_TOKEN%22:%22${input:gitea_token}%22}}&quality=insiders)
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [Gitea MCP 服务器](#gitea-mcp-服务器)
|
||||||
|
- [目录](#目录)
|
||||||
|
- [什么是 Gitea?](#什么是-gitea)
|
||||||
|
- [什么是 MCP?](#什么是-mcp)
|
||||||
|
- [🚧 安装](#-安装)
|
||||||
|
- [在 Claude Code 中使用](#在-claude-code-中使用)
|
||||||
|
- [在 VS Code 中使用](#在-vs-code-中使用)
|
||||||
|
- [📥 下载官方二进制版本](#-下载官方二进制版本)
|
||||||
|
- [🔧 从源码构建](#-从源码构建)
|
||||||
|
- [📁 加入 PATH](#-加入-path)
|
||||||
|
- [🚀 使用](#-使用)
|
||||||
|
- [✅ 可用工具](#-可用工具)
|
||||||
|
- [🐛 调试](#-调试)
|
||||||
|
- [🛠 疑难排解](#-疑难排解)
|
||||||
|
|
||||||
|
## 什么是 Gitea?
|
||||||
|
|
||||||
|
Gitea 是一个由社区管理的轻量级代码托管解决方案,使用 Go 语言编写,采用 MIT 许可证。Gitea 提供 Git 托管,包括仓库浏览、问题追踪、拉取请求等功能。
|
||||||
|
|
||||||
|
## 什么是 MCP?
|
||||||
|
|
||||||
|
Model Context Protocol (MCP) 是一种协议,允许通过聊天界面整合各种工具和系统。它能够无缝执行命令并管理仓库、用户及其他资源。
|
||||||
|
|
||||||
|
## 🚧 安装
|
||||||
|
|
||||||
|
### 在 Claude Code 中使用
|
||||||
|
|
||||||
|
此方式使用 `go run`,需要安装 [Go](https://go.dev)。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add --transport stdio --scope user gitea \
|
||||||
|
--env GITEA_ACCESS_TOKEN=token \
|
||||||
|
--env GITEA_HOST=https://gitea.com \
|
||||||
|
-- go run git.lethalbits.com/lethalbits/gitea-mcp@latest -t stdio
|
||||||
|
```
|
||||||
|
|
||||||
|
### 在 VS Code 中使用
|
||||||
|
|
||||||
|
要快速安装,请使用本 README 顶部的安装按钮。
|
||||||
|
|
||||||
|
如需手动安装,请将以下 JSON 块添加到 VS Code 的用户设置 (JSON) 文件。可通过按 `Ctrl + Shift + P` 并输入 `Preferences: Open User Settings (JSON)`。
|
||||||
|
|
||||||
|
也可添加到工作区的 `.vscode/mcp.json` 文件,方便与他人共享配置。
|
||||||
|
|
||||||
|
> `.vscode/mcp.json` 文件不需要 `mcp` 键。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcp": {
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"type": "promptString",
|
||||||
|
"id": "gitea_token",
|
||||||
|
"description": "Gitea 个人访问令牌",
|
||||||
|
"password": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"servers": {
|
||||||
|
"gitea-mcp": {
|
||||||
|
"command": "docker",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"-i",
|
||||||
|
"--rm",
|
||||||
|
"-e",
|
||||||
|
"GITEA_ACCESS_TOKEN",
|
||||||
|
"docker.gitea.com/gitea-mcp-server"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
"GITEA_ACCESS_TOKEN": "${input:gitea_token}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📥 下载官方二进制版本
|
||||||
|
|
||||||
|
可在 [官方 Gitea MCP 二进制版本](https://git.lethalbits.com/lethalbits/gitea-mcp/releases) 下载。
|
||||||
|
|
||||||
|
### 🔧 从源码构建
|
||||||
|
|
||||||
|
可用 Git 下载源码:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://git.lethalbits.com/lethalbits/gitea-mcp.git
|
||||||
|
```
|
||||||
|
|
||||||
|
构建前请先安装:
|
||||||
|
|
||||||
|
- make
|
||||||
|
- Golang(建议 Go 1.24 及以上)
|
||||||
|
|
||||||
|
然后运行:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make install
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📁 加入 PATH
|
||||||
|
|
||||||
|
安装后,将 gitea-mcp 可执行文件复制到系统 PATH 目录,例如:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp gitea-mcp /usr/local/bin/
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🚀 使用
|
||||||
|
|
||||||
|
此示例适用于 Cursor,也可在 VSCode 使用插件。
|
||||||
|
要配置 Gitea MCP 服务器,请将以下内容添加到 MCP 配置文件:
|
||||||
|
|
||||||
|
- **stdio 模式**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"gitea": {
|
||||||
|
"command": "gitea-mcp",
|
||||||
|
"args": [
|
||||||
|
"-t",
|
||||||
|
"stdio",
|
||||||
|
"--host",
|
||||||
|
"https://gitea.com"
|
||||||
|
// "--token", "<your personal access token>"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
// "GITEA_HOST": "https://gitea.com",
|
||||||
|
// "GITEA_INSECURE": "true",
|
||||||
|
"GITEA_ACCESS_TOKEN": "<your personal access token>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- **http 模式**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"gitea": {
|
||||||
|
"url": "http://localhost:8080/mcp",
|
||||||
|
"headers": {
|
||||||
|
"Authorization": "Bearer <your personal access token>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**默认日志路径**: `$HOME/.gitea-mcp/gitea-mcp.log`
|
||||||
|
|
||||||
|
> [!注意]
|
||||||
|
> 可通过命令行参数或环境变量提供 Gitea 主机和访问令牌。
|
||||||
|
> 命令行参数优先。
|
||||||
|
|
||||||
|
一切设置完成后,可在 MCP 聊天框输入:
|
||||||
|
|
||||||
|
```text
|
||||||
|
列出我所有的仓库
|
||||||
|
```
|
||||||
|
|
||||||
|
## ✅ 可用工具
|
||||||
|
|
||||||
|
Gitea MCP 服务器支持以下工具:
|
||||||
|
|
||||||
|
| 工具 | 范围 | 描述 |
|
||||||
|
| :-------------------------------: | :------: | :------------------------: |
|
||||||
|
| get_my_user_info | 用户 | 获取已认证用户信息 |
|
||||||
|
| get_user_orgs | 用户 | 获取已认证用户关联组织 |
|
||||||
|
| create_repo | 仓库 | 创建新仓库 |
|
||||||
|
| fork_repo | 仓库 | 复刻仓库 |
|
||||||
|
| list_my_repos | 仓库 | 列出用户所有仓库 |
|
||||||
|
| create_branch | 分支 | 创建新分支 |
|
||||||
|
| delete_branch | 分支 | 删除分支 |
|
||||||
|
| list_branches | 分支 | 列出所有分支 |
|
||||||
|
| create_release | 版本发布 | 创建新版本发布 |
|
||||||
|
| delete_release | 版本发布 | 删除版本发布 |
|
||||||
|
| get_release | 版本发布 | 获取版本发布 |
|
||||||
|
| get_latest_release | 版本发布 | 获取最新版本发布 |
|
||||||
|
| list_releases | 版本发布 | 列出所有版本发布 |
|
||||||
|
| create_tag | 标签 | 创建新标签 |
|
||||||
|
| delete_tag | 标签 | 删除标签 |
|
||||||
|
| get_tag | 标签 | 获取标签 |
|
||||||
|
| list_tags | 标签 | 列出所有标签 |
|
||||||
|
| list_repo_commits | 提交 | 列出所有提交 |
|
||||||
|
| get_file_content | 文件 | 获取文件内容和元数据 |
|
||||||
|
| get_dir_content | 文件 | 获取目录内容列表 |
|
||||||
|
| create_file | 文件 | 创建新文件 |
|
||||||
|
| update_file | 文件 | 更新现有文件 |
|
||||||
|
| delete_file | 文件 | 删除文件 |
|
||||||
|
| get_issue_by_index | 问题 | 按索引获取问题 |
|
||||||
|
| list_repo_issues | 问题 | 列出所有问题 |
|
||||||
|
| create_issue | 问题 | 创建新问题 |
|
||||||
|
| create_issue_comment | 问题 | 在问题上创建评论 |
|
||||||
|
| edit_issue | 问题 | 编辑问题 |
|
||||||
|
| edit_issue_comment | 问题 | 编辑问题评论 |
|
||||||
|
| get_issue_comments_by_index | 问题 | 按索引获取问题评论 |
|
||||||
|
| get_pull_request_by_index | 拉取请求 | 按索引获取拉取请求 |
|
||||||
|
| list_repo_pull_requests | 拉取请求 | 列出所有拉取请求 |
|
||||||
|
| create_pull_request | 拉取请求 | 创建新拉取请求 |
|
||||||
|
| create_pull_request_reviewer | 拉取请求 | 为拉取请求添加审查者 |
|
||||||
|
| delete_pull_request_reviewer | 拉取请求 | 移除拉取请求的审查者 |
|
||||||
|
| list_pull_request_reviews | 拉取请求 | 列出拉取请求的所有审查 |
|
||||||
|
| get_pull_request_review | 拉取请求 | 按 ID 获取特定审查 |
|
||||||
|
| list_pull_request_review_comments | 拉取请求 | 列出审查的行内评论 |
|
||||||
|
| create_pull_request_review | 拉取请求 | 创建审查(可含行内评论) |
|
||||||
|
| submit_pull_request_review | 拉取请求 | 提交待处理的审查 |
|
||||||
|
| delete_pull_request_review | 拉取请求 | 删除审查 |
|
||||||
|
| dismiss_pull_request_review | 拉取请求 | 驳回审查(可附消息) |
|
||||||
|
| merge_pull_request | 拉取请求 | 合并拉取请求 |
|
||||||
|
| search_users | 用户 | 搜索用户 |
|
||||||
|
| search_org_teams | 组织 | 搜索组织团队 |
|
||||||
|
| list_org_labels | 组织 | 列出组织标签 |
|
||||||
|
| create_org_label | 组织 | 创建组织标签 |
|
||||||
|
| edit_org_label | 组织 | 编辑组织标签 |
|
||||||
|
| delete_org_label | 组织 | 删除组织标签 |
|
||||||
|
| search_repos | 仓库 | 搜索仓库 |
|
||||||
|
| get_gitea_mcp_server_version | 服务器 | 获取 Gitea MCP 服务器版本 |
|
||||||
|
| list_wiki_pages | Wiki | 列出所有 Wiki 页面 |
|
||||||
|
| get_wiki_page | Wiki | 获取 Wiki 页面内容和元数据 |
|
||||||
|
| get_wiki_revisions | Wiki | 获取 Wiki 修订历史 |
|
||||||
|
| create_wiki_page | Wiki | 创建新 Wiki 页面 |
|
||||||
|
| update_wiki_page | Wiki | 更新现有 Wiki 页面 |
|
||||||
|
| delete_wiki_page | Wiki | 删除 Wiki 页面 |
|
||||||
|
|
||||||
|
## 🐛 调试
|
||||||
|
|
||||||
|
启用调试模式时,请在 http 模式运行 Gitea MCP 服务器时加上 `-d` 标志:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./gitea-mcp -t http [--port 8080] --token <your personal access token> -d
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🛠 疑难排解
|
||||||
|
|
||||||
|
如遇问题,可参考以下步骤:
|
||||||
|
|
||||||
|
1. **检查 PATH**:确保 `gitea-mcp` 可执行文件已在系统 PATH 目录中。
|
||||||
|
2. **验证依赖**:确认已安装 `make` 和 `Golang` 等必要依赖。
|
||||||
|
3. **检查配置**:仔细检查 MCP 配置文件是否有错误或遗漏。
|
||||||
|
4. **查看日志**:检查日志消息或警告以获取更多信息。
|
||||||
|
|
||||||
|
享受通过聊天探索和管理您的 Gitea 仓库!
|
||||||
256
README.zh-tw.md
Normal file
256
README.zh-tw.md
Normal file
@@ -0,0 +1,256 @@
|
|||||||
|
# Gitea MCP 伺服器
|
||||||
|
|
||||||
|
[English](README.md) | [简体中文](README.zh-cn.md)
|
||||||
|
|
||||||
|
**Gitea MCP 伺服器** 是一個整合插件,旨在將 Gitea 與 Model Context Protocol (MCP) 系統連接起來。這允許通過 MCP 兼容的聊天界面無縫執行命令和管理倉庫。
|
||||||
|
|
||||||
|
[](https://insiders.vscode.dev/redirect/mcp/install?name=gitea&inputs=[{%22id%22:%22gitea_token%22,%22type%22:%22promptString%22,%22description%22:%22Gitea%20Personal%20Access%20Token%22,%22password%22:true}]&config={%22command%22:%22docker%22,%22args%22:[%22run%22,%22-i%22,%22--rm%22,%22-e%22,%22GITEA_ACCESS_TOKEN%22,%22docker.gitea.com/gitea-mcp-server%22],%22env%22:{%22GITEA_ACCESS_TOKEN%22:%22${input:gitea_token}%22}}) [](https://insiders.vscode.dev/redirect/mcp/install?name=gitea&inputs=[{%22id%22:%22gitea_token%22,%22type%22:%22promptString%22,%22description%22:%22Gitea%20Personal%20Access%20Token%22,%22password%22:true}]&config={%22command%22:%22docker%22,%22args%22:[%22run%22,%22-i%22,%22--rm%22,%22-e%22,%22GITEA_ACCESS_TOKEN%22,%22docker.gitea.com/gitea-mcp-server%22],%22env%22:{%22GITEA_ACCESS_TOKEN%22:%22${input:gitea_token}%22}}&quality=insiders)
|
||||||
|
|
||||||
|
## 目錄
|
||||||
|
|
||||||
|
- [Gitea MCP 伺服器](#gitea-mcp-伺服器)
|
||||||
|
- [目錄](#目錄)
|
||||||
|
- [什麼是 Gitea?](#什麼是-gitea)
|
||||||
|
- [什麼是 MCP?](#什麼是-mcp)
|
||||||
|
- [🚧 安裝](#-安裝)
|
||||||
|
- [在 Claude Code 中使用](#在-claude-code-中使用)
|
||||||
|
- [在 VS Code 中使用](#在-vs-code-中使用)
|
||||||
|
- [📥 下載官方二進位版本](#-下載官方二進位版本)
|
||||||
|
- [🔧 從原始碼建置](#-從原始碼建置)
|
||||||
|
- [📁 加入 PATH](#-加入-path)
|
||||||
|
- [🚀 使用](#-使用)
|
||||||
|
- [✅ 可用工具](#-可用工具)
|
||||||
|
- [🐛 調試](#-調試)
|
||||||
|
- [🛠 疑難排解](#-疑難排解)
|
||||||
|
|
||||||
|
## 什麼是 Gitea?
|
||||||
|
|
||||||
|
Gitea 是一個由社群管理的輕量級程式碼託管解決方案,使用 Go 語言編寫,採用 MIT 授權。Gitea 提供 Git 託管,包括倉庫瀏覽、議題追蹤、拉取請求等功能。
|
||||||
|
|
||||||
|
## 什麼是 MCP?
|
||||||
|
|
||||||
|
Model Context Protocol (MCP) 是一種協議,允許透過聊天介面整合各種工具與系統。它能夠無縫執行命令並管理倉庫、使用者及其他資源。
|
||||||
|
|
||||||
|
## 🚧 安裝
|
||||||
|
|
||||||
|
### 在 Claude Code 中使用
|
||||||
|
|
||||||
|
此方式使用 `go run`,需要安裝 [Go](https://go.dev)。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add --transport stdio --scope user gitea \
|
||||||
|
--env GITEA_ACCESS_TOKEN=token \
|
||||||
|
--env GITEA_HOST=https://gitea.com \
|
||||||
|
-- go run git.lethalbits.com/lethalbits/gitea-mcp@latest -t stdio
|
||||||
|
```
|
||||||
|
|
||||||
|
### 在 VS Code 中使用
|
||||||
|
|
||||||
|
欲快速安裝,請使用本 README 頂部的安裝按鈕。
|
||||||
|
|
||||||
|
如需手動安裝,請將下列 JSON 區塊加入 VS Code 的使用者設定 (JSON) 檔案。可按 `Ctrl + Shift + P` 並輸入 `Preferences: Open User Settings (JSON)`。
|
||||||
|
|
||||||
|
也可加入至工作區的 `.vscode/mcp.json` 檔案,方便與他人共享設定。
|
||||||
|
|
||||||
|
> `.vscode/mcp.json` 檔案不需 `mcp` 鍵。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcp": {
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"type": "promptString",
|
||||||
|
"id": "gitea_token",
|
||||||
|
"description": "Gitea 個人存取令牌",
|
||||||
|
"password": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"servers": {
|
||||||
|
"gitea-mcp": {
|
||||||
|
"command": "docker",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"-i",
|
||||||
|
"--rm",
|
||||||
|
"-e",
|
||||||
|
"GITEA_ACCESS_TOKEN",
|
||||||
|
"docker.gitea.com/gitea-mcp-server"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
"GITEA_ACCESS_TOKEN": "${input:gitea_token}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📥 下載官方二進位版本
|
||||||
|
|
||||||
|
可至 [官方 Gitea MCP 二進位版本](https://git.lethalbits.com/lethalbits/gitea-mcp/releases) 下載。
|
||||||
|
|
||||||
|
### 🔧 從原始碼建置
|
||||||
|
|
||||||
|
可用 Git 下載原始碼:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://git.lethalbits.com/lethalbits/gitea-mcp.git
|
||||||
|
```
|
||||||
|
|
||||||
|
建置前請先安裝:
|
||||||
|
|
||||||
|
- make
|
||||||
|
- Golang(建議 Go 1.24 以上)
|
||||||
|
|
||||||
|
然後執行:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make install
|
||||||
|
```
|
||||||
|
|
||||||
|
### 📁 加入 PATH
|
||||||
|
|
||||||
|
安裝後,將 gitea-mcp 執行檔複製到系統 PATH 目錄,例如:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp gitea-mcp /usr/local/bin/
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🚀 使用
|
||||||
|
|
||||||
|
此範例適用於 Cursor,也可在 VSCode 使用插件。
|
||||||
|
欲設定 Gitea MCP 伺服器,請將下列內容加入 MCP 設定檔:
|
||||||
|
|
||||||
|
- **stdio 模式**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"gitea": {
|
||||||
|
"command": "gitea-mcp",
|
||||||
|
"args": [
|
||||||
|
"-t",
|
||||||
|
"stdio",
|
||||||
|
"--host",
|
||||||
|
"https://gitea.com"
|
||||||
|
// "--token", "<your personal access token>"
|
||||||
|
],
|
||||||
|
"env": {
|
||||||
|
// "GITEA_HOST": "https://gitea.com",
|
||||||
|
// "GITEA_INSECURE": "true",
|
||||||
|
"GITEA_ACCESS_TOKEN": "<your personal access token>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- **http 模式**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"gitea": {
|
||||||
|
"url": "http://localhost:8080/mcp",
|
||||||
|
"headers": {
|
||||||
|
"Authorization": "Bearer <your personal access token>"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**預設日誌路徑**: `$HOME/.gitea-mcp/gitea-mcp.log`
|
||||||
|
|
||||||
|
> [!注意]
|
||||||
|
> 可用命令列參數或環境變數提供 Gitea 主機與存取令牌。
|
||||||
|
> 命令列參數優先。
|
||||||
|
|
||||||
|
一切設定完成後,可在 MCP 聊天框輸入:
|
||||||
|
|
||||||
|
```text
|
||||||
|
列出我所有的倉庫
|
||||||
|
```
|
||||||
|
|
||||||
|
## ✅ 可用工具
|
||||||
|
|
||||||
|
Gitea MCP 伺服器支援以下工具:
|
||||||
|
|
||||||
|
| 工具 | 範圍 | 描述 |
|
||||||
|
| :-------------------------------: | :------: | :--------------------------: |
|
||||||
|
| get_my_user_info | 用戶 | 取得已認證用戶資訊 |
|
||||||
|
| get_user_orgs | 用戶 | 取得已認證用戶所屬組織 |
|
||||||
|
| create_repo | 倉庫 | 創建新倉庫 |
|
||||||
|
| fork_repo | 倉庫 | 復刻倉庫 |
|
||||||
|
| list_my_repos | 倉庫 | 列出用戶所有倉庫 |
|
||||||
|
| create_branch | 分支 | 創建新分支 |
|
||||||
|
| delete_branch | 分支 | 刪除分支 |
|
||||||
|
| list_branches | 分支 | 列出所有分支 |
|
||||||
|
| create_release | 版本發布 | 創建新版本發布 |
|
||||||
|
| delete_release | 版本發布 | 刪除版本發布 |
|
||||||
|
| get_release | 版本發布 | 取得版本發布 |
|
||||||
|
| get_latest_release | 版本發布 | 取得最新版本發布 |
|
||||||
|
| list_releases | 版本發布 | 列出所有版本發布 |
|
||||||
|
| create_tag | 標籤 | 創建新標籤 |
|
||||||
|
| delete_tag | 標籤 | 刪除標籤 |
|
||||||
|
| get_tag | 標籤 | 取得標籤 |
|
||||||
|
| list_tags | 標籤 | 列出所有標籤 |
|
||||||
|
| list_repo_commits | 提交 | 列出所有提交 |
|
||||||
|
| get_file_content | 文件 | 取得文件內容與中繼資料 |
|
||||||
|
| get_dir_content | 文件 | 取得目錄內容列表 |
|
||||||
|
| create_file | 文件 | 創建新文件 |
|
||||||
|
| update_file | 文件 | 更新現有文件 |
|
||||||
|
| delete_file | 文件 | 刪除文件 |
|
||||||
|
| get_issue_by_index | 問題 | 依索引取得問題 |
|
||||||
|
| list_repo_issues | 問題 | 列出所有問題 |
|
||||||
|
| create_issue | 問題 | 創建新問題 |
|
||||||
|
| create_issue_comment | 問題 | 在問題上創建評論 |
|
||||||
|
| edit_issue | 問題 | 編輯問題 |
|
||||||
|
| edit_issue_comment | 問題 | 編輯問題評論 |
|
||||||
|
| get_issue_comments_by_index | 問題 | 依索引取得問題評論 |
|
||||||
|
| get_pull_request_by_index | 拉取請求 | 依索引取得拉取請求 |
|
||||||
|
| list_repo_pull_requests | 拉取請求 | 列出所有拉取請求 |
|
||||||
|
| create_pull_request | 拉取請求 | 創建新拉取請求 |
|
||||||
|
| create_pull_request_reviewer | 拉取請求 | 為拉取請求添加審查者 |
|
||||||
|
| delete_pull_request_reviewer | 拉取請求 | 移除拉取請求的審查者 |
|
||||||
|
| list_pull_request_reviews | 拉取請求 | 列出拉取請求的所有審查 |
|
||||||
|
| get_pull_request_review | 拉取請求 | 依 ID 取得特定審查 |
|
||||||
|
| list_pull_request_review_comments | 拉取請求 | 列出審查的行內評論 |
|
||||||
|
| create_pull_request_review | 拉取請求 | 創建審查(可含行內評論) |
|
||||||
|
| submit_pull_request_review | 拉取請求 | 提交待處理的審查 |
|
||||||
|
| delete_pull_request_review | 拉取請求 | 刪除審查 |
|
||||||
|
| dismiss_pull_request_review | 拉取請求 | 駁回審查(可附訊息) |
|
||||||
|
| merge_pull_request | 拉取請求 | 合併拉取請求 |
|
||||||
|
| search_users | 用戶 | 搜尋用戶 |
|
||||||
|
| search_org_teams | 組織 | 搜尋組織團隊 |
|
||||||
|
| list_org_labels | 組織 | 列出組織標籤 |
|
||||||
|
| create_org_label | 組織 | 創建組織標籤 |
|
||||||
|
| edit_org_label | 組織 | 編輯組織標籤 |
|
||||||
|
| delete_org_label | 組織 | 刪除組織標籤 |
|
||||||
|
| search_repos | 倉庫 | 搜尋倉庫 |
|
||||||
|
| get_gitea_mcp_server_version | 伺服器 | 取得 Gitea MCP 伺服器版本 |
|
||||||
|
| list_wiki_pages | Wiki | 列出所有 Wiki 頁面 |
|
||||||
|
| get_wiki_page | Wiki | 取得 Wiki 頁面內容與中繼資料 |
|
||||||
|
| get_wiki_revisions | Wiki | 取得 Wiki 修訂歷史 |
|
||||||
|
| create_wiki_page | Wiki | 創建新 Wiki 頁面 |
|
||||||
|
| update_wiki_page | Wiki | 更新現有 Wiki 頁面 |
|
||||||
|
| delete_wiki_page | Wiki | 刪除 Wiki 頁面 |
|
||||||
|
|
||||||
|
## 🐛 調試
|
||||||
|
|
||||||
|
啟用調試模式時,請在 http 模式執行 Gitea MCP 伺服器時加上 `-d` 旗標:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./gitea-mcp -t http [--port 8080] --token <your personal access token> -d
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🛠 疑難排解
|
||||||
|
|
||||||
|
如遇問題,可參考以下步驟:
|
||||||
|
|
||||||
|
1. **檢查 PATH**:確保 `gitea-mcp` 執行檔已在系統 PATH 目錄中。
|
||||||
|
2. **驗證依賴**:確認已安裝 `make` 與 `Golang` 等必要依賴。
|
||||||
|
3. **檢查設定**:仔細檢查 MCP 設定檔是否有錯誤或遺漏。
|
||||||
|
4. **查看日誌**:檢查日誌訊息或警告以獲取更多資訊。
|
||||||
|
|
||||||
|
享受透過聊天探索與管理您的 Gitea 倉庫!
|
||||||
2
build.bat
Normal file
2
build.bat
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
@echo off
|
||||||
|
powershell -ExecutionPolicy Bypass -File "%~dp0build.ps1" %*
|
||||||
220
build.ps1
Normal file
220
build.ps1
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
#!/usr/bin/env pwsh
|
||||||
|
|
||||||
|
# PowerShell build script for gitea-mcp
|
||||||
|
# Replicates the functionality of the Makefile
|
||||||
|
|
||||||
|
param(
|
||||||
|
[string]$Target = "help"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
$EXECUTABLE = "gitea-mcp.exe"
|
||||||
|
$VERSION = & git describe --tags --always 2>$null | ForEach-Object { $_ -replace '-', '+' -replace '^v', '' }
|
||||||
|
if (-not $VERSION) { $VERSION = "dev" }
|
||||||
|
$LDFLAGS = "-X `"main.Version=$VERSION`""
|
||||||
|
|
||||||
|
# Colors for output (Windows PowerShell compatible)
|
||||||
|
$CYAN = "Cyan"
|
||||||
|
$RESET = "White"
|
||||||
|
|
||||||
|
function Write-Header {
|
||||||
|
param([string]$Message)
|
||||||
|
Write-Host "=== $Message ===" -ForegroundColor Green
|
||||||
|
}
|
||||||
|
|
||||||
|
function Write-Info {
|
||||||
|
param([string]$Message)
|
||||||
|
Write-Host $Message -ForegroundColor Yellow
|
||||||
|
}
|
||||||
|
|
||||||
|
function Write-Success {
|
||||||
|
param([string]$Message)
|
||||||
|
Write-Host $Message -ForegroundColor Green
|
||||||
|
}
|
||||||
|
|
||||||
|
function Write-Error {
|
||||||
|
param([string]$Message)
|
||||||
|
Write-Host $Message -ForegroundColor Red
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-Help {
|
||||||
|
Write-Host "Usage: .\build.ps1 [target]" -ForegroundColor Green
|
||||||
|
Write-Host ""
|
||||||
|
Write-Host "Targets:" -ForegroundColor Green
|
||||||
|
Write-Host ""
|
||||||
|
|
||||||
|
Write-Host ("{0,-30}" -f "help") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Print this help message."
|
||||||
|
Write-Host ("{0,-30}" -f "build") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Build the application."
|
||||||
|
Write-Host ("{0,-30}" -f "install") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Install the application."
|
||||||
|
Write-Host ("{0,-30}" -f "uninstall") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Uninstall the application."
|
||||||
|
Write-Host ("{0,-30}" -f "clean") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Clean the build artifacts."
|
||||||
|
Write-Host ("{0,-30}" -f "air") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Install air for hot reload."
|
||||||
|
Write-Host ("{0,-30}" -f "dev") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Run the application with hot reload."
|
||||||
|
Write-Host ("{0,-30}" -f "vendor") -ForegroundColor Cyan -NoNewline
|
||||||
|
Write-Host " Tidy and verify module dependencies."
|
||||||
|
}
|
||||||
|
|
||||||
|
function Build-App {
|
||||||
|
Write-Header "Building application"
|
||||||
|
|
||||||
|
$ldflags = "-s -w $LDFLAGS"
|
||||||
|
Write-Info "go build -v -ldflags '$ldflags' -o $EXECUTABLE"
|
||||||
|
|
||||||
|
try {
|
||||||
|
& go build -v -ldflags $ldflags -o $EXECUTABLE
|
||||||
|
if ($LASTEXITCODE -eq 0) {
|
||||||
|
Write-Success "Build successful: $EXECUTABLE"
|
||||||
|
} else {
|
||||||
|
Write-Error "Build failed with exit code: $LASTEXITCODE"
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Write-Error "Build failed: $_"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Install-App {
|
||||||
|
Write-Header "Installing application"
|
||||||
|
|
||||||
|
# First build the application
|
||||||
|
Build-App
|
||||||
|
|
||||||
|
$GOPATH = $env:GOPATH
|
||||||
|
if (-not $GOPATH) {
|
||||||
|
$GOPATH = Join-Path $env:USERPROFILE "go"
|
||||||
|
}
|
||||||
|
|
||||||
|
$installDir = Join-Path $GOPATH "bin"
|
||||||
|
$installPath = Join-Path $installDir $EXECUTABLE
|
||||||
|
|
||||||
|
Write-Info "Installing $EXECUTABLE to $installPath"
|
||||||
|
|
||||||
|
# Create directory if it doesn't exist
|
||||||
|
if (-not (Test-Path $installDir)) {
|
||||||
|
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
|
||||||
|
}
|
||||||
|
|
||||||
|
# Copy the executable
|
||||||
|
if (Test-Path $EXECUTABLE) {
|
||||||
|
Copy-Item $EXECUTABLE $installPath -Force
|
||||||
|
Write-Success "Installed $EXECUTABLE to $installPath"
|
||||||
|
Write-Info "Please add $installDir to your PATH if it is not already there."
|
||||||
|
} else {
|
||||||
|
Write-Error "Executable not found. Please build first."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Uninstall-App {
|
||||||
|
Write-Header "Uninstalling application"
|
||||||
|
|
||||||
|
$GOPATH = $env:GOPATH
|
||||||
|
if (-not $GOPATH) {
|
||||||
|
$GOPATH = Join-Path $env:USERPROFILE "go"
|
||||||
|
}
|
||||||
|
|
||||||
|
$installPath = Join-Path $GOPATH "bin" $EXECUTABLE
|
||||||
|
|
||||||
|
Write-Info "Uninstalling $EXECUTABLE from $installPath"
|
||||||
|
|
||||||
|
if (Test-Path $installPath) {
|
||||||
|
Remove-Item $installPath -Force
|
||||||
|
Write-Success "Uninstalled $EXECUTABLE from $installPath"
|
||||||
|
} else {
|
||||||
|
Write-Warning "$EXECUTABLE not found at $installPath"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Clean-Build {
|
||||||
|
Write-Header "Cleaning build artifacts"
|
||||||
|
|
||||||
|
Write-Info "Cleaning up $EXECUTABLE"
|
||||||
|
|
||||||
|
if (Test-Path $EXECUTABLE) {
|
||||||
|
Remove-Item $EXECUTABLE -Force
|
||||||
|
Write-Success "Cleaned up $EXECUTABLE"
|
||||||
|
} else {
|
||||||
|
Write-Warning "$EXECUTABLE not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Install-Air {
|
||||||
|
Write-Header "Installing air for hot reload"
|
||||||
|
|
||||||
|
# Check if air is already installed
|
||||||
|
$airPath = Get-Command air -ErrorAction SilentlyContinue
|
||||||
|
if ($airPath) {
|
||||||
|
Write-Success "air is already installed"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Info "Installing github.com/air-verse/air@latest"
|
||||||
|
try {
|
||||||
|
& go install github.com/air-verse/air@latest
|
||||||
|
if ($LASTEXITCODE -eq 0) {
|
||||||
|
Write-Success "air installed successfully"
|
||||||
|
} else {
|
||||||
|
Write-Error "Failed to install air"
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Write-Error "Failed to install air: $_"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Start-Dev {
|
||||||
|
Write-Header "Starting development mode with hot reload"
|
||||||
|
|
||||||
|
# Install air first
|
||||||
|
Install-Air
|
||||||
|
|
||||||
|
Write-Info "Starting air with build configuration"
|
||||||
|
& air --build.cmd "go build -o $EXECUTABLE" --build.bin "./$EXECUTABLE"
|
||||||
|
}
|
||||||
|
|
||||||
|
function Update-Vendor {
|
||||||
|
Write-Header "Tidying and verifying module dependencies"
|
||||||
|
|
||||||
|
Write-Info "Running go mod tidy"
|
||||||
|
& go mod tidy
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Error "go mod tidy failed"
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Info "Running go mod verify"
|
||||||
|
& go mod verify
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Error "go mod verify failed"
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Success "Dependencies updated successfully"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main execution logic
|
||||||
|
switch ($Target.ToLower()) {
|
||||||
|
"help" { Get-Help }
|
||||||
|
"build" { Build-App }
|
||||||
|
"install" { Install-App }
|
||||||
|
"uninstall" { Uninstall-App }
|
||||||
|
"clean" { Clean-Build }
|
||||||
|
"air" { Install-Air }
|
||||||
|
"dev" { Start-Dev }
|
||||||
|
"vendor" { Update-Vendor }
|
||||||
|
default {
|
||||||
|
Write-Error "Unknown target: $Target"
|
||||||
|
Write-Host ""
|
||||||
|
Get-Help
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
127
cmd/cmd.go
127
cmd/cmd.go
@@ -3,72 +3,67 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"text/tabwriter"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/operation"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation"
|
||||||
flagPkg "gitea.com/gitea/gitea-mcp/pkg/flag"
|
flagPkg "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
transport string
|
|
||||||
host string
|
host string
|
||||||
port int
|
port int
|
||||||
token string
|
token string
|
||||||
|
version bool
|
||||||
debug bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(
|
flag.StringVar(&flagPkg.Mode, "t", "stdio", "")
|
||||||
&transport,
|
flag.StringVar(&flagPkg.Mode, "transport", "stdio", "")
|
||||||
"t",
|
flag.StringVar(&host, "H", os.Getenv("GITEA_HOST"), "")
|
||||||
"stdio",
|
flag.StringVar(&host, "host", os.Getenv("GITEA_HOST"), "")
|
||||||
"Transport type (stdio or sse)",
|
flag.IntVar(&port, "p", 8080, "")
|
||||||
)
|
flag.IntVar(&port, "port", 8080, "")
|
||||||
flag.StringVar(
|
flag.StringVar(&token, "T", "", "")
|
||||||
&transport,
|
flag.StringVar(&token, "token", "", "")
|
||||||
"transport",
|
flag.BoolVar(&flagPkg.ReadOnly, "r", false, "")
|
||||||
"stdio",
|
flag.BoolVar(&flagPkg.ReadOnly, "read-only", false, "")
|
||||||
"Transport type (stdio or sse)",
|
flag.BoolVar(&flagPkg.Debug, "d", false, "")
|
||||||
)
|
flag.BoolVar(&flagPkg.Debug, "debug", false, "")
|
||||||
flag.StringVar(
|
flag.BoolVar(&flagPkg.Insecure, "k", false, "")
|
||||||
&host,
|
flag.BoolVar(&flagPkg.Insecure, "insecure", false, "")
|
||||||
"host",
|
flag.BoolVar(&version, "v", false, "")
|
||||||
"https://gitea.com",
|
flag.BoolVar(&version, "version", false, "")
|
||||||
"Gitea host",
|
|
||||||
)
|
flag.Usage = func() {
|
||||||
flag.IntVar(
|
w := tabwriter.NewWriter(os.Stderr, 0, 0, 3, ' ', 0)
|
||||||
&port,
|
fmt.Fprintln(os.Stderr, "Usage: gitea-mcp-extended [options]")
|
||||||
"port",
|
fmt.Fprintln(os.Stderr)
|
||||||
8080,
|
fmt.Fprintln(os.Stderr, "Options:")
|
||||||
"sse port",
|
fmt.Fprintf(w, " -t, -transport <type>\tTransport type: stdio or http (default: stdio)\n")
|
||||||
)
|
fmt.Fprintf(w, " -H, -host <url>\tGitea host URL (default: https://gitea.com)\n")
|
||||||
flag.StringVar(
|
fmt.Fprintf(w, " -p, -port <number>\tHTTP server port (default: 8080)\n")
|
||||||
&token,
|
fmt.Fprintf(w, " -T, -token <token>\tPersonal access token\n")
|
||||||
"token",
|
fmt.Fprintf(w, " -r, -read-only\tExpose only read-only tools\n")
|
||||||
"",
|
fmt.Fprintf(w, " -d, -debug\tEnable debug mode\n")
|
||||||
"Your personal access token",
|
fmt.Fprintf(w, " -k, -insecure\tIgnore TLS certificate errors\n")
|
||||||
)
|
fmt.Fprintf(w, " -v, -version\tPrint version and exit\n")
|
||||||
flag.BoolVar(
|
fmt.Fprintln(w)
|
||||||
&debug,
|
fmt.Fprintln(w, "Environment variables:")
|
||||||
"d",
|
fmt.Fprintf(w, " GITEA_ACCESS_TOKEN\tProvide access token\n")
|
||||||
true,
|
fmt.Fprintf(w, " GITEA_DEBUG\tSet to 'true' for debug mode\n")
|
||||||
"debug mode",
|
fmt.Fprintf(w, " GITEA_HOST\tOverride Gitea host URL\n")
|
||||||
)
|
fmt.Fprintf(w, " GITEA_INSECURE\tSet to 'true' to ignore TLS errors\n")
|
||||||
flag.BoolVar(
|
fmt.Fprintf(w, " GITEA_READONLY\tSet to 'true' for read-only mode\n")
|
||||||
&debug,
|
fmt.Fprintf(w, " MCP_MODE\tOverride transport mode\n")
|
||||||
"debug",
|
w.Flush()
|
||||||
true,
|
}
|
||||||
"debug mode",
|
|
||||||
)
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
flagPkg.Host = host
|
flagPkg.Host = host
|
||||||
if flagPkg.Host == "" {
|
|
||||||
flagPkg.Host = os.Getenv("GITEA_HOST")
|
|
||||||
}
|
|
||||||
if flagPkg.Host == "" {
|
if flagPkg.Host == "" {
|
||||||
flagPkg.Host = "https://gitea.com"
|
flagPkg.Host = "https://gitea.com"
|
||||||
}
|
}
|
||||||
@@ -80,21 +75,35 @@ func init() {
|
|||||||
flagPkg.Token = os.Getenv("GITEA_ACCESS_TOKEN")
|
flagPkg.Token = os.Getenv("GITEA_ACCESS_TOKEN")
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug {
|
if os.Getenv("MCP_MODE") != "" {
|
||||||
flagPkg.Debug = debug
|
flagPkg.Mode = os.Getenv("MCP_MODE")
|
||||||
}
|
}
|
||||||
if !debug {
|
|
||||||
flagPkg.Debug = os.Getenv("GITEA_DEBUG") == "true"
|
if os.Getenv("GITEA_READONLY") == "true" {
|
||||||
|
flagPkg.ReadOnly = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("GITEA_DEBUG") == "true" {
|
||||||
|
flagPkg.Debug = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set insecure mode based on environment variable
|
||||||
|
if os.Getenv("GITEA_INSECURE") == "true" {
|
||||||
|
flagPkg.Insecure = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(version string) {
|
func Execute() {
|
||||||
defer log.Default().Sync()
|
if version {
|
||||||
if err := operation.Run(transport, version); err != nil {
|
fmt.Fprintln(os.Stdout, flagPkg.Version)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer log.Default().Sync() //nolint:errcheck // best-effort flush
|
||||||
|
if err := operation.Run(); err != nil {
|
||||||
if err == context.Canceled {
|
if err == context.Canceled {
|
||||||
log.Info("Server shutdown due to context cancellation")
|
log.Info("Server shutdown due to context cancellation")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Fatalf("Run Gitea MCP Server Error: %v", err)
|
log.Fatalf("Run Gitea MCP Server Error: %v", err) //nolint:gocritic // intentional exit after defer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
config.json
10
config.json
@@ -2,11 +2,11 @@
|
|||||||
"mcpServers": {
|
"mcpServers": {
|
||||||
"gitea": {
|
"gitea": {
|
||||||
"command": "gitea-mcp",
|
"command": "gitea-mcp",
|
||||||
"args": {
|
"args": [
|
||||||
"-t": "stdio",
|
"-t", "stdio",
|
||||||
"--host": "https://gitea.com",
|
"--host", "https://gitea.com",
|
||||||
"--token": "<your personal access token>"
|
"--token", "<your personal access token>"
|
||||||
},
|
]
|
||||||
"env": {
|
"env": {
|
||||||
"GITEA_HOST": "https://gitea.com",
|
"GITEA_HOST": "https://gitea.com",
|
||||||
"GITEA_ACCESS_TOKEN": "<your personal access token>"
|
"GITEA_ACCESS_TOKEN": "<your personal access token>"
|
||||||
|
|||||||
26
go.mod
26
go.mod
@@ -1,21 +1,29 @@
|
|||||||
module gitea.com/gitea/gitea-mcp
|
module git.lethalbits.com/lethalbits/gitea-mcp-extended
|
||||||
|
|
||||||
go 1.24.0
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.gitea.io/sdk/gitea v0.20.0
|
code.gitea.io/sdk/gitea v0.23.2
|
||||||
github.com/mark3labs/mcp-go v0.17.0
|
github.com/mark3labs/mcp-go v0.44.0
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.1
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/42wim/httpsig v1.2.2 // indirect
|
github.com/42wim/httpsig v1.2.3 // indirect
|
||||||
|
github.com/bahlo/generic-list-go v0.2.0 // indirect
|
||||||
|
github.com/buger/jsonparser v1.1.1 // indirect
|
||||||
github.com/davidmz/go-pageant v1.0.2 // indirect
|
github.com/davidmz/go-pageant v1.0.2 // indirect
|
||||||
github.com/go-fed/httpsig v1.1.0 // indirect
|
github.com/go-fed/httpsig v1.1.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/hashicorp/go-version v1.7.0 // indirect
|
github.com/hashicorp/go-version v1.8.0 // indirect
|
||||||
|
github.com/invopop/jsonschema v0.13.0 // indirect
|
||||||
|
github.com/mailru/easyjson v0.9.1 // indirect
|
||||||
|
github.com/spf13/cast v1.10.0 // indirect
|
||||||
|
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
|
||||||
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
golang.org/x/crypto v0.36.0 // indirect
|
golang.org/x/crypto v0.48.0 // indirect
|
||||||
golang.org/x/sys v0.31.0 // indirect
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
64
go.sum
64
go.sum
@@ -1,50 +1,74 @@
|
|||||||
code.gitea.io/sdk/gitea v0.20.0 h1:Zm/QDwwZK1awoM4AxdjeAQbxolzx2rIP8dDfmKu+KoU=
|
code.gitea.io/sdk/gitea v0.23.2 h1:iJB1FDmLegwfwjX8gotBDHdPSbk/ZR8V9VmEJaVsJYg=
|
||||||
code.gitea.io/sdk/gitea v0.20.0/go.mod h1:faouBHC/zyx5wLgjmRKR62ydyvMzwWf3QnU0bH7Cw6U=
|
code.gitea.io/sdk/gitea v0.23.2/go.mod h1:yyF5+GhljqvA30sRDreoyHILruNiy4ASufugzYg0VHM=
|
||||||
github.com/42wim/httpsig v1.2.2 h1:ofAYoHUNs/MJOLqQ8hIxeyz2QxOz8qdSVvp3PX/oPgA=
|
github.com/42wim/httpsig v1.2.3 h1:xb0YyWhkYj57SPtfSttIobJUPJZB9as1nsfo7KWVcEs=
|
||||||
github.com/42wim/httpsig v1.2.2/go.mod h1:P/UYo7ytNBFwc+dg35IubuAUIs8zj5zzFIgUCEl55WY=
|
github.com/42wim/httpsig v1.2.3/go.mod h1:nZq9OlYKDrUBhptd77IHx4/sZZD+IxTBADvAPI9G/EM=
|
||||||
|
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
|
||||||
|
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
|
||||||
|
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
|
||||||
|
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0=
|
github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0=
|
||||||
github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE=
|
github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE=
|
||||||
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
|
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||||
github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=
|
github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=
|
||||||
github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM=
|
github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM=
|
||||||
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
|
github.com/hashicorp/go-version v1.8.0 h1:KAkNb1HAiZd1ukkxDFGmokVZe1Xy9HG6NUp+bPle2i4=
|
||||||
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
github.com/hashicorp/go-version v1.8.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||||
github.com/mark3labs/mcp-go v0.15.0 h1:lViiC4dk6chJHZccezaTzZLMOQVUXJDGNQPtzExr5NQ=
|
github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E=
|
||||||
github.com/mark3labs/mcp-go v0.15.0/go.mod h1:xBB350hekQsJAK7gJAii8bcEoWemboLm2mRm5/+KBaU=
|
github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
|
||||||
github.com/mark3labs/mcp-go v0.17.0 h1:5Ps6T7qXr7De/2QTqs9h6BKeZ/qdeUeGrgM5lPzi930=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/mark3labs/mcp-go v0.17.0/go.mod h1:KmJndYv7GIgcPVwEKJjNcbhVQ+hJGJhrCCB/9xITzpE=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8=
|
||||||
|
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
||||||
|
github.com/mark3labs/mcp-go v0.44.0 h1:OlYfcVviAnwNN40QZUrrzU0QZjq3En7rCU5X09a/B7I=
|
||||||
|
github.com/mark3labs/mcp-go v0.44.0/go.mod h1:YnJfOL382MIWDx1kMY+2zsRHU/q78dBg9aFb8W6Thdw=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
|
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||||
|
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
|
||||||
|
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
|
||||||
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
|
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
|
||||||
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
|
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
|
||||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||||
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||||
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
|
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
|
golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg=
|
||||||
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
|
golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
13
main.go
13
main.go
@@ -1,13 +1,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gitea.com/gitea/gitea-mcp/cmd"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/cmd"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var Version = "dev"
|
||||||
Version = "dev"
|
|
||||||
)
|
func init() {
|
||||||
|
flag.Version = Version
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute(Version)
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|||||||
8
operation/actions/actions.go
Normal file
8
operation/actions/actions.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tool is the registry for all Actions-related MCP tools.
|
||||||
|
var Tool = tool.New()
|
||||||
187
operation/actions/logs.go
Normal file
187
operation/actions/logs.go
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetRepoActionJobLogPreviewToolName = "get_repo_action_job_log_preview"
|
||||||
|
DownloadRepoActionJobLogToolName = "download_repo_action_job_log"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GetRepoActionJobLogPreviewTool = mcp.NewTool(
|
||||||
|
GetRepoActionJobLogPreviewToolName,
|
||||||
|
mcp.WithDescription("Get a repository Actions job log preview (tail/limited for chat-friendly output)"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("job_id", mcp.Required(), mcp.Description("job ID")),
|
||||||
|
mcp.WithNumber("tail_lines", mcp.Description("number of lines from the end of the log"), mcp.DefaultNumber(200), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("max_bytes", mcp.Description("max bytes to return"), mcp.DefaultNumber(65536), mcp.Min(1024)),
|
||||||
|
)
|
||||||
|
|
||||||
|
DownloadRepoActionJobLogTool = mcp.NewTool(
|
||||||
|
DownloadRepoActionJobLogToolName,
|
||||||
|
mcp.WithDescription("Download a repository Actions job log to a file on the MCP server filesystem"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("job_id", mcp.Required(), mcp.Description("job ID")),
|
||||||
|
mcp.WithString("output_path", mcp.Description("optional output file path; if omitted, uses ~/.gitea-mcp/artifacts/actions-logs/...")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoActionJobLogPreviewTool, Handler: GetRepoActionJobLogPreviewFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: DownloadRepoActionJobLogTool, Handler: DownloadRepoActionJobLogFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func logPaths(owner, repo string, jobID int64) []string {
|
||||||
|
// Primary candidate endpoints, plus a few commonly-seen variants across versions.
|
||||||
|
// We try these in order; 404/405 falls through.
|
||||||
|
return []string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/jobs/%d/logs", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/jobs/%d/log", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/tasks/%d/log", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/task/%d/log", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchJobLogBytes(ctx context.Context, owner, repo string, jobID int64) ([]byte, string, error) {
|
||||||
|
var lastErr error
|
||||||
|
for _, p := range logPaths(owner, repo, jobID) {
|
||||||
|
b, _, err := gitea.DoBytes(ctx, "GET", p, nil, nil, "text/plain")
|
||||||
|
if err == nil {
|
||||||
|
return b, p, nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
var httpErr *gitea.HTTPError
|
||||||
|
if errors.As(err, &httpErr) && (httpErr.StatusCode == http.StatusNotFound || httpErr.StatusCode == http.StatusMethodNotAllowed) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, p, err
|
||||||
|
}
|
||||||
|
return nil, "", lastErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func tailByLines(data []byte, tailLines int) []byte {
|
||||||
|
if tailLines <= 0 || len(data) == 0 {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
// Find the start index of the last N lines by scanning backwards.
|
||||||
|
lines := 0
|
||||||
|
i := len(data) - 1
|
||||||
|
for i >= 0 {
|
||||||
|
if data[i] == '\n' {
|
||||||
|
lines++
|
||||||
|
if lines > tailLines {
|
||||||
|
return data[i+1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func limitBytes(data []byte, maxBytes int) ([]byte, bool) {
|
||||||
|
if maxBytes <= 0 {
|
||||||
|
return data, false
|
||||||
|
}
|
||||||
|
if len(data) <= maxBytes {
|
||||||
|
return data, false
|
||||||
|
}
|
||||||
|
// Keep the tail so the most recent log content is preserved.
|
||||||
|
return data[len(data)-maxBytes:], true
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoActionJobLogPreviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoActionJobLogPreviewFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
jobID, err := params.GetIndex(req.GetArguments(), "job_id")
|
||||||
|
if err != nil || jobID <= 0 {
|
||||||
|
return to.ErrorResult(errors.New("job_id is required"))
|
||||||
|
}
|
||||||
|
tailLines := int(params.GetOptionalInt(req.GetArguments(), "tail_lines", 200))
|
||||||
|
maxBytes := int(params.GetOptionalInt(req.GetArguments(), "max_bytes", 65536))
|
||||||
|
raw, usedPath, err := fetchJobLogBytes(ctx, owner, repo, jobID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get job log err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
tailed := tailByLines(raw, tailLines)
|
||||||
|
limited, truncated := limitBytes(tailed, maxBytes)
|
||||||
|
|
||||||
|
return to.TextResult(map[string]any{
|
||||||
|
"endpoint": usedPath,
|
||||||
|
"job_id": jobID,
|
||||||
|
"bytes": len(raw),
|
||||||
|
"tail_lines": tailLines,
|
||||||
|
"max_bytes": maxBytes,
|
||||||
|
"truncated": truncated,
|
||||||
|
"log": string(limited),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DownloadRepoActionJobLogFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DownloadRepoActionJobLogFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
jobID, err := params.GetIndex(req.GetArguments(), "job_id")
|
||||||
|
if err != nil || jobID <= 0 {
|
||||||
|
return to.ErrorResult(errors.New("job_id is required"))
|
||||||
|
}
|
||||||
|
outputPath, _ := req.GetArguments()["output_path"].(string)
|
||||||
|
|
||||||
|
raw, usedPath, err := fetchJobLogBytes(ctx, owner, repo, jobID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("download job log err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if outputPath == "" {
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
if home == "" {
|
||||||
|
home = os.TempDir()
|
||||||
|
}
|
||||||
|
outputPath = filepath.Join(home, ".gitea-mcp", "artifacts", "actions-logs", owner, repo, fmt.Sprintf("%d.log", jobID))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(filepath.Dir(outputPath), 0o700); err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create output dir err: %v", err))
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(outputPath, raw, 0o600); err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("write log file err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(map[string]any{
|
||||||
|
"endpoint": usedPath,
|
||||||
|
"job_id": jobID,
|
||||||
|
"path": outputPath,
|
||||||
|
"bytes": len(raw),
|
||||||
|
})
|
||||||
|
}
|
||||||
22
operation/actions/logs_test.go
Normal file
22
operation/actions/logs_test.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestTailByLines(t *testing.T) {
|
||||||
|
in := []byte("a\nb\nc\nd\n")
|
||||||
|
got := string(tailByLines(in, 2))
|
||||||
|
if got != "c\nd\n" {
|
||||||
|
t.Fatalf("tailByLines(...,2) = %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLimitBytesKeepsTail(t *testing.T) {
|
||||||
|
in := []byte("0123456789")
|
||||||
|
out, truncated := limitBytes(in, 4)
|
||||||
|
if !truncated {
|
||||||
|
t.Fatalf("expected truncated=true")
|
||||||
|
}
|
||||||
|
if string(out) != "6789" {
|
||||||
|
t.Fatalf("limitBytes tail = %q, want %q", string(out), "6789")
|
||||||
|
}
|
||||||
|
}
|
||||||
446
operation/actions/runs.go
Normal file
446
operation/actions/runs.go
Normal file
@@ -0,0 +1,446 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"maps"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListRepoActionWorkflowsToolName = "list_repo_action_workflows"
|
||||||
|
GetRepoActionWorkflowToolName = "get_repo_action_workflow"
|
||||||
|
DispatchRepoActionWorkflowToolName = "dispatch_repo_action_workflow"
|
||||||
|
|
||||||
|
ListRepoActionRunsToolName = "list_repo_action_runs"
|
||||||
|
GetRepoActionRunToolName = "get_repo_action_run"
|
||||||
|
CancelRepoActionRunToolName = "cancel_repo_action_run"
|
||||||
|
RerunRepoActionRunToolName = "rerun_repo_action_run"
|
||||||
|
|
||||||
|
ListRepoActionJobsToolName = "list_repo_action_jobs"
|
||||||
|
ListRepoActionRunJobsToolName = "list_repo_action_run_jobs"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListRepoActionWorkflowsTool = mcp.NewTool(
|
||||||
|
ListRepoActionWorkflowsToolName,
|
||||||
|
mcp.WithDescription("List repository Actions workflows"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoActionWorkflowTool = mcp.NewTool(
|
||||||
|
GetRepoActionWorkflowToolName,
|
||||||
|
mcp.WithDescription("Get a repository Actions workflow by ID"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("workflow_id", mcp.Required(), mcp.Description("workflow ID or filename (e.g. 'my-workflow.yml')")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DispatchRepoActionWorkflowTool = mcp.NewTool(
|
||||||
|
DispatchRepoActionWorkflowToolName,
|
||||||
|
mcp.WithDescription("Trigger (dispatch) a repository Actions workflow"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("workflow_id", mcp.Required(), mcp.Description("workflow ID or filename (e.g. 'my-workflow.yml')")),
|
||||||
|
mcp.WithString("ref", mcp.Required(), mcp.Description("git ref (branch or tag)")),
|
||||||
|
mcp.WithObject("inputs", mcp.Description("workflow inputs object")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListRepoActionRunsTool = mcp.NewTool(
|
||||||
|
ListRepoActionRunsToolName,
|
||||||
|
mcp.WithDescription("List repository Actions workflow runs"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
||||||
|
mcp.WithString("status", mcp.Description("optional status filter")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoActionRunTool = mcp.NewTool(
|
||||||
|
GetRepoActionRunToolName,
|
||||||
|
mcp.WithDescription("Get a repository Actions run by ID"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CancelRepoActionRunTool = mcp.NewTool(
|
||||||
|
CancelRepoActionRunToolName,
|
||||||
|
mcp.WithDescription("Cancel a repository Actions run"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RerunRepoActionRunTool = mcp.NewTool(
|
||||||
|
RerunRepoActionRunToolName,
|
||||||
|
mcp.WithDescription("Rerun a repository Actions run"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListRepoActionJobsTool = mcp.NewTool(
|
||||||
|
ListRepoActionJobsToolName,
|
||||||
|
mcp.WithDescription("List repository Actions jobs"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
||||||
|
mcp.WithString("status", mcp.Description("optional status filter")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListRepoActionRunJobsTool = mcp.NewTool(
|
||||||
|
ListRepoActionRunJobsToolName,
|
||||||
|
mcp.WithDescription("List Actions jobs for a specific run"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionWorkflowsTool, Handler: ListRepoActionWorkflowsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoActionWorkflowTool, Handler: GetRepoActionWorkflowFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DispatchRepoActionWorkflowTool, Handler: DispatchRepoActionWorkflowFn})
|
||||||
|
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionRunsTool, Handler: ListRepoActionRunsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoActionRunTool, Handler: GetRepoActionRunFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CancelRepoActionRunTool, Handler: CancelRepoActionRunFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RerunRepoActionRunTool, Handler: RerunRepoActionRunFn})
|
||||||
|
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionJobsTool, Handler: ListRepoActionJobsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionRunJobsTool, Handler: ListRepoActionRunJobsFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func doJSONWithFallback(ctx context.Context, method string, paths []string, query url.Values, body, respOut any) error {
|
||||||
|
var lastErr error
|
||||||
|
for _, p := range paths {
|
||||||
|
_, err := gitea.DoJSON(ctx, method, p, query, body, respOut)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
var httpErr *gitea.HTTPError
|
||||||
|
if errors.As(err, &httpErr) && (httpErr.StatusCode == http.StatusNotFound || httpErr.StatusCode == http.StatusMethodNotAllowed) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoActionWorkflowsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoActionWorkflowsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 50)
|
||||||
|
query := url.Values{}
|
||||||
|
query.Set("page", strconv.Itoa(int(page)))
|
||||||
|
query.Set("limit", strconv.Itoa(int(pageSize)))
|
||||||
|
|
||||||
|
var result any
|
||||||
|
err := doJSONWithFallback(ctx, "GET",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/workflows", url.PathEscape(owner), url.PathEscape(repo)),
|
||||||
|
},
|
||||||
|
query, nil, &result,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list action workflows err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoActionWorkflowFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
workflowID, ok := req.GetArguments()["workflow_id"].(string)
|
||||||
|
if !ok || workflowID == "" {
|
||||||
|
return to.ErrorResult(errors.New("workflow_id is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
err := doJSONWithFallback(ctx, "GET",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/workflows/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(workflowID)),
|
||||||
|
},
|
||||||
|
nil, nil, &result,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get action workflow err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DispatchRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DispatchRepoActionWorkflowFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
workflowID, ok := req.GetArguments()["workflow_id"].(string)
|
||||||
|
if !ok || workflowID == "" {
|
||||||
|
return to.ErrorResult(errors.New("workflow_id is required"))
|
||||||
|
}
|
||||||
|
ref, ok := req.GetArguments()["ref"].(string)
|
||||||
|
if !ok || ref == "" {
|
||||||
|
return to.ErrorResult(errors.New("ref is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var inputs map[string]any
|
||||||
|
if raw, exists := req.GetArguments()["inputs"]; exists {
|
||||||
|
if m, ok := raw.(map[string]any); ok {
|
||||||
|
inputs = m
|
||||||
|
} else if m, ok := raw.(map[string]any); ok {
|
||||||
|
inputs = make(map[string]any, len(m))
|
||||||
|
maps.Copy(inputs, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body := map[string]any{
|
||||||
|
"ref": ref,
|
||||||
|
}
|
||||||
|
if inputs != nil {
|
||||||
|
body["inputs"] = inputs
|
||||||
|
}
|
||||||
|
|
||||||
|
err := doJSONWithFallback(ctx, "POST",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/workflows/%s/dispatches", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(workflowID)),
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/workflows/%s/dispatch", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(workflowID)),
|
||||||
|
},
|
||||||
|
nil, body, nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
var httpErr *gitea.HTTPError
|
||||||
|
if errors.As(err, &httpErr) && (httpErr.StatusCode == http.StatusNotFound || httpErr.StatusCode == http.StatusMethodNotAllowed) {
|
||||||
|
return to.ErrorResult(fmt.Errorf("workflow dispatch not supported on this Gitea version (endpoint returned %d). Check https://docs.gitea.com/api/1.24/ for available Actions endpoints", httpErr.StatusCode))
|
||||||
|
}
|
||||||
|
return to.ErrorResult(fmt.Errorf("dispatch action workflow err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "workflow dispatched"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoActionRunsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoActionRunsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 50)
|
||||||
|
statusFilter, _ := req.GetArguments()["status"].(string)
|
||||||
|
|
||||||
|
query := url.Values{}
|
||||||
|
query.Set("page", strconv.Itoa(int(page)))
|
||||||
|
query.Set("limit", strconv.Itoa(int(pageSize)))
|
||||||
|
if statusFilter != "" {
|
||||||
|
query.Set("status", statusFilter)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
err := doJSONWithFallback(ctx, "GET",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/runs", url.PathEscape(owner), url.PathEscape(repo)),
|
||||||
|
},
|
||||||
|
query, nil, &result,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list action runs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoActionRunFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
runID, err := params.GetIndex(req.GetArguments(), "run_id")
|
||||||
|
if err != nil || runID <= 0 {
|
||||||
|
return to.ErrorResult(errors.New("run_id is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
err = doJSONWithFallback(ctx, "GET",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/runs/%d", url.PathEscape(owner), url.PathEscape(repo), runID),
|
||||||
|
},
|
||||||
|
nil, nil, &result,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get action run err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CancelRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CancelRepoActionRunFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
runID, err := params.GetIndex(req.GetArguments(), "run_id")
|
||||||
|
if err != nil || runID <= 0 {
|
||||||
|
return to.ErrorResult(errors.New("run_id is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = doJSONWithFallback(ctx, "POST",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/runs/%d/cancel", url.PathEscape(owner), url.PathEscape(repo), runID),
|
||||||
|
},
|
||||||
|
nil, nil, nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("cancel action run err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "run cancellation requested"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RerunRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RerunRepoActionRunFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
runID, err := params.GetIndex(req.GetArguments(), "run_id")
|
||||||
|
if err != nil || runID <= 0 {
|
||||||
|
return to.ErrorResult(errors.New("run_id is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = doJSONWithFallback(ctx, "POST",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/runs/%d/rerun", url.PathEscape(owner), url.PathEscape(repo), runID),
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/runs/%d/rerun-failed-jobs", url.PathEscape(owner), url.PathEscape(repo), runID),
|
||||||
|
},
|
||||||
|
nil, nil, nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
var httpErr *gitea.HTTPError
|
||||||
|
if errors.As(err, &httpErr) && (httpErr.StatusCode == http.StatusNotFound || httpErr.StatusCode == http.StatusMethodNotAllowed) {
|
||||||
|
return to.ErrorResult(fmt.Errorf("workflow rerun not supported on this Gitea version (endpoint returned %d). Check https://docs.gitea.com/api/1.24/ for available Actions endpoints", httpErr.StatusCode))
|
||||||
|
}
|
||||||
|
return to.ErrorResult(fmt.Errorf("rerun action run err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "run rerun requested"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoActionJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoActionJobsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 50)
|
||||||
|
statusFilter, _ := req.GetArguments()["status"].(string)
|
||||||
|
|
||||||
|
query := url.Values{}
|
||||||
|
query.Set("page", strconv.Itoa(int(page)))
|
||||||
|
query.Set("limit", strconv.Itoa(int(pageSize)))
|
||||||
|
if statusFilter != "" {
|
||||||
|
query.Set("status", statusFilter)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
err := doJSONWithFallback(ctx, "GET",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/jobs", url.PathEscape(owner), url.PathEscape(repo)),
|
||||||
|
},
|
||||||
|
query, nil, &result,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list action jobs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoActionRunJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoActionRunJobsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
runID, err := params.GetIndex(req.GetArguments(), "run_id")
|
||||||
|
if err != nil || runID <= 0 {
|
||||||
|
return to.ErrorResult(errors.New("run_id is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 50)
|
||||||
|
|
||||||
|
query := url.Values{}
|
||||||
|
query.Set("page", strconv.Itoa(int(page)))
|
||||||
|
query.Set("limit", strconv.Itoa(int(pageSize)))
|
||||||
|
|
||||||
|
var result any
|
||||||
|
err = doJSONWithFallback(ctx, "GET",
|
||||||
|
[]string{
|
||||||
|
fmt.Sprintf("repos/%s/%s/actions/runs/%d/jobs", url.PathEscape(owner), url.PathEscape(repo), runID),
|
||||||
|
},
|
||||||
|
query, nil, &result,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list action run jobs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
282
operation/actions/secrets.go
Normal file
282
operation/actions/secrets.go
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListRepoActionSecretsToolName = "list_repo_action_secrets"
|
||||||
|
UpsertRepoActionSecretToolName = "upsert_repo_action_secret"
|
||||||
|
DeleteRepoActionSecretToolName = "delete_repo_action_secret"
|
||||||
|
ListOrgActionSecretsToolName = "list_org_action_secrets"
|
||||||
|
UpsertOrgActionSecretToolName = "upsert_org_action_secret"
|
||||||
|
DeleteOrgActionSecretToolName = "delete_org_action_secret"
|
||||||
|
)
|
||||||
|
|
||||||
|
type secretMeta struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"created_at,omitzero"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListRepoActionSecretsTool = mcp.NewTool(
|
||||||
|
ListRepoActionSecretsToolName,
|
||||||
|
mcp.WithDescription("List repository Actions secrets (metadata only; secret values are never returned)"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
|
||||||
|
UpsertRepoActionSecretTool = mcp.NewTool(
|
||||||
|
UpsertRepoActionSecretToolName,
|
||||||
|
mcp.WithDescription("Create or update (upsert) a repository Actions secret"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("secret name")),
|
||||||
|
mcp.WithString("data", mcp.Required(), mcp.Description("secret value")),
|
||||||
|
mcp.WithString("description", mcp.Description("secret description")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteRepoActionSecretTool = mcp.NewTool(
|
||||||
|
DeleteRepoActionSecretToolName,
|
||||||
|
mcp.WithDescription("Delete a repository Actions secret"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("secretName", mcp.Required(), mcp.Description("secret name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListOrgActionSecretsTool = mcp.NewTool(
|
||||||
|
ListOrgActionSecretsToolName,
|
||||||
|
mcp.WithDescription("List organization Actions secrets (metadata only; secret values are never returned)"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
|
||||||
|
UpsertOrgActionSecretTool = mcp.NewTool(
|
||||||
|
UpsertOrgActionSecretToolName,
|
||||||
|
mcp.WithDescription("Create or update (upsert) an organization Actions secret"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("secret name")),
|
||||||
|
mcp.WithString("data", mcp.Required(), mcp.Description("secret value")),
|
||||||
|
mcp.WithString("description", mcp.Description("secret description")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteOrgActionSecretTool = mcp.NewTool(
|
||||||
|
DeleteOrgActionSecretToolName,
|
||||||
|
mcp.WithDescription("Delete an organization Actions secret"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("secretName", mcp.Required(), mcp.Description("secret name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionSecretsTool, Handler: ListRepoActionSecretsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UpsertRepoActionSecretTool, Handler: UpsertRepoActionSecretFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteRepoActionSecretTool, Handler: DeleteRepoActionSecretFn})
|
||||||
|
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgActionSecretsTool, Handler: ListOrgActionSecretsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UpsertOrgActionSecretTool, Handler: UpsertOrgActionSecretFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteOrgActionSecretTool, Handler: DeleteOrgActionSecretFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoActionSecretsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoActionSecretsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
secrets, _, err := client.ListRepoActionSecret(owner, repo, gitea_sdk.ListRepoActionSecretOption{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list repo action secrets err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
metas := make([]secretMeta, 0, len(secrets))
|
||||||
|
for _, s := range secrets {
|
||||||
|
if s == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
metas = append(metas, secretMeta{
|
||||||
|
Name: s.Name,
|
||||||
|
Description: s.Description,
|
||||||
|
CreatedAt: s.Created,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return to.TextResult(metas)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpsertRepoActionSecretFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UpsertRepoActionSecretFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
data, ok := req.GetArguments()["data"].(string)
|
||||||
|
if !ok || data == "" {
|
||||||
|
return to.ErrorResult(errors.New("data is required"))
|
||||||
|
}
|
||||||
|
description, _ := req.GetArguments()["description"].(string)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.CreateRepoActionSecret(owner, repo, gitea_sdk.CreateSecretOption{
|
||||||
|
Name: name,
|
||||||
|
Data: data,
|
||||||
|
Description: description,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("upsert repo action secret err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "secret upserted", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteRepoActionSecretFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteRepoActionSecretFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
secretName, ok := req.GetArguments()["secretName"].(string)
|
||||||
|
if !ok || secretName == "" {
|
||||||
|
return to.ErrorResult(errors.New("secretName is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.DeleteRepoActionSecret(owner, repo, secretName)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete repo action secret err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "secret deleted", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgActionSecretsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgActionSecretsFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
secrets, _, err := client.ListOrgActionSecret(org, gitea_sdk.ListOrgActionSecretOption{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org action secrets err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
metas := make([]secretMeta, 0, len(secrets))
|
||||||
|
for _, s := range secrets {
|
||||||
|
if s == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
metas = append(metas, secretMeta{
|
||||||
|
Name: s.Name,
|
||||||
|
Description: s.Description,
|
||||||
|
CreatedAt: s.Created,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return to.TextResult(metas)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpsertOrgActionSecretFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UpsertOrgActionSecretFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
data, ok := req.GetArguments()["data"].(string)
|
||||||
|
if !ok || data == "" {
|
||||||
|
return to.ErrorResult(errors.New("data is required"))
|
||||||
|
}
|
||||||
|
description, _ := req.GetArguments()["description"].(string)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.CreateOrgActionSecret(org, gitea_sdk.CreateSecretOption{
|
||||||
|
Name: name,
|
||||||
|
Data: data,
|
||||||
|
Description: description,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("upsert org action secret err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "secret upserted", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteOrgActionSecretFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteOrgActionSecretFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
secretName, ok := req.GetArguments()["secretName"].(string)
|
||||||
|
if !ok || secretName == "" {
|
||||||
|
return to.ErrorResult(errors.New("secretName is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
escapedOrg := url.PathEscape(org)
|
||||||
|
escapedSecret := url.PathEscape(secretName)
|
||||||
|
_, err := gitea.DoJSON(ctx, "DELETE", fmt.Sprintf("orgs/%s/actions/secrets/%s", escapedOrg, escapedSecret), nil, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete org action secret err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "secret deleted"})
|
||||||
|
}
|
||||||
391
operation/actions/variables.go
Normal file
391
operation/actions/variables.go
Normal file
@@ -0,0 +1,391 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListRepoActionVariablesToolName = "list_repo_action_variables"
|
||||||
|
GetRepoActionVariableToolName = "get_repo_action_variable"
|
||||||
|
CreateRepoActionVariableToolName = "create_repo_action_variable"
|
||||||
|
UpdateRepoActionVariableToolName = "update_repo_action_variable"
|
||||||
|
DeleteRepoActionVariableToolName = "delete_repo_action_variable"
|
||||||
|
|
||||||
|
ListOrgActionVariablesToolName = "list_org_action_variables"
|
||||||
|
GetOrgActionVariableToolName = "get_org_action_variable"
|
||||||
|
CreateOrgActionVariableToolName = "create_org_action_variable"
|
||||||
|
UpdateOrgActionVariableToolName = "update_org_action_variable"
|
||||||
|
DeleteOrgActionVariableToolName = "delete_org_action_variable"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListRepoActionVariablesTool = mcp.NewTool(
|
||||||
|
ListRepoActionVariablesToolName,
|
||||||
|
mcp.WithDescription("List repository Actions variables"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoActionVariableTool = mcp.NewTool(
|
||||||
|
GetRepoActionVariableToolName,
|
||||||
|
mcp.WithDescription("Get a repository Actions variable by name"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateRepoActionVariableTool = mcp.NewTool(
|
||||||
|
CreateRepoActionVariableToolName,
|
||||||
|
mcp.WithDescription("Create a repository Actions variable"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
mcp.WithString("value", mcp.Required(), mcp.Description("variable value")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UpdateRepoActionVariableTool = mcp.NewTool(
|
||||||
|
UpdateRepoActionVariableToolName,
|
||||||
|
mcp.WithDescription("Update a repository Actions variable"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
mcp.WithString("value", mcp.Required(), mcp.Description("new variable value")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteRepoActionVariableTool = mcp.NewTool(
|
||||||
|
DeleteRepoActionVariableToolName,
|
||||||
|
mcp.WithDescription("Delete a repository Actions variable"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListOrgActionVariablesTool = mcp.NewTool(
|
||||||
|
ListOrgActionVariablesToolName,
|
||||||
|
mcp.WithDescription("List organization Actions variables"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetOrgActionVariableTool = mcp.NewTool(
|
||||||
|
GetOrgActionVariableToolName,
|
||||||
|
mcp.WithDescription("Get an organization Actions variable by name"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateOrgActionVariableTool = mcp.NewTool(
|
||||||
|
CreateOrgActionVariableToolName,
|
||||||
|
mcp.WithDescription("Create an organization Actions variable"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
mcp.WithString("value", mcp.Required(), mcp.Description("variable value")),
|
||||||
|
mcp.WithString("description", mcp.Description("variable description")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UpdateOrgActionVariableTool = mcp.NewTool(
|
||||||
|
UpdateOrgActionVariableToolName,
|
||||||
|
mcp.WithDescription("Update an organization Actions variable"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
mcp.WithString("value", mcp.Required(), mcp.Description("new variable value")),
|
||||||
|
mcp.WithString("description", mcp.Description("new variable description")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteOrgActionVariableTool = mcp.NewTool(
|
||||||
|
DeleteOrgActionVariableToolName,
|
||||||
|
mcp.WithDescription("Delete an organization Actions variable"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("variable name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionVariablesTool, Handler: ListRepoActionVariablesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoActionVariableTool, Handler: GetRepoActionVariableFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateRepoActionVariableTool, Handler: CreateRepoActionVariableFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UpdateRepoActionVariableTool, Handler: UpdateRepoActionVariableFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteRepoActionVariableTool, Handler: DeleteRepoActionVariableFn})
|
||||||
|
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgActionVariablesTool, Handler: ListOrgActionVariablesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetOrgActionVariableTool, Handler: GetOrgActionVariableFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateOrgActionVariableTool, Handler: CreateOrgActionVariableFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UpdateOrgActionVariableTool, Handler: UpdateOrgActionVariableFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteOrgActionVariableTool, Handler: DeleteOrgActionVariableFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoActionVariablesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoActionVariablesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
query := url.Values{}
|
||||||
|
query.Set("page", strconv.Itoa(int(page)))
|
||||||
|
query.Set("limit", strconv.Itoa(int(pageSize)))
|
||||||
|
|
||||||
|
var result any
|
||||||
|
_, err := gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/actions/variables", url.PathEscape(owner), url.PathEscape(repo)), query, nil, &result)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list repo action variables err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoActionVariableFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
variable, _, err := client.GetRepoActionVariable(owner, repo, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get repo action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(variable)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateRepoActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateRepoActionVariableFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
value, ok := req.GetArguments()["value"].(string)
|
||||||
|
if !ok || value == "" {
|
||||||
|
return to.ErrorResult(errors.New("value is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.CreateRepoActionVariable(owner, repo, name, value)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create repo action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "variable created", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateRepoActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UpdateRepoActionVariableFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
value, ok := req.GetArguments()["value"].(string)
|
||||||
|
if !ok || value == "" {
|
||||||
|
return to.ErrorResult(errors.New("value is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.UpdateRepoActionVariable(owner, repo, name, value)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("update repo action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "variable updated", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteRepoActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteRepoActionVariableFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.DeleteRepoActionVariable(owner, repo, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete repo action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "variable deleted", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgActionVariablesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgActionVariablesFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
variables, _, err := client.ListOrgActionVariable(org, gitea_sdk.ListOrgActionVariableOption{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org action variables err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(variables)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOrgActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetOrgActionVariableFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
variable, _, err := client.GetOrgActionVariable(org, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get org action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(variable)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOrgActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateOrgActionVariableFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
value, ok := req.GetArguments()["value"].(string)
|
||||||
|
if !ok || value == "" {
|
||||||
|
return to.ErrorResult(errors.New("value is required"))
|
||||||
|
}
|
||||||
|
description, _ := req.GetArguments()["description"].(string)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.CreateOrgActionVariable(org, gitea_sdk.CreateOrgActionVariableOption{
|
||||||
|
Name: name,
|
||||||
|
Value: value,
|
||||||
|
Description: description,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create org action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "variable created", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateOrgActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UpdateOrgActionVariableFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
value, ok := req.GetArguments()["value"].(string)
|
||||||
|
if !ok || value == "" {
|
||||||
|
return to.ErrorResult(errors.New("value is required"))
|
||||||
|
}
|
||||||
|
description, _ := req.GetArguments()["description"].(string)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
resp, err := client.UpdateOrgActionVariable(org, name, gitea_sdk.UpdateOrgActionVariableOption{
|
||||||
|
Value: value,
|
||||||
|
Description: description,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("update org action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "variable updated", "status": resp.StatusCode})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteOrgActionVariableFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteOrgActionVariableFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok || org == "" {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := gitea.DoJSON(ctx, "DELETE", fmt.Sprintf("orgs/%s/actions/variables/%s", url.PathEscape(org), url.PathEscape(name)), nil, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete org action variable err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"message": "variable deleted"})
|
||||||
|
}
|
||||||
805
operation/admin/admin.go
Normal file
805
operation/admin/admin.go
Normal file
@@ -0,0 +1,805 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
AdminListUsersToolName = "admin_list_users"
|
||||||
|
AdminCreateUserToolName = "admin_create_user"
|
||||||
|
AdminEditUserToolName = "admin_edit_user"
|
||||||
|
AdminDeleteUserToolName = "admin_delete_user"
|
||||||
|
AdminRenameUserToolName = "admin_rename_user"
|
||||||
|
AdminListOrgsToolName = "admin_list_orgs"
|
||||||
|
AdminCreateOrgToolName = "admin_create_org"
|
||||||
|
AdminCreateRepoToolName = "admin_create_repo"
|
||||||
|
AdminListHooksToolName = "admin_list_hooks"
|
||||||
|
AdminGetHookToolName = "admin_get_hook"
|
||||||
|
AdminCreateHookToolName = "admin_create_hook"
|
||||||
|
AdminEditHookToolName = "admin_edit_hook"
|
||||||
|
AdminDeleteHookToolName = "admin_delete_hook"
|
||||||
|
ListCronTasksToolName = "admin_list_cron_tasks"
|
||||||
|
RunCronTaskToolName = "admin_run_cron_task"
|
||||||
|
ListUnadoptedReposToolName = "admin_list_unadopted_repos"
|
||||||
|
AdoptUnadoptedRepoToolName = "admin_adopt_repo"
|
||||||
|
DeleteUnadoptedRepoToolName = "admin_delete_unadopted_repo"
|
||||||
|
AdminListEmailsToolName = "admin_list_emails"
|
||||||
|
AdminSearchEmailsToolName = "admin_search_emails"
|
||||||
|
ListUserBadgesToolName = "admin_list_user_badges"
|
||||||
|
AddUserBadgesToolName = "admin_add_user_badges"
|
||||||
|
DeleteUserBadgeToolName = "admin_delete_user_badge"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
AdminListUsersTool = mcp.NewTool(
|
||||||
|
AdminListUsersToolName,
|
||||||
|
mcp.WithDescription("Admin: List all users"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminCreateUserTool = mcp.NewTool(
|
||||||
|
AdminCreateUserToolName,
|
||||||
|
mcp.WithDescription("Admin: Create a new user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithString("email", mcp.Required(), mcp.Description("email address")),
|
||||||
|
mcp.WithString("password", mcp.Required(), mcp.Description("password")),
|
||||||
|
mcp.WithString("full_name", mcp.Description("full name")),
|
||||||
|
mcp.WithBoolean("must_change_password", mcp.Description("require password change on first login (default: true)")),
|
||||||
|
mcp.WithBoolean("send_notify", mcp.Description("send notification email")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminEditUserTool = mcp.NewTool(
|
||||||
|
AdminEditUserToolName,
|
||||||
|
mcp.WithDescription("Admin: Edit a user account"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to edit")),
|
||||||
|
mcp.WithString("email", mcp.Description("new email")),
|
||||||
|
mcp.WithString("password", mcp.Description("new password")),
|
||||||
|
mcp.WithString("full_name", mcp.Description("new full name")),
|
||||||
|
mcp.WithBoolean("must_change_password", mcp.Description("require password change")),
|
||||||
|
mcp.WithBoolean("admin", mcp.Description("set admin status")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("set active status")),
|
||||||
|
mcp.WithBoolean("prohibit_login", mcp.Description("prohibit login")),
|
||||||
|
mcp.WithBoolean("restricted", mcp.Description("set restricted status")),
|
||||||
|
mcp.WithBoolean("allow_create_organization", mcp.Description("allow creating organizations")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminDeleteUserTool = mcp.NewTool(
|
||||||
|
AdminDeleteUserToolName,
|
||||||
|
mcp.WithDescription("Admin: Delete a user account"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to delete")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminRenameUserTool = mcp.NewTool(
|
||||||
|
AdminRenameUserToolName,
|
||||||
|
mcp.WithDescription("Admin: Rename a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("current username")),
|
||||||
|
mcp.WithString("new_username", mcp.Required(), mcp.Description("new username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminListOrgsTool = mcp.NewTool(
|
||||||
|
AdminListOrgsToolName,
|
||||||
|
mcp.WithDescription("Admin: List all organizations"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminCreateOrgTool = mcp.NewTool(
|
||||||
|
AdminCreateOrgToolName,
|
||||||
|
mcp.WithDescription("Admin: Create an organization for a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("owner username")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("full_name", mcp.Description("organization full name")),
|
||||||
|
mcp.WithString("description", mcp.Description("organization description")),
|
||||||
|
mcp.WithString("visibility", mcp.Description("visibility: public, limited, private (default: public)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminCreateRepoTool = mcp.NewTool(
|
||||||
|
AdminCreateRepoToolName,
|
||||||
|
mcp.WithDescription("Admin: Create a repository for a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("owner username")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("description", mcp.Description("repository description")),
|
||||||
|
mcp.WithBoolean("private", mcp.Description("make repository private")),
|
||||||
|
mcp.WithBoolean("auto_init", mcp.Description("auto-initialize with README")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminListHooksTool = mcp.NewTool(
|
||||||
|
AdminListHooksToolName,
|
||||||
|
mcp.WithDescription("Admin: List system webhooks"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminGetHookTool = mcp.NewTool(
|
||||||
|
AdminGetHookToolName,
|
||||||
|
mcp.WithDescription("Admin: Get a system webhook by ID"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminCreateHookTool = mcp.NewTool(
|
||||||
|
AdminCreateHookToolName,
|
||||||
|
mcp.WithDescription("Admin: Create a system webhook"),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("hook type: gitea, gogs, slack, discord, dingtalk, telegram, msteams, feishu, wechatwork, packagist")),
|
||||||
|
mcp.WithString("url", mcp.Required(), mcp.Description("target URL")),
|
||||||
|
mcp.WithString("content_type", mcp.Description("content type: json or form (default: json)")),
|
||||||
|
mcp.WithString("secret", mcp.Description("webhook secret")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("whether the webhook is active (default: true)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminEditHookTool = mcp.NewTool(
|
||||||
|
AdminEditHookToolName,
|
||||||
|
mcp.WithDescription("Admin: Edit a system webhook"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
mcp.WithString("url", mcp.Description("target URL")),
|
||||||
|
mcp.WithString("content_type", mcp.Description("content type: json or form")),
|
||||||
|
mcp.WithString("secret", mcp.Description("webhook secret")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("whether the webhook is active")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminDeleteHookTool = mcp.NewTool(
|
||||||
|
AdminDeleteHookToolName,
|
||||||
|
mcp.WithDescription("Admin: Delete a system webhook"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListCronTasksTool = mcp.NewTool(
|
||||||
|
ListCronTasksToolName,
|
||||||
|
mcp.WithDescription("Admin: List cron tasks"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
RunCronTaskTool = mcp.NewTool(
|
||||||
|
RunCronTaskToolName,
|
||||||
|
mcp.WithDescription("Admin: Run a cron task manually"),
|
||||||
|
mcp.WithString("task", mcp.Required(), mcp.Description("cron task name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUnadoptedReposTool = mcp.NewTool(
|
||||||
|
ListUnadoptedReposToolName,
|
||||||
|
mcp.WithDescription("Admin: List unadopted repositories"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdoptUnadoptedRepoTool = mcp.NewTool(
|
||||||
|
AdoptUnadoptedRepoToolName,
|
||||||
|
mcp.WithDescription("Admin: Adopt an unadopted repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteUnadoptedRepoTool = mcp.NewTool(
|
||||||
|
DeleteUnadoptedRepoToolName,
|
||||||
|
mcp.WithDescription("Admin: Delete an unadopted repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminListEmailsTool = mcp.NewTool(
|
||||||
|
AdminListEmailsToolName,
|
||||||
|
mcp.WithDescription("Admin: List all emails"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AdminSearchEmailsTool = mcp.NewTool(
|
||||||
|
AdminSearchEmailsToolName,
|
||||||
|
mcp.WithDescription("Admin: Search emails"),
|
||||||
|
mcp.WithString("query", mcp.Required(), mcp.Description("search query")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUserBadgesTool = mcp.NewTool(
|
||||||
|
ListUserBadgesToolName,
|
||||||
|
mcp.WithDescription("Admin: List badges for a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddUserBadgesTool = mcp.NewTool(
|
||||||
|
AddUserBadgesToolName,
|
||||||
|
mcp.WithDescription("Admin: Add badges to a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithString("slugs", mcp.Required(), mcp.Description("comma-separated badge slugs to add")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteUserBadgeTool = mcp.NewTool(
|
||||||
|
DeleteUserBadgeToolName,
|
||||||
|
mcp.WithDescription("Admin: Remove badges from a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithString("slugs", mcp.Required(), mcp.Description("comma-separated badge slugs to remove")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: AdminListUsersTool, Handler: AdminListUsersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: AdminListOrgsTool, Handler: AdminListOrgsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: AdminListHooksTool, Handler: AdminListHooksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: AdminGetHookTool, Handler: AdminGetHookFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListCronTasksTool, Handler: ListCronTasksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUnadoptedReposTool, Handler: ListUnadoptedReposFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: AdminListEmailsTool, Handler: AdminListEmailsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: AdminSearchEmailsTool, Handler: AdminSearchEmailsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserBadgesTool, Handler: ListUserBadgesFn})
|
||||||
|
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminCreateUserTool, Handler: AdminCreateUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminEditUserTool, Handler: AdminEditUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminDeleteUserTool, Handler: AdminDeleteUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminRenameUserTool, Handler: AdminRenameUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminCreateOrgTool, Handler: AdminCreateOrgFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminCreateRepoTool, Handler: AdminCreateRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminCreateHookTool, Handler: AdminCreateHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminEditHookTool, Handler: AdminEditHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdminDeleteHookTool, Handler: AdminDeleteHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RunCronTaskTool, Handler: RunCronTaskFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AdoptUnadoptedRepoTool, Handler: AdoptUnadoptedRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteUnadoptedRepoTool, Handler: DeleteUnadoptedRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddUserBadgesTool, Handler: AddUserBadgesFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteUserBadgeTool, Handler: DeleteUserBadgeFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminListUsersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminListUsersFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.AdminListUsers(gitea_sdk.AdminListUsersOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin list users err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminCreateUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminCreateUserFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
email, _ := req.GetArguments()["email"].(string)
|
||||||
|
password, _ := req.GetArguments()["password"].(string)
|
||||||
|
if username == "" || email == "" || password == "" {
|
||||||
|
return to.ErrorResult(errors.New("username, email, and password are required"))
|
||||||
|
}
|
||||||
|
mustChange := true
|
||||||
|
if v, ok := req.GetArguments()["must_change_password"].(bool); ok {
|
||||||
|
mustChange = v
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateUserOption{
|
||||||
|
LoginName: username,
|
||||||
|
Username: username,
|
||||||
|
Email: email,
|
||||||
|
Password: password,
|
||||||
|
MustChangePassword: &mustChange,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||||
|
opt.FullName = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["send_notify"].(bool); ok {
|
||||||
|
opt.SendNotify = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
user, _, err := client.AdminCreateUser(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin create user err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminEditUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminEditUserFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
if username == "" {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditUserOption{}
|
||||||
|
if v, ok := req.GetArguments()["email"].(string); ok {
|
||||||
|
opt.Email = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["password"].(string); ok {
|
||||||
|
opt.Password = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||||
|
opt.FullName = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["must_change_password"].(bool); ok {
|
||||||
|
opt.MustChangePassword = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["admin"].(bool); ok {
|
||||||
|
opt.Admin = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["prohibit_login"].(bool); ok {
|
||||||
|
opt.ProhibitLogin = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["restricted"].(bool); ok {
|
||||||
|
opt.Restricted = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["allow_create_organization"].(bool); ok {
|
||||||
|
opt.AllowCreateOrganization = &v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AdminEditUser(username, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin edit user err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "updated", "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminDeleteUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminDeleteUserFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
if username == "" {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AdminDeleteUser(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin delete user err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted", "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminRenameUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminRenameUserFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
newUsername, _ := req.GetArguments()["new_username"].(string)
|
||||||
|
if username == "" || newUsername == "" {
|
||||||
|
return to.ErrorResult(errors.New("username and new_username are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AdminRenameUser(username, gitea_sdk.RenameUserOption{NewUsername: newUsername})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin rename user err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "renamed", "old": username, "new": newUsername})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminListOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminListOrgsFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
orgs, _, err := client.AdminListOrgs(gitea_sdk.AdminListOrgsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin list orgs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(orgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminCreateOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminCreateOrgFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if username == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("username and name are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateOrgOption{
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||||
|
opt.FullName = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["visibility"].(string); ok {
|
||||||
|
opt.Visibility = gitea_sdk.VisibleType(v)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
org, _, err := client.AdminCreateOrg(username, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin create org err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(org)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminCreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminCreateRepoFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if username == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("username and name are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateRepoOption{
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["private"].(bool); ok {
|
||||||
|
opt.Private = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["auto_init"].(bool); ok {
|
||||||
|
opt.AutoInit = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repo, _, err := client.AdminCreateRepo(username, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin create repo err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminListHooksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminListHooksFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hooks, _, err := client.ListAdminHooks(gitea_sdk.ListAdminHooksOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin list hooks err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminGetHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminGetHookFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.GetAdminHook(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin get hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminCreateHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminCreateHookFn")
|
||||||
|
hookType, _ := req.GetArguments()["type"].(string)
|
||||||
|
url, _ := req.GetArguments()["url"].(string)
|
||||||
|
if hookType == "" || url == "" {
|
||||||
|
return to.ErrorResult(errors.New("type and url are required"))
|
||||||
|
}
|
||||||
|
contentType := "json"
|
||||||
|
if v, ok := req.GetArguments()["content_type"].(string); ok {
|
||||||
|
contentType = v
|
||||||
|
}
|
||||||
|
config := map[string]string{
|
||||||
|
"url": url,
|
||||||
|
"content_type": contentType,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["secret"].(string); ok {
|
||||||
|
config["secret"] = v
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateHookOption{
|
||||||
|
Type: gitea_sdk.HookType(hookType),
|
||||||
|
Config: config,
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.CreateAdminHook(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin create hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminEditHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminEditHookFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditHookOption{}
|
||||||
|
config := map[string]string{}
|
||||||
|
if v, ok := req.GetArguments()["url"].(string); ok {
|
||||||
|
config["url"] = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["content_type"].(string); ok {
|
||||||
|
config["content_type"] = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["secret"].(string); ok {
|
||||||
|
config["secret"] = v
|
||||||
|
}
|
||||||
|
if len(config) > 0 {
|
||||||
|
opt.Config = config
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = &v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.EditAdminHook(id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin edit hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "updated"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminDeleteHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminDeleteHookFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteAdminHook(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin delete hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListCronTasksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListCronTasksFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tasks, _, err := client.ListCronTasks(gitea_sdk.ListCronTaskOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list cron tasks err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(tasks)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunCronTaskFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RunCronTaskFn")
|
||||||
|
task, _ := req.GetArguments()["task"].(string)
|
||||||
|
if task == "" {
|
||||||
|
return to.ErrorResult(errors.New("task is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.RunCronTasks(task)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("run cron task err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "triggered", "task": task})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUnadoptedReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUnadoptedReposFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.ListUnadoptedRepos(gitea_sdk.ListUnadoptedReposOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list unadopted repos err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdoptUnadoptedRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdoptUnadoptedRepoFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AdoptUnadoptedRepo(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("adopt repo err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "adopted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteUnadoptedRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteUnadoptedRepoFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteUnadoptedRepo(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete unadopted repo err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminListEmailsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminListEmailsFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
emails, _, err := client.ListAdminEmails(gitea_sdk.ListAdminEmailsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin list emails err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(emails)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AdminSearchEmailsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AdminSearchEmailsFn")
|
||||||
|
query, _ := req.GetArguments()["query"].(string)
|
||||||
|
if query == "" {
|
||||||
|
return to.ErrorResult(errors.New("query is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
emails, _, err := client.SearchAdminEmails(gitea_sdk.SearchAdminEmailsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
Query: query,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("admin search emails err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(emails)
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitAndTrim(s string) []string {
|
||||||
|
var result []string
|
||||||
|
for _, part := range splitByComma(s) {
|
||||||
|
trimmed := trimSpace(part)
|
||||||
|
if trimmed != "" {
|
||||||
|
result = append(result, trimmed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitByComma(s string) []string {
|
||||||
|
var result []string
|
||||||
|
start := 0
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
if s[i] == ',' {
|
||||||
|
result = append(result, s[start:i])
|
||||||
|
start = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result = append(result, s[start:])
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimSpace(s string) string {
|
||||||
|
start, end := 0, len(s)
|
||||||
|
for start < end && (s[start] == ' ' || s[start] == '\t') {
|
||||||
|
start++
|
||||||
|
}
|
||||||
|
for end > start && (s[end-1] == ' ' || s[end-1] == '\t') {
|
||||||
|
end--
|
||||||
|
}
|
||||||
|
return s[start:end]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserBadgesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserBadgesFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
if username == "" {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
badges, _, err := client.ListUserBadges(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list user badges err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(badges)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddUserBadgesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddUserBadgesFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
slugs, _ := req.GetArguments()["slugs"].(string)
|
||||||
|
if username == "" || slugs == "" {
|
||||||
|
return to.ErrorResult(errors.New("username and slugs are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AddUserBadges(username, gitea_sdk.UserBadgeOption{
|
||||||
|
BadgeSlugs: splitAndTrim(slugs),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add user badges err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "added"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteUserBadgeFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteUserBadgeFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
slugs, _ := req.GetArguments()["slugs"].(string)
|
||||||
|
if username == "" || slugs == "" {
|
||||||
|
return to.ErrorResult(errors.New("username and slugs are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteUserBadge(username, gitea_sdk.UserBadgeOption{
|
||||||
|
BadgeSlugs: splitAndTrim(slugs),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete user badge err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "removed"})
|
||||||
|
}
|
||||||
@@ -2,22 +2,30 @@ package issue
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GetIssueByIndexToolName = "get_issue_by_index"
|
GetIssueByIndexToolName = "get_issue_by_index"
|
||||||
ListRepoIssuesToolName = "list_repo_issues"
|
ListRepoIssuesToolName = "list_repo_issues"
|
||||||
CreateIssueToolName = "create_issue"
|
CreateIssueToolName = "create_issue"
|
||||||
CreateIssueCommentToolName = "create_issue_comment"
|
CreateIssueCommentToolName = "create_issue_comment"
|
||||||
|
EditIssueToolName = "edit_issue"
|
||||||
|
EditIssueCommentToolName = "edit_issue_comment"
|
||||||
|
GetIssueCommentsByIndexToolName = "get_issue_comments_by_index"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -47,6 +55,7 @@ var (
|
|||||||
mcp.WithString("title", mcp.Required(), mcp.Description("issue title")),
|
mcp.WithString("title", mcp.Required(), mcp.Description("issue title")),
|
||||||
mcp.WithString("body", mcp.Required(), mcp.Description("issue body")),
|
mcp.WithString("body", mcp.Required(), mcp.Description("issue body")),
|
||||||
)
|
)
|
||||||
|
|
||||||
CreateIssueCommentTool = mcp.NewTool(
|
CreateIssueCommentTool = mcp.NewTool(
|
||||||
CreateIssueCommentToolName,
|
CreateIssueCommentToolName,
|
||||||
mcp.WithDescription("create issue comment"),
|
mcp.WithDescription("create issue comment"),
|
||||||
@@ -55,32 +64,90 @@ var (
|
|||||||
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
||||||
mcp.WithString("body", mcp.Required(), mcp.Description("issue comment body")),
|
mcp.WithString("body", mcp.Required(), mcp.Description("issue comment body")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
EditIssueTool = mcp.NewTool(
|
||||||
|
EditIssueToolName,
|
||||||
|
mcp.WithDescription("edit issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
||||||
|
mcp.WithString("title", mcp.Description("issue title"), mcp.DefaultString("")),
|
||||||
|
mcp.WithString("body", mcp.Description("issue body content")),
|
||||||
|
mcp.WithArray("assignees", mcp.Description("usernames to assign to this issue"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
mcp.WithNumber("milestone", mcp.Description("milestone number")),
|
||||||
|
mcp.WithString("state", mcp.Description("issue state, one of open, closed, all")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditIssueCommentTool = mcp.NewTool(
|
||||||
|
EditIssueCommentToolName,
|
||||||
|
mcp.WithDescription("edit issue comment"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("commentID", mcp.Required(), mcp.Description("id of issue comment")),
|
||||||
|
mcp.WithString("body", mcp.Required(), mcp.Description("issue comment body")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetIssueCommentsByIndexTool = mcp.NewTool(
|
||||||
|
GetIssueCommentsByIndexToolName,
|
||||||
|
mcp.WithDescription("get issue comment by index"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
func init() {
|
||||||
s.AddTool(GetIssueByIndexTool, GetIssueByIndexFn)
|
Tool.RegisterRead(server.ServerTool{
|
||||||
s.AddTool(ListRepoIssuesTool, ListRepoIssuesFn)
|
Tool: GetIssueByIndexTool,
|
||||||
s.AddTool(CreateIssueTool, CreateIssueFn)
|
Handler: GetIssueByIndexFn,
|
||||||
s.AddTool(CreateIssueCommentTool, CreateIssueCommentFn)
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListRepoIssuesTool,
|
||||||
|
Handler: ListRepoIssuesFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateIssueTool,
|
||||||
|
Handler: CreateIssueFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateIssueCommentTool,
|
||||||
|
Handler: CreateIssueCommentFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditIssueTool,
|
||||||
|
Handler: EditIssueFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditIssueCommentTool,
|
||||||
|
Handler: EditIssueCommentFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetIssueCommentsByIndexTool,
|
||||||
|
Handler: GetIssueCommentsByIndexFn,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIssueByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetIssueByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetIssueByIndexFn")
|
log.Debugf("Called GetIssueByIndexFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
index, ok := req.Params.Arguments["index"].(float64)
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
if !ok {
|
|
||||||
return to.ErrorResult(fmt.Errorf("index is required"))
|
|
||||||
}
|
|
||||||
issue, _, err := gitea.Client().GetIssue(owner, repo, int64(index))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get %v/%v/issue/%v err: %v", owner, repo, int64(index), err))
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issue, _, err := client.GetIssue(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/issue/%v err: %v", owner, repo, index, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(issue)
|
return to.TextResult(issue)
|
||||||
@@ -88,26 +155,20 @@ func GetIssueByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallT
|
|||||||
|
|
||||||
func ListRepoIssuesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ListRepoIssuesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called ListIssuesFn")
|
log.Debugf("Called ListIssuesFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
state, ok := req.Params.Arguments["state"].(string)
|
state, ok := req.GetArguments()["state"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
state = "all"
|
state = "all"
|
||||||
}
|
}
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
if !ok {
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, ok := req.Params.Arguments["pageSize"].(float64)
|
|
||||||
if !ok {
|
|
||||||
pageSize = 100
|
|
||||||
}
|
|
||||||
opt := gitea_sdk.ListIssueOption{
|
opt := gitea_sdk.ListIssueOption{
|
||||||
State: gitea_sdk.StateType(state),
|
State: gitea_sdk.StateType(state),
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
@@ -115,7 +176,11 @@ func ListRepoIssuesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTo
|
|||||||
PageSize: int(pageSize),
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
issues, _, err := gitea.Client().ListRepoIssues(owner, repo, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issues, _, err := client.ListRepoIssues(owner, repo, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get %v/%v/issues err: %v", owner, repo, err))
|
return to.ErrorResult(fmt.Errorf("get %v/%v/issues err: %v", owner, repo, err))
|
||||||
}
|
}
|
||||||
@@ -124,28 +189,32 @@ func ListRepoIssuesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTo
|
|||||||
|
|
||||||
func CreateIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreateIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called CreateIssueFn")
|
log.Debugf("Called CreateIssueFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
title, ok := req.Params.Arguments["title"].(string)
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("title is required"))
|
return to.ErrorResult(errors.New("title is required"))
|
||||||
}
|
}
|
||||||
body, ok := req.Params.Arguments["body"].(string)
|
body, ok := req.GetArguments()["body"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("body is required"))
|
return to.ErrorResult(errors.New("body is required"))
|
||||||
}
|
}
|
||||||
issue, _, err := gitea.Client().CreateIssue(owner, repo, gitea_sdk.CreateIssueOption{
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issue, _, err := client.CreateIssue(owner, repo, gitea_sdk.CreateIssueOption{
|
||||||
Title: title,
|
Title: title,
|
||||||
Body: body,
|
Body: body,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("create %v/%v/issue err", owner, repo))
|
return to.ErrorResult(fmt.Errorf("create %v/%v/issue err: %v", owner, repo, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(issue)
|
return to.TextResult(issue)
|
||||||
@@ -153,29 +222,151 @@ func CreateIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
|
|||||||
|
|
||||||
func CreateIssueCommentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreateIssueCommentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called CreateIssueCommentFn")
|
log.Debugf("Called CreateIssueCommentFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
index, ok := req.Params.Arguments["index"].(float64)
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
if !ok {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("index is required"))
|
return to.ErrorResult(err)
|
||||||
}
|
}
|
||||||
body, ok := req.Params.Arguments["body"].(string)
|
body, ok := req.GetArguments()["body"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("body is required"))
|
return to.ErrorResult(errors.New("body is required"))
|
||||||
}
|
}
|
||||||
opt := gitea_sdk.CreateIssueCommentOption{
|
opt := gitea_sdk.CreateIssueCommentOption{
|
||||||
Body: body,
|
Body: body,
|
||||||
}
|
}
|
||||||
issueComment, _, err := gitea.Client().CreateIssueComment(owner, repo, int64(index), opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("create %v/%v/issue/%v/comment err", owner, repo, int64(index)))
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issueComment, _, err := client.CreateIssueComment(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create %v/%v/issue/%v/comment err: %v", owner, repo, index, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(issueComment)
|
return to.TextResult(issueComment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EditIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditIssueFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.EditIssueOption{}
|
||||||
|
|
||||||
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.Title = title
|
||||||
|
}
|
||||||
|
body, ok := req.GetArguments()["body"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.Body = new(body)
|
||||||
|
}
|
||||||
|
var assignees []string
|
||||||
|
if assigneesArg, exists := req.GetArguments()["assignees"]; exists {
|
||||||
|
if assigneesSlice, ok := assigneesArg.([]any); ok {
|
||||||
|
for _, assignee := range assigneesSlice {
|
||||||
|
if assigneeStr, ok := assignee.(string); ok {
|
||||||
|
assignees = append(assignees, assigneeStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opt.Assignees = assignees
|
||||||
|
if val, exists := req.GetArguments()["milestone"]; exists {
|
||||||
|
if milestone, ok := params.ToInt64(val); ok {
|
||||||
|
opt.Milestone = new(milestone)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state, ok := req.GetArguments()["state"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.State = new(gitea_sdk.StateType(state))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issue, _, err := client.EditIssue(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/issue/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(issue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditIssueCommentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditIssueCommentFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
commentID, err := params.GetIndex(req.GetArguments(), "commentID")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
body, ok := req.GetArguments()["body"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("body is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditIssueCommentOption{
|
||||||
|
Body: body,
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issueComment, _, err := client.EditIssueComment(owner, repo, commentID, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/issues/comments/%v err: %v", owner, repo, commentID, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(issueComment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIssueCommentsByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetIssueCommentsByIndexFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.ListIssueCommentOptions{}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issue, _, err := client.ListIssueComments(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/issues/%v/comments err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(issue)
|
||||||
|
}
|
||||||
|
|||||||
151
operation/issue/pin.go
Normal file
151
operation/issue/pin.go
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
package issue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListPinnedIssuesToolName = "list_pinned_issues"
|
||||||
|
PinIssueToolName = "pin_issue"
|
||||||
|
UnpinIssueToolName = "unpin_issue"
|
||||||
|
MoveIssuePinToolName = "move_issue_pin"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListPinnedIssuesTool = mcp.NewTool(
|
||||||
|
ListPinnedIssuesToolName,
|
||||||
|
mcp.WithDescription("List pinned issues in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
PinIssueTool = mcp.NewTool(
|
||||||
|
PinIssueToolName,
|
||||||
|
mcp.WithDescription("Pin an issue in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnpinIssueTool = mcp.NewTool(
|
||||||
|
UnpinIssueToolName,
|
||||||
|
mcp.WithDescription("Unpin an issue from a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
MoveIssuePinTool = mcp.NewTool(
|
||||||
|
MoveIssuePinToolName,
|
||||||
|
mcp.WithDescription("Move a pinned issue to a new position"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithNumber("position", mcp.Required(), mcp.Description("new position (1-based)")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListPinnedIssuesTool, Handler: ListPinnedIssuesFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: PinIssueTool, Handler: PinIssueFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnpinIssueTool, Handler: UnpinIssueFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: MoveIssuePinTool, Handler: MoveIssuePinFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListPinnedIssuesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListPinnedIssuesFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issues, _, err := client.ListRepoPinnedIssues(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list pinned issues err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(issues)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PinIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called PinIssueFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.PinIssue(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("pin issue err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "pinned"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnpinIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnpinIssueFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.UnpinIssue(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unpin issue err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unpinned"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func MoveIssuePinFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called MoveIssuePinFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
position, ok := req.GetArguments()["position"].(float64)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("position is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.MoveIssuePin(owner, repo, index, int64(position))
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("move issue pin err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "moved", "position": int64(position)})
|
||||||
|
}
|
||||||
224
operation/issue/reaction.go
Normal file
224
operation/issue/reaction.go
Normal file
@@ -0,0 +1,224 @@
|
|||||||
|
package issue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListIssueReactionsToolName = "list_issue_reactions"
|
||||||
|
PostIssueReactionToolName = "post_issue_reaction"
|
||||||
|
DeleteIssueReactionToolName = "delete_issue_reaction"
|
||||||
|
GetIssueCommentReactionsToolName = "get_issue_comment_reactions"
|
||||||
|
PostIssueCommentReactionToolName = "post_issue_comment_reaction"
|
||||||
|
DeleteIssueCommentReactionToolName = "delete_issue_comment_reaction"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListIssueReactionsTool = mcp.NewTool(
|
||||||
|
ListIssueReactionsToolName,
|
||||||
|
mcp.WithDescription("List reactions on an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
PostIssueReactionTool = mcp.NewTool(
|
||||||
|
PostIssueReactionToolName,
|
||||||
|
mcp.WithDescription("Add a reaction to an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithString("reaction", mcp.Required(), mcp.Description("reaction emoji (e.g., +1, -1, laugh, confused, heart, hooray, rocket, eyes)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteIssueReactionTool = mcp.NewTool(
|
||||||
|
DeleteIssueReactionToolName,
|
||||||
|
mcp.WithDescription("Remove a reaction from an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithString("reaction", mcp.Required(), mcp.Description("reaction emoji to remove")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetIssueCommentReactionsTool = mcp.NewTool(
|
||||||
|
GetIssueCommentReactionsToolName,
|
||||||
|
mcp.WithDescription("List reactions on an issue comment"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("comment_id", mcp.Required(), mcp.Description("comment ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
PostIssueCommentReactionTool = mcp.NewTool(
|
||||||
|
PostIssueCommentReactionToolName,
|
||||||
|
mcp.WithDescription("Add a reaction to an issue comment"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("comment_id", mcp.Required(), mcp.Description("comment ID")),
|
||||||
|
mcp.WithString("reaction", mcp.Required(), mcp.Description("reaction emoji")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteIssueCommentReactionTool = mcp.NewTool(
|
||||||
|
DeleteIssueCommentReactionToolName,
|
||||||
|
mcp.WithDescription("Remove a reaction from an issue comment"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("comment_id", mcp.Required(), mcp.Description("comment ID")),
|
||||||
|
mcp.WithString("reaction", mcp.Required(), mcp.Description("reaction emoji to remove")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListIssueReactionsTool, Handler: ListIssueReactionsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetIssueCommentReactionsTool, Handler: GetIssueCommentReactionsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: PostIssueReactionTool, Handler: PostIssueReactionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteIssueReactionTool, Handler: DeleteIssueReactionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: PostIssueCommentReactionTool, Handler: PostIssueCommentReactionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteIssueCommentReactionTool, Handler: DeleteIssueCommentReactionFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListIssueReactionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListIssueReactionsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
reactions, _, err := client.ListIssueReactions(owner, repo, index, gitea_sdk.ListIssueReactionsOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list issue reactions err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(reactions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PostIssueReactionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called PostIssueReactionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
reaction, _ := req.GetArguments()["reaction"].(string)
|
||||||
|
if owner == "" || repo == "" || reaction == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and reaction are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
r, _, err := client.PostIssueReaction(owner, repo, index, reaction)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("post issue reaction err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteIssueReactionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteIssueReactionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
reaction, _ := req.GetArguments()["reaction"].(string)
|
||||||
|
if owner == "" || repo == "" || reaction == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and reaction are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteIssueReaction(owner, repo, index, reaction)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete issue reaction err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "removed", "reaction": reaction})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIssueCommentReactionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetIssueCommentReactionsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
commentID, err := params.GetIndex(req.GetArguments(), "comment_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
reactions, _, err := client.GetIssueCommentReactions(owner, repo, commentID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get issue comment reactions err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(reactions)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PostIssueCommentReactionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called PostIssueCommentReactionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
reaction, _ := req.GetArguments()["reaction"].(string)
|
||||||
|
if owner == "" || repo == "" || reaction == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and reaction are required"))
|
||||||
|
}
|
||||||
|
commentID, err := params.GetIndex(req.GetArguments(), "comment_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
r, _, err := client.PostIssueCommentReaction(owner, repo, commentID, reaction)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("post issue comment reaction err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteIssueCommentReactionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteIssueCommentReactionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
reaction, _ := req.GetArguments()["reaction"].(string)
|
||||||
|
if owner == "" || repo == "" || reaction == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and reaction are required"))
|
||||||
|
}
|
||||||
|
commentID, err := params.GetIndex(req.GetArguments(), "comment_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteIssueCommentReaction(owner, repo, commentID, reaction)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete issue comment reaction err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "removed", "reaction": reaction})
|
||||||
|
}
|
||||||
152
operation/issue/subscription.go
Normal file
152
operation/issue/subscription.go
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
package issue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListIssueSubscribersToolName = "list_issue_subscribers"
|
||||||
|
CheckIssueSubscriptionToolName = "check_issue_subscription"
|
||||||
|
IssueSubscribeToolName = "issue_subscribe"
|
||||||
|
IssueUnSubscribeToolName = "issue_unsubscribe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListIssueSubscribersTool = mcp.NewTool(
|
||||||
|
ListIssueSubscribersToolName,
|
||||||
|
mcp.WithDescription("List users subscribed to an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckIssueSubscriptionTool = mcp.NewTool(
|
||||||
|
CheckIssueSubscriptionToolName,
|
||||||
|
mcp.WithDescription("Check if the authenticated user is subscribed to an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
IssueSubscribeTool = mcp.NewTool(
|
||||||
|
IssueSubscribeToolName,
|
||||||
|
mcp.WithDescription("Subscribe to an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
IssueUnSubscribeTool = mcp.NewTool(
|
||||||
|
IssueUnSubscribeToolName,
|
||||||
|
mcp.WithDescription("Unsubscribe from an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListIssueSubscribersTool, Handler: ListIssueSubscribersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckIssueSubscriptionTool, Handler: CheckIssueSubscriptionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: IssueSubscribeTool, Handler: IssueSubscribeFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: IssueUnSubscribeTool, Handler: IssueUnSubscribeFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListIssueSubscribersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListIssueSubscribersFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.ListIssueSubscribers(owner, repo, index, gitea_sdk.ListIssueSubscribersOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list issue subscribers err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckIssueSubscriptionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckIssueSubscriptionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
info, _, err := client.CheckIssueSubscription(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check issue subscription err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IssueSubscribeFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called IssueSubscribeFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.IssueSubscribe(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("issue subscribe err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "subscribed"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func IssueUnSubscribeFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called IssueUnSubscribeFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.IssueUnSubscribe(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("issue unsubscribe err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unsubscribed"})
|
||||||
|
}
|
||||||
89
operation/issue/timeline.go
Normal file
89
operation/issue/timeline.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
package issue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListIssueTimelineToolName = "list_issue_timeline"
|
||||||
|
GetIssueTemplatesToolName = "get_issue_templates"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListIssueTimelineTool = mcp.NewTool(
|
||||||
|
ListIssueTimelineToolName,
|
||||||
|
mcp.WithDescription("List timeline events for an issue (comments, labels, milestones, references, etc.)"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetIssueTemplatesTool = mcp.NewTool(
|
||||||
|
GetIssueTemplatesToolName,
|
||||||
|
mcp.WithDescription("Get issue templates for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListIssueTimelineTool, Handler: ListIssueTimelineFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetIssueTemplatesTool, Handler: GetIssueTemplatesFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListIssueTimelineFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListIssueTimelineFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
events, _, err := client.ListIssueTimeline(owner, repo, index, gitea_sdk.ListIssueCommentOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list issue timeline err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(events)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIssueTemplatesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetIssueTemplatesFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
templates, _, err := client.GetIssueTemplates(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get issue templates err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(templates)
|
||||||
|
}
|
||||||
624
operation/label/label.go
Normal file
624
operation/label/label.go
Normal file
@@ -0,0 +1,624 @@
|
|||||||
|
package label
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListRepoLabelsToolName = "list_repo_labels"
|
||||||
|
GetRepoLabelToolName = "get_repo_label"
|
||||||
|
CreateRepoLabelToolName = "create_repo_label"
|
||||||
|
EditRepoLabelToolName = "edit_repo_label"
|
||||||
|
DeleteRepoLabelToolName = "delete_repo_label"
|
||||||
|
AddIssueLabelsToolName = "add_issue_labels"
|
||||||
|
ReplaceIssueLabelsToolName = "replace_issue_labels"
|
||||||
|
ClearIssueLabelsToolName = "clear_issue_labels"
|
||||||
|
RemoveIssueLabelToolName = "remove_issue_label"
|
||||||
|
ListOrgLabelsToolName = "list_org_labels"
|
||||||
|
CreateOrgLabelToolName = "create_org_label"
|
||||||
|
EditOrgLabelToolName = "edit_org_label"
|
||||||
|
DeleteOrgLabelToolName = "delete_org_label"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListRepoLabelsTool = mcp.NewTool(
|
||||||
|
ListRepoLabelsToolName,
|
||||||
|
mcp.WithDescription("Lists all labels for a given repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoLabelTool = mcp.NewTool(
|
||||||
|
GetRepoLabelToolName,
|
||||||
|
mcp.WithDescription("Gets a single label by its ID for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("label ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateRepoLabelTool = mcp.NewTool(
|
||||||
|
CreateRepoLabelToolName,
|
||||||
|
mcp.WithDescription("Creates a new label for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("label name")),
|
||||||
|
mcp.WithString("color", mcp.Required(), mcp.Description("label color (hex code, e.g., #RRGGBB)")),
|
||||||
|
mcp.WithString("description", mcp.Description("label description")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditRepoLabelTool = mcp.NewTool(
|
||||||
|
EditRepoLabelToolName,
|
||||||
|
mcp.WithDescription("Edits an existing label in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("label ID")),
|
||||||
|
mcp.WithString("name", mcp.Description("new label name")),
|
||||||
|
mcp.WithString("color", mcp.Description("new label color (hex code, e.g., #RRGGBB)")),
|
||||||
|
mcp.WithString("description", mcp.Description("new label description")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteRepoLabelTool = mcp.NewTool(
|
||||||
|
DeleteRepoLabelToolName,
|
||||||
|
mcp.WithDescription("Deletes a label from a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("label ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddIssueLabelsTool = mcp.NewTool(
|
||||||
|
AddIssueLabelsToolName,
|
||||||
|
mcp.WithDescription("Adds one or more labels to an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithArray("labels", mcp.Required(), mcp.Description("array of label IDs to add"), mcp.Items(map[string]any{"type": "number"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
ReplaceIssueLabelsTool = mcp.NewTool(
|
||||||
|
ReplaceIssueLabelsToolName,
|
||||||
|
mcp.WithDescription("Replaces all labels on an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithArray("labels", mcp.Required(), mcp.Description("array of label IDs to replace with"), mcp.Items(map[string]any{"type": "number"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
ClearIssueLabelsTool = mcp.NewTool(
|
||||||
|
ClearIssueLabelsToolName,
|
||||||
|
mcp.WithDescription("Removes all labels from an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RemoveIssueLabelTool = mcp.NewTool(
|
||||||
|
RemoveIssueLabelToolName,
|
||||||
|
mcp.WithDescription("Removes a single label from an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithNumber("label_id", mcp.Required(), mcp.Description("label ID to remove")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListOrgLabelsTool = mcp.NewTool(
|
||||||
|
ListOrgLabelsToolName,
|
||||||
|
mcp.WithDescription("Lists labels defined at organization level"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateOrgLabelTool = mcp.NewTool(
|
||||||
|
CreateOrgLabelToolName,
|
||||||
|
mcp.WithDescription("Creates a new label for an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("label name")),
|
||||||
|
mcp.WithString("color", mcp.Required(), mcp.Description("label color (hex code, e.g., #RRGGBB)")),
|
||||||
|
mcp.WithString("description", mcp.Description("label description")),
|
||||||
|
mcp.WithBoolean("exclusive", mcp.Description("whether the label is exclusive"), mcp.DefaultBool(false)),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditOrgLabelTool = mcp.NewTool(
|
||||||
|
EditOrgLabelToolName,
|
||||||
|
mcp.WithDescription("Edits an existing organization label"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("label ID")),
|
||||||
|
mcp.WithString("name", mcp.Description("new label name")),
|
||||||
|
mcp.WithString("color", mcp.Description("new label color (hex code, e.g., #RRGGBB)")),
|
||||||
|
mcp.WithString("description", mcp.Description("new label description")),
|
||||||
|
mcp.WithBoolean("exclusive", mcp.Description("whether the label is exclusive")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteOrgLabelTool = mcp.NewTool(
|
||||||
|
DeleteOrgLabelToolName,
|
||||||
|
mcp.WithDescription("Deletes an organization label by ID"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("label ID")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListRepoLabelsTool,
|
||||||
|
Handler: ListRepoLabelsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetRepoLabelTool,
|
||||||
|
Handler: GetRepoLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateRepoLabelTool,
|
||||||
|
Handler: CreateRepoLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditRepoLabelTool,
|
||||||
|
Handler: EditRepoLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteRepoLabelTool,
|
||||||
|
Handler: DeleteRepoLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: AddIssueLabelsTool,
|
||||||
|
Handler: AddIssueLabelsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: ReplaceIssueLabelsTool,
|
||||||
|
Handler: ReplaceIssueLabelsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: ClearIssueLabelsTool,
|
||||||
|
Handler: ClearIssueLabelsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: RemoveIssueLabelTool,
|
||||||
|
Handler: RemoveIssueLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListOrgLabelsTool,
|
||||||
|
Handler: ListOrgLabelsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateOrgLabelTool,
|
||||||
|
Handler: CreateOrgLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditOrgLabelTool,
|
||||||
|
Handler: EditOrgLabelFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteOrgLabelTool,
|
||||||
|
Handler: DeleteOrgLabelFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoLabelsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoLabelsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
opt := gitea_sdk.ListLabelsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
labels, _, err := client.ListRepoLabels(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %v/%v/labels err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(labels)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoLabelFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
label, _, err := client.GetRepoLabel(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/label/%v err: %v", owner, repo, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(label)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateRepoLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateRepoLabelFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
color, ok := req.GetArguments()["color"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("color is required"))
|
||||||
|
}
|
||||||
|
description, _ := req.GetArguments()["description"].(string) // Optional
|
||||||
|
|
||||||
|
opt := gitea_sdk.CreateLabelOption{
|
||||||
|
Name: name,
|
||||||
|
Color: color,
|
||||||
|
Description: description,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
label, _, err := client.CreateLabel(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create %v/%v/label err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(label)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditRepoLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditRepoLabelFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.EditLabelOption{}
|
||||||
|
if name, ok := req.GetArguments()["name"].(string); ok {
|
||||||
|
opt.Name = new(name)
|
||||||
|
}
|
||||||
|
if color, ok := req.GetArguments()["color"].(string); ok {
|
||||||
|
opt.Color = new(color)
|
||||||
|
}
|
||||||
|
if description, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = new(description)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
label, _, err := client.EditLabel(owner, repo, id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/label/%v err: %v", owner, repo, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(label)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteRepoLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteRepoLabelFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteLabel(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete %v/%v/label/%v err: %v", owner, repo, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult("Label deleted successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddIssueLabelsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddIssueLabelsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
labelsRaw, ok := req.GetArguments()["labels"].([]any)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("labels (array of IDs) is required"))
|
||||||
|
}
|
||||||
|
var labels []int64
|
||||||
|
for _, l := range labelsRaw {
|
||||||
|
if labelID, ok := params.ToInt64(l); ok {
|
||||||
|
labels = append(labels, labelID)
|
||||||
|
} else {
|
||||||
|
return to.ErrorResult(errors.New("invalid label ID in labels array"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.IssueLabelsOption{
|
||||||
|
Labels: labels,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issueLabels, _, err := client.AddIssueLabels(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add labels to %v/%v/issue/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(issueLabels)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReplaceIssueLabelsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ReplaceIssueLabelsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
labelsRaw, ok := req.GetArguments()["labels"].([]any)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("labels (array of IDs) is required"))
|
||||||
|
}
|
||||||
|
var labels []int64
|
||||||
|
for _, l := range labelsRaw {
|
||||||
|
if labelID, ok := params.ToInt64(l); ok {
|
||||||
|
labels = append(labels, labelID)
|
||||||
|
} else {
|
||||||
|
return to.ErrorResult(errors.New("invalid label ID in labels array"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.IssueLabelsOption{
|
||||||
|
Labels: labels,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
issueLabels, _, err := client.ReplaceIssueLabels(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("replace labels on %v/%v/issue/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(issueLabels)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearIssueLabelsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ClearIssueLabelsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.ClearIssueLabels(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("clear labels on %v/%v/issue/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult("Labels cleared successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveIssueLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RemoveIssueLabelFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
labelID, err := params.GetIndex(req.GetArguments(), "label_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteIssueLabel(owner, repo, index, labelID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("remove label %v from %v/%v/issue/%v err: %v", labelID, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult("Label removed successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgLabelsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgLabelsFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
opt := gitea_sdk.ListOrgLabelsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
labels, _, err := client.ListOrgLabels(org, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %v/labels err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(labels)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOrgLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateOrgLabelFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
color, ok := req.GetArguments()["color"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("color is required"))
|
||||||
|
}
|
||||||
|
description, _ := req.GetArguments()["description"].(string)
|
||||||
|
exclusive, _ := req.GetArguments()["exclusive"].(bool)
|
||||||
|
|
||||||
|
opt := gitea_sdk.CreateOrgLabelOption{
|
||||||
|
Name: name,
|
||||||
|
Color: color,
|
||||||
|
Description: description,
|
||||||
|
Exclusive: exclusive,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
label, _, err := client.CreateOrgLabel(org, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create %v/labels err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(label)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditOrgLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditOrgLabelFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.EditOrgLabelOption{}
|
||||||
|
if name, ok := req.GetArguments()["name"].(string); ok {
|
||||||
|
opt.Name = new(name)
|
||||||
|
}
|
||||||
|
if color, ok := req.GetArguments()["color"].(string); ok {
|
||||||
|
opt.Color = new(color)
|
||||||
|
}
|
||||||
|
if description, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = new(description)
|
||||||
|
}
|
||||||
|
if exclusive, ok := req.GetArguments()["exclusive"].(bool); ok {
|
||||||
|
opt.Exclusive = new(exclusive)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
label, _, err := client.EditOrgLabel(org, id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/labels/%v err: %v", org, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(label)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteOrgLabelFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteOrgLabelFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteOrgLabel(org, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete %v/labels/%v err: %v", org, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult("Label deleted successfully")
|
||||||
|
}
|
||||||
270
operation/milestone/milestone.go
Normal file
270
operation/milestone/milestone.go
Normal file
@@ -0,0 +1,270 @@
|
|||||||
|
package milestone
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetMilestoneToolName = "get_milestone"
|
||||||
|
ListMilestonesToolName = "list_milestones"
|
||||||
|
CreateMilestoneToolName = "create_milestone"
|
||||||
|
EditMilestoneToolName = "edit_milestone"
|
||||||
|
DeleteMilestoneToolName = "delete_milestone"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GetMilestoneTool = mcp.NewTool(
|
||||||
|
GetMilestoneToolName,
|
||||||
|
mcp.WithDescription("get milestone by id"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("milestone id")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListMilestonesTool = mcp.NewTool(
|
||||||
|
ListMilestonesToolName,
|
||||||
|
mcp.WithDescription("List milestones"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("state", mcp.Description("milestone state"), mcp.DefaultString("all")),
|
||||||
|
mcp.WithString("name", mcp.Description("milestone name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateMilestoneTool = mcp.NewTool(
|
||||||
|
CreateMilestoneToolName,
|
||||||
|
mcp.WithDescription("create milestone"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("title", mcp.Required(), mcp.Description("milestone title")),
|
||||||
|
mcp.WithString("description", mcp.Description("milestone description")),
|
||||||
|
mcp.WithString("due_on", mcp.Description("due date")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditMilestoneTool = mcp.NewTool(
|
||||||
|
EditMilestoneToolName,
|
||||||
|
mcp.WithDescription("edit milestone"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("milestone id")),
|
||||||
|
mcp.WithString("title", mcp.Description("milestone title")),
|
||||||
|
mcp.WithString("description", mcp.Description("milestone description")),
|
||||||
|
mcp.WithString("due_on", mcp.Description("due date")),
|
||||||
|
mcp.WithString("state", mcp.Description("milestone state, one of open, closed")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteMilestoneTool = mcp.NewTool(
|
||||||
|
DeleteMilestoneToolName,
|
||||||
|
mcp.WithDescription("delete milestone"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("milestone id")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetMilestoneTool,
|
||||||
|
Handler: GetMilestoneFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListMilestonesTool,
|
||||||
|
Handler: ListMilestonesFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateMilestoneTool,
|
||||||
|
Handler: CreateMilestoneFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditMilestoneTool,
|
||||||
|
Handler: EditMilestoneFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteMilestoneTool,
|
||||||
|
Handler: DeleteMilestoneFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMilestoneFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetMilestoneFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
milestone, _, err := client.GetMilestone(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/milestone/%v err: %v", owner, repo, id, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(milestone)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMilestonesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMilestonesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
state, ok := req.GetArguments()["state"].(string)
|
||||||
|
if !ok {
|
||||||
|
state = "all"
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
name = ""
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
opt := gitea_sdk.ListMilestoneOption{
|
||||||
|
State: gitea_sdk.StateType(state),
|
||||||
|
Name: name,
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
milestones, _, err := client.ListRepoMilestones(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/milestones err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(milestones)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateMilestoneFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateMilestoneFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("title is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.CreateMilestoneOption{
|
||||||
|
Title: title,
|
||||||
|
}
|
||||||
|
|
||||||
|
description, ok := req.GetArguments()["description"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.Description = description
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
milestone, _, err := client.CreateMilestone(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create %v/%v/milestone err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(milestone)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditMilestoneFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditMilestoneFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.EditMilestoneOption{}
|
||||||
|
|
||||||
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.Title = title
|
||||||
|
}
|
||||||
|
description, ok := req.GetArguments()["description"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.Description = new(description)
|
||||||
|
}
|
||||||
|
state, ok := req.GetArguments()["state"].(string)
|
||||||
|
if ok {
|
||||||
|
opt.State = new(gitea_sdk.StateType(state))
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
milestone, _, err := client.EditMilestone(owner, repo, id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/milestone/%v err: %v", owner, repo, id, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(milestone)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteMilestoneFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteMilestoneFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteMilestone(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete %v/%v/milestone/%v err: %v", owner, repo, id, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult("Milestone deleted successfully")
|
||||||
|
}
|
||||||
325
operation/miscellaneous/miscellaneous.go
Normal file
325
operation/miscellaneous/miscellaneous.go
Normal file
@@ -0,0 +1,325 @@
|
|||||||
|
package miscellaneous
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetServerVersionToolName = "get_server_version"
|
||||||
|
ListGitignoreTemplatesToolName = "list_gitignore_templates"
|
||||||
|
GetGitignoreTemplateToolName = "get_gitignore_template"
|
||||||
|
ListLabelTemplatesToolName = "list_label_templates"
|
||||||
|
GetLabelTemplateToolName = "get_label_template"
|
||||||
|
ListLicenseTemplatesToolName = "list_license_templates"
|
||||||
|
GetLicenseTemplateToolName = "get_license_template"
|
||||||
|
RenderMarkdownToolName = "render_markdown"
|
||||||
|
RenderMarkupToolName = "render_markup"
|
||||||
|
GetNodeInfoToolName = "get_node_info"
|
||||||
|
GetSigningKeyGPGToolName = "get_signing_key_gpg"
|
||||||
|
GetSigningKeySSHToolName = "get_signing_key_ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GetServerVersionTool = mcp.NewTool(
|
||||||
|
GetServerVersionToolName,
|
||||||
|
mcp.WithDescription("Get the Gitea server version"),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListGitignoreTemplatesTool = mcp.NewTool(
|
||||||
|
ListGitignoreTemplatesToolName,
|
||||||
|
mcp.WithDescription("List available .gitignore templates"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetGitignoreTemplateTool = mcp.NewTool(
|
||||||
|
GetGitignoreTemplateToolName,
|
||||||
|
mcp.WithDescription("Get the content of a .gitignore template by name"),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("template name (e.g., Go, Python, Node)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListLabelTemplatesTool = mcp.NewTool(
|
||||||
|
ListLabelTemplatesToolName,
|
||||||
|
mcp.WithDescription("List available label templates"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetLabelTemplateTool = mcp.NewTool(
|
||||||
|
GetLabelTemplateToolName,
|
||||||
|
mcp.WithDescription("Get the labels from a label template"),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("template name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListLicenseTemplatesTool = mcp.NewTool(
|
||||||
|
ListLicenseTemplatesToolName,
|
||||||
|
mcp.WithDescription("List available license templates"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetLicenseTemplateTool = mcp.NewTool(
|
||||||
|
GetLicenseTemplateToolName,
|
||||||
|
mcp.WithDescription("Get the content of a license template"),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("license name (e.g., MIT, GPL-3.0, Apache-2.0)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RenderMarkdownTool = mcp.NewTool(
|
||||||
|
RenderMarkdownToolName,
|
||||||
|
mcp.WithDescription("Render markdown text to HTML"),
|
||||||
|
mcp.WithString("text", mcp.Required(), mcp.Description("markdown text to render")),
|
||||||
|
mcp.WithString("mode", mcp.Description("render mode: markdown, gfm, comment (default: markdown)")),
|
||||||
|
mcp.WithString("context", mcp.Description("context for relative links (owner/repo format)")),
|
||||||
|
mcp.WithBoolean("wiki", mcp.Description("treat text as wiki content")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RenderMarkupTool = mcp.NewTool(
|
||||||
|
RenderMarkupToolName,
|
||||||
|
mcp.WithDescription("Render markup content to HTML (supports multiple markup languages)"),
|
||||||
|
mcp.WithString("text", mcp.Required(), mcp.Description("markup text to render")),
|
||||||
|
mcp.WithString("mode", mcp.Description("render mode")),
|
||||||
|
mcp.WithString("context", mcp.Description("context for relative links (owner/repo format)")),
|
||||||
|
mcp.WithString("filepath", mcp.Description("file path to determine markup language")),
|
||||||
|
mcp.WithBoolean("wiki", mcp.Description("treat text as wiki content")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetNodeInfoTool = mcp.NewTool(
|
||||||
|
GetNodeInfoToolName,
|
||||||
|
mcp.WithDescription("Get the NodeInfo of the Gitea instance (federation metadata)"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetSigningKeyGPGTool = mcp.NewTool(
|
||||||
|
GetSigningKeyGPGToolName,
|
||||||
|
mcp.WithDescription("Get the GPG signing key of the Gitea instance"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetSigningKeySSHTool = mcp.NewTool(
|
||||||
|
GetSigningKeySSHToolName,
|
||||||
|
mcp.WithDescription("Get the SSH signing key of the Gitea instance"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetServerVersionTool, Handler: GetServerVersionFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListGitignoreTemplatesTool, Handler: ListGitignoreTemplatesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetGitignoreTemplateTool, Handler: GetGitignoreTemplateFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListLabelTemplatesTool, Handler: ListLabelTemplatesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetLabelTemplateTool, Handler: GetLabelTemplateFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListLicenseTemplatesTool, Handler: ListLicenseTemplatesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetLicenseTemplateTool, Handler: GetLicenseTemplateFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetNodeInfoTool, Handler: GetNodeInfoFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetSigningKeyGPGTool, Handler: GetSigningKeyGPGFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetSigningKeySSHTool, Handler: GetSigningKeySSHFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RenderMarkdownTool, Handler: RenderMarkdownFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RenderMarkupTool, Handler: RenderMarkupFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetServerVersionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetServerVersionFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
version, _, err := client.ServerVersion()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get server version err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"version": version})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListGitignoreTemplatesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListGitignoreTemplatesFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
templates, _, err := client.ListGitignoresTemplates()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list gitignore templates err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(templates)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetGitignoreTemplateFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetGitignoreTemplateFn")
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
info, _, err := client.GetGitignoreTemplateInfo(name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitignore template err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListLabelTemplatesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListLabelTemplatesFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
templates, _, err := client.ListLabelTemplates()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list label templates err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(templates)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLabelTemplateFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetLabelTemplateFn")
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
labels, _, err := client.GetLabelTemplate(name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get label template err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(labels)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListLicenseTemplatesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListLicenseTemplatesFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
templates, _, err := client.ListLicenseTemplates()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list license templates err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(templates)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLicenseTemplateFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetLicenseTemplateFn")
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if name == "" {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
info, _, err := client.GetLicenseTemplateInfo(name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get license template err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderMarkdownFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RenderMarkdownFn")
|
||||||
|
text, _ := req.GetArguments()["text"].(string)
|
||||||
|
if text == "" {
|
||||||
|
return to.ErrorResult(errors.New("text is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.MarkdownOption{
|
||||||
|
Text: text,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["mode"].(string); ok {
|
||||||
|
opt.Mode = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["context"].(string); ok {
|
||||||
|
opt.Context = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["wiki"].(bool); ok {
|
||||||
|
opt.Wiki = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
html, _, err := client.RenderMarkdown(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("render markdown err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"html": html})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderMarkupFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RenderMarkupFn")
|
||||||
|
text, _ := req.GetArguments()["text"].(string)
|
||||||
|
if text == "" {
|
||||||
|
return to.ErrorResult(errors.New("text is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.MarkupOption{
|
||||||
|
Text: text,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["mode"].(string); ok {
|
||||||
|
opt.Mode = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["context"].(string); ok {
|
||||||
|
opt.Context = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["filepath"].(string); ok {
|
||||||
|
opt.FilePath = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["wiki"].(bool); ok {
|
||||||
|
opt.Wiki = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
html, _, err := client.RenderMarkup(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("render markup err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"html": html})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNodeInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetNodeInfoFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
info, _, err := client.GetNodeInfo()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get node info err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSigningKeyGPGFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetSigningKeyGPGFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
key, _, err := client.GetSigningKeyGPG()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get signing key GPG err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"gpg_key": key})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSigningKeySSHFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetSigningKeySSHFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
key, _, err := client.GetSigningKeySSH()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get signing key SSH err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"ssh_key": key})
|
||||||
|
}
|
||||||
219
operation/notification/notification.go
Normal file
219
operation/notification/notification.go
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
package notification
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListNotificationsToolName = "list_notifications"
|
||||||
|
CheckNewNotificationsToolName = "check_new_notifications"
|
||||||
|
GetNotificationToolName = "get_notification"
|
||||||
|
ReadNotificationToolName = "read_notification"
|
||||||
|
ReadAllNotificationsToolName = "read_all_notifications"
|
||||||
|
ListRepoNotificationsToolName = "list_repo_notifications"
|
||||||
|
ReadRepoNotificationsToolName = "read_repo_notifications"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListNotificationsTool = mcp.NewTool(
|
||||||
|
ListNotificationsToolName,
|
||||||
|
mcp.WithDescription("List the authenticated user's notifications"),
|
||||||
|
mcp.WithString("status", mcp.Description("filter by status: read, unread, pinned")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckNewNotificationsTool = mcp.NewTool(
|
||||||
|
CheckNewNotificationsToolName,
|
||||||
|
mcp.WithDescription("Check if there are new notifications"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetNotificationTool = mcp.NewTool(
|
||||||
|
GetNotificationToolName,
|
||||||
|
mcp.WithDescription("Get a notification thread by ID"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("notification thread ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ReadNotificationTool = mcp.NewTool(
|
||||||
|
ReadNotificationToolName,
|
||||||
|
mcp.WithDescription("Mark a notification as read"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("notification thread ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ReadAllNotificationsTool = mcp.NewTool(
|
||||||
|
ReadAllNotificationsToolName,
|
||||||
|
mcp.WithDescription("Mark all notifications as read"),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListRepoNotificationsTool = mcp.NewTool(
|
||||||
|
ListRepoNotificationsToolName,
|
||||||
|
mcp.WithDescription("List notifications for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("status", mcp.Description("filter by status: read, unread, pinned")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ReadRepoNotificationsTool = mcp.NewTool(
|
||||||
|
ReadRepoNotificationsToolName,
|
||||||
|
mcp.WithDescription("Mark all notifications in a repository as read"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListNotificationsTool, Handler: ListNotificationsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckNewNotificationsTool, Handler: CheckNewNotificationsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetNotificationTool, Handler: GetNotificationFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoNotificationsTool, Handler: ListRepoNotificationsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: ReadNotificationTool, Handler: ReadNotificationFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: ReadAllNotificationsTool, Handler: ReadAllNotificationsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: ReadRepoNotificationsTool, Handler: ReadRepoNotificationsFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListNotificationsFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
opt := gitea_sdk.ListNotificationOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["status"].(string); ok {
|
||||||
|
opt.Status = []gitea_sdk.NotifyStatus{gitea_sdk.NotifyStatus(v)}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
notifications, _, err := client.ListNotifications(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list notifications err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(notifications)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckNewNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckNewNotificationsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
count, _, err := client.CheckNotifications()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check notifications err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]int64{"new_notifications": count})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNotificationFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetNotificationFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
notification, _, err := client.GetNotification(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get notification %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(notification)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadNotificationFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ReadNotificationFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
notification, _, err := client.ReadNotification(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("read notification %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(notification)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadAllNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ReadAllNotificationsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
notifications, _, err := client.ReadNotifications(gitea_sdk.MarkNotificationOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("read all notifications err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(notifications)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoNotificationsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
opt := gitea_sdk.ListNotificationOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["status"].(string); ok {
|
||||||
|
opt.Status = []gitea_sdk.NotifyStatus{gitea_sdk.NotifyStatus(v)}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
notifications, _, err := client.ListRepoNotifications(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %s/%s notifications err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(notifications)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadRepoNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ReadRepoNotificationsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
notifications, _, err := client.ReadRepoNotifications(owner, repo, gitea_sdk.MarkNotificationOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("read %s/%s notifications err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(notifications)
|
||||||
|
}
|
||||||
@@ -1,61 +1,171 @@
|
|||||||
package operation
|
package operation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/operation/issue"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/actions"
|
||||||
"gitea.com/gitea/gitea-mcp/operation/pull"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/admin"
|
||||||
"gitea.com/gitea/gitea-mcp/operation/repo"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/issue"
|
||||||
"gitea.com/gitea/gitea-mcp/operation/search"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/label"
|
||||||
"gitea.com/gitea/gitea-mcp/operation/user"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/milestone"
|
||||||
"gitea.com/gitea/gitea-mcp/operation/version"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/miscellaneous"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/notification"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/organization"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/packages"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/pull"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/repo"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/search"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/settings"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/timetracking"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/user"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/version"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/operation/wiki"
|
||||||
|
mcpContext "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/context"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var mcpServer *server.MCPServer
|
||||||
mcpServer *server.MCPServer
|
|
||||||
)
|
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
func RegisterTool(s *server.MCPServer) {
|
||||||
// User Tool
|
// User Tool
|
||||||
user.RegisterTool(s)
|
s.AddTools(user.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Actions Tool
|
||||||
|
s.AddTools(actions.Tool.Tools()...)
|
||||||
|
|
||||||
// Repo Tool
|
// Repo Tool
|
||||||
repo.RegisterTool(s)
|
s.AddTools(repo.Tool.Tools()...)
|
||||||
|
|
||||||
// Issue Tool
|
// Issue Tool
|
||||||
issue.RegisterTool(s)
|
s.AddTools(issue.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Label Tool
|
||||||
|
s.AddTools(label.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Milestone Tool
|
||||||
|
s.AddTools(milestone.Tool.Tools()...)
|
||||||
|
|
||||||
// Pull Tool
|
// Pull Tool
|
||||||
pull.RegisterTool(s)
|
s.AddTools(pull.Tool.Tools()...)
|
||||||
|
|
||||||
// Search Tool
|
// Search Tool
|
||||||
search.RegisterTool(s)
|
s.AddTools(search.Tool.Tools()...)
|
||||||
|
|
||||||
// Version Tool
|
// Version Tool
|
||||||
version.RegisterTool(s)
|
s.AddTools(version.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Wiki Tool
|
||||||
|
s.AddTools(wiki.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Time Tracking Tool
|
||||||
|
s.AddTools(timetracking.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Organization Tool
|
||||||
|
s.AddTools(organization.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Notification Tool
|
||||||
|
s.AddTools(notification.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Settings Tool
|
||||||
|
s.AddTools(settings.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Package Tool
|
||||||
|
s.AddTools(packages.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Miscellaneous Tool
|
||||||
|
s.AddTools(miscellaneous.Tool.Tools()...)
|
||||||
|
|
||||||
|
// Admin Tool
|
||||||
|
s.AddTools(admin.Tool.Tools()...)
|
||||||
|
|
||||||
|
s.DeleteTools("")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(transport, version string) error {
|
// parseAuthToken extracts the token from an Authorization header.
|
||||||
flag.Version = version
|
// Supports "Bearer <token>" (case-insensitive per RFC 7235) and
|
||||||
mcpServer = newMCPServer(version)
|
// Gitea-style "token <token>" formats.
|
||||||
|
// Returns the token and true if valid, empty string and false otherwise.
|
||||||
|
func parseAuthToken(authHeader string) (string, bool) {
|
||||||
|
if len(authHeader) > 7 && strings.EqualFold(authHeader[:7], "Bearer ") {
|
||||||
|
token := strings.TrimSpace(authHeader[7:])
|
||||||
|
if token != "" {
|
||||||
|
return token, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(authHeader) > 6 && strings.EqualFold(authHeader[:6], "token ") {
|
||||||
|
token := strings.TrimSpace(authHeader[6:])
|
||||||
|
if token != "" {
|
||||||
|
return token, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func getContextWithToken(ctx context.Context, r *http.Request) context.Context {
|
||||||
|
authHeader := r.Header.Get("Authorization")
|
||||||
|
if authHeader == "" {
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
token, ok := parseAuthToken(authHeader)
|
||||||
|
if !ok {
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.WithValue(ctx, mcpContext.TokenContextKey, token)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run() error {
|
||||||
|
mcpServer = newMCPServer(flag.Version)
|
||||||
RegisterTool(mcpServer)
|
RegisterTool(mcpServer)
|
||||||
switch transport {
|
switch flag.Mode {
|
||||||
case "stdio":
|
case "stdio":
|
||||||
if err := server.ServeStdio(mcpServer); err != nil {
|
if err := server.ServeStdio(
|
||||||
|
mcpServer,
|
||||||
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case "sse":
|
case "http":
|
||||||
sseServer := server.NewSSEServer(mcpServer)
|
httpServer := server.NewStreamableHTTPServer(
|
||||||
log.Infof("Gitea MCP SSE server listening on :%d", flag.Port)
|
mcpServer,
|
||||||
if err := sseServer.Start(fmt.Sprintf(":%d", flag.Port)); err != nil {
|
server.WithLogger(log.New()),
|
||||||
|
server.WithHeartbeatInterval(30*time.Second),
|
||||||
|
server.WithHTTPContextFunc(getContextWithToken),
|
||||||
|
)
|
||||||
|
log.Infof("Gitea MCP HTTP server listening on :%d", flag.Port)
|
||||||
|
|
||||||
|
// Graceful shutdown setup
|
||||||
|
sigCh := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||||
|
shutdownDone := make(chan struct{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-sigCh
|
||||||
|
log.Infof("Shutdown signal received, gracefully stopping HTTP server...")
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||||
|
log.Errorf("HTTP server shutdown error: %v", err)
|
||||||
|
}
|
||||||
|
close(shutdownDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := httpServer.Start(fmt.Sprintf(":%d", flag.Port)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
<-shutdownDone // Wait for shutdown to finish
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid transport type: %s. Must be 'stdio' or 'sse'", transport)
|
return fmt.Errorf("invalid transport type: %s. Must be 'stdio' or 'http'", flag.Mode)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -64,6 +174,8 @@ func newMCPServer(version string) *server.MCPServer {
|
|||||||
return server.NewMCPServer(
|
return server.NewMCPServer(
|
||||||
"Gitea MCP Server",
|
"Gitea MCP Server",
|
||||||
version,
|
version,
|
||||||
|
server.WithToolCapabilities(true),
|
||||||
server.WithLogging(),
|
server.WithLogging(),
|
||||||
|
server.WithRecovery(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
105
operation/operation_test.go
Normal file
105
operation/operation_test.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package operation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseAuthToken(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
header string
|
||||||
|
wantToken string
|
||||||
|
wantOK bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid Bearer token",
|
||||||
|
header: "Bearer validtoken",
|
||||||
|
wantToken: "validtoken",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "lowercase bearer",
|
||||||
|
header: "bearer lowercase",
|
||||||
|
wantToken: "lowercase",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uppercase BEARER",
|
||||||
|
header: "BEARER uppercase",
|
||||||
|
wantToken: "uppercase",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "token with spaces trimmed",
|
||||||
|
header: "Bearer spacedToken ",
|
||||||
|
wantToken: "spacedToken",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bearer with no token",
|
||||||
|
header: "Bearer ",
|
||||||
|
wantToken: "",
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bearer with only spaces",
|
||||||
|
header: "Bearer ",
|
||||||
|
wantToken: "",
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing space after Bearer",
|
||||||
|
header: "Bearertoken",
|
||||||
|
wantToken: "",
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Gitea token format",
|
||||||
|
header: "token giteaapitoken",
|
||||||
|
wantToken: "giteaapitoken",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Gitea Token format capitalized",
|
||||||
|
header: "Token giteaapitoken",
|
||||||
|
wantToken: "giteaapitoken",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "token with no value",
|
||||||
|
header: "token ",
|
||||||
|
wantToken: "",
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "different auth type",
|
||||||
|
header: "Basic dXNlcjpwYXNz",
|
||||||
|
wantToken: "",
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty header",
|
||||||
|
header: "",
|
||||||
|
wantToken: "",
|
||||||
|
wantOK: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bearer token with internal spaces",
|
||||||
|
header: "Bearer token with spaces",
|
||||||
|
wantToken: "token with spaces",
|
||||||
|
wantOK: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
gotToken, gotOK := parseAuthToken(tt.header)
|
||||||
|
if gotToken != tt.wantToken {
|
||||||
|
t.Errorf("parseAuthToken() token = %q, want %q", gotToken, tt.wantToken)
|
||||||
|
}
|
||||||
|
if gotOK != tt.wantOK {
|
||||||
|
t.Errorf("parseAuthToken() ok = %v, want %v", gotOK, tt.wantOK)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
86
operation/organization/activity.go
Normal file
86
operation/organization/activity.go
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package organization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListOrgActivityFeedsToolName = "list_org_activity_feeds"
|
||||||
|
ListTeamActivityFeedsToolName = "list_team_activity_feeds"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListOrgActivityFeedsTool = mcp.NewTool(
|
||||||
|
ListOrgActivityFeedsToolName,
|
||||||
|
mcp.WithDescription("List an organization's activity feeds"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListTeamActivityFeedsTool = mcp.NewTool(
|
||||||
|
ListTeamActivityFeedsToolName,
|
||||||
|
mcp.WithDescription("List a team's activity feeds"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgActivityFeedsTool, Handler: ListOrgActivityFeedsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListTeamActivityFeedsTool, Handler: ListTeamActivityFeedsFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgActivityFeedsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgActivityFeedsFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
feeds, _, err := client.ListOrgActivityFeeds(org, gitea_sdk.ListOrgActivityFeedsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s activity feeds err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(feeds)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListTeamActivityFeedsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListTeamActivityFeedsFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
feeds, _, err := client.ListTeamActivityFeeds(id, gitea_sdk.ListTeamActivityFeedsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list team %d activity feeds err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(feeds)
|
||||||
|
}
|
||||||
145
operation/organization/blocks.go
Normal file
145
operation/organization/blocks.go
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package organization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListOrgBlocksToolName = "list_org_blocks"
|
||||||
|
CheckOrgBlockToolName = "check_org_block"
|
||||||
|
BlockOrgUserToolName = "block_org_user"
|
||||||
|
UnblockOrgUserToolName = "unblock_org_user"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListOrgBlocksTool = mcp.NewTool(
|
||||||
|
ListOrgBlocksToolName,
|
||||||
|
mcp.WithDescription("List users blocked by an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckOrgBlockTool = mcp.NewTool(
|
||||||
|
CheckOrgBlockToolName,
|
||||||
|
mcp.WithDescription("Check if a user is blocked by an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to check")),
|
||||||
|
)
|
||||||
|
|
||||||
|
BlockOrgUserTool = mcp.NewTool(
|
||||||
|
BlockOrgUserToolName,
|
||||||
|
mcp.WithDescription("Block a user from an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to block")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnblockOrgUserTool = mcp.NewTool(
|
||||||
|
UnblockOrgUserToolName,
|
||||||
|
mcp.WithDescription("Unblock a user from an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to unblock")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgBlocksTool, Handler: ListOrgBlocksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckOrgBlockTool, Handler: CheckOrgBlockFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: BlockOrgUserTool, Handler: BlockOrgUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnblockOrgUserTool, Handler: UnblockOrgUserFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgBlocksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgBlocksFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.ListOrgBlocks(org, gitea_sdk.ListOrgBlocksOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s blocks err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckOrgBlockFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckOrgBlockFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isBlocked, _, err := client.CheckOrgBlock(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check org %s block for %s err: %v", org, username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"org": org, "username": username, "is_blocked": isBlocked})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BlockOrgUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called BlockOrgUserFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.BlockOrgUser(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("block user %s from org %s err: %v", username, org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "blocked", "org": org, "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnblockOrgUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnblockOrgUserFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.UnblockOrgUser(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unblock user %s from org %s err: %v", username, org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unblocked", "org": org, "username": username})
|
||||||
|
}
|
||||||
251
operation/organization/hooks.go
Normal file
251
operation/organization/hooks.go
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
package organization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListOrgHooksToolName = "list_org_hooks"
|
||||||
|
GetOrgHookToolName = "get_org_hook"
|
||||||
|
CreateOrgHookToolName = "create_org_hook"
|
||||||
|
EditOrgHookToolName = "edit_org_hook"
|
||||||
|
DeleteOrgHookToolName = "delete_org_hook"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListOrgHooksTool = mcp.NewTool(
|
||||||
|
ListOrgHooksToolName,
|
||||||
|
mcp.WithDescription("List an organization's webhooks"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetOrgHookTool = mcp.NewTool(
|
||||||
|
GetOrgHookToolName,
|
||||||
|
mcp.WithDescription("Get an organization webhook by ID"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateOrgHookTool = mcp.NewTool(
|
||||||
|
CreateOrgHookToolName,
|
||||||
|
mcp.WithDescription("Create an organization webhook"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("hook type: gitea, slack, discord, dingtalk, telegram, msteams, feishu, matrix, wechatwork, packagist")),
|
||||||
|
mcp.WithString("url", mcp.Required(), mcp.Description("target URL for the webhook")),
|
||||||
|
mcp.WithString("content_type", mcp.Description("content type: json or form"), mcp.DefaultString("json")),
|
||||||
|
mcp.WithString("secret", mcp.Description("webhook secret")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("whether the webhook is active (default: true)")),
|
||||||
|
mcp.WithArray("events", mcp.Description("list of events to trigger on"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditOrgHookTool = mcp.NewTool(
|
||||||
|
EditOrgHookToolName,
|
||||||
|
mcp.WithDescription("Edit an organization webhook"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
mcp.WithString("url", mcp.Description("target URL")),
|
||||||
|
mcp.WithString("content_type", mcp.Description("content type: json or form")),
|
||||||
|
mcp.WithString("secret", mcp.Description("webhook secret")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("whether the webhook is active")),
|
||||||
|
mcp.WithArray("events", mcp.Description("list of events to trigger on"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteOrgHookTool = mcp.NewTool(
|
||||||
|
DeleteOrgHookToolName,
|
||||||
|
mcp.WithDescription("Delete an organization webhook"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgHooksTool, Handler: ListOrgHooksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetOrgHookTool, Handler: GetOrgHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateOrgHookTool, Handler: CreateOrgHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: EditOrgHookTool, Handler: EditOrgHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteOrgHookTool, Handler: DeleteOrgHookFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgHooksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgHooksFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hooks, _, err := client.ListOrgHooks(org, gitea_sdk.ListHooksOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s hooks err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOrgHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetOrgHookFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.GetOrgHook(org, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get org %s hook %d err: %v", org, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOrgHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateOrgHookFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
hookType, ok := req.GetArguments()["type"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("type is required"))
|
||||||
|
}
|
||||||
|
hookURL, ok := req.GetArguments()["url"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("url is required"))
|
||||||
|
}
|
||||||
|
contentType := "json"
|
||||||
|
if v, ok := req.GetArguments()["content_type"].(string); ok {
|
||||||
|
contentType = v
|
||||||
|
}
|
||||||
|
config := map[string]string{
|
||||||
|
"url": hookURL,
|
||||||
|
"content_type": contentType,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["secret"].(string); ok {
|
||||||
|
config["secret"] = v
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateHookOption{
|
||||||
|
Type: gitea_sdk.HookType(hookType),
|
||||||
|
Config: config,
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = v
|
||||||
|
}
|
||||||
|
if eventsArg, exists := req.GetArguments()["events"]; exists {
|
||||||
|
if eventsSlice, ok := eventsArg.([]any); ok {
|
||||||
|
events := make([]string, 0, len(eventsSlice))
|
||||||
|
for _, e := range eventsSlice {
|
||||||
|
if s, ok := e.(string); ok {
|
||||||
|
events = append(events, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opt.Events = events
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.CreateOrgHook(org, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create org %s hook err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditOrgHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditOrgHookFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditHookOption{}
|
||||||
|
config := map[string]string{}
|
||||||
|
if v, ok := req.GetArguments()["url"].(string); ok {
|
||||||
|
config["url"] = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["content_type"].(string); ok {
|
||||||
|
config["content_type"] = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["secret"].(string); ok {
|
||||||
|
config["secret"] = v
|
||||||
|
}
|
||||||
|
if len(config) > 0 {
|
||||||
|
opt.Config = config
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = &v
|
||||||
|
}
|
||||||
|
if eventsArg, exists := req.GetArguments()["events"]; exists {
|
||||||
|
if eventsSlice, ok := eventsArg.([]any); ok {
|
||||||
|
events := make([]string, 0, len(eventsSlice))
|
||||||
|
for _, e := range eventsSlice {
|
||||||
|
if s, ok := e.(string); ok {
|
||||||
|
events = append(events, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opt.Events = events
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.EditOrgHook(org, id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit org %s hook %d err: %v", org, id, err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.GetOrgHook(org, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get org %s hook %d after edit err: %v", org, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteOrgHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteOrgHookFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteOrgHook(org, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete org %s hook %d err: %v", org, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "deleted", "org": org, "hook_id": id})
|
||||||
|
}
|
||||||
241
operation/organization/members.go
Normal file
241
operation/organization/members.go
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
package organization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListOrgMembersToolName = "list_org_members"
|
||||||
|
CheckOrgMembershipToolName = "check_org_membership"
|
||||||
|
RemoveOrgMemberToolName = "remove_org_member"
|
||||||
|
ListOrgPublicMembersToolName = "list_org_public_members"
|
||||||
|
CheckOrgPublicMemberToolName = "check_org_public_member"
|
||||||
|
SetOrgPublicMemberToolName = "set_org_public_member"
|
||||||
|
GetOrgPermissionsToolName = "get_org_permissions"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListOrgMembersTool = mcp.NewTool(
|
||||||
|
ListOrgMembersToolName,
|
||||||
|
mcp.WithDescription("List an organization's members"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckOrgMembershipTool = mcp.NewTool(
|
||||||
|
CheckOrgMembershipToolName,
|
||||||
|
mcp.WithDescription("Check if a user is a member of an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to check")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RemoveOrgMemberTool = mcp.NewTool(
|
||||||
|
RemoveOrgMemberToolName,
|
||||||
|
mcp.WithDescription("Remove a member from an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to remove")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListOrgPublicMembersTool = mcp.NewTool(
|
||||||
|
ListOrgPublicMembersToolName,
|
||||||
|
mcp.WithDescription("List an organization's public members"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckOrgPublicMemberTool = mcp.NewTool(
|
||||||
|
CheckOrgPublicMemberToolName,
|
||||||
|
mcp.WithDescription("Check if a user is a public member of an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to check")),
|
||||||
|
)
|
||||||
|
|
||||||
|
SetOrgPublicMemberTool = mcp.NewTool(
|
||||||
|
SetOrgPublicMemberToolName,
|
||||||
|
mcp.WithDescription("Set or remove a user's public membership in an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithBoolean("visible", mcp.Required(), mcp.Description("true to make public, false to make private")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetOrgPermissionsTool = mcp.NewTool(
|
||||||
|
GetOrgPermissionsToolName,
|
||||||
|
mcp.WithDescription("Get a user's permissions in an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgMembersTool, Handler: ListOrgMembersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckOrgMembershipTool, Handler: CheckOrgMembershipFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgPublicMembersTool, Handler: ListOrgPublicMembersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckOrgPublicMemberTool, Handler: CheckOrgPublicMemberFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetOrgPermissionsTool, Handler: GetOrgPermissionsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RemoveOrgMemberTool, Handler: RemoveOrgMemberFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: SetOrgPublicMemberTool, Handler: SetOrgPublicMemberFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgMembersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgMembersFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
members, _, err := client.ListOrgMembership(org, gitea_sdk.ListOrgMembershipOption{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s members err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(members)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckOrgMembershipFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckOrgMembershipFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isMember, _, err := client.CheckOrgMembership(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check org %s membership for %s err: %v", org, username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"org": org, "username": username, "is_member": isMember})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveOrgMemberFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RemoveOrgMemberFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteOrgMembership(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("remove member %s from org %s err: %v", username, org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "removed", "org": org, "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgPublicMembersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgPublicMembersFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
members, _, err := client.ListPublicOrgMembership(org, gitea_sdk.ListOrgMembershipOption{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s public members err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(members)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckOrgPublicMemberFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckOrgPublicMemberFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isPublic, _, err := client.CheckPublicOrgMembership(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check org %s public membership for %s err: %v", org, username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"org": org, "username": username, "is_public_member": isPublic})
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetOrgPublicMemberFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called SetOrgPublicMemberFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
visible, ok := req.GetArguments()["visible"].(bool)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("visible is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.SetPublicOrgMembership(org, username, visible)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("set public membership for %s in org %s err: %v", username, org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"org": org, "username": username, "visible": visible})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOrgPermissionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetOrgPermissionsFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
perms, _, err := client.GetOrgPermissions(org, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get permissions for %s in org %s err: %v", username, org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(perms)
|
||||||
|
}
|
||||||
319
operation/organization/org.go
Normal file
319
operation/organization/org.go
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
package organization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListOrgsToolName = "list_orgs"
|
||||||
|
GetOrgToolName = "get_org"
|
||||||
|
CreateOrgToolName = "create_org"
|
||||||
|
EditOrgToolName = "edit_org"
|
||||||
|
DeleteOrgToolName = "delete_org"
|
||||||
|
RenameOrgToolName = "rename_org"
|
||||||
|
ListOrgReposToolName = "list_org_repos"
|
||||||
|
CreateOrgRepoToolName = "create_org_repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListOrgsTool = mcp.NewTool(
|
||||||
|
ListOrgsToolName,
|
||||||
|
mcp.WithDescription("List all visible organizations"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetOrgTool = mcp.NewTool(
|
||||||
|
GetOrgToolName,
|
||||||
|
mcp.WithDescription("Get an organization by name"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateOrgTool = mcp.NewTool(
|
||||||
|
CreateOrgToolName,
|
||||||
|
mcp.WithDescription("Create an organization"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("organization username")),
|
||||||
|
mcp.WithString("full_name", mcp.Description("organization full name")),
|
||||||
|
mcp.WithString("description", mcp.Description("organization description")),
|
||||||
|
mcp.WithString("website", mcp.Description("organization website")),
|
||||||
|
mcp.WithString("location", mcp.Description("organization location")),
|
||||||
|
mcp.WithString("visibility", mcp.Description("visibility: public, limited, or private"), mcp.DefaultString("public")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditOrgTool = mcp.NewTool(
|
||||||
|
EditOrgToolName,
|
||||||
|
mcp.WithDescription("Edit an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("full_name", mcp.Description("organization full name")),
|
||||||
|
mcp.WithString("description", mcp.Description("organization description")),
|
||||||
|
mcp.WithString("website", mcp.Description("organization website")),
|
||||||
|
mcp.WithString("location", mcp.Description("organization location")),
|
||||||
|
mcp.WithString("visibility", mcp.Description("visibility: public, limited, or private")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteOrgTool = mcp.NewTool(
|
||||||
|
DeleteOrgToolName,
|
||||||
|
mcp.WithDescription("Delete an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RenameOrgTool = mcp.NewTool(
|
||||||
|
RenameOrgToolName,
|
||||||
|
mcp.WithDescription("Rename an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("current organization name")),
|
||||||
|
mcp.WithString("new_name", mcp.Required(), mcp.Description("new organization name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListOrgReposTool = mcp.NewTool(
|
||||||
|
ListOrgReposToolName,
|
||||||
|
mcp.WithDescription("List an organization's repositories"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateOrgRepoTool = mcp.NewTool(
|
||||||
|
CreateOrgRepoToolName,
|
||||||
|
mcp.WithDescription("Create a repository in an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("description", mcp.Description("repository description")),
|
||||||
|
mcp.WithBoolean("private", mcp.Description("whether the repository is private")),
|
||||||
|
mcp.WithBoolean("auto_init", mcp.Description("whether to auto-initialize with README")),
|
||||||
|
mcp.WithString("default_branch", mcp.Description("default branch name")),
|
||||||
|
mcp.WithString("gitignores", mcp.Description("gitignore template")),
|
||||||
|
mcp.WithString("license", mcp.Description("license template")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgsTool, Handler: ListOrgsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetOrgTool, Handler: GetOrgFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgReposTool, Handler: ListOrgReposFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateOrgTool, Handler: CreateOrgFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: EditOrgTool, Handler: EditOrgFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteOrgTool, Handler: DeleteOrgFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RenameOrgTool, Handler: RenameOrgFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateOrgRepoTool, Handler: CreateOrgRepoFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgsFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
orgs, _, err := client.ListOrgs(gitea_sdk.ListOrgsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list orgs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(orgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetOrgFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
organization, _, err := client.GetOrg(org)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get org %s err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(organization)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateOrgFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateOrgOption{
|
||||||
|
Name: username,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||||
|
opt.FullName = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["website"].(string); ok {
|
||||||
|
opt.Website = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["location"].(string); ok {
|
||||||
|
opt.Location = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["visibility"].(string); ok {
|
||||||
|
opt.Visibility = gitea_sdk.VisibleType(v)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
organization, _, err := client.CreateOrg(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create org %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(organization)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditOrgFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditOrgOption{}
|
||||||
|
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||||
|
opt.FullName = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["website"].(string); ok {
|
||||||
|
opt.Website = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["location"].(string); ok {
|
||||||
|
opt.Location = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["visibility"].(string); ok {
|
||||||
|
opt.Visibility = gitea_sdk.VisibleType(v)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.EditOrg(org, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit org %s err: %v", org, err))
|
||||||
|
}
|
||||||
|
updated, _, err := client.GetOrg(org)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get org %s after edit err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(updated)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteOrgFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteOrg(org)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete org %s err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted", "org": org})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenameOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RenameOrgFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
newName, ok := req.GetArguments()["new_name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("new_name is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.RenameOrg(org, gitea_sdk.RenameOrgOption{NewName: newName})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("rename org %s to %s err: %v", org, newName, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "renamed", "old_name": org, "new_name": newName})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgReposFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.ListOrgRepos(org, gitea_sdk.ListOrgReposOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s repos err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateOrgRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateOrgRepoFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateRepoOption{
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["private"].(bool); ok {
|
||||||
|
opt.Private = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["auto_init"].(bool); ok {
|
||||||
|
opt.AutoInit = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["default_branch"].(string); ok {
|
||||||
|
opt.DefaultBranch = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["gitignores"].(string); ok {
|
||||||
|
opt.Gitignores = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["license"].(string); ok {
|
||||||
|
opt.License = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repo, _, err := client.CreateOrgRepo(org, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create repo %s/%s err: %v", org, name, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repo)
|
||||||
|
}
|
||||||
427
operation/organization/teams.go
Normal file
427
operation/organization/teams.go
Normal file
@@ -0,0 +1,427 @@
|
|||||||
|
package organization
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListOrgTeamsToolName = "list_org_teams"
|
||||||
|
GetTeamToolName = "get_team"
|
||||||
|
CreateTeamToolName = "create_team"
|
||||||
|
EditTeamToolName = "edit_team"
|
||||||
|
DeleteTeamToolName = "delete_team"
|
||||||
|
ListTeamMembersToolName = "list_team_members"
|
||||||
|
GetTeamMemberToolName = "get_team_member"
|
||||||
|
AddTeamMemberToolName = "add_team_member"
|
||||||
|
RemoveTeamMemberToolName = "remove_team_member"
|
||||||
|
ListTeamReposToolName = "list_team_repos"
|
||||||
|
AddTeamRepoToolName = "add_team_repo"
|
||||||
|
RemoveTeamRepoToolName = "remove_team_repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListOrgTeamsTool = mcp.NewTool(
|
||||||
|
ListOrgTeamsToolName,
|
||||||
|
mcp.WithDescription("List an organization's teams"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetTeamTool = mcp.NewTool(
|
||||||
|
GetTeamToolName,
|
||||||
|
mcp.WithDescription("Get a team by ID"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateTeamTool = mcp.NewTool(
|
||||||
|
CreateTeamToolName,
|
||||||
|
mcp.WithDescription("Create a team in an organization"),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("team name")),
|
||||||
|
mcp.WithString("description", mcp.Description("team description")),
|
||||||
|
mcp.WithString("permission", mcp.Description("team permission: read, write, admin"), mcp.DefaultString("read")),
|
||||||
|
mcp.WithBoolean("can_create_org_repo", mcp.Description("whether team members can create repos in the org")),
|
||||||
|
mcp.WithBoolean("includes_all_repositories", mcp.Description("whether team has access to all repos")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditTeamTool = mcp.NewTool(
|
||||||
|
EditTeamToolName,
|
||||||
|
mcp.WithDescription("Edit a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithString("name", mcp.Description("team name")),
|
||||||
|
mcp.WithString("description", mcp.Description("team description")),
|
||||||
|
mcp.WithString("permission", mcp.Description("team permission: read, write, admin")),
|
||||||
|
mcp.WithBoolean("can_create_org_repo", mcp.Description("whether team members can create repos in the org")),
|
||||||
|
mcp.WithBoolean("includes_all_repositories", mcp.Description("whether team has access to all repos")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteTeamTool = mcp.NewTool(
|
||||||
|
DeleteTeamToolName,
|
||||||
|
mcp.WithDescription("Delete a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListTeamMembersTool = mcp.NewTool(
|
||||||
|
ListTeamMembersToolName,
|
||||||
|
mcp.WithDescription("List a team's members"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetTeamMemberTool = mcp.NewTool(
|
||||||
|
GetTeamMemberToolName,
|
||||||
|
mcp.WithDescription("Get a specific member of a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddTeamMemberTool = mcp.NewTool(
|
||||||
|
AddTeamMemberToolName,
|
||||||
|
mcp.WithDescription("Add a member to a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to add")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RemoveTeamMemberTool = mcp.NewTool(
|
||||||
|
RemoveTeamMemberToolName,
|
||||||
|
mcp.WithDescription("Remove a member from a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to remove")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListTeamReposTool = mcp.NewTool(
|
||||||
|
ListTeamReposToolName,
|
||||||
|
mcp.WithDescription("List a team's repositories"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddTeamRepoTool = mcp.NewTool(
|
||||||
|
AddTeamRepoToolName,
|
||||||
|
mcp.WithDescription("Add a repository to a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RemoveTeamRepoTool = mcp.NewTool(
|
||||||
|
RemoveTeamRepoToolName,
|
||||||
|
mcp.WithDescription("Remove a repository from a team"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("team ID")),
|
||||||
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListOrgTeamsTool, Handler: ListOrgTeamsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetTeamTool, Handler: GetTeamFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListTeamMembersTool, Handler: ListTeamMembersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetTeamMemberTool, Handler: GetTeamMemberFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListTeamReposTool, Handler: ListTeamReposFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateTeamTool, Handler: CreateTeamFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: EditTeamTool, Handler: EditTeamFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteTeamTool, Handler: DeleteTeamFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddTeamMemberTool, Handler: AddTeamMemberFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RemoveTeamMemberTool, Handler: RemoveTeamMemberFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddTeamRepoTool, Handler: AddTeamRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RemoveTeamRepoTool, Handler: RemoveTeamRepoFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListOrgTeamsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListOrgTeamsFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
teams, _, err := client.ListOrgTeams(org, gitea_sdk.ListTeamsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list org %s teams err: %v", org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(teams)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTeamFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetTeamFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
team, _, err := client.GetTeam(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get team %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(team)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateTeamFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateTeamFn")
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("name is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateTeamOption{
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["permission"].(string); ok {
|
||||||
|
opt.Permission = gitea_sdk.AccessMode(v)
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["can_create_org_repo"].(bool); ok {
|
||||||
|
opt.CanCreateOrgRepo = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["includes_all_repositories"].(bool); ok {
|
||||||
|
opt.IncludesAllRepositories = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
team, _, err := client.CreateTeam(org, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create team %s in org %s err: %v", name, org, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(team)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditTeamFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditTeamFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditTeamOption{}
|
||||||
|
if v, ok := req.GetArguments()["name"].(string); ok {
|
||||||
|
opt.Name = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["permission"].(string); ok {
|
||||||
|
perm := gitea_sdk.AccessMode(v)
|
||||||
|
opt.Permission = perm
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["can_create_org_repo"].(bool); ok {
|
||||||
|
opt.CanCreateOrgRepo = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["includes_all_repositories"].(bool); ok {
|
||||||
|
opt.IncludesAllRepositories = &v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.EditTeam(id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit team %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
team, _, err := client.GetTeam(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get team %d after edit err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(team)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteTeamFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteTeamFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteTeam(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete team %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "deleted", "team_id": id})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListTeamMembersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListTeamMembersFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
members, _, err := client.ListTeamMembers(id, gitea_sdk.ListTeamMembersOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list team %d members err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(members)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTeamMemberFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetTeamMemberFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
member, _, err := client.GetTeamMember(id, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get team %d member %s err: %v", id, username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(member)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddTeamMemberFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddTeamMemberFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AddTeamMember(id, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add member %s to team %d err: %v", username, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "added", "team_id": id, "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveTeamMemberFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RemoveTeamMemberFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.RemoveTeamMember(id, username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("remove member %s from team %d err: %v", username, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "removed", "team_id": id, "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListTeamReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListTeamReposFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.ListTeamRepositories(id, gitea_sdk.ListTeamRepositoriesOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list team %d repos err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddTeamRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddTeamRepoFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AddTeamRepository(id, org, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add repo %s/%s to team %d err: %v", org, repo, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "added", "team_id": id, "org": org, "repo": repo})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveTeamRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RemoveTeamRepoFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("org is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.RemoveTeamRepository(id, org, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("remove repo %s/%s from team %d err: %v", org, repo, id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "removed", "team_id": id, "org": org, "repo": repo})
|
||||||
|
}
|
||||||
242
operation/packages/packages.go
Normal file
242
operation/packages/packages.go
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
package packages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListPackagesToolName = "list_packages"
|
||||||
|
GetPackageToolName = "get_package"
|
||||||
|
DeletePackageToolName = "delete_package"
|
||||||
|
ListPackageFilesToolName = "list_package_files"
|
||||||
|
GetLatestPackageToolName = "get_latest_package"
|
||||||
|
LinkPackageToolName = "link_package"
|
||||||
|
UnlinkPackageToolName = "unlink_package"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListPackagesTool = mcp.NewTool(
|
||||||
|
ListPackagesToolName,
|
||||||
|
mcp.WithDescription("List packages for a user or organization"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner (user or org)")),
|
||||||
|
mcp.WithString("type", mcp.Description("package type filter (e.g., generic, container, npm, pypi, rubygems, maven, nuget, debian, alpine, go, cargo, chef, composer, conan, conda, cran, pub, helm, rpm, swift, vagrant)")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetPackageTool = mcp.NewTool(
|
||||||
|
GetPackageToolName,
|
||||||
|
mcp.WithDescription("Get a specific package by type, name, and version"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("package type (e.g., generic, container, npm)")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("package name")),
|
||||||
|
mcp.WithString("version", mcp.Required(), mcp.Description("package version")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeletePackageTool = mcp.NewTool(
|
||||||
|
DeletePackageToolName,
|
||||||
|
mcp.WithDescription("Delete a specific package version"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("package type")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("package name")),
|
||||||
|
mcp.WithString("version", mcp.Required(), mcp.Description("package version")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListPackageFilesTool = mcp.NewTool(
|
||||||
|
ListPackageFilesToolName,
|
||||||
|
mcp.WithDescription("List files of a package version"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("package type")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("package name")),
|
||||||
|
mcp.WithString("version", mcp.Required(), mcp.Description("package version")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetLatestPackageTool = mcp.NewTool(
|
||||||
|
GetLatestPackageToolName,
|
||||||
|
mcp.WithDescription("Get the latest version of a package"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("package type")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("package name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
LinkPackageTool = mcp.NewTool(
|
||||||
|
LinkPackageToolName,
|
||||||
|
mcp.WithDescription("Link a package to a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("package type")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("package name")),
|
||||||
|
mcp.WithString("repo_name", mcp.Required(), mcp.Description("repository name to link to")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnlinkPackageTool = mcp.NewTool(
|
||||||
|
UnlinkPackageToolName,
|
||||||
|
mcp.WithDescription("Unlink a package from its repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("package owner")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("package type")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("package name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListPackagesTool, Handler: ListPackagesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetPackageTool, Handler: GetPackageFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListPackageFilesTool, Handler: ListPackageFilesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetLatestPackageTool, Handler: GetLatestPackageFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeletePackageTool, Handler: DeletePackageFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: LinkPackageTool, Handler: LinkPackageFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnlinkPackageTool, Handler: UnlinkPackageFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListPackagesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListPackagesFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
if owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
opt := gitea_sdk.ListPackagesOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
packages, _, err := client.ListPackages(owner, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list packages err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(packages)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPackageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetPackageFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
pkgType, _ := req.GetArguments()["type"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
version, _ := req.GetArguments()["version"].(string)
|
||||||
|
if owner == "" || pkgType == "" || name == "" || version == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, type, name, and version are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
pkg, _, err := client.GetPackage(owner, pkgType, name, version)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get package err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(pkg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePackageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeletePackageFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
pkgType, _ := req.GetArguments()["type"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
version, _ := req.GetArguments()["version"].(string)
|
||||||
|
if owner == "" || pkgType == "" || name == "" || version == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, type, name, and version are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeletePackage(owner, pkgType, name, version)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete package err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListPackageFilesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListPackageFilesFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
pkgType, _ := req.GetArguments()["type"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
version, _ := req.GetArguments()["version"].(string)
|
||||||
|
if owner == "" || pkgType == "" || name == "" || version == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, type, name, and version are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
files, _, err := client.ListPackageFiles(owner, pkgType, name, version)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list package files err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(files)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLatestPackageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetLatestPackageFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
pkgType, _ := req.GetArguments()["type"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if owner == "" || pkgType == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, type, and name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
pkg, _, err := client.GetLatestPackage(owner, pkgType, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get latest package err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(pkg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LinkPackageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called LinkPackageFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
pkgType, _ := req.GetArguments()["type"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
repoName, _ := req.GetArguments()["repo_name"].(string)
|
||||||
|
if owner == "" || pkgType == "" || name == "" || repoName == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, type, name, and repo_name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.LinkPackage(owner, pkgType, name, repoName)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("link package err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "linked", "repo": repoName})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnlinkPackageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnlinkPackageFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
pkgType, _ := req.GetArguments()["type"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if owner == "" || pkgType == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, type, and name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.UnlinkPackage(owner, pkgType, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unlink package err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unlinked"})
|
||||||
|
}
|
||||||
@@ -2,21 +2,38 @@ package pull
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GetPullRequestByIndexToolName = "get_pull_request_by_index"
|
GetPullRequestByIndexToolName = "get_pull_request_by_index"
|
||||||
|
GetPullRequestDiffToolName = "get_pull_request_diff"
|
||||||
ListRepoPullRequestsToolName = "list_repo_pull_requests"
|
ListRepoPullRequestsToolName = "list_repo_pull_requests"
|
||||||
CreatePullRequestToolName = "create_pull_request"
|
CreatePullRequestToolName = "create_pull_request"
|
||||||
|
CreatePullRequestReviewerToolName = "create_pull_request_reviewer"
|
||||||
|
DeletePullRequestReviewerToolName = "delete_pull_request_reviewer"
|
||||||
|
ListPullRequestReviewsToolName = "list_pull_request_reviews"
|
||||||
|
GetPullRequestReviewToolName = "get_pull_request_review"
|
||||||
|
ListPullRequestReviewCommentsToolName = "list_pull_request_review_comments"
|
||||||
|
CreatePullRequestReviewToolName = "create_pull_request_review"
|
||||||
|
SubmitPullRequestReviewToolName = "submit_pull_request_review"
|
||||||
|
DeletePullRequestReviewToolName = "delete_pull_request_review"
|
||||||
|
DismissPullRequestReviewToolName = "dismiss_pull_request_review"
|
||||||
|
MergePullRequestToolName = "merge_pull_request"
|
||||||
|
EditPullRequestToolName = "edit_pull_request"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -28,6 +45,15 @@ var (
|
|||||||
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository pull request index")),
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository pull request index")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
GetPullRequestDiffTool = mcp.NewTool(
|
||||||
|
GetPullRequestDiffToolName,
|
||||||
|
mcp.WithDescription("get pull request diff"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository pull request index")),
|
||||||
|
mcp.WithBoolean("binary", mcp.Description("whether to include binary file changes")),
|
||||||
|
)
|
||||||
|
|
||||||
ListRepoPullRequestsTool = mcp.NewTool(
|
ListRepoPullRequestsTool = mcp.NewTool(
|
||||||
ListRepoPullRequestsToolName,
|
ListRepoPullRequestsToolName,
|
||||||
mcp.WithDescription("List repository pull requests"),
|
mcp.WithDescription("List repository pull requests"),
|
||||||
@@ -50,70 +76,292 @@ var (
|
|||||||
mcp.WithString("head", mcp.Required(), mcp.Description("pull request head")),
|
mcp.WithString("head", mcp.Required(), mcp.Description("pull request head")),
|
||||||
mcp.WithString("base", mcp.Required(), mcp.Description("pull request base")),
|
mcp.WithString("base", mcp.Required(), mcp.Description("pull request base")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CreatePullRequestReviewerTool = mcp.NewTool(
|
||||||
|
CreatePullRequestReviewerToolName,
|
||||||
|
mcp.WithDescription("create pull request reviewer"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithArray("reviewers", mcp.Description("list of reviewer usernames"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
mcp.WithArray("team_reviewers", mcp.Description("list of team reviewer names"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeletePullRequestReviewerTool = mcp.NewTool(
|
||||||
|
DeletePullRequestReviewerToolName,
|
||||||
|
mcp.WithDescription("remove reviewer requests from a pull request"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithArray("reviewers", mcp.Description("list of reviewer usernames to remove"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
mcp.WithArray("team_reviewers", mcp.Description("list of team reviewer names to remove"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListPullRequestReviewsTool = mcp.NewTool(
|
||||||
|
ListPullRequestReviewsToolName,
|
||||||
|
mcp.WithDescription("list all reviews for a pull request"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetPullRequestReviewTool = mcp.NewTool(
|
||||||
|
GetPullRequestReviewToolName,
|
||||||
|
mcp.WithDescription("get a specific review for a pull request"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListPullRequestReviewCommentsTool = mcp.NewTool(
|
||||||
|
ListPullRequestReviewCommentsToolName,
|
||||||
|
mcp.WithDescription("list all comments for a specific pull request review"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreatePullRequestReviewTool = mcp.NewTool(
|
||||||
|
CreatePullRequestReviewToolName,
|
||||||
|
mcp.WithDescription("create a review for a pull request"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithString("state", mcp.Description("review state"), mcp.Enum("APPROVED", "REQUEST_CHANGES", "COMMENT", "PENDING")),
|
||||||
|
mcp.WithString("body", mcp.Description("review body/comment")),
|
||||||
|
mcp.WithString("commit_id", mcp.Description("commit SHA to review")),
|
||||||
|
mcp.WithArray("comments", mcp.Description("inline review comments (objects with path, body, old_line_num, new_line_num)"), mcp.Items(map[string]any{
|
||||||
|
"type": "object",
|
||||||
|
"properties": map[string]any{
|
||||||
|
"path": map[string]any{"type": "string", "description": "file path to comment on"},
|
||||||
|
"body": map[string]any{"type": "string", "description": "comment body"},
|
||||||
|
"old_line_num": map[string]any{"type": "number", "description": "line number in the old file (for deletions/changes)"},
|
||||||
|
"new_line_num": map[string]any{"type": "number", "description": "line number in the new file (for additions/changes)"},
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
)
|
||||||
|
|
||||||
|
SubmitPullRequestReviewTool = mcp.NewTool(
|
||||||
|
SubmitPullRequestReviewToolName,
|
||||||
|
mcp.WithDescription("submit a pending pull request review"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
||||||
|
mcp.WithString("state", mcp.Required(), mcp.Description("final review state"), mcp.Enum("APPROVED", "REQUEST_CHANGES", "COMMENT")),
|
||||||
|
mcp.WithString("body", mcp.Description("submission message")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeletePullRequestReviewTool = mcp.NewTool(
|
||||||
|
DeletePullRequestReviewToolName,
|
||||||
|
mcp.WithDescription("delete a pull request review"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DismissPullRequestReviewTool = mcp.NewTool(
|
||||||
|
DismissPullRequestReviewToolName,
|
||||||
|
mcp.WithDescription("dismiss a pull request review"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
||||||
|
mcp.WithString("message", mcp.Description("dismissal reason")),
|
||||||
|
)
|
||||||
|
|
||||||
|
MergePullRequestTool = mcp.NewTool(
|
||||||
|
MergePullRequestToolName,
|
||||||
|
mcp.WithDescription("merge a pull request"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithString("merge_style", mcp.Description("merge style: merge, rebase, rebase-merge, squash, fast-forward-only"), mcp.Enum("merge", "rebase", "rebase-merge", "squash", "fast-forward-only"), mcp.DefaultString("merge")),
|
||||||
|
mcp.WithString("title", mcp.Description("custom merge commit title")),
|
||||||
|
mcp.WithString("message", mcp.Description("custom merge commit message")),
|
||||||
|
mcp.WithBoolean("delete_branch", mcp.Description("delete the branch after merge"), mcp.DefaultBool(false)),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditPullRequestTool = mcp.NewTool(
|
||||||
|
EditPullRequestToolName,
|
||||||
|
mcp.WithDescription("edit a pull request"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("pull request index")),
|
||||||
|
mcp.WithString("title", mcp.Description("pull request title")),
|
||||||
|
mcp.WithString("body", mcp.Description("pull request body content")),
|
||||||
|
mcp.WithString("base", mcp.Description("pull request base branch")),
|
||||||
|
mcp.WithString("assignee", mcp.Description("username to assign")),
|
||||||
|
mcp.WithArray("assignees", mcp.Description("usernames to assign"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
mcp.WithNumber("milestone", mcp.Description("milestone number")),
|
||||||
|
mcp.WithString("state", mcp.Description("pull request state"), mcp.Enum("open", "closed")),
|
||||||
|
mcp.WithBoolean("allow_maintainer_edit", mcp.Description("allow maintainer to edit the pull request")),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
func init() {
|
||||||
s.AddTool(GetPullRequestByIndexTool, GetPullRequestByIndexFn)
|
Tool.RegisterRead(server.ServerTool{
|
||||||
s.AddTool(ListRepoPullRequestsTool, ListRepoPullRequestsFn)
|
Tool: GetPullRequestByIndexTool,
|
||||||
s.AddTool(CreatePullRequestTool, CreatePullRequestFn)
|
Handler: GetPullRequestByIndexFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetPullRequestDiffTool,
|
||||||
|
Handler: GetPullRequestDiffFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListRepoPullRequestsTool,
|
||||||
|
Handler: ListRepoPullRequestsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListPullRequestReviewsTool,
|
||||||
|
Handler: ListPullRequestReviewsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetPullRequestReviewTool,
|
||||||
|
Handler: GetPullRequestReviewFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListPullRequestReviewCommentsTool,
|
||||||
|
Handler: ListPullRequestReviewCommentsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreatePullRequestTool,
|
||||||
|
Handler: CreatePullRequestFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreatePullRequestReviewerTool,
|
||||||
|
Handler: CreatePullRequestReviewerFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeletePullRequestReviewerTool,
|
||||||
|
Handler: DeletePullRequestReviewerFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreatePullRequestReviewTool,
|
||||||
|
Handler: CreatePullRequestReviewFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: SubmitPullRequestReviewTool,
|
||||||
|
Handler: SubmitPullRequestReviewFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeletePullRequestReviewTool,
|
||||||
|
Handler: DeletePullRequestReviewFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DismissPullRequestReviewTool,
|
||||||
|
Handler: DismissPullRequestReviewFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: MergePullRequestTool,
|
||||||
|
Handler: MergePullRequestFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditPullRequestTool,
|
||||||
|
Handler: EditPullRequestFn,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPullRequestByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetPullRequestByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetPullRequestByIndexFn")
|
log.Debugf("Called GetPullRequestByIndexFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
index, ok := req.Params.Arguments["index"].(float64)
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
if !ok {
|
|
||||||
return to.ErrorResult(fmt.Errorf("index is required"))
|
|
||||||
}
|
|
||||||
pr, _, err := gitea.Client().GetPullRequest(owner, repo, int64(index))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get %v/%v/pr/%v err: %v", owner, repo, int64(index), err))
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
pr, _, err := client.GetPullRequest(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(pr)
|
return to.TextResult(pr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPullRequestDiffFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetPullRequestDiffFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
binary, _ := req.GetArguments()["binary"].(bool)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
diffBytes, _, err := client.GetPullRequestDiff(owner, repo, index, gitea_sdk.PullRequestDiffOptions{
|
||||||
|
Binary: binary,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %v/%v/pr/%v diff err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
result := map[string]any{
|
||||||
|
"diff": string(diffBytes),
|
||||||
|
"binary": binary,
|
||||||
|
"index": index,
|
||||||
|
"repo": repo,
|
||||||
|
"owner": owner,
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
func ListRepoPullRequestsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ListRepoPullRequestsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called ListRepoPullRequests")
|
log.Debugf("Called ListRepoPullRequests")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
state, _ := req.Params.Arguments["state"].(string)
|
state, _ := req.GetArguments()["state"].(string)
|
||||||
sort, ok := req.Params.Arguments["sort"].(string)
|
sort, ok := req.GetArguments()["sort"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
sort = "recentupdate"
|
sort = "recentupdate"
|
||||||
}
|
}
|
||||||
milestone, _ := req.Params.Arguments["milestone"].(float64)
|
milestone := params.GetOptionalInt(req.GetArguments(), "milestone", 0)
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
if !ok {
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, ok := req.Params.Arguments["pageSize"].(float64)
|
|
||||||
if !ok {
|
|
||||||
pageSize = 100
|
|
||||||
}
|
|
||||||
opt := gitea_sdk.ListPullRequestsOptions{
|
opt := gitea_sdk.ListPullRequestsOptions{
|
||||||
State: gitea_sdk.StateType(state),
|
State: gitea_sdk.StateType(state),
|
||||||
Sort: sort,
|
Sort: sort,
|
||||||
Milestone: int64(milestone),
|
Milestone: milestone,
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
Page: int(page),
|
Page: int(page),
|
||||||
PageSize: int(pageSize),
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
pullRequests, _, err := gitea.Client().ListRepoPullRequests(owner, repo, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
pullRequests, _, err := client.ListRepoPullRequests(owner, repo, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("list %v/%v/pull_requests err: %v", owner, repo, err))
|
return to.ErrorResult(fmt.Errorf("list %v/%v/pull_requests err: %v", owner, repo, err))
|
||||||
}
|
}
|
||||||
@@ -123,31 +371,35 @@ func ListRepoPullRequestsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.
|
|||||||
|
|
||||||
func CreatePullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreatePullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called CreatePullRequestFn")
|
log.Debugf("Called CreatePullRequestFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
title, ok := req.Params.Arguments["title"].(string)
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("title is required"))
|
return to.ErrorResult(errors.New("title is required"))
|
||||||
}
|
}
|
||||||
body, ok := req.Params.Arguments["body"].(string)
|
body, ok := req.GetArguments()["body"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("body is required"))
|
return to.ErrorResult(errors.New("body is required"))
|
||||||
}
|
}
|
||||||
head, ok := req.Params.Arguments["head"].(string)
|
head, ok := req.GetArguments()["head"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("head is required"))
|
return to.ErrorResult(errors.New("head is required"))
|
||||||
}
|
}
|
||||||
base, ok := req.Params.Arguments["base"].(string)
|
base, ok := req.GetArguments()["base"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("base is required"))
|
return to.ErrorResult(errors.New("base is required"))
|
||||||
}
|
}
|
||||||
pr, _, err := gitea.Client().CreatePullRequest(owner, repo, gitea_sdk.CreatePullRequestOption{
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
pr, _, err := client.CreatePullRequest(owner, repo, gitea_sdk.CreatePullRequestOption{
|
||||||
Title: title,
|
Title: title,
|
||||||
Body: body,
|
Body: body,
|
||||||
Head: head,
|
Head: head,
|
||||||
@@ -159,3 +411,551 @@ func CreatePullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.Cal
|
|||||||
|
|
||||||
return to.TextResult(pr)
|
return to.TextResult(pr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreatePullRequestReviewerFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreatePullRequestReviewerFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var reviewers []string
|
||||||
|
if reviewersArg, exists := req.GetArguments()["reviewers"]; exists {
|
||||||
|
if reviewersSlice, ok := reviewersArg.([]any); ok {
|
||||||
|
for _, reviewer := range reviewersSlice {
|
||||||
|
if reviewerStr, ok := reviewer.(string); ok {
|
||||||
|
reviewers = append(reviewers, reviewerStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var teamReviewers []string
|
||||||
|
if teamReviewersArg, exists := req.GetArguments()["team_reviewers"]; exists {
|
||||||
|
if teamReviewersSlice, ok := teamReviewersArg.([]any); ok {
|
||||||
|
for _, teamReviewer := range teamReviewersSlice {
|
||||||
|
if teamReviewerStr, ok := teamReviewer.(string); ok {
|
||||||
|
teamReviewers = append(teamReviewers, teamReviewerStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.CreateReviewRequests(owner, repo, index, gitea_sdk.PullReviewRequestOptions{
|
||||||
|
Reviewers: reviewers,
|
||||||
|
TeamReviewers: teamReviewers,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create review requests for %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a success message instead of the Response object which contains non-serializable functions
|
||||||
|
successMsg := map[string]any{
|
||||||
|
"message": "Successfully created review requests",
|
||||||
|
"reviewers": reviewers,
|
||||||
|
"team_reviewers": teamReviewers,
|
||||||
|
"pr_index": index,
|
||||||
|
"repository": fmt.Sprintf("%s/%s", owner, repo),
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(successMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePullRequestReviewerFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeletePullRequestReviewerFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var reviewers []string
|
||||||
|
if reviewersArg, exists := req.GetArguments()["reviewers"]; exists {
|
||||||
|
if reviewersSlice, ok := reviewersArg.([]any); ok {
|
||||||
|
for _, reviewer := range reviewersSlice {
|
||||||
|
if reviewerStr, ok := reviewer.(string); ok {
|
||||||
|
reviewers = append(reviewers, reviewerStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var teamReviewers []string
|
||||||
|
if teamReviewersArg, exists := req.GetArguments()["team_reviewers"]; exists {
|
||||||
|
if teamReviewersSlice, ok := teamReviewersArg.([]any); ok {
|
||||||
|
for _, teamReviewer := range teamReviewersSlice {
|
||||||
|
if teamReviewerStr, ok := teamReviewer.(string); ok {
|
||||||
|
teamReviewers = append(teamReviewers, teamReviewerStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.DeleteReviewRequests(owner, repo, index, gitea_sdk.PullReviewRequestOptions{
|
||||||
|
Reviewers: reviewers,
|
||||||
|
TeamReviewers: teamReviewers,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete review requests for %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
successMsg := map[string]any{
|
||||||
|
"message": "Successfully deleted review requests",
|
||||||
|
"reviewers": reviewers,
|
||||||
|
"team_reviewers": teamReviewers,
|
||||||
|
"pr_index": index,
|
||||||
|
"repository": fmt.Sprintf("%s/%s", owner, repo),
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(successMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListPullRequestReviewsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListPullRequestReviewsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
reviews, _, err := client.ListPullReviews(owner, repo, index, gitea_sdk.ListPullReviewsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list reviews for %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(reviews)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPullRequestReviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetPullRequestReviewFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
reviewID, err := params.GetIndex(req.GetArguments(), "review_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
review, _, err := client.GetPullReview(owner, repo, index, reviewID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get review %v for %v/%v/pr/%v err: %v", reviewID, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(review)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListPullRequestReviewCommentsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListPullRequestReviewCommentsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
reviewID, err := params.GetIndex(req.GetArguments(), "review_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
comments, _, err := client.ListPullReviewComments(owner, repo, index, reviewID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list review comments for review %v on %v/%v/pr/%v err: %v", reviewID, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(comments)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreatePullRequestReviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreatePullRequestReviewFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.CreatePullReviewOptions{}
|
||||||
|
|
||||||
|
if state, ok := req.GetArguments()["state"].(string); ok {
|
||||||
|
opt.State = gitea_sdk.ReviewStateType(state)
|
||||||
|
}
|
||||||
|
if body, ok := req.GetArguments()["body"].(string); ok {
|
||||||
|
opt.Body = body
|
||||||
|
}
|
||||||
|
if commitID, ok := req.GetArguments()["commit_id"].(string); ok {
|
||||||
|
opt.CommitID = commitID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse inline comments
|
||||||
|
if commentsArg, exists := req.GetArguments()["comments"]; exists {
|
||||||
|
if commentsSlice, ok := commentsArg.([]any); ok {
|
||||||
|
for _, comment := range commentsSlice {
|
||||||
|
if commentMap, ok := comment.(map[string]any); ok {
|
||||||
|
reviewComment := gitea_sdk.CreatePullReviewComment{}
|
||||||
|
if path, ok := commentMap["path"].(string); ok {
|
||||||
|
reviewComment.Path = path
|
||||||
|
}
|
||||||
|
if body, ok := commentMap["body"].(string); ok {
|
||||||
|
reviewComment.Body = body
|
||||||
|
}
|
||||||
|
if oldLineNum, ok := params.ToInt64(commentMap["old_line_num"]); ok {
|
||||||
|
reviewComment.OldLineNum = oldLineNum
|
||||||
|
}
|
||||||
|
if newLineNum, ok := params.ToInt64(commentMap["new_line_num"]); ok {
|
||||||
|
reviewComment.NewLineNum = newLineNum
|
||||||
|
}
|
||||||
|
opt.Comments = append(opt.Comments, reviewComment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
review, _, err := client.CreatePullReview(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create review for %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(review)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SubmitPullRequestReviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called SubmitPullRequestReviewFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
reviewID, err := params.GetIndex(req.GetArguments(), "review_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
state, ok := req.GetArguments()["state"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("state is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.SubmitPullReviewOptions{
|
||||||
|
State: gitea_sdk.ReviewStateType(state),
|
||||||
|
}
|
||||||
|
if body, ok := req.GetArguments()["body"].(string); ok {
|
||||||
|
opt.Body = body
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
review, _, err := client.SubmitPullReview(owner, repo, index, reviewID, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("submit review %v for %v/%v/pr/%v err: %v", reviewID, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(review)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePullRequestReviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeletePullRequestReviewFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
reviewID, err := params.GetIndex(req.GetArguments(), "review_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.DeletePullReview(owner, repo, index, reviewID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete review %v for %v/%v/pr/%v err: %v", reviewID, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
successMsg := map[string]any{
|
||||||
|
"message": "Successfully deleted review",
|
||||||
|
"review_id": reviewID,
|
||||||
|
"pr_index": index,
|
||||||
|
"repository": fmt.Sprintf("%s/%s", owner, repo),
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(successMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DismissPullRequestReviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DismissPullRequestReviewFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
reviewID, err := params.GetIndex(req.GetArguments(), "review_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.DismissPullReviewOptions{}
|
||||||
|
if message, ok := req.GetArguments()["message"].(string); ok {
|
||||||
|
opt.Message = message
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.DismissPullReview(owner, repo, index, reviewID, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("dismiss review %v for %v/%v/pr/%v err: %v", reviewID, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
successMsg := map[string]any{
|
||||||
|
"message": "Successfully dismissed review",
|
||||||
|
"review_id": reviewID,
|
||||||
|
"pr_index": index,
|
||||||
|
"repository": fmt.Sprintf("%s/%s", owner, repo),
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(successMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func MergePullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called MergePullRequestFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeStyle := "merge"
|
||||||
|
if style, exists := req.GetArguments()["merge_style"].(string); exists && style != "" {
|
||||||
|
mergeStyle = style
|
||||||
|
}
|
||||||
|
|
||||||
|
title := ""
|
||||||
|
if t, exists := req.GetArguments()["title"].(string); exists {
|
||||||
|
title = t
|
||||||
|
}
|
||||||
|
|
||||||
|
message := ""
|
||||||
|
if msg, exists := req.GetArguments()["message"].(string); exists {
|
||||||
|
message = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteBranch := false
|
||||||
|
if del, exists := req.GetArguments()["delete_branch"].(bool); exists {
|
||||||
|
deleteBranch = del
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.MergePullRequestOption{
|
||||||
|
Style: gitea_sdk.MergeStyle(mergeStyle),
|
||||||
|
Title: title,
|
||||||
|
Message: message,
|
||||||
|
DeleteBranchAfterMerge: deleteBranch,
|
||||||
|
}
|
||||||
|
|
||||||
|
merged, resp, err := client.MergePullRequest(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("merge %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !merged && resp != nil && resp.StatusCode >= 400 {
|
||||||
|
return to.ErrorResult(fmt.Errorf("merge %v/%v/pr/%v failed: HTTP %d %s", owner, repo, index, resp.StatusCode, resp.Status))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !merged {
|
||||||
|
return to.ErrorResult(fmt.Errorf("merge %v/%v/pr/%v returned merged=false", owner, repo, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
successMsg := map[string]any{
|
||||||
|
"merged": merged,
|
||||||
|
"pr_index": index,
|
||||||
|
"repository": fmt.Sprintf("%s/%s", owner, repo),
|
||||||
|
"merge_style": mergeStyle,
|
||||||
|
"branch_deleted": deleteBranch,
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(successMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditPullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditPullRequestFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.EditPullRequestOption{}
|
||||||
|
|
||||||
|
if title, ok := req.GetArguments()["title"].(string); ok {
|
||||||
|
opt.Title = title
|
||||||
|
}
|
||||||
|
if body, ok := req.GetArguments()["body"].(string); ok {
|
||||||
|
opt.Body = new(body)
|
||||||
|
}
|
||||||
|
if base, ok := req.GetArguments()["base"].(string); ok {
|
||||||
|
opt.Base = base
|
||||||
|
}
|
||||||
|
if assignee, ok := req.GetArguments()["assignee"].(string); ok {
|
||||||
|
opt.Assignee = assignee
|
||||||
|
}
|
||||||
|
if assigneesArg, exists := req.GetArguments()["assignees"]; exists {
|
||||||
|
if assigneesSlice, ok := assigneesArg.([]any); ok {
|
||||||
|
var assignees []string
|
||||||
|
for _, a := range assigneesSlice {
|
||||||
|
if s, ok := a.(string); ok {
|
||||||
|
assignees = append(assignees, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opt.Assignees = assignees
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if val, exists := req.GetArguments()["milestone"]; exists {
|
||||||
|
if milestone, ok := params.ToInt64(val); ok {
|
||||||
|
opt.Milestone = milestone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if state, ok := req.GetArguments()["state"].(string); ok {
|
||||||
|
opt.State = new(gitea_sdk.StateType(state))
|
||||||
|
}
|
||||||
|
if allowMaintainerEdit, ok := req.GetArguments()["allow_maintainer_edit"].(bool); ok {
|
||||||
|
opt.AllowMaintainerEdit = new(allowMaintainerEdit)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
pr, _, err := client.EditPullRequest(owner, repo, index, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/pr/%v err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(pr)
|
||||||
|
}
|
||||||
|
|||||||
397
operation/pull/pull_test.go
Normal file
397
operation/pull/pull_test.go
Normal file
@@ -0,0 +1,397 @@
|
|||||||
|
package pull
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEditPullRequestFn(t *testing.T) {
|
||||||
|
const (
|
||||||
|
owner = "octo"
|
||||||
|
repo = "demo"
|
||||||
|
index = 7
|
||||||
|
)
|
||||||
|
|
||||||
|
indexInputs := []struct {
|
||||||
|
name string
|
||||||
|
val any
|
||||||
|
}{
|
||||||
|
{"float64", float64(index)},
|
||||||
|
{"string", "7"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ii := range indexInputs {
|
||||||
|
t.Run(ii.name, func(t *testing.T) {
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
gotMethod string
|
||||||
|
gotPath string
|
||||||
|
gotBody map[string]any
|
||||||
|
)
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/v1/version":
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"version":"1.12.0"}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s", owner, repo):
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"private":false}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index):
|
||||||
|
mu.Lock()
|
||||||
|
gotMethod = r.Method
|
||||||
|
gotPath = r.URL.Path
|
||||||
|
var body map[string]any
|
||||||
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||||
|
gotBody = body
|
||||||
|
mu.Unlock()
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write(fmt.Appendf(nil, `{"number":%d,"title":"%s","state":"open"}`, index, body["title"]))
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
origHost := flag.Host
|
||||||
|
origToken := flag.Token
|
||||||
|
origVersion := flag.Version
|
||||||
|
flag.Host = server.URL
|
||||||
|
flag.Token = ""
|
||||||
|
flag.Version = "test"
|
||||||
|
defer func() {
|
||||||
|
flag.Host = origHost
|
||||||
|
flag.Token = origToken
|
||||||
|
flag.Version = origVersion
|
||||||
|
}()
|
||||||
|
|
||||||
|
req := mcp.CallToolRequest{
|
||||||
|
Params: mcp.CallToolParams{
|
||||||
|
Arguments: map[string]any{
|
||||||
|
"owner": owner,
|
||||||
|
"repo": repo,
|
||||||
|
"index": ii.val,
|
||||||
|
"title": "WIP: my feature",
|
||||||
|
"state": "open",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := EditPullRequestFn(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("EditPullRequestFn() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
if gotMethod != http.MethodPatch {
|
||||||
|
t.Fatalf("expected PATCH request, got %s", gotMethod)
|
||||||
|
}
|
||||||
|
if gotPath != fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index) {
|
||||||
|
t.Fatalf("unexpected path: %s", gotPath)
|
||||||
|
}
|
||||||
|
if gotBody["title"] != "WIP: my feature" {
|
||||||
|
t.Fatalf("expected title 'WIP: my feature', got %v", gotBody["title"])
|
||||||
|
}
|
||||||
|
if gotBody["state"] != "open" {
|
||||||
|
t.Fatalf("expected state 'open', got %v", gotBody["state"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Content) == 0 {
|
||||||
|
t.Fatalf("expected content in result")
|
||||||
|
}
|
||||||
|
textContent, ok := mcp.AsTextContent(result.Content[0])
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected text content, got %T", result.Content[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed struct {
|
||||||
|
Result map[string]any `json:"Result"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(textContent.Text), &parsed); err != nil {
|
||||||
|
t.Fatalf("unmarshal result text: %v", err)
|
||||||
|
}
|
||||||
|
if got := parsed.Result["title"].(string); got != "WIP: my feature" {
|
||||||
|
t.Fatalf("result title = %q, want %q", got, "WIP: my feature")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergePullRequestFn(t *testing.T) {
|
||||||
|
const (
|
||||||
|
owner = "octo"
|
||||||
|
repo = "demo"
|
||||||
|
index = 5
|
||||||
|
)
|
||||||
|
|
||||||
|
indexInputs := []struct {
|
||||||
|
name string
|
||||||
|
val any
|
||||||
|
}{
|
||||||
|
{"float64", float64(index)},
|
||||||
|
{"string", "5"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ii := range indexInputs {
|
||||||
|
t.Run(ii.name, func(t *testing.T) {
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
gotMethod string
|
||||||
|
gotPath string
|
||||||
|
gotBody map[string]any
|
||||||
|
)
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/v1/version":
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"version":"1.12.0"}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s", owner, repo):
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"private":false}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner, repo, index):
|
||||||
|
mu.Lock()
|
||||||
|
gotMethod = r.Method
|
||||||
|
gotPath = r.URL.Path
|
||||||
|
var body map[string]any
|
||||||
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||||
|
gotBody = body
|
||||||
|
mu.Unlock()
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
origHost := flag.Host
|
||||||
|
origToken := flag.Token
|
||||||
|
origVersion := flag.Version
|
||||||
|
flag.Host = server.URL
|
||||||
|
flag.Token = ""
|
||||||
|
flag.Version = "test"
|
||||||
|
defer func() {
|
||||||
|
flag.Host = origHost
|
||||||
|
flag.Token = origToken
|
||||||
|
flag.Version = origVersion
|
||||||
|
}()
|
||||||
|
|
||||||
|
req := mcp.CallToolRequest{
|
||||||
|
Params: mcp.CallToolParams{
|
||||||
|
Arguments: map[string]any{
|
||||||
|
"owner": owner,
|
||||||
|
"repo": repo,
|
||||||
|
"index": ii.val,
|
||||||
|
"merge_style": "squash",
|
||||||
|
"title": "feat: my squashed commit",
|
||||||
|
"message": "Squash merge of PR #5",
|
||||||
|
"delete_branch": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := MergePullRequestFn(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("MergePullRequestFn() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
if gotMethod != http.MethodPost {
|
||||||
|
t.Fatalf("expected POST request, got %s", gotMethod)
|
||||||
|
}
|
||||||
|
if gotPath != fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner, repo, index) {
|
||||||
|
t.Fatalf("unexpected path: %s", gotPath)
|
||||||
|
}
|
||||||
|
if gotBody["Do"] != "squash" {
|
||||||
|
t.Fatalf("expected Do 'squash', got %v", gotBody["Do"])
|
||||||
|
}
|
||||||
|
if gotBody["MergeTitleField"] != "feat: my squashed commit" {
|
||||||
|
t.Fatalf("expected MergeTitleField 'feat: my squashed commit', got %v", gotBody["MergeTitleField"])
|
||||||
|
}
|
||||||
|
if gotBody["MergeMessageField"] != "Squash merge of PR #5" {
|
||||||
|
t.Fatalf("expected MergeMessageField 'Squash merge of PR #5', got %v", gotBody["MergeMessageField"])
|
||||||
|
}
|
||||||
|
if gotBody["delete_branch_after_merge"] != true {
|
||||||
|
t.Fatalf("expected delete_branch_after_merge true, got %v", gotBody["delete_branch_after_merge"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Content) == 0 {
|
||||||
|
t.Fatalf("expected content in result")
|
||||||
|
}
|
||||||
|
textContent, ok := mcp.AsTextContent(result.Content[0])
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected text content, got %T", result.Content[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed struct {
|
||||||
|
Result map[string]any `json:"Result"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(textContent.Text), &parsed); err != nil {
|
||||||
|
t.Fatalf("unmarshal result text: %v", err)
|
||||||
|
}
|
||||||
|
if parsed.Result["merged"] != true {
|
||||||
|
t.Fatalf("expected merged=true, got %v", parsed.Result["merged"])
|
||||||
|
}
|
||||||
|
if parsed.Result["merge_style"] != "squash" {
|
||||||
|
t.Fatalf("expected merge_style 'squash', got %v", parsed.Result["merge_style"])
|
||||||
|
}
|
||||||
|
if parsed.Result["branch_deleted"] != true {
|
||||||
|
t.Fatalf("expected branch_deleted=true, got %v", parsed.Result["branch_deleted"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetPullRequestDiffFn(t *testing.T) {
|
||||||
|
const (
|
||||||
|
owner = "octo"
|
||||||
|
repo = "demo"
|
||||||
|
index = 12
|
||||||
|
diffRaw = "diff --git a/file.txt b/file.txt\n+line\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
indexInputs := []struct {
|
||||||
|
name string
|
||||||
|
val any
|
||||||
|
}{
|
||||||
|
{"float64", float64(index)},
|
||||||
|
{"string", "12"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ii := range indexInputs {
|
||||||
|
t.Run(ii.name, func(t *testing.T) {
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
diffRequested bool
|
||||||
|
binaryValue string
|
||||||
|
)
|
||||||
|
errCh := make(chan error, 1)
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/v1/version":
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"version":"1.12.0"}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s", owner, repo):
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"private":false}`))
|
||||||
|
case fmt.Sprintf("/%s/%s/pulls/%d.diff", owner, repo, index):
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
select {
|
||||||
|
case errCh <- fmt.Errorf("unexpected method: %s", r.Method):
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
diffRequested = true
|
||||||
|
binaryValue = r.URL.Query().Get("binary")
|
||||||
|
mu.Unlock()
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
_, _ = w.Write([]byte(diffRaw))
|
||||||
|
default:
|
||||||
|
select {
|
||||||
|
case errCh <- fmt.Errorf("unexpected request path: %s", r.URL.Path):
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
origHost := flag.Host
|
||||||
|
origToken := flag.Token
|
||||||
|
origVersion := flag.Version
|
||||||
|
flag.Host = server.URL
|
||||||
|
flag.Token = ""
|
||||||
|
flag.Version = "test"
|
||||||
|
defer func() {
|
||||||
|
flag.Host = origHost
|
||||||
|
flag.Token = origToken
|
||||||
|
flag.Version = origVersion
|
||||||
|
}()
|
||||||
|
|
||||||
|
req := mcp.CallToolRequest{
|
||||||
|
Params: mcp.CallToolParams{
|
||||||
|
Arguments: map[string]any{
|
||||||
|
"owner": owner,
|
||||||
|
"repo": repo,
|
||||||
|
"index": ii.val,
|
||||||
|
"binary": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := GetPullRequestDiffFn(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetPullRequestDiffFn() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case reqErr := <-errCh:
|
||||||
|
t.Fatalf("handler error: %v", reqErr)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
requested := diffRequested
|
||||||
|
gotBinary := binaryValue
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
if !requested {
|
||||||
|
t.Fatalf("expected diff request to be made")
|
||||||
|
}
|
||||||
|
if gotBinary != "true" {
|
||||||
|
t.Fatalf("expected binary=true query param, got %q", gotBinary)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Content) == 0 {
|
||||||
|
t.Fatalf("expected content in result")
|
||||||
|
}
|
||||||
|
|
||||||
|
textContent, ok := mcp.AsTextContent(result.Content[0])
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected text content, got %T", result.Content[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed struct {
|
||||||
|
Result map[string]any `json:"Result"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(textContent.Text), &parsed); err != nil {
|
||||||
|
t.Fatalf("unmarshal result text: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, ok := parsed.Result["diff"].(string); !ok || got != diffRaw {
|
||||||
|
t.Fatalf("diff = %q, want %q", got, diffRaw)
|
||||||
|
}
|
||||||
|
if got, ok := parsed.Result["binary"].(bool); !ok || got != true {
|
||||||
|
t.Fatalf("binary = %v, want true", got)
|
||||||
|
}
|
||||||
|
if got, ok := parsed.Result["index"].(float64); !ok || int64(got) != int64(index) {
|
||||||
|
t.Fatalf("index = %v, want %d", got, index)
|
||||||
|
}
|
||||||
|
if got, ok := parsed.Result["owner"].(string); !ok || got != owner {
|
||||||
|
t.Fatalf("owner = %q, want %q", got, owner)
|
||||||
|
}
|
||||||
|
if got, ok := parsed.Result["repo"].(string); !ok || got != repo {
|
||||||
|
t.Fatalf("repo = %q, want %q", got, repo)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,16 @@ package repo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -44,23 +46,42 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateBranchTool,
|
||||||
|
Handler: CreateBranchFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteBranchTool,
|
||||||
|
Handler: DeleteBranchFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListBranchesTool,
|
||||||
|
Handler: ListBranchesFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called CreateBranchFn")
|
log.Debugf("Called CreateBranchFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
branch, ok := req.Params.Arguments["branch"].(string)
|
branch, ok := req.GetArguments()["branch"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("branch is required"))
|
return to.ErrorResult(errors.New("branch is required"))
|
||||||
}
|
}
|
||||||
oldBranch, _ := req.Params.Arguments["old_branch"].(string)
|
oldBranch, _ := req.GetArguments()["old_branch"].(string)
|
||||||
|
|
||||||
_, _, err := gitea.Client().CreateBranch(owner, repo, gitea_sdk.CreateBranchOption{
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.CreateBranch(owner, repo, gitea_sdk.CreateBranchOption{
|
||||||
BranchName: branch,
|
BranchName: branch,
|
||||||
OldBranchName: oldBranch,
|
OldBranchName: oldBranch,
|
||||||
})
|
})
|
||||||
@@ -73,19 +94,23 @@ func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
|
|||||||
|
|
||||||
func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called DeleteBranchFn")
|
log.Debugf("Called DeleteBranchFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
branch, ok := req.Params.Arguments["branch"].(string)
|
branch, ok := req.GetArguments()["branch"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("branch is required"))
|
return to.ErrorResult(errors.New("branch is required"))
|
||||||
}
|
}
|
||||||
_, _, err := gitea.Client().DeleteRepoBranch(owner, repo, branch)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.DeleteRepoBranch(owner, repo, branch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("delete branch error: %v", err))
|
return to.ErrorResult(fmt.Errorf("delete branch error: %v", err))
|
||||||
}
|
}
|
||||||
@@ -95,13 +120,13 @@ func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
|
|||||||
|
|
||||||
func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called ListBranchesFn")
|
log.Debugf("Called ListBranchesFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
opt := gitea_sdk.ListRepoBranchesOptions{
|
opt := gitea_sdk.ListRepoBranchesOptions{
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
@@ -109,7 +134,11 @@ func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTool
|
|||||||
PageSize: 100,
|
PageSize: 100,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
branches, _, err := gitea.Client().ListRepoBranches(owner, repo, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
branches, _, err := client.ListRepoBranches(owner, repo, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("list branches error: %v", err))
|
return to.ErrorResult(fmt.Errorf("list branches error: %v", err))
|
||||||
}
|
}
|
||||||
|
|||||||
226
operation/repo/branch_protection.go
Normal file
226
operation/repo/branch_protection.go
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListBranchProtectionsToolName = "list_branch_protections"
|
||||||
|
GetBranchProtectionToolName = "get_branch_protection"
|
||||||
|
CreateBranchProtectionToolName = "create_branch_protection"
|
||||||
|
EditBranchProtectionToolName = "edit_branch_protection"
|
||||||
|
DeleteBranchProtectionToolName = "delete_branch_protection"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListBranchProtectionsTool = mcp.NewTool(
|
||||||
|
ListBranchProtectionsToolName,
|
||||||
|
mcp.WithDescription("List branch protections for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetBranchProtectionTool = mcp.NewTool(
|
||||||
|
GetBranchProtectionToolName,
|
||||||
|
mcp.WithDescription("Get a branch protection rule by name"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("branch protection rule name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateBranchProtectionTool = mcp.NewTool(
|
||||||
|
CreateBranchProtectionToolName,
|
||||||
|
mcp.WithDescription("Create a branch protection rule"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("rule_name", mcp.Required(), mcp.Description("rule name (glob pattern for branch matching)")),
|
||||||
|
mcp.WithBoolean("enable_push", mcp.Description("enable push to protected branch")),
|
||||||
|
mcp.WithBoolean("enable_merge_whitelist", mcp.Description("enable merge whitelist")),
|
||||||
|
mcp.WithBoolean("enable_status_check", mcp.Description("enable status check")),
|
||||||
|
mcp.WithNumber("required_approvals", mcp.Description("number of required approvals")),
|
||||||
|
mcp.WithBoolean("block_on_rejected_reviews", mcp.Description("block merge on rejected reviews")),
|
||||||
|
mcp.WithBoolean("dismiss_stale_approvals", mcp.Description("dismiss stale approvals on new commits")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditBranchProtectionTool = mcp.NewTool(
|
||||||
|
EditBranchProtectionToolName,
|
||||||
|
mcp.WithDescription("Edit a branch protection rule"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("branch protection rule name")),
|
||||||
|
mcp.WithBoolean("enable_push", mcp.Description("enable push to protected branch")),
|
||||||
|
mcp.WithBoolean("enable_merge_whitelist", mcp.Description("enable merge whitelist")),
|
||||||
|
mcp.WithBoolean("enable_status_check", mcp.Description("enable status check")),
|
||||||
|
mcp.WithNumber("required_approvals", mcp.Description("number of required approvals")),
|
||||||
|
mcp.WithBoolean("block_on_rejected_reviews", mcp.Description("block merge on rejected reviews")),
|
||||||
|
mcp.WithBoolean("dismiss_stale_approvals", mcp.Description("dismiss stale approvals on new commits")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteBranchProtectionTool = mcp.NewTool(
|
||||||
|
DeleteBranchProtectionToolName,
|
||||||
|
mcp.WithDescription("Delete a branch protection rule"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("branch protection rule name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListBranchProtectionsTool, Handler: ListBranchProtectionsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetBranchProtectionTool, Handler: GetBranchProtectionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateBranchProtectionTool, Handler: CreateBranchProtectionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: EditBranchProtectionTool, Handler: EditBranchProtectionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteBranchProtectionTool, Handler: DeleteBranchProtectionFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListBranchProtectionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListBranchProtectionsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
bps, _, err := client.ListBranchProtections(owner, repo, gitea_sdk.ListBranchProtectionsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list branch protections err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(bps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBranchProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetBranchProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if owner == "" || repo == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
bp, _, err := client.GetBranchProtection(owner, repo, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get branch protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(bp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateBranchProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateBranchProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
ruleName, _ := req.GetArguments()["rule_name"].(string)
|
||||||
|
if owner == "" || repo == "" || ruleName == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and rule_name are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateBranchProtectionOption{
|
||||||
|
RuleName: ruleName,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["enable_push"].(bool); ok {
|
||||||
|
opt.EnablePush = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["enable_merge_whitelist"].(bool); ok {
|
||||||
|
opt.EnableMergeWhitelist = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["enable_status_check"].(bool); ok {
|
||||||
|
opt.EnableStatusCheck = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["required_approvals"].(float64); ok {
|
||||||
|
opt.RequiredApprovals = int64(v)
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["block_on_rejected_reviews"].(bool); ok {
|
||||||
|
opt.BlockOnRejectedReviews = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["dismiss_stale_approvals"].(bool); ok {
|
||||||
|
opt.DismissStaleApprovals = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
bp, _, err := client.CreateBranchProtection(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create branch protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(bp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditBranchProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditBranchProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if owner == "" || repo == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and name are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditBranchProtectionOption{}
|
||||||
|
if v, ok := req.GetArguments()["enable_push"].(bool); ok {
|
||||||
|
opt.EnablePush = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["enable_merge_whitelist"].(bool); ok {
|
||||||
|
opt.EnableMergeWhitelist = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["enable_status_check"].(bool); ok {
|
||||||
|
opt.EnableStatusCheck = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["required_approvals"].(float64); ok {
|
||||||
|
approvals := int64(v)
|
||||||
|
opt.RequiredApprovals = &approvals
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["block_on_rejected_reviews"].(bool); ok {
|
||||||
|
opt.BlockOnRejectedReviews = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["dismiss_stale_approvals"].(bool); ok {
|
||||||
|
opt.DismissStaleApprovals = &v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
bp, _, err := client.EditBranchProtection(owner, repo, name, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit branch protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(bp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteBranchProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteBranchProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
if owner == "" || repo == "" || name == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteBranchProtection(owner, repo, name)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete branch protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
233
operation/repo/collaborator.go
Normal file
233
operation/repo/collaborator.go
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListCollaboratorsToolName = "list_collaborators"
|
||||||
|
IsCollaboratorToolName = "is_collaborator"
|
||||||
|
CollaboratorPermissionToolName = "collaborator_permission"
|
||||||
|
AddCollaboratorToolName = "add_collaborator"
|
||||||
|
DeleteCollaboratorToolName = "delete_collaborator"
|
||||||
|
GetReviewersToolName = "get_reviewers"
|
||||||
|
GetAssigneesToolName = "get_assignees"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListCollaboratorsTool = mcp.NewTool(
|
||||||
|
ListCollaboratorsToolName,
|
||||||
|
mcp.WithDescription("List a repository's collaborators"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
IsCollaboratorTool = mcp.NewTool(
|
||||||
|
IsCollaboratorToolName,
|
||||||
|
mcp.WithDescription("Check if a user is a collaborator of a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("collaborator", mcp.Required(), mcp.Description("username to check")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CollaboratorPermissionTool = mcp.NewTool(
|
||||||
|
CollaboratorPermissionToolName,
|
||||||
|
mcp.WithDescription("Get a collaborator's permission level on a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("collaborator", mcp.Required(), mcp.Description("collaborator username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddCollaboratorTool = mcp.NewTool(
|
||||||
|
AddCollaboratorToolName,
|
||||||
|
mcp.WithDescription("Add a collaborator to a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("collaborator", mcp.Required(), mcp.Description("username to add")),
|
||||||
|
mcp.WithString("permission", mcp.Description("permission level: read, write, admin (default: write)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteCollaboratorTool = mcp.NewTool(
|
||||||
|
DeleteCollaboratorToolName,
|
||||||
|
mcp.WithDescription("Remove a collaborator from a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("collaborator", mcp.Required(), mcp.Description("username to remove")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetReviewersTool = mcp.NewTool(
|
||||||
|
GetReviewersToolName,
|
||||||
|
mcp.WithDescription("Get the list of users who can review PRs in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetAssigneesTool = mcp.NewTool(
|
||||||
|
GetAssigneesToolName,
|
||||||
|
mcp.WithDescription("Get the list of users who can be assigned to issues in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListCollaboratorsTool, Handler: ListCollaboratorsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: IsCollaboratorTool, Handler: IsCollaboratorFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CollaboratorPermissionTool, Handler: CollaboratorPermissionFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetReviewersTool, Handler: GetReviewersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetAssigneesTool, Handler: GetAssigneesFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddCollaboratorTool, Handler: AddCollaboratorFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteCollaboratorTool, Handler: DeleteCollaboratorFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListCollaboratorsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListCollaboratorsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.ListCollaborators(owner, repo, gitea_sdk.ListCollaboratorsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list collaborators err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsCollaboratorFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called IsCollaboratorFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
collaborator, _ := req.GetArguments()["collaborator"].(string)
|
||||||
|
if owner == "" || repo == "" || collaborator == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and collaborator are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isCollab, _, err := client.IsCollaborator(owner, repo, collaborator)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check collaborator err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"collaborator": collaborator, "is_collaborator": isCollab})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CollaboratorPermissionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CollaboratorPermissionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
collaborator, _ := req.GetArguments()["collaborator"].(string)
|
||||||
|
if owner == "" || repo == "" || collaborator == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and collaborator are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
perm, _, err := client.CollaboratorPermission(owner, repo, collaborator)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get collaborator permission err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(perm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddCollaboratorFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddCollaboratorFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
collaborator, _ := req.GetArguments()["collaborator"].(string)
|
||||||
|
if owner == "" || repo == "" || collaborator == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and collaborator are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.AddCollaboratorOption{}
|
||||||
|
if v, ok := req.GetArguments()["permission"].(string); ok {
|
||||||
|
perm := gitea_sdk.AccessMode(v)
|
||||||
|
opt.Permission = &perm
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AddCollaborator(owner, repo, collaborator, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add collaborator err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "added", "collaborator": collaborator})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteCollaboratorFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteCollaboratorFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
collaborator, _ := req.GetArguments()["collaborator"].(string)
|
||||||
|
if owner == "" || repo == "" || collaborator == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and collaborator are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteCollaborator(owner, repo, collaborator)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete collaborator err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "removed", "collaborator": collaborator})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetReviewersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetReviewersFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.GetReviewers(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get reviewers err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAssigneesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetAssigneesFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.GetAssignees(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get assignees err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
@@ -2,22 +2,24 @@ package repo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ListRepoCommitsToolName = "list_repo_commits"
|
ListRepoCommitsToolName = "list_repo_commits"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var ListRepoCommitsTool = mcp.NewTool(
|
||||||
ListRepoCommitsTool = mcp.NewTool(
|
|
||||||
ListRepoCommitsToolName,
|
ListRepoCommitsToolName,
|
||||||
mcp.WithDescription("List repository commits"),
|
mcp.WithDescription("List repository commits"),
|
||||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
@@ -26,29 +28,35 @@ var (
|
|||||||
mcp.WithString("path", mcp.Description("path indicates that only commits that include the path's file/dir should be returned.")),
|
mcp.WithString("path", mcp.Description("path indicates that only commits that include the path's file/dir should be returned.")),
|
||||||
mcp.WithNumber("page", mcp.Required(), mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
mcp.WithNumber("page", mcp.Required(), mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
mcp.WithNumber("page_size", mcp.Required(), mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
mcp.WithNumber("page_size", mcp.Required(), mcp.Description("page size"), mcp.DefaultNumber(50), mcp.Min(1)),
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListRepoCommitsTool,
|
||||||
|
Handler: ListRepoCommitsFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func ListRepoCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ListRepoCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called ListRepoCommitsFn")
|
log.Debugf("Called ListRepoCommitsFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
page, err := params.GetIndex(req.GetArguments(), "page")
|
||||||
if !ok {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("page is required"))
|
return to.ErrorResult(err)
|
||||||
}
|
}
|
||||||
pageSize, ok := req.Params.Arguments["page_size"].(float64)
|
pageSize, err := params.GetIndex(req.GetArguments(), "page_size")
|
||||||
if !ok {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("page_size is required"))
|
return to.ErrorResult(err)
|
||||||
}
|
}
|
||||||
sha, _ := req.Params.Arguments["sha"].(string)
|
sha, _ := req.GetArguments()["sha"].(string)
|
||||||
path, _ := req.Params.Arguments["path"].(string)
|
path, _ := req.GetArguments()["path"].(string)
|
||||||
opt := gitea_sdk.ListCommitOptions{
|
opt := gitea_sdk.ListCommitOptions{
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
Page: int(page),
|
Page: int(page),
|
||||||
@@ -57,7 +65,11 @@ func ListRepoCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallT
|
|||||||
SHA: sha,
|
SHA: sha,
|
||||||
Path: path,
|
Path: path,
|
||||||
}
|
}
|
||||||
commits, _, err := gitea.Client().ListRepoCommits(owner, repo, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
commits, _, err := client.ListRepoCommits(owner, repo, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("list repo commits err: %v", err))
|
return to.ErrorResult(fmt.Errorf("list repo commits err: %v", err))
|
||||||
}
|
}
|
||||||
|
|||||||
161
operation/repo/deploy_key.go
Normal file
161
operation/repo/deploy_key.go
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListDeployKeysToolName = "list_deploy_keys"
|
||||||
|
GetDeployKeyToolName = "get_deploy_key"
|
||||||
|
CreateDeployKeyToolName = "create_deploy_key"
|
||||||
|
DeleteDeployKeyToolName = "delete_deploy_key"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListDeployKeysTool = mcp.NewTool(
|
||||||
|
ListDeployKeysToolName,
|
||||||
|
mcp.WithDescription("List a repository's deploy keys"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetDeployKeyTool = mcp.NewTool(
|
||||||
|
GetDeployKeyToolName,
|
||||||
|
mcp.WithDescription("Get a deploy key by ID"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("key_id", mcp.Required(), mcp.Description("deploy key ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateDeployKeyTool = mcp.NewTool(
|
||||||
|
CreateDeployKeyToolName,
|
||||||
|
mcp.WithDescription("Add a deploy key to a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("title", mcp.Required(), mcp.Description("key title")),
|
||||||
|
mcp.WithString("key", mcp.Required(), mcp.Description("public key content")),
|
||||||
|
mcp.WithBoolean("read_only", mcp.Description("whether the key has read-only access (default: true)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteDeployKeyTool = mcp.NewTool(
|
||||||
|
DeleteDeployKeyToolName,
|
||||||
|
mcp.WithDescription("Remove a deploy key from a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("key_id", mcp.Required(), mcp.Description("deploy key ID")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListDeployKeysTool, Handler: ListDeployKeysFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetDeployKeyTool, Handler: GetDeployKeyFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateDeployKeyTool, Handler: CreateDeployKeyFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteDeployKeyTool, Handler: DeleteDeployKeyFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListDeployKeysFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListDeployKeysFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
keys, _, err := client.ListDeployKeys(owner, repo, gitea_sdk.ListDeployKeysOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list deploy keys err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDeployKeyFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetDeployKeyFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
keyID, err := params.GetIndex(req.GetArguments(), "key_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
key, _, err := client.GetDeployKey(owner, repo, keyID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get deploy key err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateDeployKeyFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateDeployKeyFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
title, _ := req.GetArguments()["title"].(string)
|
||||||
|
key, _ := req.GetArguments()["key"].(string)
|
||||||
|
if owner == "" || repo == "" || title == "" || key == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, title, and key are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateKeyOption{
|
||||||
|
Title: title,
|
||||||
|
Key: key,
|
||||||
|
ReadOnly: true,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["read_only"].(bool); ok {
|
||||||
|
opt.ReadOnly = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
dk, _, err := client.CreateDeployKey(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create deploy key err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(dk)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteDeployKeyFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteDeployKeyFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
keyID, err := params.GetIndex(req.GetArguments(), "key_id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteDeployKey(owner, repo, keyID)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete deploy key err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
@@ -1,20 +1,26 @@
|
|||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GetFileToolName = "get_file_content"
|
GetFileToolName = "get_file_content"
|
||||||
|
GetDirToolName = "get_dir_content"
|
||||||
CreateFileToolName = "create_file"
|
CreateFileToolName = "create_file"
|
||||||
UpdateFileToolName = "update_file"
|
UpdateFileToolName = "update_file"
|
||||||
DeleteFileToolName = "delete_file"
|
DeleteFileToolName = "delete_file"
|
||||||
@@ -28,6 +34,16 @@ var (
|
|||||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
mcp.WithString("ref", mcp.Required(), mcp.Description("ref can be branch/tag/commit")),
|
mcp.WithString("ref", mcp.Required(), mcp.Description("ref can be branch/tag/commit")),
|
||||||
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
||||||
|
mcp.WithBoolean("withLines", mcp.Description("whether to return file content with lines")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetDirContentTool = mcp.NewTool(
|
||||||
|
GetDirToolName,
|
||||||
|
mcp.WithDescription("Get a list of entries in a directory"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("ref", mcp.Required(), mcp.Description("ref can be branch/tag/commit")),
|
||||||
|
mcp.WithString("filePath", mcp.Required(), mcp.Description("directory path")),
|
||||||
)
|
)
|
||||||
|
|
||||||
CreateFileTool = mcp.NewTool(
|
CreateFileTool = mcp.NewTool(
|
||||||
@@ -49,7 +65,7 @@ var (
|
|||||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
||||||
mcp.WithString("sha", mcp.Required(), mcp.Description("sha is the SHA for the file that already exists")),
|
mcp.WithString("sha", mcp.Required(), mcp.Description("sha is the SHA for the file that already exists")),
|
||||||
mcp.WithString("content", mcp.Required(), mcp.Description("file content, base64 encoded")),
|
mcp.WithString("content", mcp.Required(), mcp.Description("file content")),
|
||||||
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
|
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
|
||||||
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
|
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
|
||||||
)
|
)
|
||||||
@@ -66,45 +82,140 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetFileContentTool,
|
||||||
|
Handler: GetFileContentFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetDirContentTool,
|
||||||
|
Handler: GetDirContentFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateFileTool,
|
||||||
|
Handler: CreateFileFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: UpdateFileTool,
|
||||||
|
Handler: UpdateFileFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteFileTool,
|
||||||
|
Handler: DeleteFileFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type ContentLine struct {
|
||||||
|
LineNumber int `json:"line"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetFileContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetFileFn")
|
log.Debugf("Called GetFileFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
ref, _ := req.Params.Arguments["ref"].(string)
|
ref, _ := req.GetArguments()["ref"].(string)
|
||||||
filePath, ok := req.Params.Arguments["filePath"].(string)
|
filePath, ok := req.GetArguments()["filePath"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("filePath is required"))
|
return to.ErrorResult(errors.New("filePath is required"))
|
||||||
}
|
}
|
||||||
content, _, err := gitea.Client().GetContents(owner, repo, ref, filePath)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
content, _, err := client.GetContents(owner, repo, ref, filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get file err: %v", err))
|
return to.ErrorResult(fmt.Errorf("get file err: %v", err))
|
||||||
}
|
}
|
||||||
|
withLines, _ := req.GetArguments()["withLines"].(bool)
|
||||||
|
if withLines {
|
||||||
|
rawContent, err := base64.StdEncoding.DecodeString(*content.Content)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("decode base64 content err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
contentLines := make([]ContentLine, 0)
|
||||||
|
line := 0
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(bytes.NewReader(rawContent))
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line++
|
||||||
|
|
||||||
|
contentLines = append(contentLines, ContentLine{
|
||||||
|
LineNumber: line,
|
||||||
|
Content: scanner.Text(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("scan content err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the last blank line if exists
|
||||||
|
// git does not consider the last line as a new line
|
||||||
|
if contentLines[len(contentLines)-1].Content == "" {
|
||||||
|
contentLines = contentLines[:len(contentLines)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
contentBytes, err := json.MarshalIndent(contentLines, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("marshal content lines err: %v", err))
|
||||||
|
}
|
||||||
|
contentStr := string(contentBytes)
|
||||||
|
content.Content = &contentStr
|
||||||
|
}
|
||||||
|
return to.TextResult(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDirContentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetDirContentFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
ref, _ := req.GetArguments()["ref"].(string)
|
||||||
|
filePath, ok := req.GetArguments()["filePath"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("filePath is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
content, _, err := client.ListContents(owner, repo, ref, filePath)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get dir content err: %v", err))
|
||||||
|
}
|
||||||
return to.TextResult(content)
|
return to.TextResult(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called CreateFileFn")
|
log.Debugf("Called CreateFileFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
filePath, ok := req.Params.Arguments["filePath"].(string)
|
filePath, ok := req.GetArguments()["filePath"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("filePath is required"))
|
return to.ErrorResult(errors.New("filePath is required"))
|
||||||
}
|
}
|
||||||
content, _ := req.Params.Arguments["content"].(string)
|
content, _ := req.GetArguments()["content"].(string)
|
||||||
message, _ := req.Params.Arguments["message"].(string)
|
message, _ := req.GetArguments()["message"].(string)
|
||||||
branchName, _ := req.Params.Arguments["branch_name"].(string)
|
branchName, _ := req.GetArguments()["branch_name"].(string)
|
||||||
opt := gitea_sdk.CreateFileOptions{
|
opt := gitea_sdk.CreateFileOptions{
|
||||||
Content: base64.StdEncoding.EncodeToString([]byte(content)),
|
Content: base64.StdEncoding.EncodeToString([]byte(content)),
|
||||||
FileOptions: gitea_sdk.FileOptions{
|
FileOptions: gitea_sdk.FileOptions{
|
||||||
@@ -113,7 +224,11 @@ func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err := gitea.Client().CreateFile(owner, repo, filePath, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.CreateFile(owner, repo, filePath, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("create file err: %v", err))
|
return to.ErrorResult(fmt.Errorf("create file err: %v", err))
|
||||||
}
|
}
|
||||||
@@ -122,35 +237,39 @@ func CreateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
|
|
||||||
func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called UpdateFileFn")
|
log.Debugf("Called UpdateFileFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
filePath, ok := req.Params.Arguments["filePath"].(string)
|
filePath, ok := req.GetArguments()["filePath"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("filePath is required"))
|
return to.ErrorResult(errors.New("filePath is required"))
|
||||||
}
|
}
|
||||||
sha, ok := req.Params.Arguments["sha"].(string)
|
sha, ok := req.GetArguments()["sha"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("sha is required"))
|
return to.ErrorResult(errors.New("sha is required"))
|
||||||
}
|
}
|
||||||
content, _ := req.Params.Arguments["content"].(string)
|
content, _ := req.GetArguments()["content"].(string)
|
||||||
message, _ := req.Params.Arguments["message"].(string)
|
message, _ := req.GetArguments()["message"].(string)
|
||||||
branchName, _ := req.Params.Arguments["branch_name"].(string)
|
branchName, _ := req.GetArguments()["branch_name"].(string)
|
||||||
|
|
||||||
opt := gitea_sdk.UpdateFileOptions{
|
opt := gitea_sdk.UpdateFileOptions{
|
||||||
SHA: sha,
|
SHA: sha,
|
||||||
Content: content,
|
Content: base64.StdEncoding.EncodeToString([]byte(content)),
|
||||||
FileOptions: gitea_sdk.FileOptions{
|
FileOptions: gitea_sdk.FileOptions{
|
||||||
Message: message,
|
Message: message,
|
||||||
BranchName: branchName,
|
BranchName: branchName,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, _, err := gitea.Client().UpdateFile(owner, repo, filePath, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.UpdateFile(owner, repo, filePath, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("update file err: %v", err))
|
return to.ErrorResult(fmt.Errorf("update file err: %v", err))
|
||||||
}
|
}
|
||||||
@@ -159,23 +278,23 @@ func UpdateFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
|
|
||||||
func DeleteFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func DeleteFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called DeleteFileFn")
|
log.Debugf("Called DeleteFileFn")
|
||||||
owner, ok := req.Params.Arguments["owner"].(string)
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("owner is required"))
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("repo is required"))
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
}
|
}
|
||||||
filePath, ok := req.Params.Arguments["filePath"].(string)
|
filePath, ok := req.GetArguments()["filePath"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("filePath is required"))
|
return to.ErrorResult(errors.New("filePath is required"))
|
||||||
}
|
}
|
||||||
message, _ := req.Params.Arguments["message"].(string)
|
message, _ := req.GetArguments()["message"].(string)
|
||||||
branchName, _ := req.Params.Arguments["branch_name"].(string)
|
branchName, _ := req.GetArguments()["branch_name"].(string)
|
||||||
sha, ok := req.Params.Arguments["sha"].(string)
|
sha, ok := req.GetArguments()["sha"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("sha is required"))
|
return to.ErrorResult(errors.New("sha is required"))
|
||||||
}
|
}
|
||||||
opt := gitea_sdk.DeleteFileOptions{
|
opt := gitea_sdk.DeleteFileOptions{
|
||||||
FileOptions: gitea_sdk.FileOptions{
|
FileOptions: gitea_sdk.FileOptions{
|
||||||
@@ -184,7 +303,11 @@ func DeleteFileFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
},
|
},
|
||||||
SHA: sha,
|
SHA: sha,
|
||||||
}
|
}
|
||||||
_, err := gitea.Client().DeleteFile(owner, repo, filePath, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteFile(owner, repo, filePath, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("delete file err: %v", err))
|
return to.ErrorResult(fmt.Errorf("delete file err: %v", err))
|
||||||
}
|
}
|
||||||
|
|||||||
175
operation/repo/git.go
Normal file
175
operation/repo/git.go
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetRepoRefToolName = "get_repo_ref"
|
||||||
|
ListAllGitRefsToolName = "list_all_git_refs"
|
||||||
|
GetTreeToolName = "get_tree"
|
||||||
|
GetRepoNoteToolName = "get_repo_note"
|
||||||
|
CompareCommitsToolName = "compare_commits"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GetRepoRefTool = mcp.NewTool(
|
||||||
|
GetRepoRefToolName,
|
||||||
|
mcp.WithDescription("Get a git reference from a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("ref", mcp.Required(), mcp.Description("git reference (e.g., refs/heads/main, refs/tags/v1.0)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListAllGitRefsTool = mcp.NewTool(
|
||||||
|
ListAllGitRefsToolName,
|
||||||
|
mcp.WithDescription("List all git references in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetTreeTool = mcp.NewTool(
|
||||||
|
GetTreeToolName,
|
||||||
|
mcp.WithDescription("Get the tree of a repository at a given SHA"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("sha", mcp.Required(), mcp.Description("SHA of the tree")),
|
||||||
|
mcp.WithBoolean("recursive", mcp.Description("show all items recursively")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoNoteTool = mcp.NewTool(
|
||||||
|
GetRepoNoteToolName,
|
||||||
|
mcp.WithDescription("Get a git note for a commit"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("sha", mcp.Required(), mcp.Description("commit SHA")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CompareCommitsTool = mcp.NewTool(
|
||||||
|
CompareCommitsToolName,
|
||||||
|
mcp.WithDescription("Compare two commits in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("base", mcp.Required(), mcp.Description("base commit SHA or branch")),
|
||||||
|
mcp.WithString("head", mcp.Required(), mcp.Description("head commit SHA or branch")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoRefTool, Handler: GetRepoRefFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListAllGitRefsTool, Handler: ListAllGitRefsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetTreeTool, Handler: GetTreeFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoNoteTool, Handler: GetRepoNoteFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CompareCommitsTool, Handler: CompareCommitsFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoRefFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoRefFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
ref, _ := req.GetArguments()["ref"].(string)
|
||||||
|
if owner == "" || repo == "" || ref == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and ref are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
reference, _, err := client.GetRepoRef(owner, repo, ref)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get repo ref err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(reference)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListAllGitRefsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListAllGitRefsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
refs, _, err := client.ListAllGitRefs(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list git refs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(refs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTreeFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetTreeFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
sha, _ := req.GetArguments()["sha"].(string)
|
||||||
|
if owner == "" || repo == "" || sha == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and sha are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.ListTreeOptions{
|
||||||
|
Ref: sha,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["recursive"].(bool); ok && v {
|
||||||
|
opt.Recursive = true
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tree, _, err := client.GetTrees(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get tree err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(tree)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoNoteFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoNoteFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
sha, _ := req.GetArguments()["sha"].(string)
|
||||||
|
if owner == "" || repo == "" || sha == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and sha are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
note, _, err := client.GetRepoNote(owner, repo, sha, gitea_sdk.GetRepoNoteOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get repo note err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(note)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CompareCommitsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CompareCommitsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
base, _ := req.GetArguments()["base"].(string)
|
||||||
|
head, _ := req.GetArguments()["head"].(string)
|
||||||
|
if owner == "" || repo == "" || base == "" || head == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, base, and head are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
compare, _, err := client.CompareCommits(owner, repo, base, head)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("compare commits err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(compare)
|
||||||
|
}
|
||||||
227
operation/repo/hook.go
Normal file
227
operation/repo/hook.go
Normal file
@@ -0,0 +1,227 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListRepoHooksToolName = "list_repo_hooks"
|
||||||
|
GetRepoHookToolName = "get_repo_hook"
|
||||||
|
CreateRepoHookToolName = "create_repo_hook"
|
||||||
|
EditRepoHookToolName = "edit_repo_hook"
|
||||||
|
DeleteRepoHookToolName = "delete_repo_hook"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListRepoHooksTool = mcp.NewTool(
|
||||||
|
ListRepoHooksToolName,
|
||||||
|
mcp.WithDescription("List a repository's webhooks"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoHookTool = mcp.NewTool(
|
||||||
|
GetRepoHookToolName,
|
||||||
|
mcp.WithDescription("Get a repository webhook by ID"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateRepoHookTool = mcp.NewTool(
|
||||||
|
CreateRepoHookToolName,
|
||||||
|
mcp.WithDescription("Create a webhook for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("type", mcp.Required(), mcp.Description("hook type: gitea, gogs, slack, discord, dingtalk, telegram, msteams, feishu, wechatwork, packagist")),
|
||||||
|
mcp.WithString("url", mcp.Required(), mcp.Description("target URL for the webhook")),
|
||||||
|
mcp.WithString("content_type", mcp.Description("content type: json or form (default: json)")),
|
||||||
|
mcp.WithString("secret", mcp.Description("webhook secret")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("whether the webhook is active (default: true)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditRepoHookTool = mcp.NewTool(
|
||||||
|
EditRepoHookToolName,
|
||||||
|
mcp.WithDescription("Edit a repository webhook"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
mcp.WithString("url", mcp.Description("target URL")),
|
||||||
|
mcp.WithString("content_type", mcp.Description("content type: json or form")),
|
||||||
|
mcp.WithString("secret", mcp.Description("webhook secret")),
|
||||||
|
mcp.WithBoolean("active", mcp.Description("whether the webhook is active")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteRepoHookTool = mcp.NewTool(
|
||||||
|
DeleteRepoHookToolName,
|
||||||
|
mcp.WithDescription("Delete a repository webhook"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("webhook ID")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoHooksTool, Handler: ListRepoHooksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoHookTool, Handler: GetRepoHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateRepoHookTool, Handler: CreateRepoHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: EditRepoHookTool, Handler: EditRepoHookFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteRepoHookTool, Handler: DeleteRepoHookFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoHooksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoHooksFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hooks, _, err := client.ListRepoHooks(owner, repo, gitea_sdk.ListHooksOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list repo hooks err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoHookFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.GetRepoHook(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get repo hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateRepoHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateRepoHookFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
hookType, _ := req.GetArguments()["type"].(string)
|
||||||
|
url, _ := req.GetArguments()["url"].(string)
|
||||||
|
if owner == "" || repo == "" || hookType == "" || url == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, type, and url are required"))
|
||||||
|
}
|
||||||
|
contentType := "json"
|
||||||
|
if v, ok := req.GetArguments()["content_type"].(string); ok {
|
||||||
|
contentType = v
|
||||||
|
}
|
||||||
|
config := map[string]string{
|
||||||
|
"url": url,
|
||||||
|
"content_type": contentType,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["secret"].(string); ok {
|
||||||
|
config["secret"] = v
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateHookOption{
|
||||||
|
Type: gitea_sdk.HookType(hookType),
|
||||||
|
Config: config,
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
hook, _, err := client.CreateRepoHook(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create repo hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(hook)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditRepoHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditRepoHookFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditHookOption{}
|
||||||
|
config := map[string]string{}
|
||||||
|
if v, ok := req.GetArguments()["url"].(string); ok {
|
||||||
|
config["url"] = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["content_type"].(string); ok {
|
||||||
|
config["content_type"] = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["secret"].(string); ok {
|
||||||
|
config["secret"] = v
|
||||||
|
}
|
||||||
|
if len(config) > 0 {
|
||||||
|
opt.Config = config
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["active"].(bool); ok {
|
||||||
|
opt.Active = &v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.EditRepoHook(owner, repo, id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit repo hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "updated"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteRepoHookFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteRepoHookFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteRepoHook(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete repo hook err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
156
operation/repo/mirror.go
Normal file
156
operation/repo/mirror.go
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CreatePushMirrorToolName = "create_push_mirror"
|
||||||
|
ListPushMirrorsToolName = "list_push_mirrors"
|
||||||
|
GetPushMirrorByRemoteNameToolName = "get_push_mirror"
|
||||||
|
DeletePushMirrorToolName = "delete_push_mirror"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
CreatePushMirrorTool = mcp.NewTool(
|
||||||
|
CreatePushMirrorToolName,
|
||||||
|
mcp.WithDescription("Create a push mirror for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("remote_address", mcp.Required(), mcp.Description("remote git URL to push to")),
|
||||||
|
mcp.WithString("remote_username", mcp.Description("remote username for authentication")),
|
||||||
|
mcp.WithString("remote_password", mcp.Description("remote password/token for authentication")),
|
||||||
|
mcp.WithString("interval", mcp.Description("sync interval (e.g., 8h0m0s)")),
|
||||||
|
mcp.WithBoolean("sync_on_commit", mcp.Description("sync on commit")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListPushMirrorsTool = mcp.NewTool(
|
||||||
|
ListPushMirrorsToolName,
|
||||||
|
mcp.WithDescription("List push mirrors for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetPushMirrorByRemoteNameTool = mcp.NewTool(
|
||||||
|
GetPushMirrorByRemoteNameToolName,
|
||||||
|
mcp.WithDescription("Get a push mirror by remote name"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("remote_name", mcp.Required(), mcp.Description("remote name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeletePushMirrorTool = mcp.NewTool(
|
||||||
|
DeletePushMirrorToolName,
|
||||||
|
mcp.WithDescription("Delete a push mirror by remote name"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("remote_name", mcp.Required(), mcp.Description("remote name to delete")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListPushMirrorsTool, Handler: ListPushMirrorsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetPushMirrorByRemoteNameTool, Handler: GetPushMirrorByRemoteNameFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreatePushMirrorTool, Handler: CreatePushMirrorFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeletePushMirrorTool, Handler: DeletePushMirrorFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreatePushMirrorFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreatePushMirrorFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
remoteAddr, _ := req.GetArguments()["remote_address"].(string)
|
||||||
|
if owner == "" || repo == "" || remoteAddr == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and remote_address are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreatePushMirrorOption{
|
||||||
|
RemoteAddress: remoteAddr,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["remote_username"].(string); ok {
|
||||||
|
opt.RemoteUsername = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["remote_password"].(string); ok {
|
||||||
|
opt.RemotePassword = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["interval"].(string); ok {
|
||||||
|
opt.Interval = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["sync_on_commit"].(bool); ok {
|
||||||
|
opt.SyncONCommit = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
mirror, _, err := client.PushMirrors(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create push mirror err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(mirror)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListPushMirrorsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListPushMirrorsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
mirrors, _, err := client.ListPushMirrors(owner, repo, gitea_sdk.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list push mirrors err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(mirrors)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPushMirrorByRemoteNameFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetPushMirrorByRemoteNameFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
remoteName, _ := req.GetArguments()["remote_name"].(string)
|
||||||
|
if owner == "" || repo == "" || remoteName == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and remote_name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
mirror, _, err := client.GetPushMirrorByRemoteName(owner, repo, remoteName)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get push mirror err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(mirror)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePushMirrorFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeletePushMirrorFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
remoteName, _ := req.GetArguments()["remote_name"].(string)
|
||||||
|
if owner == "" || repo == "" || remoteName == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and remote_name are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeletePushMirror(owner, repo, remoteName)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete push mirror err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
287
operation/repo/release.go
Normal file
287
operation/repo/release.go
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CreateReleaseToolName = "create_release"
|
||||||
|
DeleteReleaseToolName = "delete_release"
|
||||||
|
GetReleaseToolName = "get_release"
|
||||||
|
GetLatestReleaseToolName = "get_latest_release"
|
||||||
|
ListReleasesToolName = "list_releases"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
CreateReleaseTool = mcp.NewTool(
|
||||||
|
CreateReleaseToolName,
|
||||||
|
mcp.WithDescription("Create release"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")),
|
||||||
|
mcp.WithString("target", mcp.Required(), mcp.Description("target commitish")),
|
||||||
|
mcp.WithString("title", mcp.Required(), mcp.Description("release title")),
|
||||||
|
mcp.WithBoolean("is_draft", mcp.Description("Whether the release is draft"), mcp.DefaultBool(false)),
|
||||||
|
mcp.WithBoolean("is_pre_release", mcp.Description("Whether the release is pre-release"), mcp.DefaultBool(false)),
|
||||||
|
mcp.WithString("body", mcp.Description("release body")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteReleaseTool = mcp.NewTool(
|
||||||
|
DeleteReleaseToolName,
|
||||||
|
mcp.WithDescription("Delete release"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("release id")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetReleaseTool = mcp.NewTool(
|
||||||
|
GetReleaseToolName,
|
||||||
|
mcp.WithDescription("Get release"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("release id")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetLatestReleaseTool = mcp.NewTool(
|
||||||
|
GetLatestReleaseToolName,
|
||||||
|
mcp.WithDescription("Get latest release"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListReleasesTool = mcp.NewTool(
|
||||||
|
ListReleasesToolName,
|
||||||
|
mcp.WithDescription("List releases"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithBoolean("is_draft", mcp.Description("Whether the release is draft"), mcp.DefaultBool(false)),
|
||||||
|
mcp.WithBoolean("is_pre_release", mcp.Description("Whether the release is pre-release"), mcp.DefaultBool(false)),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(20), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateReleaseTool,
|
||||||
|
Handler: CreateReleaseFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteReleaseTool,
|
||||||
|
Handler: DeleteReleaseFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetReleaseTool,
|
||||||
|
Handler: GetReleaseFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetLatestReleaseTool,
|
||||||
|
Handler: GetLatestReleaseFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListReleasesTool,
|
||||||
|
Handler: ListReleasesFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// To avoid return too many tokens, we need to provide at least information as possible
|
||||||
|
// llm can call get release to get more information
|
||||||
|
type ListReleaseResult struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
Target string `json:"target_commitish"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
IsDraft bool `json:"draft"`
|
||||||
|
IsPrerelease bool `json:"prerelease"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
PublishedAt time.Time `json:"published_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateReleaseFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateReleasesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
tagName, ok := req.GetArguments()["tag_name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("tag_name is required")
|
||||||
|
}
|
||||||
|
target, ok := req.GetArguments()["target"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("target is required")
|
||||||
|
}
|
||||||
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("title is required")
|
||||||
|
}
|
||||||
|
isDraft, _ := req.GetArguments()["is_draft"].(bool)
|
||||||
|
isPreRelease, _ := req.GetArguments()["is_pre_release"].(bool)
|
||||||
|
body, _ := req.GetArguments()["body"].(string)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.CreateRelease(owner, repo, gitea_sdk.CreateReleaseOption{
|
||||||
|
TagName: tagName,
|
||||||
|
Target: target,
|
||||||
|
Title: title,
|
||||||
|
Note: body,
|
||||||
|
IsDraft: isDraft,
|
||||||
|
IsPrerelease: isPreRelease,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create release error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mcp.NewToolResultText("Release Created"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteReleaseFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteReleaseFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteRelease(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("delete release error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult("Release deleted successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetReleaseFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetReleaseFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
release, _, err := client.GetRelease(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get release error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(release)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLatestReleaseFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetLatestReleaseFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
release, _, err := client.GetLatestRelease(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get latest release error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(release)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListReleasesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListReleasesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
var pIsDraft *bool
|
||||||
|
isDraft, ok := req.GetArguments()["is_draft"].(bool)
|
||||||
|
if ok {
|
||||||
|
pIsDraft = new(isDraft)
|
||||||
|
}
|
||||||
|
var pIsPreRelease *bool
|
||||||
|
isPreRelease, ok := req.GetArguments()["is_pre_release"].(bool)
|
||||||
|
if ok {
|
||||||
|
pIsPreRelease = new(isPreRelease)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 20)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
releases, _, err := client.ListReleases(owner, repo, gitea_sdk.ListReleasesOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
IsDraft: pIsDraft,
|
||||||
|
IsPreRelease: pIsPreRelease,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list releases error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]ListReleaseResult, len(releases))
|
||||||
|
for _, release := range releases {
|
||||||
|
results = append(results, ListReleaseResult{
|
||||||
|
ID: release.ID,
|
||||||
|
TagName: release.TagName,
|
||||||
|
Target: release.Target,
|
||||||
|
Title: release.Title,
|
||||||
|
IsDraft: release.IsDraft,
|
||||||
|
IsPrerelease: release.IsPrerelease,
|
||||||
|
CreatedAt: release.CreatedAt,
|
||||||
|
PublishedAt: release.PublishedAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return to.TextResult(results)
|
||||||
|
}
|
||||||
@@ -5,16 +5,19 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/ptr"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CreateRepoToolName = "create_repo"
|
CreateRepoToolName = "create_repo"
|
||||||
ForkRepoToolName = "fork_repo"
|
ForkRepoToolName = "fork_repo"
|
||||||
@@ -24,7 +27,7 @@ const (
|
|||||||
var (
|
var (
|
||||||
CreateRepoTool = mcp.NewTool(
|
CreateRepoTool = mcp.NewTool(
|
||||||
CreateRepoToolName,
|
CreateRepoToolName,
|
||||||
mcp.WithDescription("Create repository"),
|
mcp.WithDescription("Create repository in personal account or organization"),
|
||||||
mcp.WithString("name", mcp.Required(), mcp.Description("Name of the repository to create")),
|
mcp.WithString("name", mcp.Required(), mcp.Description("Name of the repository to create")),
|
||||||
mcp.WithString("description", mcp.Description("Description of the repository to create")),
|
mcp.WithString("description", mcp.Description("Description of the repository to create")),
|
||||||
mcp.WithBoolean("private", mcp.Description("Whether the repository is private")),
|
mcp.WithBoolean("private", mcp.Description("Whether the repository is private")),
|
||||||
@@ -35,6 +38,7 @@ var (
|
|||||||
mcp.WithString("license", mcp.Description("License to use")),
|
mcp.WithString("license", mcp.Description("License to use")),
|
||||||
mcp.WithString("readme", mcp.Description("Readme of the repository to create")),
|
mcp.WithString("readme", mcp.Description("Readme of the repository to create")),
|
||||||
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)")),
|
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)")),
|
||||||
|
mcp.WithString("organization", mcp.Description("Organization name to create repository in (optional - defaults to personal account)")),
|
||||||
)
|
)
|
||||||
|
|
||||||
ForkRepoTool = mcp.NewTool(
|
ForkRepoTool = mcp.NewTool(
|
||||||
@@ -54,6 +58,21 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateRepoTool,
|
||||||
|
Handler: CreateRepoFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: ForkRepoTool,
|
||||||
|
Handler: ForkRepoFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListMyReposTool,
|
||||||
|
Handler: ListMyReposFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
func RegisterTool(s *server.MCPServer) {
|
||||||
s.AddTool(CreateRepoTool, CreateRepoFn)
|
s.AddTool(CreateRepoTool, CreateRepoFn)
|
||||||
s.AddTool(ForkRepoTool, ForkRepoFn)
|
s.AddTool(ForkRepoTool, ForkRepoFn)
|
||||||
@@ -70,25 +89,39 @@ func RegisterTool(s *server.MCPServer) {
|
|||||||
s.AddTool(DeleteBranchTool, DeleteBranchFn)
|
s.AddTool(DeleteBranchTool, DeleteBranchFn)
|
||||||
s.AddTool(ListBranchesTool, ListBranchesFn)
|
s.AddTool(ListBranchesTool, ListBranchesFn)
|
||||||
|
|
||||||
|
// Release
|
||||||
|
s.AddTool(CreateReleaseTool, CreateReleaseFn)
|
||||||
|
s.AddTool(DeleteReleaseTool, DeleteReleaseFn)
|
||||||
|
s.AddTool(GetReleaseTool, GetReleaseFn)
|
||||||
|
s.AddTool(GetLatestReleaseTool, GetLatestReleaseFn)
|
||||||
|
s.AddTool(ListReleasesTool, ListReleasesFn)
|
||||||
|
|
||||||
|
// Tag
|
||||||
|
s.AddTool(CreateTagTool, CreateTagFn)
|
||||||
|
s.AddTool(DeleteTagTool, DeleteTagFn)
|
||||||
|
s.AddTool(GetTagTool, GetTagFn)
|
||||||
|
s.AddTool(ListTagsTool, ListTagsFn)
|
||||||
|
|
||||||
// Commit
|
// Commit
|
||||||
s.AddTool(ListRepoCommitsTool, ListRepoCommitsFn)
|
s.AddTool(ListRepoCommitsTool, ListRepoCommitsFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called CreateRepoFn")
|
log.Debugf("Called CreateRepoFn")
|
||||||
name, ok := req.Params.Arguments["name"].(string)
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(errors.New("repository name is required"))
|
return to.ErrorResult(errors.New("repository name is required"))
|
||||||
}
|
}
|
||||||
description, _ := req.Params.Arguments["description"].(string)
|
description, _ := req.GetArguments()["description"].(string)
|
||||||
private, _ := req.Params.Arguments["private"].(bool)
|
private, _ := req.GetArguments()["private"].(bool)
|
||||||
issueLabels, _ := req.Params.Arguments["issue_labels"].(string)
|
issueLabels, _ := req.GetArguments()["issue_labels"].(string)
|
||||||
autoInit, _ := req.Params.Arguments["auto_init"].(bool)
|
autoInit, _ := req.GetArguments()["auto_init"].(bool)
|
||||||
template, _ := req.Params.Arguments["template"].(bool)
|
template, _ := req.GetArguments()["template"].(bool)
|
||||||
gitignores, _ := req.Params.Arguments["gitignores"].(string)
|
gitignores, _ := req.GetArguments()["gitignores"].(string)
|
||||||
license, _ := req.Params.Arguments["license"].(string)
|
license, _ := req.GetArguments()["license"].(string)
|
||||||
readme, _ := req.Params.Arguments["readme"].(string)
|
readme, _ := req.GetArguments()["readme"].(string)
|
||||||
defaultBranch, _ := req.Params.Arguments["default_branch"].(string)
|
defaultBranch, _ := req.GetArguments()["default_branch"].(string)
|
||||||
|
organization, _ := req.GetArguments()["organization"].(string)
|
||||||
|
|
||||||
opt := gitea_sdk.CreateRepoOption{
|
opt := gitea_sdk.CreateRepoOption{
|
||||||
Name: name,
|
Name: name,
|
||||||
@@ -102,30 +135,43 @@ func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
Readme: readme,
|
Readme: readme,
|
||||||
DefaultBranch: defaultBranch,
|
DefaultBranch: defaultBranch,
|
||||||
}
|
}
|
||||||
repo, _, err := gitea.Client().CreateRepo(opt)
|
|
||||||
|
var repo *gitea_sdk.Repository
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("create repo err: %v", err))
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
if organization != "" {
|
||||||
|
repo, _, err = client.CreateOrgRepo(organization, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create organization repository '%s' in '%s' err: %v", name, organization, err))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
repo, _, err = client.CreateRepo(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create repository '%s' err: %v", name, err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return to.TextResult(repo)
|
return to.TextResult(repo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ForkRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ForkRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called ForkRepoFn")
|
log.Debugf("Called ForkRepoFn")
|
||||||
user, ok := req.Params.Arguments["user"].(string)
|
user, ok := req.GetArguments()["user"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(errors.New("user name is required"))
|
return to.ErrorResult(errors.New("user name is required"))
|
||||||
}
|
}
|
||||||
repo, ok := req.Params.Arguments["repo"].(string)
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(errors.New("repository name is required"))
|
return to.ErrorResult(errors.New("repository name is required"))
|
||||||
}
|
}
|
||||||
organization, ok := req.Params.Arguments["organization"].(string)
|
organization, ok := req.GetArguments()["organization"].(string)
|
||||||
organizationPtr := ptr.To(organization)
|
organizationPtr := new(organization)
|
||||||
if !ok || organization == "" {
|
if !ok || organization == "" {
|
||||||
organizationPtr = nil
|
organizationPtr = nil
|
||||||
}
|
}
|
||||||
name, ok := req.Params.Arguments["name"].(string)
|
name, ok := req.GetArguments()["name"].(string)
|
||||||
namePtr := ptr.To(name)
|
namePtr := new(name)
|
||||||
if !ok || name == "" {
|
if !ok || name == "" {
|
||||||
namePtr = nil
|
namePtr = nil
|
||||||
}
|
}
|
||||||
@@ -133,30 +179,32 @@ func ForkRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResu
|
|||||||
Organization: organizationPtr,
|
Organization: organizationPtr,
|
||||||
Name: namePtr,
|
Name: namePtr,
|
||||||
}
|
}
|
||||||
_, _, err := gitea.Client().CreateFork(user, repo, opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("fork repository error %v", err))
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.CreateFork(user, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("fork repository error: %v", err))
|
||||||
}
|
}
|
||||||
return to.TextResult("Fork success")
|
return to.TextResult("Fork success")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListMyReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ListMyReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called ListMyReposFn")
|
log.Debugf("Called ListMyReposFn")
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
if !ok {
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, ok := req.Params.Arguments["pageSize"].(float64)
|
|
||||||
if !ok {
|
|
||||||
pageSize = 100
|
|
||||||
}
|
|
||||||
opt := gitea_sdk.ListReposOptions{
|
opt := gitea_sdk.ListReposOptions{
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
Page: int(page),
|
Page: int(page),
|
||||||
PageSize: int(pageSize),
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
repos, _, err := gitea.Client().ListMyRepos(opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.ListMyRepos(opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("list my repositories error: %v", err))
|
return to.ErrorResult(fmt.Errorf("list my repositories error: %v", err))
|
||||||
}
|
}
|
||||||
|
|||||||
216
operation/repo/star_watch.go
Normal file
216
operation/repo/star_watch.go
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListStargazersToolName = "list_stargazers"
|
||||||
|
ListForksToolName = "list_forks"
|
||||||
|
GetWatchedReposToolName = "get_watched_repos"
|
||||||
|
GetMyWatchedReposToolName = "get_my_watched_repos"
|
||||||
|
CheckRepoWatchToolName = "check_repo_watch"
|
||||||
|
WatchRepoToolName = "watch_repo"
|
||||||
|
UnWatchRepoToolName = "unwatch_repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListStargazersTool = mcp.NewTool(
|
||||||
|
ListStargazersToolName,
|
||||||
|
mcp.WithDescription("List a repository's stargazers"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListForksTool = mcp.NewTool(
|
||||||
|
ListForksToolName,
|
||||||
|
mcp.WithDescription("List a repository's forks"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetWatchedReposTool = mcp.NewTool(
|
||||||
|
GetWatchedReposToolName,
|
||||||
|
mcp.WithDescription("List repositories watched by a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetMyWatchedReposTool = mcp.NewTool(
|
||||||
|
GetMyWatchedReposToolName,
|
||||||
|
mcp.WithDescription("List repositories watched by the authenticated user"),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckRepoWatchTool = mcp.NewTool(
|
||||||
|
CheckRepoWatchToolName,
|
||||||
|
mcp.WithDescription("Check if the authenticated user is watching a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
WatchRepoTool = mcp.NewTool(
|
||||||
|
WatchRepoToolName,
|
||||||
|
mcp.WithDescription("Watch a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnWatchRepoTool = mcp.NewTool(
|
||||||
|
UnWatchRepoToolName,
|
||||||
|
mcp.WithDescription("Unwatch a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListStargazersTool, Handler: ListStargazersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListForksTool, Handler: ListForksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetWatchedReposTool, Handler: GetWatchedReposFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetMyWatchedReposTool, Handler: GetMyWatchedReposFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckRepoWatchTool, Handler: CheckRepoWatchFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: WatchRepoTool, Handler: WatchRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnWatchRepoTool, Handler: UnWatchRepoFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListStargazersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListStargazersFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.ListRepoStargazers(owner, repo, gitea_sdk.ListStargazersOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list stargazers err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListForksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListForksFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.ListForks(owner, repo, gitea_sdk.ListForksOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list forks err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWatchedReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetWatchedReposFn")
|
||||||
|
username, _ := req.GetArguments()["username"].(string)
|
||||||
|
if username == "" {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.GetWatchedRepos(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get watched repos err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMyWatchedReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetMyWatchedReposFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.GetMyWatchedRepos()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get my watched repos err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckRepoWatchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckRepoWatchFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
watching, _, err := client.CheckRepoWatch(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check repo watch err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"watching": watching})
|
||||||
|
}
|
||||||
|
|
||||||
|
func WatchRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called WatchRepoFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.WatchRepo(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("watch repo err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "watching"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnWatchRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnWatchRepoFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.UnWatchRepo(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unwatch repo err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unwatched"})
|
||||||
|
}
|
||||||
134
operation/repo/status.go
Normal file
134
operation/repo/status.go
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CreateStatusToolName = "create_commit_status"
|
||||||
|
ListStatusesToolName = "list_commit_statuses"
|
||||||
|
GetCombinedStatusToolName = "get_combined_status"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
CreateStatusTool = mcp.NewTool(
|
||||||
|
CreateStatusToolName,
|
||||||
|
mcp.WithDescription("Create a commit status"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("sha", mcp.Required(), mcp.Description("commit SHA")),
|
||||||
|
mcp.WithString("state", mcp.Required(), mcp.Description("status state: pending, success, error, failure, warning")),
|
||||||
|
mcp.WithString("target_url", mcp.Description("URL for status details")),
|
||||||
|
mcp.WithString("description", mcp.Description("status description")),
|
||||||
|
mcp.WithString("context", mcp.Description("status context (e.g., ci/build)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListStatusesTool = mcp.NewTool(
|
||||||
|
ListStatusesToolName,
|
||||||
|
mcp.WithDescription("List commit statuses for a ref"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("ref", mcp.Required(), mcp.Description("commit SHA, branch, or tag")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetCombinedStatusTool = mcp.NewTool(
|
||||||
|
GetCombinedStatusToolName,
|
||||||
|
mcp.WithDescription("Get the combined status for a ref"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("ref", mcp.Required(), mcp.Description("commit SHA, branch, or tag")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListStatusesTool, Handler: ListStatusesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetCombinedStatusTool, Handler: GetCombinedStatusFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateStatusTool, Handler: CreateStatusFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateStatusFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateStatusFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
sha, _ := req.GetArguments()["sha"].(string)
|
||||||
|
state, _ := req.GetArguments()["state"].(string)
|
||||||
|
if owner == "" || repo == "" || sha == "" || state == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, sha, and state are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateStatusOption{
|
||||||
|
State: gitea_sdk.StatusState(state),
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["target_url"].(string); ok {
|
||||||
|
opt.TargetURL = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["context"].(string); ok {
|
||||||
|
opt.Context = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
status, _, err := client.CreateStatus(owner, repo, sha, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create status err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListStatusesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListStatusesFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
ref, _ := req.GetArguments()["ref"].(string)
|
||||||
|
if owner == "" || repo == "" || ref == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and ref are required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
statuses, _, err := client.ListStatuses(owner, repo, ref, gitea_sdk.ListStatusesOption{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list statuses err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(statuses)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCombinedStatusFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetCombinedStatusFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
ref, _ := req.GetArguments()["ref"].(string)
|
||||||
|
if owner == "" || repo == "" || ref == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and ref are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
combined, _, err := client.GetCombinedStatus(owner, repo, ref)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get combined status err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(combined)
|
||||||
|
}
|
||||||
213
operation/repo/tag.go
Normal file
213
operation/repo/tag.go
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
CreateTagToolName = "create_tag"
|
||||||
|
DeleteTagToolName = "delete_tag"
|
||||||
|
GetTagToolName = "get_tag"
|
||||||
|
ListTagsToolName = "list_tags"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
CreateTagTool = mcp.NewTool(
|
||||||
|
CreateTagToolName,
|
||||||
|
mcp.WithDescription("Create tag"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")),
|
||||||
|
mcp.WithString("target", mcp.Description("target commitish"), mcp.DefaultString("")),
|
||||||
|
mcp.WithString("message", mcp.Description("tag message"), mcp.DefaultString("")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteTagTool = mcp.NewTool(
|
||||||
|
DeleteTagToolName,
|
||||||
|
mcp.WithDescription("Delete tag"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetTagTool = mcp.NewTool(
|
||||||
|
GetTagToolName,
|
||||||
|
mcp.WithDescription("Get tag"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListTagsTool = mcp.NewTool(
|
||||||
|
ListTagsToolName,
|
||||||
|
mcp.WithDescription("List tags"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(20), mcp.Min(1)),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateTagTool,
|
||||||
|
Handler: CreateTagFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteTagTool,
|
||||||
|
Handler: DeleteTagFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetTagTool,
|
||||||
|
Handler: GetTagFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListTagsTool,
|
||||||
|
Handler: ListTagsFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// To avoid return too many tokens, we need to provide at least information as possible
|
||||||
|
// llm can call get tag to get more information
|
||||||
|
type ListTagResult struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Commit *gitea_sdk.CommitMeta `json:"commit"`
|
||||||
|
// message may be a long text, so we should not provide it here
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateTagFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateTagFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
tagName, ok := req.GetArguments()["tag_name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("tag_name is required")
|
||||||
|
}
|
||||||
|
target, _ := req.GetArguments()["target"].(string)
|
||||||
|
message, _ := req.GetArguments()["message"].(string)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, _, err = client.CreateTag(owner, repo, gitea_sdk.CreateTagOption{
|
||||||
|
TagName: tagName,
|
||||||
|
Target: target,
|
||||||
|
Message: message,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create tag error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mcp.NewToolResultText("Tag Created"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteTagFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteTagFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
tagName, ok := req.GetArguments()["tag_name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("tag_name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteTag(owner, repo, tagName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("delete tag error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult("Tag deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTagFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetTagFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
tagName, ok := req.GetArguments()["tag_name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("tag_name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tag, _, err := client.GetTag(owner, repo, tagName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get tag error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListTagsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListTagsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("owner is required")
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("repo is required")
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 20)
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tags, _, err := client.ListRepoTags(owner, repo, gitea_sdk.ListRepoTagsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list tags error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := make([]ListTagResult, 0, len(tags))
|
||||||
|
for _, tag := range tags {
|
||||||
|
results = append(results, ListTagResult{
|
||||||
|
ID: tag.ID,
|
||||||
|
Name: tag.Name,
|
||||||
|
Commit: tag.Commit,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return to.TextResult(results)
|
||||||
|
}
|
||||||
200
operation/repo/tag_protection.go
Normal file
200
operation/repo/tag_protection.go
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListTagProtectionsToolName = "list_tag_protections"
|
||||||
|
GetTagProtectionToolName = "get_tag_protection"
|
||||||
|
CreateTagProtectionToolName = "create_tag_protection"
|
||||||
|
EditTagProtectionToolName = "edit_tag_protection"
|
||||||
|
DeleteTagProtectionToolName = "delete_tag_protection"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListTagProtectionsTool = mcp.NewTool(
|
||||||
|
ListTagProtectionsToolName,
|
||||||
|
mcp.WithDescription("List tag protections for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetTagProtectionTool = mcp.NewTool(
|
||||||
|
GetTagProtectionToolName,
|
||||||
|
mcp.WithDescription("Get a tag protection rule by ID"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("tag protection ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateTagProtectionTool = mcp.NewTool(
|
||||||
|
CreateTagProtectionToolName,
|
||||||
|
mcp.WithDescription("Create a tag protection rule"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("name_pattern", mcp.Required(), mcp.Description("glob pattern for tag name matching")),
|
||||||
|
mcp.WithString("whitelist_usernames", mcp.Description("comma-separated list of allowed usernames")),
|
||||||
|
mcp.WithString("whitelist_teams", mcp.Description("comma-separated list of allowed team names")),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditTagProtectionTool = mcp.NewTool(
|
||||||
|
EditTagProtectionToolName,
|
||||||
|
mcp.WithDescription("Edit a tag protection rule"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("tag protection ID")),
|
||||||
|
mcp.WithString("name_pattern", mcp.Description("glob pattern for tag name matching")),
|
||||||
|
mcp.WithString("whitelist_usernames", mcp.Description("comma-separated list of allowed usernames")),
|
||||||
|
mcp.WithString("whitelist_teams", mcp.Description("comma-separated list of allowed team names")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteTagProtectionTool = mcp.NewTool(
|
||||||
|
DeleteTagProtectionToolName,
|
||||||
|
mcp.WithDescription("Delete a tag protection rule"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("tag protection ID")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListTagProtectionsTool, Handler: ListTagProtectionsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetTagProtectionTool, Handler: GetTagProtectionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateTagProtectionTool, Handler: CreateTagProtectionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: EditTagProtectionTool, Handler: EditTagProtectionFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteTagProtectionTool, Handler: DeleteTagProtectionFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListTagProtectionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListTagProtectionsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tps, _, err := client.ListTagProtection(owner, repo, gitea_sdk.ListRepoTagProtectionsOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list tag protections err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(tps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTagProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetTagProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tp, _, err := client.GetTagProtection(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get tag protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateTagProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateTagProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
namePattern, _ := req.GetArguments()["name_pattern"].(string)
|
||||||
|
if owner == "" || repo == "" || namePattern == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and name_pattern are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateTagProtectionOption{
|
||||||
|
NamePattern: namePattern,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["whitelist_usernames"].(string); ok && v != "" {
|
||||||
|
opt.WhitelistUsernames = splitAndTrim(v)
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["whitelist_teams"].(string); ok && v != "" {
|
||||||
|
opt.WhitelistTeams = splitAndTrim(v)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tp, _, err := client.CreateTagProtection(owner, repo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create tag protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditTagProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditTagProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.EditTagProtectionOption{}
|
||||||
|
if v, ok := req.GetArguments()["name_pattern"].(string); ok {
|
||||||
|
opt.NamePattern = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["whitelist_usernames"].(string); ok {
|
||||||
|
opt.WhitelistUsernames = splitAndTrim(v)
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["whitelist_teams"].(string); ok {
|
||||||
|
opt.WhitelistTeams = splitAndTrim(v)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
tp, _, err := client.EditTagProtection(owner, repo, id, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit tag protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteTagProtectionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteTagProtectionFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteTagProtection(owner, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete tag protection err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
157
operation/repo/topic.go
Normal file
157
operation/repo/topic.go
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListRepoTopicsToolName = "list_repo_topics"
|
||||||
|
SetRepoTopicsToolName = "set_repo_topics"
|
||||||
|
AddRepoTopicToolName = "add_repo_topic"
|
||||||
|
DeleteRepoTopicToolName = "delete_repo_topic"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListRepoTopicsTool = mcp.NewTool(
|
||||||
|
ListRepoTopicsToolName,
|
||||||
|
mcp.WithDescription("List a repository's topics"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
SetRepoTopicsTool = mcp.NewTool(
|
||||||
|
SetRepoTopicsToolName,
|
||||||
|
mcp.WithDescription("Replace all topics of a repository with a new list"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("topics", mcp.Required(), mcp.Description("comma-separated list of topics")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddRepoTopicTool = mcp.NewTool(
|
||||||
|
AddRepoTopicToolName,
|
||||||
|
mcp.WithDescription("Add a topic to a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("topic", mcp.Required(), mcp.Description("topic name to add")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteRepoTopicTool = mcp.NewTool(
|
||||||
|
DeleteRepoTopicToolName,
|
||||||
|
mcp.WithDescription("Remove a topic from a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("topic", mcp.Required(), mcp.Description("topic name to remove")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoTopicsTool, Handler: ListRepoTopicsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: SetRepoTopicsTool, Handler: SetRepoTopicsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddRepoTopicTool, Handler: AddRepoTopicFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteRepoTopicTool, Handler: DeleteRepoTopicFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoTopicsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoTopicsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
topics, _, err := client.ListRepoTopics(owner, repo, gitea_sdk.ListRepoTopicsOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list repo topics err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(topics)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetRepoTopicsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called SetRepoTopicsFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
topicsStr, _ := req.GetArguments()["topics"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
var topics []string
|
||||||
|
if topicsStr != "" {
|
||||||
|
for _, t := range splitAndTrim(topicsStr) {
|
||||||
|
if t != "" {
|
||||||
|
topics = append(topics, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.SetRepoTopics(owner, repo, topics)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("set repo topics err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "updated", "topics": topics})
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddRepoTopicFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddRepoTopicFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
topic, _ := req.GetArguments()["topic"].(string)
|
||||||
|
if owner == "" || repo == "" || topic == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and topic are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.AddRepoTopic(owner, repo, topic)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add repo topic err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "added", "topic": topic})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteRepoTopicFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteRepoTopicFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
topic, _ := req.GetArguments()["topic"].(string)
|
||||||
|
if owner == "" || repo == "" || topic == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and topic are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteRepoTopic(owner, repo, topic)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete repo topic err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "deleted", "topic": topic})
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitAndTrim(s string) []string {
|
||||||
|
var result []string
|
||||||
|
for _, part := range strings.Split(s, ",") {
|
||||||
|
trimmed := strings.TrimSpace(part)
|
||||||
|
if trimmed != "" {
|
||||||
|
result = append(result, trimmed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
167
operation/repo/transfer.go
Normal file
167
operation/repo/transfer.go
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TransferRepoToolName = "transfer_repo"
|
||||||
|
AcceptRepoTransferToolName = "accept_repo_transfer"
|
||||||
|
RejectRepoTransferToolName = "reject_repo_transfer"
|
||||||
|
CreateFromTemplateToolName = "create_repo_from_template"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
TransferRepoTool = mcp.NewTool(
|
||||||
|
TransferRepoToolName,
|
||||||
|
mcp.WithDescription("Transfer a repository to a new owner"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("current repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("new_owner", mcp.Required(), mcp.Description("new owner username or org")),
|
||||||
|
)
|
||||||
|
|
||||||
|
AcceptRepoTransferTool = mcp.NewTool(
|
||||||
|
AcceptRepoTransferToolName,
|
||||||
|
mcp.WithDescription("Accept a pending repository transfer"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
RejectRepoTransferTool = mcp.NewTool(
|
||||||
|
RejectRepoTransferToolName,
|
||||||
|
mcp.WithDescription("Reject a pending repository transfer"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateFromTemplateTool = mcp.NewTool(
|
||||||
|
CreateFromTemplateToolName,
|
||||||
|
mcp.WithDescription("Create a repository from a template"),
|
||||||
|
mcp.WithString("template_owner", mcp.Required(), mcp.Description("template repository owner")),
|
||||||
|
mcp.WithString("template_repo", mcp.Required(), mcp.Description("template repository name")),
|
||||||
|
mcp.WithString("name", mcp.Required(), mcp.Description("name for the new repository")),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("owner of the new repository (user or org)")),
|
||||||
|
mcp.WithString("description", mcp.Description("description for the new repository")),
|
||||||
|
mcp.WithBoolean("private", mcp.Description("whether the new repository is private")),
|
||||||
|
mcp.WithBoolean("git_content", mcp.Description("copy git content from template (default: true)")),
|
||||||
|
mcp.WithBoolean("topics", mcp.Description("copy topics from template")),
|
||||||
|
mcp.WithBoolean("labels", mcp.Description("copy labels from template")),
|
||||||
|
mcp.WithBoolean("webhooks", mcp.Description("copy webhooks from template")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: TransferRepoTool, Handler: TransferRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AcceptRepoTransferTool, Handler: AcceptRepoTransferFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: RejectRepoTransferTool, Handler: RejectRepoTransferFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreateFromTemplateTool, Handler: CreateFromTemplateFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TransferRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called TransferRepoFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
newOwner, _ := req.GetArguments()["new_owner"].(string)
|
||||||
|
if owner == "" || repo == "" || newOwner == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner, repo, and new_owner are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repository, _, err := client.TransferRepo(owner, repo, gitea_sdk.TransferRepoOption{
|
||||||
|
NewOwner: newOwner,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("transfer repo err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repository)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AcceptRepoTransferFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AcceptRepoTransferFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repository, _, err := client.AcceptRepoTransfer(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("accept repo transfer err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repository)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RejectRepoTransferFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called RejectRepoTransferFn")
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
repo, _ := req.GetArguments()["repo"].(string)
|
||||||
|
if owner == "" || repo == "" {
|
||||||
|
return to.ErrorResult(errors.New("owner and repo are required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repository, _, err := client.RejectRepoTransfer(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("reject repo transfer err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repository)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateFromTemplateFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateFromTemplateFn")
|
||||||
|
templateOwner, _ := req.GetArguments()["template_owner"].(string)
|
||||||
|
templateRepo, _ := req.GetArguments()["template_repo"].(string)
|
||||||
|
name, _ := req.GetArguments()["name"].(string)
|
||||||
|
owner, _ := req.GetArguments()["owner"].(string)
|
||||||
|
if templateOwner == "" || templateRepo == "" || name == "" || owner == "" {
|
||||||
|
return to.ErrorResult(errors.New("template_owner, template_repo, name, and owner are required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateRepoFromTemplateOption{
|
||||||
|
Name: name,
|
||||||
|
Owner: owner,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["private"].(bool); ok {
|
||||||
|
opt.Private = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["git_content"].(bool); ok {
|
||||||
|
opt.GitContent = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["topics"].(bool); ok {
|
||||||
|
opt.Topics = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["labels"].(bool); ok {
|
||||||
|
opt.Labels = v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["webhooks"].(bool); ok {
|
||||||
|
opt.Webhooks = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repository, _, err := client.CreateRepoFromTemplate(templateOwner, templateRepo, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create from template err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repository)
|
||||||
|
}
|
||||||
@@ -2,18 +2,22 @@ package search
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/ptr"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SearchUsersToolName = "search_users"
|
SearchUsersToolName = "search_users"
|
||||||
SearchOrgTeamsToolName = "search_org_teams"
|
SearchOrgTeamsToolName = "search_org_teams"
|
||||||
@@ -24,7 +28,7 @@ var (
|
|||||||
SearchUsersTool = mcp.NewTool(
|
SearchUsersTool = mcp.NewTool(
|
||||||
SearchUsersToolName,
|
SearchUsersToolName,
|
||||||
mcp.WithDescription("search users"),
|
mcp.WithDescription("search users"),
|
||||||
mcp.WithString("keyword", mcp.Description("Keyword")),
|
mcp.WithString("keyword", mcp.Required(), mcp.Description("Keyword")),
|
||||||
mcp.WithNumber("page", mcp.Description("Page"), mcp.DefaultNumber(1)),
|
mcp.WithNumber("page", mcp.Description("Page"), mcp.DefaultNumber(1)),
|
||||||
mcp.WithNumber("pageSize", mcp.Description("PageSize"), mcp.DefaultNumber(100)),
|
mcp.WithNumber("pageSize", mcp.Description("PageSize"), mcp.DefaultNumber(100)),
|
||||||
)
|
)
|
||||||
@@ -32,8 +36,8 @@ var (
|
|||||||
SearOrgTeamsTool = mcp.NewTool(
|
SearOrgTeamsTool = mcp.NewTool(
|
||||||
SearchOrgTeamsToolName,
|
SearchOrgTeamsToolName,
|
||||||
mcp.WithDescription("search organization teams"),
|
mcp.WithDescription("search organization teams"),
|
||||||
mcp.WithString("org", mcp.Description("organization name")),
|
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||||
mcp.WithString("query", mcp.Description("search organization teams")),
|
mcp.WithString("query", mcp.Required(), mcp.Description("search organization teams")),
|
||||||
mcp.WithBoolean("includeDescription", mcp.Description("include description?")),
|
mcp.WithBoolean("includeDescription", mcp.Description("include description?")),
|
||||||
mcp.WithNumber("page", mcp.Description("Page"), mcp.DefaultNumber(1)),
|
mcp.WithNumber("page", mcp.Description("Page"), mcp.DefaultNumber(1)),
|
||||||
mcp.WithNumber("pageSize", mcp.Description("PageSize"), mcp.DefaultNumber(100)),
|
mcp.WithNumber("pageSize", mcp.Description("PageSize"), mcp.DefaultNumber(100)),
|
||||||
@@ -42,7 +46,7 @@ var (
|
|||||||
SearchReposTool = mcp.NewTool(
|
SearchReposTool = mcp.NewTool(
|
||||||
SearchReposToolName,
|
SearchReposToolName,
|
||||||
mcp.WithDescription("search repos"),
|
mcp.WithDescription("search repos"),
|
||||||
mcp.WithString("keyword", mcp.Description("Keyword")),
|
mcp.WithString("keyword", mcp.Required(), mcp.Description("Keyword")),
|
||||||
mcp.WithBoolean("keywordIsTopic", mcp.Description("KeywordIsTopic")),
|
mcp.WithBoolean("keywordIsTopic", mcp.Description("KeywordIsTopic")),
|
||||||
mcp.WithBoolean("keywordInDescription", mcp.Description("KeywordInDescription")),
|
mcp.WithBoolean("keywordInDescription", mcp.Description("KeywordInDescription")),
|
||||||
mcp.WithNumber("ownerID", mcp.Description("OwnerID")),
|
mcp.WithNumber("ownerID", mcp.Description("OwnerID")),
|
||||||
@@ -55,26 +59,29 @@ var (
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
func init() {
|
||||||
s.AddTool(SearchUsersTool, SearchUsersFn)
|
Tool.RegisterRead(server.ServerTool{
|
||||||
s.AddTool(SearOrgTeamsTool, SearchOrgTeamsFn)
|
Tool: SearchUsersTool,
|
||||||
s.AddTool(SearchReposTool, SearchReposFn)
|
Handler: UsersFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: SearOrgTeamsTool,
|
||||||
|
Handler: OrgTeamsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: SearchReposTool,
|
||||||
|
Handler: ReposFn,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchUsersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func UsersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called SearchUsersFn")
|
log.Debugf("Called UsersFn")
|
||||||
keyword, ok := req.Params.Arguments["keyword"].(string)
|
keyword, ok := req.GetArguments()["keyword"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("keyword is required"))
|
return to.ErrorResult(errors.New("keyword is required"))
|
||||||
}
|
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
|
||||||
if !ok {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, ok := req.Params.Arguments["pageSize"].(float64)
|
|
||||||
if !ok {
|
|
||||||
pageSize = 100
|
|
||||||
}
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
opt := gitea_sdk.SearchUsersOption{
|
opt := gitea_sdk.SearchUsersOption{
|
||||||
KeyWord: keyword,
|
KeyWord: keyword,
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
@@ -82,32 +89,30 @@ func SearchUsersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
|
|||||||
PageSize: int(pageSize),
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
users, _, err := gitea.Client().SearchUsers(opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.SearchUsers(opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("search users err: %v", err))
|
return to.ErrorResult(fmt.Errorf("search users err: %v", err))
|
||||||
}
|
}
|
||||||
return to.TextResult(users)
|
return to.TextResult(users)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchOrgTeamsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func OrgTeamsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called SearchOrgTeamsFn")
|
log.Debugf("Called OrgTeamsFn")
|
||||||
org, ok := req.Params.Arguments["org"].(string)
|
org, ok := req.GetArguments()["org"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("organization is required"))
|
return to.ErrorResult(errors.New("organization is required"))
|
||||||
}
|
}
|
||||||
query, ok := req.Params.Arguments["query"].(string)
|
query, ok := req.GetArguments()["query"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("query is required"))
|
return to.ErrorResult(errors.New("query is required"))
|
||||||
}
|
|
||||||
includeDescription, _ := req.Params.Arguments["includeDescription"].(bool)
|
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
|
||||||
if !ok {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, ok := req.Params.Arguments["pageSize"].(float64)
|
|
||||||
if !ok {
|
|
||||||
pageSize = 100
|
|
||||||
}
|
}
|
||||||
|
includeDescription, _ := req.GetArguments()["includeDescription"].(bool)
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
opt := gitea_sdk.SearchTeamsOptions{
|
opt := gitea_sdk.SearchTeamsOptions{
|
||||||
Query: query,
|
Query: query,
|
||||||
IncludeDescription: includeDescription,
|
IncludeDescription: includeDescription,
|
||||||
@@ -116,41 +121,47 @@ func SearchOrgTeamsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallTo
|
|||||||
PageSize: int(pageSize),
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
teams, _, err := gitea.Client().SearchOrgTeams(org, &opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
teams, _, err := client.SearchOrgTeams(org, &opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("search organization teams error: %v", err))
|
return to.ErrorResult(fmt.Errorf("search organization teams error: %v", err))
|
||||||
}
|
}
|
||||||
return to.TextResult(teams)
|
return to.TextResult(teams)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func ReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called SearchReposFn")
|
log.Debugf("Called ReposFn")
|
||||||
keyword, ok := req.Params.Arguments["keyword"].(string)
|
keyword, ok := req.GetArguments()["keyword"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return to.ErrorResult(fmt.Errorf("keyword is required"))
|
return to.ErrorResult(errors.New("keyword is required"))
|
||||||
}
|
}
|
||||||
keywordIsTopic, _ := req.Params.Arguments["keywordIsTopic"].(bool)
|
keywordIsTopic, _ := req.GetArguments()["keywordIsTopic"].(bool)
|
||||||
keywordInDescription, _ := req.Params.Arguments["keywordInDescription"].(bool)
|
keywordInDescription, _ := req.GetArguments()["keywordInDescription"].(bool)
|
||||||
ownerID, _ := req.Params.Arguments["ownerID"].(float64)
|
ownerID := params.GetOptionalInt(req.GetArguments(), "ownerID", 0)
|
||||||
isPrivate, _ := req.Params.Arguments["isPrivate"].(bool)
|
var pIsPrivate *bool
|
||||||
isArchived, _ := req.Params.Arguments["isArchived"].(bool)
|
isPrivate, ok := req.GetArguments()["isPrivate"].(bool)
|
||||||
sort, _ := req.Params.Arguments["sort"].(string)
|
if ok {
|
||||||
order, _ := req.Params.Arguments["order"].(string)
|
pIsPrivate = new(isPrivate)
|
||||||
page, ok := req.Params.Arguments["page"].(float64)
|
|
||||||
if !ok {
|
|
||||||
page = 1
|
|
||||||
}
|
}
|
||||||
pageSize, ok := req.Params.Arguments["pageSize"].(float64)
|
var pIsArchived *bool
|
||||||
if !ok {
|
isArchived, ok := req.GetArguments()["isArchived"].(bool)
|
||||||
pageSize = 100
|
if ok {
|
||||||
|
pIsArchived = new(isArchived)
|
||||||
}
|
}
|
||||||
|
sort, _ := req.GetArguments()["sort"].(string)
|
||||||
|
order, _ := req.GetArguments()["order"].(string)
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
opt := gitea_sdk.SearchRepoOptions{
|
opt := gitea_sdk.SearchRepoOptions{
|
||||||
Keyword: keyword,
|
Keyword: keyword,
|
||||||
KeywordIsTopic: keywordIsTopic,
|
KeywordIsTopic: keywordIsTopic,
|
||||||
KeywordInDescription: keywordInDescription,
|
KeywordInDescription: keywordInDescription,
|
||||||
OwnerID: int64(ownerID),
|
OwnerID: ownerID,
|
||||||
IsPrivate: ptr.To(isPrivate),
|
IsPrivate: pIsPrivate,
|
||||||
IsArchived: ptr.To(isArchived),
|
IsArchived: pIsArchived,
|
||||||
Sort: sort,
|
Sort: sort,
|
||||||
Order: order,
|
Order: order,
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
@@ -158,7 +169,11 @@ func SearchReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
|
|||||||
PageSize: int(pageSize),
|
PageSize: int(pageSize),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
repos, _, err := gitea.Client().SearchRepos(opt)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.SearchRepos(opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("search repos error: %v", err))
|
return to.ErrorResult(fmt.Errorf("search repos error: %v", err))
|
||||||
}
|
}
|
||||||
|
|||||||
42
operation/search/search_test.go
Normal file
42
operation/search/search_test.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package search
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSearchToolsRequiredFields(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
tool mcp.Tool
|
||||||
|
required []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "search_users",
|
||||||
|
tool: SearchUsersTool,
|
||||||
|
required: []string{"keyword"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "search_org_teams",
|
||||||
|
tool: SearOrgTeamsTool,
|
||||||
|
required: []string{"org", "query"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "search_repos",
|
||||||
|
tool: SearchReposTool,
|
||||||
|
required: []string{"keyword"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
for _, field := range tt.required {
|
||||||
|
if !slices.Contains(tt.tool.InputSchema.Required, field) {
|
||||||
|
t.Errorf("tool %s: expected %q to be required, got required=%v", tt.name, field, tt.tool.InputSchema.Required)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
104
operation/settings/settings.go
Normal file
104
operation/settings/settings.go
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package settings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetAPISettingsToolName = "get_api_settings"
|
||||||
|
GetAttachmentSettingsToolName = "get_attachment_settings"
|
||||||
|
GetRepoSettingsToolName = "get_repo_settings"
|
||||||
|
GetUISettingsToolName = "get_ui_settings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GetAPISettingsTool = mcp.NewTool(
|
||||||
|
GetAPISettingsToolName,
|
||||||
|
mcp.WithDescription("Get the global API settings of the Gitea instance"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetAttachmentSettingsTool = mcp.NewTool(
|
||||||
|
GetAttachmentSettingsToolName,
|
||||||
|
mcp.WithDescription("Get the global attachment settings"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetRepoSettingsTool = mcp.NewTool(
|
||||||
|
GetRepoSettingsToolName,
|
||||||
|
mcp.WithDescription("Get the global repository settings"),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetUISettingsTool = mcp.NewTool(
|
||||||
|
GetUISettingsToolName,
|
||||||
|
mcp.WithDescription("Get the global UI settings"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetAPISettingsTool, Handler: GetAPISettingsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetAttachmentSettingsTool, Handler: GetAttachmentSettingsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetRepoSettingsTool, Handler: GetRepoSettingsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetUISettingsTool, Handler: GetUISettingsFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAPISettingsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetAPISettingsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
settings, _, err := client.GetGlobalAPISettings()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get API settings err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAttachmentSettingsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetAttachmentSettingsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
settings, _, err := client.GetGlobalAttachmentSettings()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get attachment settings err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRepoSettingsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetRepoSettingsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
settings, _, err := client.GetGlobalRepoSettings()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get repo settings err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUISettingsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetUISettingsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
settings, _, err := client.GetGlobalUISettings()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get UI settings err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(settings)
|
||||||
|
}
|
||||||
366
operation/timetracking/timetracking.go
Normal file
366
operation/timetracking/timetracking.go
Normal file
@@ -0,0 +1,366 @@
|
|||||||
|
// Package timetracking provides MCP tools for Gitea time tracking operations
|
||||||
|
package timetracking
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Stopwatch tools
|
||||||
|
StartStopwatchToolName = "start_stopwatch"
|
||||||
|
StopStopwatchToolName = "stop_stopwatch"
|
||||||
|
DeleteStopwatchToolName = "delete_stopwatch"
|
||||||
|
GetMyStopwatchesToolName = "get_my_stopwatches"
|
||||||
|
|
||||||
|
// Tracked time tools
|
||||||
|
ListTrackedTimesToolName = "list_tracked_times"
|
||||||
|
AddTrackedTimeToolName = "add_tracked_time"
|
||||||
|
DeleteTrackedTimeToolName = "delete_tracked_time"
|
||||||
|
ListRepoTimesToolName = "list_repo_times"
|
||||||
|
GetMyTimesToolName = "get_my_times"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Stopwatch tools
|
||||||
|
StartStopwatchTool = mcp.NewTool(
|
||||||
|
StartStopwatchToolName,
|
||||||
|
mcp.WithDescription("Start a stopwatch on an issue to track time spent"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
StopStopwatchTool = mcp.NewTool(
|
||||||
|
StopStopwatchToolName,
|
||||||
|
mcp.WithDescription("Stop a running stopwatch on an issue and record the tracked time"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteStopwatchTool = mcp.NewTool(
|
||||||
|
DeleteStopwatchToolName,
|
||||||
|
mcp.WithDescription("Delete/cancel a running stopwatch without recording time"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetMyStopwatchesTool = mcp.NewTool(
|
||||||
|
GetMyStopwatchesToolName,
|
||||||
|
mcp.WithDescription("Get all currently running stopwatches for the authenticated user"),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tracked time tools
|
||||||
|
ListTrackedTimesTool = mcp.NewTool(
|
||||||
|
ListTrackedTimesToolName,
|
||||||
|
mcp.WithDescription("List tracked times for a specific issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddTrackedTimeTool = mcp.NewTool(
|
||||||
|
AddTrackedTimeToolName,
|
||||||
|
mcp.WithDescription("Manually add tracked time to an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithNumber("time", mcp.Required(), mcp.Description("time to add in seconds")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteTrackedTimeTool = mcp.NewTool(
|
||||||
|
DeleteTrackedTimeToolName,
|
||||||
|
mcp.WithDescription("Delete a tracked time entry from an issue"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("issue index")),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("tracked time entry ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListRepoTimesTool = mcp.NewTool(
|
||||||
|
ListRepoTimesToolName,
|
||||||
|
mcp.WithDescription("List all tracked times for a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetMyTimesTool = mcp.NewTool(
|
||||||
|
GetMyTimesToolName,
|
||||||
|
mcp.WithDescription("Get all tracked times for the authenticated user"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Stopwatch tools
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: StartStopwatchTool, Handler: StartStopwatchFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: StopStopwatchTool, Handler: StopStopwatchFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteStopwatchTool, Handler: DeleteStopwatchFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetMyStopwatchesTool, Handler: GetMyStopwatchesFn})
|
||||||
|
|
||||||
|
// Tracked time tools
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListTrackedTimesTool, Handler: ListTrackedTimesFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddTrackedTimeTool, Handler: AddTrackedTimeFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteTrackedTimeTool, Handler: DeleteTrackedTimeFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListRepoTimesTool, Handler: ListRepoTimesFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetMyTimesTool, Handler: GetMyTimesFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stopwatch handler functions
|
||||||
|
|
||||||
|
func StartStopwatchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called StartStopwatchFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.StartIssueStopWatch(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("start stopwatch on %s/%s#%d err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(fmt.Sprintf("Stopwatch started on issue %s/%s#%d", owner, repo, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
func StopStopwatchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called StopStopwatchFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.StopIssueStopWatch(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("stop stopwatch on %s/%s#%d err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(fmt.Sprintf("Stopwatch stopped on issue %s/%s#%d - time recorded", owner, repo, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteStopwatchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteStopwatchFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteIssueStopwatch(owner, repo, index)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete stopwatch on %s/%s#%d err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(fmt.Sprintf("Stopwatch deleted/cancelled on issue %s/%s#%d", owner, repo, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMyStopwatchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetMyStopwatchesFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
stopwatches, _, err := client.ListMyStopwatches(gitea_sdk.ListStopwatchesOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get stopwatches err: %v", err))
|
||||||
|
}
|
||||||
|
if len(stopwatches) == 0 {
|
||||||
|
return to.TextResult("No active stopwatches")
|
||||||
|
}
|
||||||
|
return to.TextResult(stopwatches)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tracked time handler functions
|
||||||
|
|
||||||
|
func ListTrackedTimesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListTrackedTimesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
times, _, err := client.ListIssueTrackedTimes(owner, repo, index, gitea_sdk.ListTrackedTimesOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list tracked times for %s/%s#%d err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
if len(times) == 0 {
|
||||||
|
return to.TextResult(fmt.Sprintf("No tracked times for issue %s/%s#%d", owner, repo, index))
|
||||||
|
}
|
||||||
|
return to.TextResult(times)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddTrackedTimeFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddTrackedTimeFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeSeconds, err := params.GetIndex(req.GetArguments(), "time")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
trackedTime, _, err := client.AddTime(owner, repo, index, gitea_sdk.AddTimeOption{
|
||||||
|
Time: timeSeconds,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add tracked time to %s/%s#%d err: %v", owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(trackedTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteTrackedTimeFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteTrackedTimeFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteTime(owner, repo, index, id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete tracked time %d from %s/%s#%d err: %v", id, owner, repo, index, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(fmt.Sprintf("Tracked time entry %d deleted from issue %s/%s#%d", id, owner, repo, index))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListRepoTimesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListRepoTimesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
times, _, err := client.ListRepoTrackedTimes(owner, repo, gitea_sdk.ListTrackedTimesOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: int(page),
|
||||||
|
PageSize: int(pageSize),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list repo tracked times for %s/%s err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
if len(times) == 0 {
|
||||||
|
return to.TextResult(fmt.Sprintf("No tracked times for repository %s/%s", owner, repo))
|
||||||
|
}
|
||||||
|
return to.TextResult(times)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMyTimesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetMyTimesFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
times, _, err := client.ListMyTrackedTimes(gitea_sdk.ListTrackedTimesOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get tracked times err: %v", err))
|
||||||
|
}
|
||||||
|
if len(times) == 0 {
|
||||||
|
return to.TextResult("No tracked times found")
|
||||||
|
}
|
||||||
|
return to.TextResult(times)
|
||||||
|
}
|
||||||
125
operation/user/blocks.go
Normal file
125
operation/user/blocks.go
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListMyBlocksToolName = "list_my_blocks"
|
||||||
|
CheckUserBlockToolName = "check_user_block"
|
||||||
|
BlockUserToolName = "block_user"
|
||||||
|
UnblockUserToolName = "unblock_user"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListMyBlocksTool = mcp.NewTool(
|
||||||
|
ListMyBlocksToolName,
|
||||||
|
mcp.WithDescription("List users blocked by the authenticated user"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckUserBlockTool = mcp.NewTool(
|
||||||
|
CheckUserBlockToolName,
|
||||||
|
mcp.WithDescription("Check if the authenticated user has blocked a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to check")),
|
||||||
|
)
|
||||||
|
|
||||||
|
BlockUserTool = mcp.NewTool(
|
||||||
|
BlockUserToolName,
|
||||||
|
mcp.WithDescription("Block a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to block")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnblockUserTool = mcp.NewTool(
|
||||||
|
UnblockUserToolName,
|
||||||
|
mcp.WithDescription("Unblock a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to unblock")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListMyBlocksTool, Handler: ListMyBlocksFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckUserBlockTool, Handler: CheckUserBlockFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: BlockUserTool, Handler: BlockUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnblockUserTool, Handler: UnblockUserFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMyBlocksFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMyBlocksFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
users, _, err := client.ListMyBlocks(gitea_sdk.ListUserBlocksOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list my blocks err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckUserBlockFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckUserBlockFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isBlocked, _, err := client.CheckUserBlock(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check block %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"username": username, "is_blocked": isBlocked})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BlockUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called BlockUserFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.BlockUser(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("block user %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "blocked", "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnblockUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnblockUserFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.UnblockUser(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unblock user %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unblocked", "username": username})
|
||||||
|
}
|
||||||
119
operation/user/emails.go
Normal file
119
operation/user/emails.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListEmailsToolName = "list_emails"
|
||||||
|
AddEmailToolName = "add_email"
|
||||||
|
DeleteEmailToolName = "delete_email"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListEmailsTool = mcp.NewTool(
|
||||||
|
ListEmailsToolName,
|
||||||
|
mcp.WithDescription("List the authenticated user's email addresses"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
AddEmailTool = mcp.NewTool(
|
||||||
|
AddEmailToolName,
|
||||||
|
mcp.WithDescription("Add email addresses for the authenticated user"),
|
||||||
|
mcp.WithArray("emails", mcp.Required(), mcp.Description("email addresses to add"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteEmailTool = mcp.NewTool(
|
||||||
|
DeleteEmailToolName,
|
||||||
|
mcp.WithDescription("Delete email addresses for the authenticated user"),
|
||||||
|
mcp.WithArray("emails", mcp.Required(), mcp.Description("email addresses to delete"), mcp.Items(map[string]any{"type": "string"})),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListEmailsTool, Handler: ListEmailsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: AddEmailTool, Handler: AddEmailFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeleteEmailTool, Handler: DeleteEmailFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListEmailsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListEmailsFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
emails, _, err := client.ListEmails(gitea_sdk.ListEmailsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list emails err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(emails)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddEmailFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called AddEmailFn")
|
||||||
|
emailsArg, exists := req.GetArguments()["emails"]
|
||||||
|
if !exists {
|
||||||
|
return to.ErrorResult(fmt.Errorf("emails is required"))
|
||||||
|
}
|
||||||
|
emailsSlice, ok := emailsArg.([]any)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("emails must be an array"))
|
||||||
|
}
|
||||||
|
emails := make([]string, 0, len(emailsSlice))
|
||||||
|
for _, e := range emailsSlice {
|
||||||
|
if s, ok := e.(string); ok {
|
||||||
|
emails = append(emails, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
result, _, err := client.AddEmail(gitea_sdk.CreateEmailOption{Emails: emails})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("add emails err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteEmailFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteEmailFn")
|
||||||
|
emailsArg, exists := req.GetArguments()["emails"]
|
||||||
|
if !exists {
|
||||||
|
return to.ErrorResult(fmt.Errorf("emails is required"))
|
||||||
|
}
|
||||||
|
emailsSlice, ok := emailsArg.([]any)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("emails must be an array"))
|
||||||
|
}
|
||||||
|
emails := make([]string, 0, len(emailsSlice))
|
||||||
|
for _, e := range emailsSlice {
|
||||||
|
if s, ok := e.(string); ok {
|
||||||
|
emails = append(emails, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeleteEmail(gitea_sdk.DeleteEmailOption{Emails: emails})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete emails err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "deleted", "emails": emails})
|
||||||
|
}
|
||||||
210
operation/user/followers.go
Normal file
210
operation/user/followers.go
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListMyFollowersToolName = "list_my_followers"
|
||||||
|
ListMyFollowingToolName = "list_my_following"
|
||||||
|
ListUserFollowersToolName = "list_user_followers"
|
||||||
|
ListUserFollowingToolName = "list_user_following"
|
||||||
|
CheckFollowingToolName = "check_following"
|
||||||
|
FollowUserToolName = "follow_user"
|
||||||
|
UnfollowUserToolName = "unfollow_user"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListMyFollowersTool = mcp.NewTool(
|
||||||
|
ListMyFollowersToolName,
|
||||||
|
mcp.WithDescription("List the authenticated user's followers"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListMyFollowingTool = mcp.NewTool(
|
||||||
|
ListMyFollowingToolName,
|
||||||
|
mcp.WithDescription("List users the authenticated user is following"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUserFollowersTool = mcp.NewTool(
|
||||||
|
ListUserFollowersToolName,
|
||||||
|
mcp.WithDescription("List a user's followers"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUserFollowingTool = mcp.NewTool(
|
||||||
|
ListUserFollowingToolName,
|
||||||
|
mcp.WithDescription("List users a user is following"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckFollowingTool = mcp.NewTool(
|
||||||
|
CheckFollowingToolName,
|
||||||
|
mcp.WithDescription("Check if the authenticated user is following a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to check")),
|
||||||
|
)
|
||||||
|
|
||||||
|
FollowUserTool = mcp.NewTool(
|
||||||
|
FollowUserToolName,
|
||||||
|
mcp.WithDescription("Follow a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to follow")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnfollowUserTool = mcp.NewTool(
|
||||||
|
UnfollowUserToolName,
|
||||||
|
mcp.WithDescription("Unfollow a user"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username to unfollow")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListMyFollowersTool, Handler: ListMyFollowersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListMyFollowingTool, Handler: ListMyFollowingFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserFollowersTool, Handler: ListUserFollowersFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserFollowingTool, Handler: ListUserFollowingFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckFollowingTool, Handler: CheckFollowingFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: FollowUserTool, Handler: FollowUserFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnfollowUserTool, Handler: UnfollowUserFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMyFollowersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMyFollowersFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
followers, _, err := client.ListMyFollowers(gitea_sdk.ListFollowersOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list my followers err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(followers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMyFollowingFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMyFollowingFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
following, _, err := client.ListMyFollowing(gitea_sdk.ListFollowingOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list my following err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(following)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserFollowersFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserFollowersFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
followers, _, err := client.ListFollowers(username, gitea_sdk.ListFollowersOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %s followers err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(followers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserFollowingFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserFollowingFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
following, _, err := client.ListFollowing(username, gitea_sdk.ListFollowingOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %s following err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(following)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckFollowingFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckFollowingFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isFollowing, _ := client.IsFollowing(username)
|
||||||
|
return to.TextResult(map[string]any{"username": username, "is_following": isFollowing})
|
||||||
|
}
|
||||||
|
|
||||||
|
func FollowUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called FollowUserFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.Follow(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("follow user %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "following", "username": username})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnfollowUserFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnfollowUserFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.Unfollow(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unfollow user %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unfollowed", "username": username})
|
||||||
|
}
|
||||||
169
operation/user/keys.go
Normal file
169
operation/user/keys.go
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListMyPublicKeysToolName = "list_my_public_keys"
|
||||||
|
ListUserPublicKeysToolName = "list_user_public_keys"
|
||||||
|
GetPublicKeyToolName = "get_public_key"
|
||||||
|
CreatePublicKeyToolName = "create_public_key"
|
||||||
|
DeletePublicKeyToolName = "delete_public_key"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListMyPublicKeysTool = mcp.NewTool(
|
||||||
|
ListMyPublicKeysToolName,
|
||||||
|
mcp.WithDescription("List the authenticated user's SSH public keys"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUserPublicKeysTool = mcp.NewTool(
|
||||||
|
ListUserPublicKeysToolName,
|
||||||
|
mcp.WithDescription("List a user's SSH public keys"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetPublicKeyTool = mcp.NewTool(
|
||||||
|
GetPublicKeyToolName,
|
||||||
|
mcp.WithDescription("Get an SSH public key by ID"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("key ID")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreatePublicKeyTool = mcp.NewTool(
|
||||||
|
CreatePublicKeyToolName,
|
||||||
|
mcp.WithDescription("Create an SSH public key for the authenticated user"),
|
||||||
|
mcp.WithString("title", mcp.Required(), mcp.Description("key title")),
|
||||||
|
mcp.WithString("key", mcp.Required(), mcp.Description("SSH public key content")),
|
||||||
|
mcp.WithBoolean("read_only", mcp.Description("whether the key is read-only")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeletePublicKeyTool = mcp.NewTool(
|
||||||
|
DeletePublicKeyToolName,
|
||||||
|
mcp.WithDescription("Delete an SSH public key"),
|
||||||
|
mcp.WithNumber("id", mcp.Required(), mcp.Description("key ID")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListMyPublicKeysTool, Handler: ListMyPublicKeysFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserPublicKeysTool, Handler: ListUserPublicKeysFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetPublicKeyTool, Handler: GetPublicKeyFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: CreatePublicKeyTool, Handler: CreatePublicKeyFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: DeletePublicKeyTool, Handler: DeletePublicKeyFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMyPublicKeysFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMyPublicKeysFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
keys, _, err := client.ListMyPublicKeys(gitea_sdk.ListPublicKeysOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list my public keys err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserPublicKeysFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserPublicKeysFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
keys, _, err := client.ListPublicKeys(username, gitea_sdk.ListPublicKeysOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %s public keys err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPublicKeyFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetPublicKeyFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
key, _, err := client.GetPublicKey(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get public key %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreatePublicKeyFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreatePublicKeyFn")
|
||||||
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("title is required"))
|
||||||
|
}
|
||||||
|
key, ok := req.GetArguments()["key"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("key is required"))
|
||||||
|
}
|
||||||
|
opt := gitea_sdk.CreateKeyOption{
|
||||||
|
Title: title,
|
||||||
|
Key: key,
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["read_only"].(bool); ok {
|
||||||
|
opt.ReadOnly = v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
pubKey, _, err := client.CreatePublicKey(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create public key err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(pubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePublicKeyFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeletePublicKeyFn")
|
||||||
|
id, err := params.GetIndex(req.GetArguments(), "id")
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(err)
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.DeletePublicKey(id)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete public key %d err: %v", id, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"status": "deleted", "key_id": id})
|
||||||
|
}
|
||||||
121
operation/user/profile.go
Normal file
121
operation/user/profile.go
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GetUserInfoToolName = "get_user_info"
|
||||||
|
GetUserSettingsToolName = "get_user_settings"
|
||||||
|
UpdateUserSettingsToolName = "update_user_settings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GetUserInfoTool = mcp.NewTool(
|
||||||
|
GetUserInfoToolName,
|
||||||
|
mcp.WithDescription("Get a user's public information by username"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetUserSettingsTool = mcp.NewTool(
|
||||||
|
GetUserSettingsToolName,
|
||||||
|
mcp.WithDescription("Get the authenticated user's settings"),
|
||||||
|
)
|
||||||
|
|
||||||
|
UpdateUserSettingsTool = mcp.NewTool(
|
||||||
|
UpdateUserSettingsToolName,
|
||||||
|
mcp.WithDescription("Update the authenticated user's settings"),
|
||||||
|
mcp.WithString("full_name", mcp.Description("full name")),
|
||||||
|
mcp.WithString("description", mcp.Description("user bio/description")),
|
||||||
|
mcp.WithString("website", mcp.Description("website URL")),
|
||||||
|
mcp.WithString("location", mcp.Description("location")),
|
||||||
|
mcp.WithString("language", mcp.Description("language preference")),
|
||||||
|
mcp.WithString("theme", mcp.Description("UI theme")),
|
||||||
|
mcp.WithBoolean("hide_email", mcp.Description("hide email from profile")),
|
||||||
|
mcp.WithBoolean("hide_activity", mcp.Description("hide activity from profile")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetUserInfoTool, Handler: GetUserByNameFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: GetUserSettingsTool, Handler: GetUserSettingsFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UpdateUserSettingsTool, Handler: UpdateUserSettingsFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserByNameFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetUserByNameFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
user, _, err := client.GetUserInfo(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get user %s err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserSettingsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetUserSettingsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
settings, _, err := client.GetUserSettings()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get user settings err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(settings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateUserSettingsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UpdateUserSettingsFn")
|
||||||
|
opt := gitea_sdk.UserSettingsOptions{}
|
||||||
|
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||||
|
opt.FullName = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||||
|
opt.Description = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["website"].(string); ok {
|
||||||
|
opt.Website = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["location"].(string); ok {
|
||||||
|
opt.Location = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["language"].(string); ok {
|
||||||
|
opt.Language = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["theme"].(string); ok {
|
||||||
|
opt.Theme = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["hide_email"].(bool); ok {
|
||||||
|
opt.HideEmail = &v
|
||||||
|
}
|
||||||
|
if v, ok := req.GetArguments()["hide_activity"].(bool); ok {
|
||||||
|
opt.HideActivity = &v
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
settings, _, err := client.UpdateUserSettings(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("update user settings err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(settings)
|
||||||
|
}
|
||||||
254
operation/user/repos.go
Normal file
254
operation/user/repos.go
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListUserReposToolName = "list_user_repos"
|
||||||
|
ListMyStarredToolName = "list_my_starred"
|
||||||
|
StarRepoToolName = "star_repo"
|
||||||
|
UnstarRepoToolName = "unstar_repo"
|
||||||
|
CheckStarredToolName = "check_starred"
|
||||||
|
ListMySubscriptionsToolName = "list_my_subscriptions"
|
||||||
|
ListUserHeatmapToolName = "list_user_heatmap"
|
||||||
|
ListUserActivityToolName = "list_user_activity"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListUserReposTool = mcp.NewTool(
|
||||||
|
ListUserReposToolName,
|
||||||
|
mcp.WithDescription("List a user's repositories"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListMyStarredTool = mcp.NewTool(
|
||||||
|
ListMyStarredToolName,
|
||||||
|
mcp.WithDescription("List repositories starred by the authenticated user"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
|
||||||
|
StarRepoTool = mcp.NewTool(
|
||||||
|
StarRepoToolName,
|
||||||
|
mcp.WithDescription("Star a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UnstarRepoTool = mcp.NewTool(
|
||||||
|
UnstarRepoToolName,
|
||||||
|
mcp.WithDescription("Unstar a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CheckStarredTool = mcp.NewTool(
|
||||||
|
CheckStarredToolName,
|
||||||
|
mcp.WithDescription("Check if the authenticated user has starred a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListMySubscriptionsTool = mcp.NewTool(
|
||||||
|
ListMySubscriptionsToolName,
|
||||||
|
mcp.WithDescription("List repositories watched by the authenticated user"),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUserHeatmapTool = mcp.NewTool(
|
||||||
|
ListUserHeatmapToolName,
|
||||||
|
mcp.WithDescription("Get a user's contribution heatmap data"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
)
|
||||||
|
|
||||||
|
ListUserActivityTool = mcp.NewTool(
|
||||||
|
ListUserActivityToolName,
|
||||||
|
mcp.WithDescription("List a user's activity feeds"),
|
||||||
|
mcp.WithString("username", mcp.Required(), mcp.Description("username")),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserReposTool, Handler: ListUserReposFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListMyStarredTool, Handler: ListMyStarredFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: CheckStarredTool, Handler: CheckStarredFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListMySubscriptionsTool, Handler: ListMySubscriptionsFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserHeatmapTool, Handler: ListUserHeatmapFn})
|
||||||
|
Tool.RegisterRead(server.ServerTool{Tool: ListUserActivityTool, Handler: ListUserActivityFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: StarRepoTool, Handler: StarRepoFn})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{Tool: UnstarRepoTool, Handler: UnstarRepoFn})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserReposFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.ListUserRepos(username, gitea_sdk.ListReposOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %s repos err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMyStarredFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMyStarredFn")
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.GetMyStarredRepos()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list my starred repos err: %v", err))
|
||||||
|
}
|
||||||
|
// Manual pagination since SDK returns all
|
||||||
|
start := (int(page) - 1) * int(pageSize)
|
||||||
|
if start >= len(repos) {
|
||||||
|
return to.TextResult([]*gitea_sdk.Repository{})
|
||||||
|
}
|
||||||
|
end := start + int(pageSize)
|
||||||
|
if end > len(repos) {
|
||||||
|
end = len(repos)
|
||||||
|
}
|
||||||
|
return to.TextResult(repos[start:end])
|
||||||
|
}
|
||||||
|
|
||||||
|
func StarRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called StarRepoFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.StarRepo(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("star %s/%s err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "starred", "owner": owner, "repo": repo})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnstarRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UnstarRepoFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
_, err = client.UnStarRepo(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("unstar %s/%s err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]string{"status": "unstarred", "owner": owner, "repo": repo})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckStarredFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CheckStarredFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
isStarred, _, err := client.IsRepoStarring(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("check starred %s/%s err: %v", owner, repo, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(map[string]any{"owner": owner, "repo": repo, "is_starred": isStarred})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMySubscriptionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListMySubscriptionsFn")
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
repos, _, err := client.GetMyWatchedRepos()
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list my subscriptions err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(repos)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserHeatmapFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserHeatmapFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
heatmap, _, err := client.GetUserHeatmap(username)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get %s heatmap err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(heatmap)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUserActivityFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListUserActivityFn")
|
||||||
|
username, ok := req.GetArguments()["username"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("username is required"))
|
||||||
|
}
|
||||||
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||||
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
feeds, _, err := client.ListUserActivityFeeds(username, gitea_sdk.ListUserActivityFeedsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list %s activity err: %v", username, err))
|
||||||
|
}
|
||||||
|
return to.TextResult(feeds)
|
||||||
|
}
|
||||||
@@ -4,35 +4,114 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// GetMyUserInfoToolName is the unique tool name used for MCP registration and lookup of the get_my_user_info command.
|
||||||
GetMyUserInfoToolName = "get_my_user_info"
|
GetMyUserInfoToolName = "get_my_user_info"
|
||||||
|
// GetUserOrgsToolName is the unique tool name used for MCP registration and lookup of the get_user_orgs command.
|
||||||
|
GetUserOrgsToolName = "get_user_orgs"
|
||||||
|
|
||||||
|
// defaultPage is the default starting page number used for paginated organization listings.
|
||||||
|
defaultPage = 1
|
||||||
|
// defaultPageSize is the default number of organizations per page for paginated queries.
|
||||||
|
defaultPageSize = 100
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Tool is the MCP tool manager instance for registering all MCP tools in this package.
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// GetMyUserInfoTool is the MCP tool for retrieving the current user's info.
|
||||||
|
// It is registered with a specific name and a description string.
|
||||||
GetMyUserInfoTool = mcp.NewTool(
|
GetMyUserInfoTool = mcp.NewTool(
|
||||||
GetMyUserInfoToolName,
|
GetMyUserInfoToolName,
|
||||||
mcp.WithDescription("Get my user info"),
|
mcp.WithDescription("Get my user info"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetUserOrgsTool is the MCP tool for listing organizations for the authenticated user.
|
||||||
|
// It supports pagination via "page" and "pageSize" arguments with default values specified above.
|
||||||
|
GetUserOrgsTool = mcp.NewTool(
|
||||||
|
GetUserOrgsToolName,
|
||||||
|
mcp.WithDescription("Get organizations associated with the authenticated user"),
|
||||||
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(defaultPage)),
|
||||||
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(defaultPageSize)),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
// init registers all MCP tools in Tool at package initialization.
|
||||||
s.AddTool(GetMyUserInfoTool, GetUserInfoFn)
|
// This function ensures the handler functions are registered before server usage.
|
||||||
|
func init() {
|
||||||
|
registerTools()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// registerTools registers all local MCP tool definitions and their handler functions.
|
||||||
|
// To add new functionality, append your tool/handler pair to the tools slice below.
|
||||||
|
func registerTools() {
|
||||||
|
tools := []server.ServerTool{
|
||||||
|
{Tool: GetMyUserInfoTool, Handler: GetUserInfoFn},
|
||||||
|
{Tool: GetUserOrgsTool, Handler: GetUserOrgsFn},
|
||||||
|
}
|
||||||
|
for _, t := range tools {
|
||||||
|
Tool.RegisterRead(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getIntArg parses an integer argument from the MCP request arguments map.
|
||||||
|
// Returns def if missing, not a number, or less than 1. Used for pagination arguments.
|
||||||
|
func getIntArg(req mcp.CallToolRequest, name string, def int) int {
|
||||||
|
v := params.GetOptionalInt(req.GetArguments(), name, int64(def))
|
||||||
|
if v < 1 {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return int(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUserInfoFn is the handler for "get_my_user_info" MCP tool requests.
|
||||||
|
// Logs invocation, fetches current user info from gitea, wraps result for MCP.
|
||||||
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetUserInfoFn")
|
log.Debugf("[User] Called GetUserInfoFn")
|
||||||
user, _, err := gitea.Client().GetMyUserInfo()
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
user, _, err := client.GetMyUserInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
|
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(user)
|
return to.TextResult(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserOrgsFn is the handler for "get_user_orgs" MCP tool requests.
|
||||||
|
// Logs invocation, pulls validated pagination arguments from request,
|
||||||
|
// performs Gitea organization listing, and wraps the result for MCP.
|
||||||
|
func GetUserOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("[User] Called GetUserOrgsFn")
|
||||||
|
page := getIntArg(req, "page", defaultPage)
|
||||||
|
pageSize := getIntArg(req, "pageSize", defaultPageSize)
|
||||||
|
|
||||||
|
opt := gitea_sdk.ListOrgsOptions{
|
||||||
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
orgs, _, err := client.ListMyOrgs(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get user orgs err: %v", err))
|
||||||
|
}
|
||||||
|
return to.TextResult(orgs)
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,27 +4,31 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/mark3labs/mcp-go/server"
|
"github.com/mark3labs/mcp-go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GetGiteaMCPServerVersion = "get_gitea_mcp_server_version"
|
GetGiteaMCPServerVersion = "get_gitea_mcp_server_version"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var GetGiteaMCPServerVersionTool = mcp.NewTool(
|
||||||
GetGiteaMCPServerVersionTool = mcp.NewTool(
|
|
||||||
GetGiteaMCPServerVersion,
|
GetGiteaMCPServerVersion,
|
||||||
mcp.WithDescription("Get Gitea MCP Server Version"),
|
mcp.WithDescription("Get Gitea MCP Server Version"),
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterTool(s *server.MCPServer) {
|
func init() {
|
||||||
s.AddTool(GetGiteaMCPServerVersionTool, GetGiteaMCPServerVersionFn)
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetGiteaMCPServerVersionTool,
|
||||||
|
Handler: GetGiteaMCPServerVersionFn,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGiteaMCPServerVersionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetGiteaMCPServerVersionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
|||||||
285
operation/wiki/wiki.go
Normal file
285
operation/wiki/wiki.go
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
package wiki
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool"
|
||||||
|
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Tool = tool.New()
|
||||||
|
|
||||||
|
const (
|
||||||
|
ListWikiPagesToolName = "list_wiki_pages"
|
||||||
|
GetWikiPageToolName = "get_wiki_page"
|
||||||
|
GetWikiRevisionsToolName = "get_wiki_revisions"
|
||||||
|
CreateWikiPageToolName = "create_wiki_page"
|
||||||
|
UpdateWikiPageToolName = "update_wiki_page"
|
||||||
|
DeleteWikiPageToolName = "delete_wiki_page"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ListWikiPagesTool = mcp.NewTool(
|
||||||
|
ListWikiPagesToolName,
|
||||||
|
mcp.WithDescription("List all wiki pages in a repository"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetWikiPageTool = mcp.NewTool(
|
||||||
|
GetWikiPageToolName,
|
||||||
|
mcp.WithDescription("Get a wiki page content and metadata"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("pageName", mcp.Required(), mcp.Description("wiki page name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
GetWikiRevisionsTool = mcp.NewTool(
|
||||||
|
GetWikiRevisionsToolName,
|
||||||
|
mcp.WithDescription("Get revisions history of a wiki page"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("pageName", mcp.Required(), mcp.Description("wiki page name")),
|
||||||
|
)
|
||||||
|
|
||||||
|
CreateWikiPageTool = mcp.NewTool(
|
||||||
|
CreateWikiPageToolName,
|
||||||
|
mcp.WithDescription("Create a new wiki page"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("title", mcp.Required(), mcp.Description("wiki page title")),
|
||||||
|
mcp.WithString("content_base64", mcp.Required(), mcp.Description("page content, base64 encoded")),
|
||||||
|
mcp.WithString("message", mcp.Description("commit message (optional)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
UpdateWikiPageTool = mcp.NewTool(
|
||||||
|
UpdateWikiPageToolName,
|
||||||
|
mcp.WithDescription("Update an existing wiki page"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("pageName", mcp.Required(), mcp.Description("current wiki page name")),
|
||||||
|
mcp.WithString("title", mcp.Description("new page title (optional)")),
|
||||||
|
mcp.WithString("content_base64", mcp.Required(), mcp.Description("page content, base64 encoded")),
|
||||||
|
mcp.WithString("message", mcp.Description("commit message (optional)")),
|
||||||
|
)
|
||||||
|
|
||||||
|
DeleteWikiPageTool = mcp.NewTool(
|
||||||
|
DeleteWikiPageToolName,
|
||||||
|
mcp.WithDescription("Delete a wiki page"),
|
||||||
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||||
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
|
mcp.WithString("pageName", mcp.Required(), mcp.Description("wiki page name to delete")),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: ListWikiPagesTool,
|
||||||
|
Handler: ListWikiPagesFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetWikiPageTool,
|
||||||
|
Handler: GetWikiPageFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterRead(server.ServerTool{
|
||||||
|
Tool: GetWikiRevisionsTool,
|
||||||
|
Handler: GetWikiRevisionsFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: CreateWikiPageTool,
|
||||||
|
Handler: CreateWikiPageFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: UpdateWikiPageTool,
|
||||||
|
Handler: UpdateWikiPageFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: DeleteWikiPageTool,
|
||||||
|
Handler: DeleteWikiPageFn,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListWikiPagesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called ListWikiPagesFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use direct HTTP request because SDK does not support yet wikis
|
||||||
|
var result any
|
||||||
|
_, err := gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/wiki/pages", url.QueryEscape(owner), url.QueryEscape(repo)), nil, nil, &result)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("list wiki pages err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWikiPageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetWikiPageFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
pageName, ok := req.GetArguments()["pageName"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("pageName is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
_, err := gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/wiki/page/%s", url.QueryEscape(owner), url.QueryEscape(repo), url.QueryEscape(pageName)), nil, nil, &result)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get wiki page err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWikiRevisionsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called GetWikiRevisionsFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
pageName, ok := req.GetArguments()["pageName"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("pageName is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
_, err := gitea.DoJSON(ctx, "GET", fmt.Sprintf("repos/%s/%s/wiki/revisions/%s", url.QueryEscape(owner), url.QueryEscape(repo), url.QueryEscape(pageName)), nil, nil, &result)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get wiki revisions err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateWikiPageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called CreateWikiPageFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
title, ok := req.GetArguments()["title"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("title is required"))
|
||||||
|
}
|
||||||
|
contentBase64, ok := req.GetArguments()["content_base64"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("content_base64 is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
message, _ := req.GetArguments()["message"].(string)
|
||||||
|
if message == "" {
|
||||||
|
message = fmt.Sprintf("Create wiki page '%s'", title)
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBody := map[string]string{
|
||||||
|
"title": title,
|
||||||
|
"content_base64": contentBase64,
|
||||||
|
"message": message,
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
_, err := gitea.DoJSON(ctx, "POST", fmt.Sprintf("repos/%s/%s/wiki/new", url.QueryEscape(owner), url.QueryEscape(repo)), nil, requestBody, &result)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create wiki page err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateWikiPageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called UpdateWikiPageFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
pageName, ok := req.GetArguments()["pageName"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("pageName is required"))
|
||||||
|
}
|
||||||
|
contentBase64, ok := req.GetArguments()["content_base64"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("content_base64 is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBody := map[string]string{
|
||||||
|
"content_base64": contentBase64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// If title is given, use it. Otherwise, keep current page name
|
||||||
|
if title, ok := req.GetArguments()["title"].(string); ok && title != "" {
|
||||||
|
requestBody["title"] = title
|
||||||
|
} else {
|
||||||
|
// Utiliser pageName comme fallback pour éviter "unnamed"
|
||||||
|
requestBody["title"] = pageName
|
||||||
|
}
|
||||||
|
|
||||||
|
if message, ok := req.GetArguments()["message"].(string); ok && message != "" {
|
||||||
|
requestBody["message"] = message
|
||||||
|
} else {
|
||||||
|
requestBody["message"] = fmt.Sprintf("Update wiki page '%s'", pageName)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result any
|
||||||
|
_, err := gitea.DoJSON(ctx, "PATCH", fmt.Sprintf("repos/%s/%s/wiki/page/%s", url.QueryEscape(owner), url.QueryEscape(repo), url.QueryEscape(pageName)), nil, requestBody, &result)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("update wiki page err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteWikiPageFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called DeleteWikiPageFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("repo is required"))
|
||||||
|
}
|
||||||
|
pageName, ok := req.GetArguments()["pageName"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(errors.New("pageName is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := gitea.DoJSON(ctx, "DELETE", fmt.Sprintf("repos/%s/%s/wiki/page/%s", url.QueryEscape(owner), url.QueryEscape(repo), url.QueryEscape(pageName)), nil, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("delete wiki page err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(map[string]string{"message": "Wiki page deleted successfully"})
|
||||||
|
}
|
||||||
7
pkg/context/context.go
Normal file
7
pkg/context/context.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package context
|
||||||
|
|
||||||
|
type contextKey string
|
||||||
|
|
||||||
|
const (
|
||||||
|
TokenContextKey = contextKey("token")
|
||||||
|
)
|
||||||
@@ -5,6 +5,9 @@ var (
|
|||||||
Port int
|
Port int
|
||||||
Token string
|
Token string
|
||||||
Version string
|
Version string
|
||||||
|
Mode string
|
||||||
|
|
||||||
|
Insecure bool
|
||||||
|
ReadOnly bool
|
||||||
Debug bool
|
Debug bool
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,28 +1,47 @@
|
|||||||
package gitea
|
package gitea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
"fmt"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"net/http"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
|
mcpContext "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/context"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func NewClient(token string) (*gitea.Client, error) {
|
||||||
client *gitea.Client
|
httpClient := &http.Client{
|
||||||
clientOnce sync.Once
|
Transport: http.DefaultTransport,
|
||||||
)
|
}
|
||||||
|
|
||||||
func Client() *gitea.Client {
|
opts := []gitea.ClientOption{
|
||||||
clientOnce.Do(func() {
|
gitea.SetToken(token),
|
||||||
if client == nil {
|
}
|
||||||
c, err := gitea.NewClient(flag.Host, gitea.SetToken(flag.Token))
|
if flag.Insecure {
|
||||||
|
httpClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opts = append(opts, gitea.SetHTTPClient(httpClient))
|
||||||
|
if flag.Debug {
|
||||||
|
opts = append(opts, gitea.SetDebugMode())
|
||||||
|
}
|
||||||
|
client, err := gitea.NewClient(flag.Host, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("create gitea client err: %v", err)
|
return nil, fmt.Errorf("create gitea client err: %w", err)
|
||||||
}
|
}
|
||||||
client = c
|
|
||||||
}
|
// Set user agent for the client
|
||||||
})
|
client.SetUserAgent("gitea-mcp-server/" + flag.Version)
|
||||||
return client
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClientFromContext(ctx context.Context) (*gitea.Client, error) {
|
||||||
|
token, ok := ctx.Value(mcpContext.TokenContextKey).(string)
|
||||||
|
if !ok {
|
||||||
|
token = flag.Token
|
||||||
|
}
|
||||||
|
return NewClient(token)
|
||||||
}
|
}
|
||||||
|
|||||||
174
pkg/gitea/rest.go
Normal file
174
pkg/gitea/rest.go
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mcpContext "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/context"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPError struct {
|
||||||
|
StatusCode int
|
||||||
|
Body string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *HTTPError) Error() string {
|
||||||
|
if e.Body == "" {
|
||||||
|
return fmt.Sprintf("request failed with status %d", e.StatusCode)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("request failed with status %d: %s", e.StatusCode, e.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tokenFromContext(ctx context.Context) string {
|
||||||
|
if ctx != nil {
|
||||||
|
if token, ok := ctx.Value(mcpContext.TokenContextKey).(string); ok && token != "" {
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flag.Token
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRESTHTTPClient() *http.Client {
|
||||||
|
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
|
if flag.Insecure {
|
||||||
|
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // user-requested insecure mode
|
||||||
|
}
|
||||||
|
return &http.Client{
|
||||||
|
Transport: transport,
|
||||||
|
Timeout: 60 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildAPIURL(path string, query url.Values) (string, error) {
|
||||||
|
host := strings.TrimRight(flag.Host, "/")
|
||||||
|
if host == "" {
|
||||||
|
return "", errors.New("gitea host is empty")
|
||||||
|
}
|
||||||
|
p := strings.TrimLeft(path, "/")
|
||||||
|
u, err := url.Parse(fmt.Sprintf("%s/api/v1/%s", host, p))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if query != nil {
|
||||||
|
u.RawQuery = query.Encode()
|
||||||
|
}
|
||||||
|
return u.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoJSON performs an API request and decodes a JSON response into respOut (if non-nil).
|
||||||
|
// It returns the HTTP status code.
|
||||||
|
func DoJSON(ctx context.Context, method, path string, query url.Values, body, respOut any) (int, error) {
|
||||||
|
var bodyReader io.Reader
|
||||||
|
if body != nil {
|
||||||
|
b, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("marshal request body: %w", err)
|
||||||
|
}
|
||||||
|
bodyReader = bytes.NewReader(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := buildAPIURL(path, query)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
token := tokenFromContext(ctx)
|
||||||
|
if token != "" {
|
||||||
|
req.Header.Set("Authorization", "token "+token)
|
||||||
|
}
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
if body != nil {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := newRESTHTTPClient()
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("do request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
bodySnippet, _ := io.ReadAll(io.LimitReader(resp.Body, 8192))
|
||||||
|
return resp.StatusCode, &HTTPError{StatusCode: resp.StatusCode, Body: strings.TrimSpace(string(bodySnippet))}
|
||||||
|
}
|
||||||
|
|
||||||
|
if respOut == nil {
|
||||||
|
_, _ = io.Copy(io.Discard, resp.Body) // best-effort
|
||||||
|
return resp.StatusCode, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(respOut); err != nil {
|
||||||
|
return resp.StatusCode, fmt.Errorf("decode response: %w", err)
|
||||||
|
}
|
||||||
|
return resp.StatusCode, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoBytes performs an API request and returns the raw response bytes.
|
||||||
|
// It returns the HTTP status code.
|
||||||
|
func DoBytes(ctx context.Context, method, path string, query url.Values, body any, accept string) ([]byte, int, error) {
|
||||||
|
var bodyReader io.Reader
|
||||||
|
if body != nil {
|
||||||
|
b, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("marshal request body: %w", err)
|
||||||
|
}
|
||||||
|
bodyReader = bytes.NewReader(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
u, err := buildAPIURL(path, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
token := tokenFromContext(ctx)
|
||||||
|
if token != "" {
|
||||||
|
req.Header.Set("Authorization", "token "+token)
|
||||||
|
}
|
||||||
|
if accept != "" {
|
||||||
|
req.Header.Set("Accept", accept)
|
||||||
|
}
|
||||||
|
if body != nil {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
|
||||||
|
client := newRESTHTTPClient()
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("do request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
respBytes, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, resp.StatusCode, fmt.Errorf("read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
bodySnippet := respBytes
|
||||||
|
if len(bodySnippet) > 8192 {
|
||||||
|
bodySnippet = bodySnippet[:8192]
|
||||||
|
}
|
||||||
|
return nil, resp.StatusCode, &HTTPError{StatusCode: resp.StatusCode, Body: strings.TrimSpace(string(bodySnippet))}
|
||||||
|
}
|
||||||
|
|
||||||
|
return respBytes, resp.StatusCode, nil
|
||||||
|
}
|
||||||
30
pkg/gitea/rest_test.go
Normal file
30
pkg/gitea/rest_test.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
mcpContext "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/context"
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTokenFromContext(t *testing.T) {
|
||||||
|
orig := flag.Token
|
||||||
|
defer func() { flag.Token = orig }()
|
||||||
|
|
||||||
|
flag.Token = "flag-token"
|
||||||
|
|
||||||
|
t.Run("context token wins", func(t *testing.T) {
|
||||||
|
ctx := context.WithValue(context.Background(), mcpContext.TokenContextKey, "ctx-token")
|
||||||
|
if got := tokenFromContext(ctx); got != "ctx-token" {
|
||||||
|
t.Fatalf("tokenFromContext() = %q, want %q", got, "ctx-token")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("fallback to flag token", func(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
if got := tokenFromContext(ctx); got != "flag-token" {
|
||||||
|
t.Fatalf("tokenFromContext() = %q, want %q", got, "flag-token")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -5,9 +5,10 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -17,15 +18,39 @@ var (
|
|||||||
|
|
||||||
func Default() *zap.Logger {
|
func Default() *zap.Logger {
|
||||||
defaultLoggerOnce.Do(func() {
|
defaultLoggerOnce.Do(func() {
|
||||||
if defaultLogger == nil {
|
if defaultLogger != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ec := zap.NewProductionEncoderConfig()
|
ec := zap.NewProductionEncoderConfig()
|
||||||
ec.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime)
|
ec.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime)
|
||||||
ec.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
ec.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||||
|
|
||||||
var ws zapcore.WriteSyncer
|
var ws zapcore.WriteSyncer
|
||||||
var wss []zapcore.WriteSyncer
|
var wss []zapcore.WriteSyncer
|
||||||
|
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
if home == "" {
|
||||||
|
home = os.TempDir()
|
||||||
|
}
|
||||||
|
|
||||||
|
logDir := home + "/.gitea-mcp"
|
||||||
|
if err := os.MkdirAll(logDir, 0o700); err != nil {
|
||||||
|
// Fallback to temp directory if creation fails
|
||||||
|
logDir = os.TempDir()
|
||||||
|
}
|
||||||
|
|
||||||
|
wss = append(wss, zapcore.AddSync(&lumberjack.Logger{
|
||||||
|
Filename: logDir + "/gitea-mcp.log",
|
||||||
|
MaxSize: 100,
|
||||||
|
MaxBackups: 10,
|
||||||
|
MaxAge: 30,
|
||||||
|
}))
|
||||||
|
|
||||||
|
if flag.Mode == "http" {
|
||||||
wss = append(wss, zapcore.AddSync(os.Stdout))
|
wss = append(wss, zapcore.AddSync(os.Stdout))
|
||||||
|
}
|
||||||
|
|
||||||
ws = zapcore.NewMultiWriteSyncer(wss...)
|
ws = zapcore.NewMultiWriteSyncer(wss...)
|
||||||
|
|
||||||
enc := zapcore.NewConsoleEncoder(ec)
|
enc := zapcore.NewConsoleEncoder(ec)
|
||||||
@@ -37,12 +62,11 @@ func Default() *zap.Logger {
|
|||||||
}
|
}
|
||||||
core := zapcore.NewCore(enc, ws, level)
|
core := zapcore.NewCore(enc, ws, level)
|
||||||
options := []zap.Option{
|
options := []zap.Option{
|
||||||
zap.AddStacktrace(zapcore.ErrorLevel),
|
zap.AddStacktrace(zapcore.DPanicLevel),
|
||||||
zap.AddCaller(),
|
zap.AddCaller(),
|
||||||
zap.AddCallerSkip(1),
|
zap.AddCallerSkip(1),
|
||||||
}
|
}
|
||||||
defaultLogger = zap.New(core, options...)
|
defaultLogger = zap.New(core, options...)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return defaultLogger
|
return defaultLogger
|
||||||
@@ -54,8 +78,22 @@ func SetDefault(logger *zap.Logger) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Logger() *zap.Logger {
|
func New() *Logger {
|
||||||
return defaultLogger
|
return &Logger{
|
||||||
|
defaultLogger: Default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
defaultLogger *zap.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Infof(msg string, args ...any) {
|
||||||
|
l.defaultLogger.Sugar().Infof(msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logger) Errorf(msg string, args ...any) {
|
||||||
|
l.defaultLogger.Sugar().Errorf(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Debug(msg string, fields ...zap.Field) {
|
func Debug(msg string, fields ...zap.Field) {
|
||||||
|
|||||||
58
pkg/params/params.go
Normal file
58
pkg/params/params.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package params
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ToInt64 converts a value to int64, accepting both float64 (JSON number) and
|
||||||
|
// string representations. Returns false if the value cannot be converted.
|
||||||
|
func ToInt64(val any) (int64, bool) {
|
||||||
|
switch v := val.(type) {
|
||||||
|
case float64:
|
||||||
|
return int64(v), true
|
||||||
|
case string:
|
||||||
|
i, err := strconv.ParseInt(v, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return i, true
|
||||||
|
default:
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIndex extracts a required integer parameter from MCP tool arguments.
|
||||||
|
// It accepts both numeric (float64 from JSON) and string representations.
|
||||||
|
// This provides better UX for LLM callers that may naturally use strings
|
||||||
|
// for identifiers like issue/PR numbers.
|
||||||
|
func GetIndex(args map[string]any, key string) (int64, error) {
|
||||||
|
val, exists := args[key]
|
||||||
|
if !exists {
|
||||||
|
return 0, fmt.Errorf("%s is required", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := ToInt64(val); ok {
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if s, ok := val.(string); ok {
|
||||||
|
return 0, fmt.Errorf("%s must be a valid integer (got %q)", key, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, fmt.Errorf("%s must be a number or numeric string", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOptionalInt extracts an optional integer parameter from MCP tool arguments.
|
||||||
|
// Returns defaultVal if the key is missing or the value cannot be parsed.
|
||||||
|
// Accepts both float64 (JSON number) and string representations.
|
||||||
|
func GetOptionalInt(args map[string]any, key string, defaultVal int64) int64 {
|
||||||
|
val, exists := args[key]
|
||||||
|
if !exists {
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
if i, ok := ToInt64(val); ok {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
161
pkg/params/params_test.go
Normal file
161
pkg/params/params_test.go
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
package params
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToInt64(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
val any
|
||||||
|
want int64
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
{"float64", float64(42), 42, true},
|
||||||
|
{"float64 zero", float64(0), 0, true},
|
||||||
|
{"float64 negative", float64(-5), -5, true},
|
||||||
|
{"string", "123", 123, true},
|
||||||
|
{"string zero", "0", 0, true},
|
||||||
|
{"string negative", "-10", -10, true},
|
||||||
|
{"invalid string", "abc", 0, false},
|
||||||
|
{"decimal string", "1.5", 0, false},
|
||||||
|
{"bool", true, 0, false},
|
||||||
|
{"nil", nil, 0, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, ok := ToInt64(tt.val)
|
||||||
|
if ok != tt.ok {
|
||||||
|
t.Errorf("ToInt64() ok = %v, want %v", ok, tt.ok)
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("ToInt64() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetOptionalInt(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args map[string]any
|
||||||
|
key string
|
||||||
|
defaultVal int64
|
||||||
|
want int64
|
||||||
|
}{
|
||||||
|
{"present float64", map[string]any{"page": float64(3)}, "page", 1, 3},
|
||||||
|
{"present string", map[string]any{"page": "5"}, "page", 1, 5},
|
||||||
|
{"missing key", map[string]any{}, "page", 1, 1},
|
||||||
|
{"invalid string", map[string]any{"page": "abc"}, "page", 1, 1},
|
||||||
|
{"invalid type", map[string]any{"page": true}, "page", 1, 1},
|
||||||
|
{"zero value", map[string]any{"id": float64(0)}, "id", 99, 0},
|
||||||
|
{"string zero", map[string]any{"id": "0"}, "id", 99, 0},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := GetOptionalInt(tt.args, tt.key, tt.defaultVal)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("GetOptionalInt() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetIndex(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args map[string]any
|
||||||
|
key string
|
||||||
|
wantIndex int64
|
||||||
|
wantErr bool
|
||||||
|
errMsg string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid float64",
|
||||||
|
args: map[string]any{"index": float64(123)},
|
||||||
|
key: "index",
|
||||||
|
wantIndex: 123,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid string",
|
||||||
|
args: map[string]any{"index": "456"},
|
||||||
|
key: "index",
|
||||||
|
wantIndex: 456,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid string with large number",
|
||||||
|
args: map[string]any{"index": "999999"},
|
||||||
|
key: "index",
|
||||||
|
wantIndex: 999999,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "missing parameter",
|
||||||
|
args: map[string]any{},
|
||||||
|
key: "index",
|
||||||
|
wantErr: true,
|
||||||
|
errMsg: "index is required",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid string (not a number)",
|
||||||
|
args: map[string]any{"index": "abc"},
|
||||||
|
key: "index",
|
||||||
|
wantErr: true,
|
||||||
|
errMsg: "must be a valid integer",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid string (decimal)",
|
||||||
|
args: map[string]any{"index": "12.34"},
|
||||||
|
key: "index",
|
||||||
|
wantErr: true,
|
||||||
|
errMsg: "must be a valid integer",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid type (bool)",
|
||||||
|
args: map[string]any{"index": true},
|
||||||
|
key: "index",
|
||||||
|
wantErr: true,
|
||||||
|
errMsg: "must be a number or numeric string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid type (map)",
|
||||||
|
args: map[string]any{"index": map[string]string{"foo": "bar"}},
|
||||||
|
key: "index",
|
||||||
|
wantErr: true,
|
||||||
|
errMsg: "must be a number or numeric string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "custom key name",
|
||||||
|
args: map[string]any{"pr_index": "789"},
|
||||||
|
key: "pr_index",
|
||||||
|
wantIndex: 789,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
gotIndex, err := GetIndex(tt.args, tt.key)
|
||||||
|
if tt.wantErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("GetIndex() expected error but got nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
|
||||||
|
t.Errorf("GetIndex() error = %v, want error containing %q", err, tt.errMsg)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GetIndex() unexpected error = %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if gotIndex != tt.wantIndex {
|
||||||
|
t.Errorf("GetIndex() = %v, want %v", gotIndex, tt.wantIndex)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2023 The Kubernetes Authors.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package ptr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when,
|
|
||||||
// for example, an API struct is handled by plugins which need to distinguish
|
|
||||||
// "no plugin accepted this spec" from "this spec is empty".
|
|
||||||
//
|
|
||||||
// This function is only valid for structs and pointers to structs. Any other
|
|
||||||
// type will cause a panic. Passing a typed nil pointer will return true.
|
|
||||||
func AllPtrFieldsNil(obj interface{}) bool {
|
|
||||||
v := reflect.ValueOf(obj)
|
|
||||||
if !v.IsValid() {
|
|
||||||
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
|
|
||||||
}
|
|
||||||
if v.Kind() == reflect.Ptr {
|
|
||||||
if v.IsNil() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
for i := 0; i < v.NumField(); i++ {
|
|
||||||
if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// To returns a pointer to the given value.
|
|
||||||
func To[T any](v T) *T {
|
|
||||||
return &v
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deref dereferences ptr and returns the value it points to if no nil, or else
|
|
||||||
// returns def.
|
|
||||||
func Deref[T any](ptr *T, def T) T {
|
|
||||||
if ptr != nil {
|
|
||||||
return *ptr
|
|
||||||
}
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
|
|
||||||
// Equal returns true if both arguments are nil or both arguments
|
|
||||||
// dereference to the same value.
|
|
||||||
func Equal[T comparable](a, b *T) bool {
|
|
||||||
if (a == nil) != (b == nil) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if a == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return *a == *b
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
37
pkg/tool/tool.go
Normal file
37
pkg/tool/tool.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package tool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/flag"
|
||||||
|
"github.com/mark3labs/mcp-go/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tool struct {
|
||||||
|
write []server.ServerTool
|
||||||
|
read []server.ServerTool
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Tool {
|
||||||
|
return &Tool{
|
||||||
|
write: make([]server.ServerTool, 0, 100),
|
||||||
|
read: make([]server.ServerTool, 0, 100),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tool) RegisterWrite(s server.ServerTool) {
|
||||||
|
t.write = append(t.write, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tool) RegisterRead(s server.ServerTool) {
|
||||||
|
t.read = append(t.read, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tool) Tools() []server.ServerTool {
|
||||||
|
tools := make([]server.ServerTool, 0, len(t.write)+len(t.read))
|
||||||
|
if flag.ReadOnly {
|
||||||
|
tools = append(tools, t.read...)
|
||||||
|
return tools
|
||||||
|
}
|
||||||
|
tools = append(tools, t.write...)
|
||||||
|
tools = append(tools, t.read...)
|
||||||
|
return tools
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user