mirror of
https://gitea.com/gitea/tea.git
synced 2026-04-05 16:03:32 +02:00
## Summary - Add `tea pr edit` subcommand to support editing pull request properties (description, title, milestone, deadline, assignees, labels, reviewers) - Add `--add-reviewers` / `--remove-reviewers` flags for managing PR reviewers via `CreateReviewRequests` / `DeleteReviewRequests` API - Extract shared helpers (`ResolveLabelOpts`, `ApplyLabelChanges`, `ApplyReviewerChanges`, `ResolveMilestoneID`) into `modules/task/labels.go` to reduce duplication between issue and PR editing - Refactor existing `EditIssue` to use the same shared helpers - Wrap original error in `ResolveMilestoneID` to preserve underlying error context ## Usage ```bash # Edit PR description tea pr edit 1 --description "new description" # Edit PR title tea pr edit 1 --title "new title" # Edit multiple fields tea pr edit 1 --title "new title" --description "new desc" --add-labels "bug" # Edit multiple PRs tea pr edit 1 2 3 --add-assignees "user1" # Add reviewers tea pr edit 1 --add-reviewers "user1,user2" # Remove reviewers tea pr edit 1 --remove-reviewers "user1" ``` ## Test plan - [x] `go build .` succeeds - [x] `go test ./...` passes - [x] `make clean && make vet && make lint && make fmt-check && make docs-check && make build` all pass - [x] `tea pr edit <idx> --description "test"` updates PR description on a Gitea instance - [x] `tea pr edit <idx> --title "test"` updates PR title - [x] `tea pr edit <idx> --add-labels "bug"` adds label - [x] `tea pr edit <idx> --add-reviewers "user"` requests review - [x] `tea pr edit <idx> --remove-reviewers "user"` removes reviewer - [x] Existing `tea issues edit` still works correctly after refactor Reviewed-on: https://gitea.com/gitea/tea/pulls/944 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: appleboy <appleboy.tw@gmail.com> Co-committed-by: appleboy <appleboy.tw@gmail.com>
80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package task
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"code.gitea.io/tea/modules/context"
|
|
)
|
|
|
|
// EditPull edits a pull request and returns the updated pull request.
|
|
func EditPull(ctx *context.TeaContext, client *gitea.Client, opts EditIssueOption) (*gitea.PullRequest, error) {
|
|
if client == nil {
|
|
client = ctx.Login.Client()
|
|
}
|
|
|
|
addLabelOpts, err := ResolveLabelOpts(client, ctx.Owner, ctx.Repo, opts.AddLabels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rmLabelOpts, err := ResolveLabelOpts(client, ctx.Owner, ctx.Repo, opts.RemoveLabels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
prOpts := gitea.EditPullRequestOption{}
|
|
var prOptsDirty bool
|
|
if opts.Title != nil {
|
|
prOpts.Title = *opts.Title
|
|
prOptsDirty = true
|
|
}
|
|
if opts.Body != nil {
|
|
prOpts.Body = opts.Body
|
|
prOptsDirty = true
|
|
}
|
|
if opts.Milestone != nil {
|
|
id, err := ResolveMilestoneID(client, ctx.Owner, ctx.Repo, *opts.Milestone)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
prOpts.Milestone = id
|
|
prOptsDirty = true
|
|
}
|
|
if opts.Deadline != nil {
|
|
prOpts.Deadline = opts.Deadline
|
|
prOptsDirty = true
|
|
if opts.Deadline.IsZero() {
|
|
prOpts.RemoveDeadline = gitea.OptionalBool(true)
|
|
}
|
|
}
|
|
if len(opts.AddAssignees) != 0 {
|
|
prOpts.Assignees = opts.AddAssignees
|
|
prOptsDirty = true
|
|
}
|
|
|
|
if err := ApplyLabelChanges(client, ctx.Owner, ctx.Repo, opts.Index, addLabelOpts, rmLabelOpts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := ApplyReviewerChanges(client, ctx.Owner, ctx.Repo, opts.Index, opts.AddReviewers, opts.RemoveReviewers); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var pr *gitea.PullRequest
|
|
if prOptsDirty {
|
|
pr, _, err = client.EditPullRequest(ctx.Owner, ctx.Repo, opts.Index, prOpts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not edit pull request: %s", err)
|
|
}
|
|
} else {
|
|
pr, _, err = client.GetPullRequest(ctx.Owner, ctx.Repo, opts.Index)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get pull request: %s", err)
|
|
}
|
|
}
|
|
return pr, nil
|
|
}
|