From bff965e84ce3ccc2a887ecabee8f21fe24471726 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Wed, 4 Jan 2023 03:21:28 -0500 Subject: [PATCH 1/9] Support auto detecting branch for PRs --- cmd/pulls/merge.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index 3213db3..cf127dc 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -44,13 +44,27 @@ var CmdPullsMerge = cli.Command{ ctx := context.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) - if ctx.Args().Len() != 1 { - return fmt.Errorf("Must specify a PR index") - } + var idx int64 + var err error + if ctx.Args().Len() == 1 { + idx, err = utils.ArgToIndex(ctx.Args().First()) + if err != nil { + return err + } + } else { + if ctx.LocalRepo == nil { + return fmt.Errorf("Must specify a PR index") + } - idx, err := utils.ArgToIndex(ctx.Args().First()) - if err != nil { - return err + branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA() + if err != nil { + return err + } + + idx, err = GetPullIndexByBranch(ctx, branch) + if err != nil { + return err + } } success, _, err := ctx.Login.Client().MergePullRequest(ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ @@ -68,3 +82,19 @@ var CmdPullsMerge = cli.Command{ return nil }, } + +func GetPullIndexByBranch(ctx *context.TeaContext, branch string) (int64, error) { + prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ + State: gitea.StateOpen, + }) + if err != nil { + return 0, err + } + + for _, pr := range prs { + if pr.Head.Ref == branch { + return pr.Index, nil + } + } + return 0, fmt.Errorf("No open PR for branch %s", branch) +} -- 2.40.1 From e4ff7b8397a8e9c70ac9f22d7cd0caa1eddd991b Mon Sep 17 00:00:00 2001 From: harryzcy Date: Wed, 4 Jan 2023 14:29:08 -0500 Subject: [PATCH 2/9] Make function unexported --- cmd/pulls/merge.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index cf127dc..22ae452 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -61,7 +61,7 @@ var CmdPullsMerge = cli.Command{ return err } - idx, err = GetPullIndexByBranch(ctx, branch) + idx, err = getPullIndexByBranch(ctx, branch) if err != nil { return err } @@ -83,7 +83,7 @@ var CmdPullsMerge = cli.Command{ }, } -func GetPullIndexByBranch(ctx *context.TeaContext, branch string) (int64, error) { +func getPullIndexByBranch(ctx *context.TeaContext, branch string) (int64, error) { prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ State: gitea.StateOpen, }) -- 2.40.1 From 5a0ac6e1e851f455c1d46fd5d4dabbd25dfd9df3 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Sat, 7 Jan 2023 17:47:11 -0500 Subject: [PATCH 3/9] List multiple PRs and offer choice --- cmd/pulls/merge.go | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index 22ae452..e1785f0 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -6,12 +6,14 @@ package pulls import ( "fmt" + "strings" "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" "code.gitea.io/tea/modules/utils" + "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" ) @@ -61,7 +63,7 @@ var CmdPullsMerge = cli.Command{ return err } - idx, err = getPullIndexByBranch(ctx, branch) + idx, err = getPullIndex(ctx, branch) if err != nil { return err } @@ -83,18 +85,50 @@ var CmdPullsMerge = cli.Command{ }, } -func getPullIndexByBranch(ctx *context.TeaContext, branch string) (int64, error) { +func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) { prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ State: gitea.StateOpen, }) if err != nil { return 0, err } + if len(prs) == 0 { + return 0, fmt.Errorf("No open PRs found") + } + prOptions := make([]string, len(prs)) + + // get the PR index for the current branch first for _, pr := range prs { if pr.Head.Ref == branch { - return pr.Index, nil + prOptions[0] = fmt.Sprintf("#%d: %s", pr.Index, pr.Title) } } - return 0, fmt.Errorf("No open PR for branch %s", branch) + + // then get the rest of the PRs + i := 1 + for _, pr := range prs { + if pr.Head.Ref != branch { + prOptions[i] = fmt.Sprintf("#%d: %s", pr.Index, pr.Title) + } + i++ + } + + selected := "" + q := &survey.Select{ + Message: "Select a PR to merge", + Options: prOptions, + PageSize: 10, + } + survey.AskOne(q, &selected) + + // get the index from the selected option + before, _, _ := strings.Cut(selected, ":") + before = strings.TrimPrefix(before, "#") + idx, err := utils.ArgToIndex(before) + if err != nil { + return 0, err + } + + return idx, nil } -- 2.40.1 From 783d167f37f854d472f2fc8e6c30f93a0a79549a Mon Sep 17 00:00:00 2001 From: harryzcy Date: Tue, 10 Jan 2023 23:54:03 -0500 Subject: [PATCH 4/9] Update branch filtering --- cmd/pulls/merge.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index e1785f0..775dae5 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -96,22 +96,21 @@ func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) { return 0, fmt.Errorf("No open PRs found") } - prOptions := make([]string, len(prs)) + prOptions := make([]string, 0) - // get the PR index for the current branch first + // get the PR indexes where head branch is the current branch for _, pr := range prs { if pr.Head.Ref == branch { - prOptions[0] = fmt.Sprintf("#%d: %s", pr.Index, pr.Title) + prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title)) } } - // then get the rest of the PRs - i := 1 + // then get the PR indexes where base branch is the current branch for _, pr := range prs { - if pr.Head.Ref != branch { - prOptions[i] = fmt.Sprintf("#%d: %s", pr.Index, pr.Title) + // don't add the same PR twice, so `pr.Head.Ref != branch` + if pr.Base.Ref == branch && pr.Head.Ref != branch { + prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title)) } - i++ } selected := "" -- 2.40.1 From 26a5c6bc49967b76cf04affe424202437ebd2885 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Tue, 21 Feb 2023 14:48:06 -0500 Subject: [PATCH 5/9] Check command line prompt error --- cmd/pulls/merge.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index 775dae5..3a1e27b 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -119,7 +119,10 @@ func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) { Options: prOptions, PageSize: 10, } - survey.AskOne(q, &selected) + err = survey.AskOne(q, &selected) + if err != nil { + return 0, err + } // get the index from the selected option before, _, _ := strings.Cut(selected, ":") -- 2.40.1 From 86bb8dbcb82e1ebce1ecd9a8bf713ae55983d012 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Mon, 3 Apr 2023 18:49:10 -0400 Subject: [PATCH 6/9] Move interactive part to interact package --- cmd/pulls/merge.go | 81 ++------------------------------ modules/interact/pull_merge.go | 86 ++++++++++++++++++++++++++++++++++ modules/task/pull_merge.go | 21 +++++++++ 3 files changed, 112 insertions(+), 76 deletions(-) create mode 100644 modules/interact/pull_merge.go create mode 100644 modules/task/pull_merge.go diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index 3a1e27b..bb705e6 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -5,15 +5,13 @@ package pulls import ( - "fmt" - "strings" - "code.gitea.io/sdk/gitea" "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/interact" + "code.gitea.io/tea/modules/task" "code.gitea.io/tea/modules/utils" - "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" ) @@ -54,83 +52,14 @@ var CmdPullsMerge = cli.Command{ return err } } else { - if ctx.LocalRepo == nil { - return fmt.Errorf("Must specify a PR index") - } - - branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA() - if err != nil { - return err - } - - idx, err = getPullIndex(ctx, branch) - if err != nil { - return err - } + // If no PR index is provided, try interactive mode + return interact.MergePull(ctx) } - success, _, err := ctx.Login.Client().MergePullRequest(ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ + return task.PullMerge(ctx.Login, ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ Style: gitea.MergeStyle(ctx.String("style")), Title: ctx.String("title"), Message: ctx.String("message"), }) - - if err != nil { - return err - } - if !success { - return fmt.Errorf("Failed to merge PR. Is it still open?") - } - return nil }, } - -func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) { - prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ - State: gitea.StateOpen, - }) - if err != nil { - return 0, err - } - if len(prs) == 0 { - return 0, fmt.Errorf("No open PRs found") - } - - prOptions := make([]string, 0) - - // get the PR indexes where head branch is the current branch - for _, pr := range prs { - if pr.Head.Ref == branch { - prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title)) - } - } - - // then get the PR indexes where base branch is the current branch - for _, pr := range prs { - // don't add the same PR twice, so `pr.Head.Ref != branch` - if pr.Base.Ref == branch && pr.Head.Ref != branch { - prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title)) - } - } - - selected := "" - q := &survey.Select{ - Message: "Select a PR to merge", - Options: prOptions, - PageSize: 10, - } - err = survey.AskOne(q, &selected) - if err != nil { - return 0, err - } - - // get the index from the selected option - before, _, _ := strings.Cut(selected, ":") - before = strings.TrimPrefix(before, "#") - idx, err := utils.ArgToIndex(before) - if err != nil { - return 0, err - } - - return idx, nil -} diff --git a/modules/interact/pull_merge.go b/modules/interact/pull_merge.go new file mode 100644 index 0000000..651c6cf --- /dev/null +++ b/modules/interact/pull_merge.go @@ -0,0 +1,86 @@ +package interact + +import ( + "fmt" + "strings" + + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/task" + "code.gitea.io/tea/modules/utils" + "github.com/AlecAivazis/survey/v2" +) + +// MergePull interactively creates a PR +func MergePull(ctx *context.TeaContext) error { + if ctx.LocalRepo == nil { + return fmt.Errorf("Must specify a PR index") + } + + branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA() + if err != nil { + return err + } + + idx, err := getPullIndex(ctx, branch) + if err != nil { + return err + } + + return task.PullMerge(ctx.Login, ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ + Style: gitea.MergeStyle(ctx.String("style")), + Title: ctx.String("title"), + Message: ctx.String("message"), + }) +} + +// getPullIndex interactively determines the PR index +func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) { + prs, _, err := ctx.Login.Client().ListRepoPullRequests(ctx.Owner, ctx.Repo, gitea.ListPullRequestsOptions{ + State: gitea.StateOpen, + }) + if err != nil { + return 0, err + } + if len(prs) == 0 { + return 0, fmt.Errorf("No open PRs found") + } + + prOptions := make([]string, 0) + + // get the PR indexes where head branch is the current branch + for _, pr := range prs { + if pr.Head.Ref == branch { + prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title)) + } + } + + // then get the PR indexes where base branch is the current branch + for _, pr := range prs { + // don't add the same PR twice, so `pr.Head.Ref != branch` + if pr.Base.Ref == branch && pr.Head.Ref != branch { + prOptions = append(prOptions, fmt.Sprintf("#%d: %s", pr.Index, pr.Title)) + } + } + + selected := "" + q := &survey.Select{ + Message: "Select a PR to merge", + Options: prOptions, + PageSize: 10, + } + err = survey.AskOne(q, &selected) + if err != nil { + return 0, err + } + + // get the index from the selected option + before, _, _ := strings.Cut(selected, ":") + before = strings.TrimPrefix(before, "#") + idx, err := utils.ArgToIndex(before) + if err != nil { + return 0, err + } + + return idx, nil +} diff --git a/modules/task/pull_merge.go b/modules/task/pull_merge.go new file mode 100644 index 0000000..ad25540 --- /dev/null +++ b/modules/task/pull_merge.go @@ -0,0 +1,21 @@ +package task + +import ( + "fmt" + + "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/modules/config" +) + +// PullMerge merges a PR +func PullMerge(login *config.Login, repoOwner, repoName string, index int64, opt gitea.MergePullRequestOption) error { + client := login.Client() + success, _, err := client.MergePullRequest(repoOwner, repoName, index, opt) + if err != nil { + return err + } + if !success { + return fmt.Errorf("Failed to merge PR. Is it still open?") + } + return nil +} -- 2.40.1 From 3ce904ec7d90203bdcb3e17d840b4b02c3a1c276 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Mon, 17 Apr 2023 23:42:39 -0400 Subject: [PATCH 7/9] Add copyright to fix lint --- modules/interact/pull_merge.go | 4 ++++ modules/task/pull_merge.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/modules/interact/pull_merge.go b/modules/interact/pull_merge.go index 651c6cf..43f38df 100644 --- a/modules/interact/pull_merge.go +++ b/modules/interact/pull_merge.go @@ -1,3 +1,7 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package interact import ( diff --git a/modules/task/pull_merge.go b/modules/task/pull_merge.go index ad25540..8d5be0b 100644 --- a/modules/task/pull_merge.go +++ b/modules/task/pull_merge.go @@ -1,3 +1,7 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package task import ( -- 2.40.1 From 96cccedd3767ff5caa231edb1458d7652b3d6807 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Sat, 6 May 2023 14:01:45 -0400 Subject: [PATCH 8/9] Change copyright to 2023 --- modules/interact/pull_merge.go | 2 +- modules/task/pull_merge.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/interact/pull_merge.go b/modules/interact/pull_merge.go index 43f38df..d1c9e50 100644 --- a/modules/interact/pull_merge.go +++ b/modules/interact/pull_merge.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. +// Copyright 2023 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. diff --git a/modules/task/pull_merge.go b/modules/task/pull_merge.go index 8d5be0b..82e1af8 100644 --- a/modules/task/pull_merge.go +++ b/modules/task/pull_merge.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. +// Copyright 2023 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -- 2.40.1 From 9fdfb1debca52f14bffeb68290a99e1ceadd202a Mon Sep 17 00:00:00 2001 From: harryzcy Date: Sat, 6 May 2023 14:03:27 -0400 Subject: [PATCH 9/9] Readibility update --- cmd/pulls/merge.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/cmd/pulls/merge.go b/cmd/pulls/merge.go index bb705e6..829b271 100644 --- a/cmd/pulls/merge.go +++ b/cmd/pulls/merge.go @@ -44,18 +44,16 @@ var CmdPullsMerge = cli.Command{ ctx := context.InitCommand(cmd) ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) - var idx int64 - var err error - if ctx.Args().Len() == 1 { - idx, err = utils.ArgToIndex(ctx.Args().First()) - if err != nil { - return err - } - } else { + if ctx.Args().Len() != 1 { // If no PR index is provided, try interactive mode return interact.MergePull(ctx) } + idx, err := utils.ArgToIndex(ctx.Args().First()) + if err != nil { + return err + } + return task.PullMerge(ctx.Login, ctx.Owner, ctx.Repo, idx, gitea.MergePullRequestOption{ Style: gitea.MergeStyle(ctx.String("style")), Title: ctx.String("title"), -- 2.40.1