feat(tools): pr_merge
This commit is contained in:
@@ -103,6 +103,25 @@ func (c *Client) GetPullRequestDiff(ctx context.Context, owner, repo string, ind
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
type MergePRArgs struct {
|
||||
Do string `json:"Do"`
|
||||
Title string `json:"merge_message_title,omitempty"`
|
||||
Body string `json:"merge_message_field,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) MergePullRequest(ctx context.Context, owner, repo string, index int, args MergePRArgs) error {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner, repo, index)
|
||||
payload, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body, status, err := c.PostJSON(ctx, p, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return MapStatus(status, body)
|
||||
}
|
||||
|
||||
func (c *Client) ListPullRequests(ctx context.Context, owner, repo, state, head string, page, limit int) ([]PullRequest, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
|
||||
@@ -154,3 +154,37 @@ func TestListPullRequests(t *testing.T) {
|
||||
assert.Equal(t, 7, prs[0].Number)
|
||||
assert.Equal(t, "feat/x", prs[0].Head.Ref)
|
||||
}
|
||||
|
||||
func TestMergePullRequestSuccess(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/7/merge", 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.StatusNoContent)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
err := c.MergePullRequest(context.Background(), "o", "r", 7, gitea.MergePRArgs{Do: "squash"})
|
||||
require.NoError(t, err)
|
||||
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(captured, &payload))
|
||||
assert.Equal(t, "squash", payload["Do"])
|
||||
}
|
||||
|
||||
func TestMergePullRequestConflict(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_, _ = w.Write([]byte(`{"message":"merge conflict"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
err := c.MergePullRequest(context.Background(), "o", "r", 7, gitea.MergePRArgs{Do: "merge"})
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, gitea.ErrConflict)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user