2018-09-03 08:43:00 +02:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2023-09-08 03:40:02 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-09-03 08:43:00 +02:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-12-08 05:06:05 +01:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-30 07:11:33 +02:00
|
|
|
"code.gitea.io/tea/cmd/pulls"
|
2020-12-15 18:38:22 +01:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-12-21 17:07:35 +01:00
|
|
|
"code.gitea.io/tea/modules/interact"
|
2020-10-02 16:15:18 +02:00
|
|
|
"code.gitea.io/tea/modules/print"
|
2020-09-30 07:11:33 +02:00
|
|
|
"code.gitea.io/tea/modules/utils"
|
2021-03-07 20:45:50 +01:00
|
|
|
"code.gitea.io/tea/modules/workaround"
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2020-12-08 05:06:05 +01:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-01-04 18:44:25 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-09-03 08:43:00 +02:00
|
|
|
)
|
|
|
|
|
2020-04-19 05:09:03 +02:00
|
|
|
// CmdPulls is the main command to operate on PRs
|
2018-09-03 08:43:00 +02:00
|
|
|
var CmdPulls = cli.Command{
|
|
|
|
Name: "pulls",
|
2020-04-19 05:09:03 +02:00
|
|
|
Aliases: []string{"pull", "pr"},
|
2020-12-21 14:37:20 +01:00
|
|
|
Category: catEntities,
|
2020-12-16 23:47:12 +01:00
|
|
|
Usage: "Manage and checkout pull requests",
|
2020-12-21 17:07:35 +01:00
|
|
|
Description: `Lists PRs when called without argument. If PR index is provided, will show it in detail.`,
|
2020-09-21 07:03:20 +02:00
|
|
|
ArgsUsage: "[<pull index>]",
|
2018-09-03 08:43:00 +02:00
|
|
|
Action: runPulls,
|
2020-12-21 17:07:35 +01:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "comments",
|
2022-02-01 00:11:37 +01:00
|
|
|
Usage: "Whether to display comments (will prompt if not provided & run interactively)",
|
2020-12-21 17:07:35 +01:00
|
|
|
},
|
|
|
|
}, pulls.CmdPullsList.Flags...),
|
2020-04-19 05:09:03 +02:00
|
|
|
Subcommands: []*cli.Command{
|
2020-09-30 07:11:33 +02:00
|
|
|
&pulls.CmdPullsList,
|
|
|
|
&pulls.CmdPullsCheckout,
|
|
|
|
&pulls.CmdPullsClean,
|
|
|
|
&pulls.CmdPullsCreate,
|
2020-12-16 23:47:12 +01:00
|
|
|
&pulls.CmdPullsClose,
|
|
|
|
&pulls.CmdPullsReopen,
|
2020-12-21 16:22:22 +01:00
|
|
|
&pulls.CmdPullsReview,
|
|
|
|
&pulls.CmdPullsApprove,
|
|
|
|
&pulls.CmdPullsReject,
|
2021-03-17 20:56:05 +01:00
|
|
|
&pulls.CmdPullsMerge,
|
2020-04-19 05:09:03 +02:00
|
|
|
},
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runPulls(ctx *cli.Context) error {
|
2020-09-21 07:03:20 +02:00
|
|
|
if ctx.Args().Len() == 1 {
|
2020-12-15 18:38:22 +01:00
|
|
|
return runPullDetail(ctx, ctx.Args().First())
|
2020-09-21 07:03:20 +02:00
|
|
|
}
|
2020-09-30 07:11:33 +02:00
|
|
|
return pulls.RunPullsList(ctx)
|
2020-09-21 07:03:20 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 18:38:22 +01:00
|
|
|
func runPullDetail(cmd *cli.Context, index string) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
2020-09-30 07:11:33 +02:00
|
|
|
idx, err := utils.ArgToIndex(index)
|
2020-09-21 07:03:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-08 05:06:05 +01:00
|
|
|
|
2020-12-15 18:38:22 +01:00
|
|
|
client := ctx.Login.Client()
|
|
|
|
pr, _, err := client.GetPullRequest(ctx.Owner, ctx.Repo, idx)
|
2020-09-21 07:03:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-07 20:45:50 +01:00
|
|
|
if err := workaround.FixPullHeadSha(client, pr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-21 07:03:20 +02:00
|
|
|
|
2022-04-07 21:44:16 +02:00
|
|
|
reviews, _, err := client.ListPullReviews(ctx.Owner, ctx.Repo, idx, gitea.ListPullReviewsOptions{
|
|
|
|
ListOptions: gitea.ListOptions{Page: -1},
|
|
|
|
})
|
2020-12-08 05:06:05 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error while loading reviews: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:16:50 +01:00
|
|
|
ci, _, err := client.GetCombinedStatus(ctx.Owner, ctx.Repo, pr.Head.Sha)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error while loading CI: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
print.PullDetails(pr, reviews, ci)
|
2020-12-21 17:07:35 +01:00
|
|
|
|
|
|
|
if pr.Comments > 0 {
|
|
|
|
err = interact.ShowCommentsMaybeInteractive(ctx, idx, pr.Comments)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error loading comments: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 07:03:20 +02:00
|
|
|
return nil
|
|
|
|
}
|