feat(tools): pr_create with identity footer
This commit is contained in:
66
internal/gitea/pulls.go
Normal file
66
internal/gitea/pulls.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PullRequest struct {
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
State string `json:"state"`
|
||||
Draft bool `json:"draft"`
|
||||
Head struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"head"`
|
||||
Base struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"base"`
|
||||
}
|
||||
|
||||
type CreatePullRequestArgs struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Head string `json:"head"`
|
||||
Base string `json:"base"`
|
||||
Draft bool `json:"draft"`
|
||||
}
|
||||
|
||||
func (c *Client) CreatePullRequest(ctx context.Context, owner, repo string, args CreatePullRequestArgs) (*PullRequest, error) {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner, repo)
|
||||
payload, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body, status, err := c.PostJSON(ctx, p, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := MapStatus(status, body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pr PullRequest
|
||||
if err := json.Unmarshal(body, &pr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pr, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetPullRequest(ctx context.Context, owner, repo string, index int) (*PullRequest, error) {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index)
|
||||
body, status, err := c.GetJSON(ctx, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := MapStatus(status, body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pr PullRequest
|
||||
if err := json.Unmarshal(body, &pr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pr, nil
|
||||
}
|
||||
95
internal/gitea/pulls_test.go
Normal file
95
internal/gitea/pulls_test.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package gitea_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const pullFixture = `{
|
||||
"number": 7,
|
||||
"title": "Add feature X",
|
||||
"body": "This PR adds feature X",
|
||||
"html_url": "http://example.com/pulls/7",
|
||||
"state": "open",
|
||||
"draft": false,
|
||||
"head": {"ref": "feat/x"},
|
||||
"base": {"ref": "main"}
|
||||
}`
|
||||
|
||||
func TestCreatePullRequestSendsPayload(t *testing.T) {
|
||||
var captured []byte
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/repos/o/r/pulls", r.URL.Path)
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
var err error
|
||||
captured, err = io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, _ = w.Write([]byte(pullFixture))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
pr, err := c.CreatePullRequest(context.Background(), "o", "r", gitea.CreatePullRequestArgs{
|
||||
Title: "Add feature X",
|
||||
Body: "This PR adds feature X",
|
||||
Head: "feat/x",
|
||||
Base: "main",
|
||||
Draft: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(captured, &payload))
|
||||
assert.Equal(t, "Add feature X", payload["title"])
|
||||
assert.Equal(t, "This PR adds feature X", payload["body"])
|
||||
assert.Equal(t, "feat/x", payload["head"])
|
||||
assert.Equal(t, "main", payload["base"])
|
||||
assert.Equal(t, false, payload["draft"])
|
||||
|
||||
assert.Equal(t, 7, pr.Number)
|
||||
assert.Equal(t, "Add feature X", pr.Title)
|
||||
assert.Equal(t, "http://example.com/pulls/7", pr.HTMLURL)
|
||||
assert.Equal(t, "feat/x", pr.Head.Ref)
|
||||
assert.Equal(t, "main", pr.Base.Ref)
|
||||
assert.Equal(t, "open", pr.State)
|
||||
assert.False(t, pr.Draft)
|
||||
}
|
||||
|
||||
func TestGetPullRequest(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/repos/o/r/pulls/42", r.URL.Path)
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{
|
||||
"number": 42,
|
||||
"title": "Fix bug Y",
|
||||
"body": "Fixes Y",
|
||||
"html_url": "http://example.com/pulls/42",
|
||||
"state": "open",
|
||||
"draft": true,
|
||||
"head": {"ref": "fix/y"},
|
||||
"base": {"ref": "main"}
|
||||
}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
pr, err := c.GetPullRequest(context.Background(), "o", "r", 42)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 42, pr.Number)
|
||||
assert.Equal(t, "Fix bug Y", pr.Title)
|
||||
assert.Equal(t, "http://example.com/pulls/42", pr.HTMLURL)
|
||||
assert.Equal(t, "fix/y", pr.Head.Ref)
|
||||
assert.Equal(t, "main", pr.Base.Ref)
|
||||
assert.Equal(t, "open", pr.State)
|
||||
assert.True(t, pr.Draft)
|
||||
}
|
||||
Reference in New Issue
Block a user