feat(tools): file_write_branch
Add BranchExists/CreateBranch/UpsertFile gitea client methods and the file_write_branch MCP tool. Branch is auto-created from base (or repo default_branch) when it doesn't exist; file is upserted via PUT contents. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -32,3 +32,82 @@ func (c *Client) GetFileContents(ctx context.Context, owner, repo, path, ref str
|
||||
}
|
||||
return &fc, nil
|
||||
}
|
||||
|
||||
type Branch struct {
|
||||
Name string `json:"name"`
|
||||
Commit struct {
|
||||
ID string `json:"id"`
|
||||
URL string `json:"url"`
|
||||
} `json:"commit"`
|
||||
}
|
||||
|
||||
// BranchExists returns (true, nil) if the branch exists, (false, nil) on 404, (false, err) otherwise.
|
||||
func (c *Client) BranchExists(ctx context.Context, owner, repo, branch string) (bool, error) {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", owner, repo, branch)
|
||||
body, status, err := c.GetJSON(ctx, p)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if status == 404 {
|
||||
return false, nil
|
||||
}
|
||||
if err := MapStatus(status, body); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateBranch(ctx context.Context, owner, repo, newBranch, oldBranch string) error {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/branches", owner, repo)
|
||||
payload, err := json.Marshal(map[string]string{
|
||||
"new_branch_name": newBranch,
|
||||
"old_branch_name": oldBranch,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body, status, err := c.PostJSON(ctx, p, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return MapStatus(status, body)
|
||||
}
|
||||
|
||||
type UpsertFileArgs struct {
|
||||
Branch string `json:"branch"`
|
||||
Content string `json:"content"` // already base64-encoded
|
||||
Message string `json:"message"`
|
||||
Sha string `json:"sha,omitempty"`
|
||||
}
|
||||
|
||||
type FileWriteResult struct {
|
||||
Content struct {
|
||||
Path string `json:"path"`
|
||||
Sha string `json:"sha"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
} `json:"content"`
|
||||
Commit struct {
|
||||
Sha string `json:"sha"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
} `json:"commit"`
|
||||
}
|
||||
|
||||
func (c *Client) UpsertFile(ctx context.Context, owner, repo, path string, args UpsertFileArgs) (*FileWriteResult, error) {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
|
||||
payload, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body, status, err := c.PutJSON(ctx, p, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := MapStatus(status, body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out FileWriteResult
|
||||
if err := json.Unmarshal(body, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user