Skip to content

Commit

Permalink
pr edit - auto select pr for current branch if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
3ximus committed Feb 5, 2024
1 parent ec08eaa commit ecda880
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmd/pr/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var EditCmd = &cobra.Command{
Long: `Allows edits to an existing pull request
If no options are given to edit title or description it will open your EDITOR to write any changes to them.
By default title is on first line and description on the lines bellow`,
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: func(comd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var opt = []string{}
for pr := range api.GetPrList(util.GetCurrentRepo(), []string{string(api.OPEN)}, "", "", "", "", 1, false, false) {
Expand All @@ -28,12 +28,24 @@ var EditCmd = &cobra.Command{
return opt, cobra.ShellCompDirectiveDefault
},
Run: func(cmd *cobra.Command, args []string) {
// TODO try reading pr from current branch
id, err := strconv.Atoi(args[0])
cobra.CheckErr(err)

repo := viper.GetString("repo")

var id int
var err error
if len(args) == 0 {
branch, err := util.GetCurrentBranch()
cobra.CheckErr(err)
// retrieve id of pr for current branch
pr := <-api.GetPrList(repo, []string{string(api.OPEN), string(api.MERGED), string(api.DECLINED), string(api.SUPERSEDED)}, "", "", branch, "", 1, false, false)
if pr.ID == 0 {
cobra.CheckErr("No pr found for this branch")
}
id = pr.ID // get the first one's ID
} else {
id, err = strconv.Atoi(args[0])
cobra.CheckErr(err)
}

title, _ := cmd.Flags().GetString("title")
description, _ := cmd.Flags().GetString("body")
source, _ := cmd.Flags().GetString("source")
Expand Down

0 comments on commit ecda880

Please sign in to comment.