This commit is contained in:
8
operation/actions/actions.go
Normal file
8
operation/actions/actions.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"gitea.com/gitea/gitea-mcp/pkg/tool"
|
||||
)
|
||||
|
||||
// Tool is the registry for all Actions-related MCP tools.
|
||||
var Tool = tool.New()
|
||||
534
operation/actions/config.go
Normal file
534
operation/actions/config.go
Normal file
@@ -0,0 +1,534 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/annotation"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/params"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
||||
|
||||
gitea_sdk "gitea.dev/sdk"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
)
|
||||
|
||||
const (
|
||||
ActionsConfigReadToolName = "actions_config_read"
|
||||
ActionsConfigWriteToolName = "actions_config_write"
|
||||
)
|
||||
|
||||
type secretMeta struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at,omitzero"`
|
||||
}
|
||||
|
||||
func toSecretMetas(secrets []*gitea_sdk.Secret) []secretMeta {
|
||||
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 metas
|
||||
}
|
||||
|
||||
var (
|
||||
ActionsConfigReadTool = mcp.NewTool(
|
||||
ActionsConfigReadToolName,
|
||||
mcp.WithDescription("Read Actions secrets and variables."),
|
||||
mcp.WithToolAnnotation(annotation.ReadOnly("Read Actions secrets and variables")),
|
||||
mcp.WithString("method", mcp.Required(), mcp.Enum("list_repo_secrets", "list_org_secrets", "list_repo_variables", "get_repo_variable", "list_org_variables", "get_org_variable")),
|
||||
mcp.WithString("owner", mcp.Description("for repo methods")),
|
||||
mcp.WithString("repo", mcp.Description("for repo methods")),
|
||||
mcp.WithString("org", mcp.Description("for org methods")),
|
||||
mcp.WithString("name", mcp.Description("for get methods")),
|
||||
mcp.WithNumber("page", mcp.Description(params.PageDesc), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||
mcp.WithNumber("per_page", mcp.Description(params.PaginationDesc), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
)
|
||||
|
||||
ActionsConfigWriteTool = mcp.NewTool(
|
||||
ActionsConfigWriteToolName,
|
||||
mcp.WithDescription("Write Actions secrets and variables: upsert, create, update, delete."),
|
||||
mcp.WithToolAnnotation(annotation.Destructive("Manage Actions secrets and variables")),
|
||||
mcp.WithString("method", mcp.Required(), mcp.Enum("upsert_repo_secret", "delete_repo_secret", "upsert_org_secret", "delete_org_secret", "create_repo_variable", "update_repo_variable", "delete_repo_variable", "create_org_variable", "update_org_variable", "delete_org_variable")),
|
||||
mcp.WithString("owner", mcp.Description("for repo methods")),
|
||||
mcp.WithString("repo", mcp.Description("for repo methods")),
|
||||
mcp.WithString("org", mcp.Description("for org methods")),
|
||||
mcp.WithString("name", mcp.Description("secret or variable name")),
|
||||
mcp.WithString("data", mcp.Description("secret value (upsert)")),
|
||||
mcp.WithString("value", mcp.Description("variable value")),
|
||||
mcp.WithString("description"),
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ActionsConfigReadTool, Handler: configReadFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: ActionsConfigWriteTool, Handler: configWriteFn})
|
||||
}
|
||||
|
||||
func configReadFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
method, err := params.GetString(req.GetArguments(), "method")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
switch method {
|
||||
case "list_repo_secrets":
|
||||
return listRepoActionSecretsFn(ctx, req)
|
||||
case "list_org_secrets":
|
||||
return listOrgActionSecretsFn(ctx, req)
|
||||
case "list_repo_variables":
|
||||
return listRepoActionVariablesFn(ctx, req)
|
||||
case "get_repo_variable":
|
||||
return getRepoActionVariableFn(ctx, req)
|
||||
case "list_org_variables":
|
||||
return listOrgActionVariablesFn(ctx, req)
|
||||
case "get_org_variable":
|
||||
return getOrgActionVariableFn(ctx, req)
|
||||
default:
|
||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||
}
|
||||
}
|
||||
|
||||
func configWriteFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
method, err := params.GetString(req.GetArguments(), "method")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
switch method {
|
||||
case "upsert_repo_secret":
|
||||
return upsertRepoActionSecretFn(ctx, req)
|
||||
case "delete_repo_secret":
|
||||
return deleteRepoActionSecretFn(ctx, req)
|
||||
case "upsert_org_secret":
|
||||
return upsertOrgActionSecretFn(ctx, req)
|
||||
case "delete_org_secret":
|
||||
return deleteOrgActionSecretFn(ctx, req)
|
||||
case "create_repo_variable":
|
||||
return createRepoActionVariableFn(ctx, req)
|
||||
case "update_repo_variable":
|
||||
return updateRepoActionVariableFn(ctx, req)
|
||||
case "delete_repo_variable":
|
||||
return deleteRepoActionVariableFn(ctx, req)
|
||||
case "create_org_variable":
|
||||
return createOrgActionVariableFn(ctx, req)
|
||||
case "update_org_variable":
|
||||
return updateOrgActionVariableFn(ctx, req)
|
||||
case "delete_org_variable":
|
||||
return deleteOrgActionVariableFn(ctx, req)
|
||||
default:
|
||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||
}
|
||||
}
|
||||
|
||||
func listRepoActionSecretsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
|
||||
secrets, _, err := client.Actions.ListRepoSecrets(ctx, owner, repo, gitea_sdk.ListRepoActionsSecretOption{
|
||||
ListOptions: gitea_sdk.ListOptions{Page: page, PageSize: pageSize},
|
||||
})
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("list repo action secrets err: %v", err))
|
||||
}
|
||||
|
||||
return to.TextResult(toSecretMetas(secrets))
|
||||
}
|
||||
|
||||
func upsertRepoActionSecretFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
data, err := params.GetString(req.GetArguments(), "data")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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.Actions.CreateRepoSecret(ctx, owner, repo, name, gitea_sdk.CreateOrUpdateSecretOption{
|
||||
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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
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))
|
||||
}
|
||||
resp, err := client.Actions.DeleteRepoSecret(ctx, owner, repo, name)
|
||||
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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
|
||||
secrets, _, err := client.Actions.ListOrgSecrets(ctx, org, gitea_sdk.ListOrgActionsSecretOption{
|
||||
ListOptions: gitea_sdk.ListOptions{Page: page, PageSize: pageSize},
|
||||
})
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("list org action secrets err: %v", err))
|
||||
}
|
||||
|
||||
return to.TextResult(toSecretMetas(secrets))
|
||||
}
|
||||
|
||||
func upsertOrgActionSecretFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
data, err := params.GetString(req.GetArguments(), "data")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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.Actions.CreateOrgSecret(ctx, org, name, gitea_sdk.CreateOrUpdateSecretOption{
|
||||
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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
|
||||
escapedOrg := url.PathEscape(org)
|
||||
escapedSecret := url.PathEscape(name)
|
||||
_, 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"})
|
||||
}
|
||||
|
||||
func listRepoActionVariablesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
query.Set("limit", strconv.Itoa(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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
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))
|
||||
}
|
||||
variable, _, err := client.Actions.GetRepoVariable(ctx, 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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
value, err := params.GetString(req.GetArguments(), "value")
|
||||
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))
|
||||
}
|
||||
resp, err := client.Actions.CreateRepoVariable(ctx, 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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
value, err := params.GetString(req.GetArguments(), "value")
|
||||
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))
|
||||
}
|
||||
resp, err := client.Actions.UpdateRepoVariable(ctx, 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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
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))
|
||||
}
|
||||
resp, err := client.Actions.DeleteRepoVariable(ctx, 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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
variables, _, err := client.Actions.ListOrgVariables(ctx, org, gitea_sdk.ListOrgActionsVariableOption{
|
||||
ListOptions: gitea_sdk.ListOptions{Page: page, PageSize: 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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
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))
|
||||
}
|
||||
variable, _, err := client.Actions.GetOrgVariable(ctx, 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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
value, err := params.GetString(req.GetArguments(), "value")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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.Actions.CreateOrgVariable(ctx, org, name, gitea_sdk.CreateActionsVariableOption{
|
||||
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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
value, err := params.GetString(req.GetArguments(), "value")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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.Actions.UpdateOrgVariable(ctx, org, name, gitea_sdk.UpdateActionsVariableOption{
|
||||
Name: name,
|
||||
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) {
|
||||
org, err := params.GetString(req.GetArguments(), "org")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
name, err := params.GetString(req.GetArguments(), "name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
|
||||
_, 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"})
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
538
operation/actions/runs.go
Normal file
538
operation/actions/runs.go
Normal file
@@ -0,0 +1,538 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/annotation"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/params"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
)
|
||||
|
||||
const (
|
||||
ActionsRunReadToolName = "actions_run_read"
|
||||
ActionsRunWriteToolName = "actions_run_write"
|
||||
)
|
||||
|
||||
var (
|
||||
ActionsRunReadTool = mcp.NewTool(
|
||||
ActionsRunReadToolName,
|
||||
mcp.WithDescription("Read Actions workflows, runs, jobs, and logs."),
|
||||
mcp.WithToolAnnotation(annotation.ReadOnly("Read Actions workflow, run, and job data")),
|
||||
mcp.WithString("method", mcp.Required(), mcp.Enum("list_workflows", "get_workflow", "list_runs", "get_run", "list_jobs", "list_run_jobs", "get_job_log_preview", "download_job_log")),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description(params.OwnerDesc)),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description(params.RepoDesc)),
|
||||
mcp.WithString("workflow_id", mcp.Description("ID or filename (for 'get_workflow')")),
|
||||
mcp.WithNumber("run_id", mcp.Description("for 'get_run'/'list_run_jobs'")),
|
||||
mcp.WithNumber("job_id", mcp.Description("for log methods")),
|
||||
mcp.WithString("status", mcp.Description("filter for 'list_runs'/'list_jobs'")),
|
||||
mcp.WithNumber("tail_lines", mcp.Description("log tail lines"), mcp.DefaultNumber(200), mcp.Min(1)),
|
||||
mcp.WithNumber("max_bytes", mcp.Description("max log bytes"), mcp.DefaultNumber(65536), mcp.Min(1024)),
|
||||
mcp.WithString("output_path", mcp.Description("for 'download_job_log'")),
|
||||
mcp.WithNumber("page", mcp.Description(params.PageDesc), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||
mcp.WithNumber("per_page", mcp.Description(params.PaginationDesc), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
)
|
||||
|
||||
ActionsRunWriteTool = mcp.NewTool(
|
||||
ActionsRunWriteToolName,
|
||||
mcp.WithDescription("Write Actions runs: dispatch, cancel, rerun."),
|
||||
mcp.WithToolAnnotation(annotation.Write("Trigger, cancel, or rerun Actions workflows")),
|
||||
mcp.WithString("method", mcp.Required(), mcp.Enum("dispatch_workflow", "cancel_run", "rerun_run")),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description(params.OwnerDesc)),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description(params.RepoDesc)),
|
||||
mcp.WithString("workflow_id", mcp.Description("ID or filename (for 'dispatch_workflow')")),
|
||||
mcp.WithString("ref", mcp.Description("branch or tag (for 'dispatch_workflow')")),
|
||||
mcp.WithObject("inputs", mcp.Description("for 'dispatch_workflow'")),
|
||||
mcp.WithNumber("run_id", mcp.Description("for 'cancel_run'/'rerun_run'")),
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ActionsRunReadTool, Handler: runReadFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: ActionsRunWriteTool, Handler: runWriteFn})
|
||||
}
|
||||
|
||||
func runReadFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
method, err := params.GetString(req.GetArguments(), "method")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
switch method {
|
||||
case "list_workflows":
|
||||
return listRepoActionWorkflowsFn(ctx, req)
|
||||
case "get_workflow":
|
||||
return getRepoActionWorkflowFn(ctx, req)
|
||||
case "list_runs":
|
||||
return listRepoActionRunsFn(ctx, req)
|
||||
case "get_run":
|
||||
return getRepoActionRunFn(ctx, req)
|
||||
case "list_jobs":
|
||||
return listRepoActionJobsFn(ctx, req)
|
||||
case "list_run_jobs":
|
||||
return listRepoActionRunJobsFn(ctx, req)
|
||||
case "get_job_log_preview":
|
||||
return getRepoActionJobLogPreviewFn(ctx, req)
|
||||
case "download_job_log":
|
||||
return downloadRepoActionJobLogFn(ctx, req)
|
||||
default:
|
||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||
}
|
||||
}
|
||||
|
||||
func runWriteFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
method, err := params.GetString(req.GetArguments(), "method")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
switch method {
|
||||
case "dispatch_workflow":
|
||||
return dispatchRepoActionWorkflowFn(ctx, req)
|
||||
case "cancel_run":
|
||||
return cancelRepoActionRunFn(ctx, req)
|
||||
case "rerun_run":
|
||||
return rerunRepoActionRunFn(ctx, req)
|
||||
default:
|
||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
query := url.Values{}
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
query.Set("limit", strconv.Itoa(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(slimActionWorkflows(result))
|
||||
}
|
||||
|
||||
func getRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
workflowID, err := params.GetString(req.GetArguments(), "workflow_id")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
|
||||
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(slimActionWorkflow(result))
|
||||
}
|
||||
|
||||
func dispatchRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
workflowID, err := params.GetString(req.GetArguments(), "workflow_id")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
ref, err := params.GetString(req.GetArguments(), "ref")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
|
||||
var inputs map[string]any
|
||||
if raw, exists := req.GetArguments()["inputs"]; exists {
|
||||
if m, ok := raw.(map[string]any); ok {
|
||||
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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
statusFilter, _ := req.GetArguments()["status"].(string)
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
query.Set("limit", strconv.Itoa(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(slimActionRuns(result))
|
||||
}
|
||||
|
||||
func getRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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(slimActionRun(result))
|
||||
}
|
||||
|
||||
func cancelRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
statusFilter, _ := req.GetArguments()["status"].(string)
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
query.Set("limit", strconv.Itoa(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(slimActionJobs(result))
|
||||
}
|
||||
|
||||
func listRepoActionRunJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
runID, err := params.GetIndex(req.GetArguments(), "run_id")
|
||||
if err != nil || runID <= 0 {
|
||||
return to.ErrorResult(errors.New("run_id is required"))
|
||||
}
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), 30)
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
query.Set("limit", strconv.Itoa(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(slimActionJobs(result))
|
||||
}
|
||||
|
||||
func logPaths(owner, repo string, jobID int64) []string {
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
return data[len(data)-maxBytes:], true
|
||||
}
|
||||
|
||||
func getRepoActionJobLogPreviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
jobID, err := params.GetIndex(req.GetArguments(), "job_id")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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) {
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
jobID, err := params.GetIndex(req.GetArguments(), "job_id")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
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),
|
||||
})
|
||||
}
|
||||
92
operation/actions/slim.go
Normal file
92
operation/actions/slim.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package actions
|
||||
|
||||
func pick(m map[string]any, keys ...string) map[string]any {
|
||||
out := make(map[string]any, len(keys))
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
out[k] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func slimPaginated(raw any, itemFn func(map[string]any) map[string]any) any {
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return raw
|
||||
}
|
||||
result := make(map[string]any)
|
||||
if tc, ok := m["total_count"]; ok {
|
||||
result["total_count"] = tc
|
||||
}
|
||||
for key, val := range m {
|
||||
if key == "total_count" {
|
||||
continue
|
||||
}
|
||||
arr, ok := val.([]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
slimmed := make([]any, 0, len(arr))
|
||||
for _, item := range arr {
|
||||
if im, ok := item.(map[string]any); ok {
|
||||
slimmed = append(slimmed, itemFn(im))
|
||||
}
|
||||
}
|
||||
result[key] = slimmed
|
||||
break
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func slimRun(m map[string]any) map[string]any {
|
||||
return pick(m, "id", "name", "head_branch", "head_sha", "run_number",
|
||||
"event", "status", "conclusion", "workflow_id",
|
||||
"html_url", "created_at", "updated_at")
|
||||
}
|
||||
|
||||
func slimJob(m map[string]any) map[string]any {
|
||||
out := pick(m, "id", "run_id", "name", "workflow_name",
|
||||
"status", "conclusion", "html_url",
|
||||
"started_at", "completed_at")
|
||||
if steps, ok := m["steps"].([]any); ok {
|
||||
slim := make([]any, 0, len(steps))
|
||||
for _, s := range steps {
|
||||
if sm, ok := s.(map[string]any); ok {
|
||||
slim = append(slim, pick(sm, "name", "number", "status", "conclusion"))
|
||||
}
|
||||
}
|
||||
out["steps"] = slim
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func slimWorkflow(m map[string]any) map[string]any {
|
||||
return pick(m, "id", "name", "path", "state", "html_url", "created_at", "updated_at")
|
||||
}
|
||||
|
||||
func slimActionRun(raw any) any {
|
||||
if m, ok := raw.(map[string]any); ok {
|
||||
return slimRun(m)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func slimActionRuns(raw any) any {
|
||||
return slimPaginated(raw, slimRun)
|
||||
}
|
||||
|
||||
func slimActionJobs(raw any) any {
|
||||
return slimPaginated(raw, slimJob)
|
||||
}
|
||||
|
||||
func slimActionWorkflow(raw any) any {
|
||||
if m, ok := raw.(map[string]any); ok {
|
||||
return slimWorkflow(m)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func slimActionWorkflows(raw any) any {
|
||||
return slimPaginated(raw, slimWorkflow)
|
||||
}
|
||||
Reference in New Issue
Block a user