go-forge/pulls.go
Virgil 9cdab89c6c Fix service path segment escaping
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-23 06:47:10 +00:00

88 lines
3.1 KiB
Go

package forge
import (
"context"
"iter"
"strconv"
"dappco.re/go/core/forge/types"
)
// PullService handles pull request operations within a repository.
type PullService struct {
Resource[types.PullRequest, types.CreatePullRequestOption, types.EditPullRequestOption]
}
func newPullService(c *Client) *PullService {
return &PullService{
Resource: *NewResource[types.PullRequest, types.CreatePullRequestOption, types.EditPullRequestOption](
c, "/api/v1/repos/{owner}/{repo}/pulls/{index}",
),
}
}
// Merge merges a pull request. Method is one of "merge", "rebase", "rebase-merge", "squash", "fast-forward-only", "manually-merged".
func (s *PullService) Merge(ctx context.Context, owner, repo string, index int64, method string) error {
path := escapePathSegments("/api/v1/repos", owner, repo, "pulls", strconv.FormatInt(index, 10), "merge")
body := map[string]string{"Do": method}
return s.client.Post(ctx, path, body, nil)
}
// Update updates a pull request branch with the base branch.
func (s *PullService) Update(ctx context.Context, owner, repo string, index int64) error {
path := escapePathSegments("/api/v1/repos", owner, repo, "pulls", strconv.FormatInt(index, 10), "update")
return s.client.Post(ctx, path, nil, nil)
}
// ListReviews returns all reviews on a pull request.
func (s *PullService) ListReviews(ctx context.Context, owner, repo string, index int64) ([]types.PullReview, error) {
path := escapePathSegments("/api/v1/repos", owner, repo, "pulls", strconv.FormatInt(index, 10), "reviews")
return ListAll[types.PullReview](ctx, s.client, path, nil)
}
// IterReviews returns an iterator over all reviews on a pull request.
func (s *PullService) IterReviews(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.PullReview, error] {
path := escapePathSegments("/api/v1/repos", owner, repo, "pulls", strconv.FormatInt(index, 10), "reviews")
return ListIter[types.PullReview](ctx, s.client, path, nil)
}
// SubmitReview creates a new review on a pull request.
func (s *PullService) SubmitReview(ctx context.Context, owner, repo string, index int64, review map[string]any) (*types.PullReview, error) {
path := escapePathSegments("/api/v1/repos", owner, repo, "pulls", strconv.FormatInt(index, 10), "reviews")
var out types.PullReview
if err := s.client.Post(ctx, path, review, &out); err != nil {
return nil, err
}
return &out, nil
}
// DismissReview dismisses a pull request review.
func (s *PullService) DismissReview(ctx context.Context, owner, repo string, index, reviewID int64, msg string) error {
path := escapePathSegments(
"/api/v1/repos",
owner,
repo,
"pulls",
strconv.FormatInt(index, 10),
"reviews",
strconv.FormatInt(reviewID, 10),
"dismissals",
)
body := map[string]string{"message": msg}
return s.client.Post(ctx, path, body, nil)
}
// UndismissReview undismisses a pull request review.
func (s *PullService) UndismissReview(ctx context.Context, owner, repo string, index, reviewID int64) error {
path := escapePathSegments(
"/api/v1/repos",
owner,
repo,
"pulls",
strconv.FormatInt(index, 10),
"reviews",
strconv.FormatInt(reviewID, 10),
"undismissals",
)
return s.client.Post(ctx, path, nil, nil)
}