diff --git a/cmd/cli/pr.go b/cmd/cli/pr.go new file mode 100644 index 0000000..a5fa6e8 --- /dev/null +++ b/cmd/cli/pr.go @@ -0,0 +1,112 @@ +/* +Copyright © 2025 NAME HERE +*/ +package cli + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/google/go-github/v57/github" + "github.com/spf13/cobra" +) + +type PRStructure struct { + branch string + title string + head string + body string +} + +// prCmd represents the pr command +var prCmd = &cobra.Command{ + Use: "pr", + Short: "Create a pull request", + Long: `Create a pull request`, + + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("pr called") + branch, err := cmd.Flags().GetString("branch") + if err != nil { + fmt.Println("Erro ao obter a flag 'branch':", err.Error()) + os.Exit(1) + } + if branch == "" { + fmt.Println("A flag 'branch' é obrigatória") + os.Exit(1) + } + + title, err := cmd.Flags().GetString("title") + if err != nil { + fmt.Println("Erro ao obter a flag 'title':", err.Error()) + os.Exit(1) + } + + if title == "" { + fmt.Println("A flag 'title' é obrigatória") + os.Exit(1) + } + + body, err := cmd.Flags().GetString("body") + if err != nil { + fmt.Println("Erro ao obter a flag 'body':", err.Error()) + os.Exit(1) + } + if body == "" { + fmt.Println("A flag 'body' é obrigatória") + os.Exit(1) + } + + head, err := cmd.Flags().GetString("head") + if err != nil { + fmt.Println("Erro ao obter a flag 'head':", err.Error()) + os.Exit(1) + } + + log.Println("Title:", title) + log.Println("Branch:", branch) + log.Println("Body:", body) + log.Println("Head:", head) + + pr := PRStructure{ + branch: branch, + title: title, + head: head, + body: body, + } + createPR(&pr) + }, +} + +func init() { + + rootCmd.AddCommand(prCmd) + prCmd.Flags().StringP("branch", "b", "", "Destination branch") + prCmd.Flags().StringP("title", "t", "", "PR title") + prCmd.Flags().StringP("head", "x", "", "Feature branch") + prCmd.Flags().StringP("body", "d", "", "Create body") + +} + +func createPR(prData *PRStructure) { + + client := github.NewClient(nil) + newPR := &github.NewPullRequest{ + Title: github.String(prData.title), + Head: github.String(prData.head), + Base: github.String(prData.branch), + Body: github.String(prData.body), + MaintainerCanModify: github.Bool(true), // Permite que o mantenedor modifique o PR + } + ctx := context.Background() + pr, response, err := client.PullRequests.Create(ctx, "Tomelin", "gCommit", newPR) + if err != nil { + log.Fatalf("Erro ao criar o Pull Request: %v", err) + } + + log.Println(response) + fmt.Printf("Pull Request criado com sucesso! URL: %s\n", pr.GetHTMLURL()) + +} diff --git a/cmd/cli/root.go b/cmd/cli/root.go index 64e4444..f5c70cd 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -37,9 +37,11 @@ You can also reset the commit to the head os.Exit(0) } + shortDesc, _ := cmd.Flags().GetBool("short") + interactive, _ := cmd.Flags().GetBool("interactive") if interactive { - interactive, err := interactiveMode() + interactive, err := interactiveMode(shortDesc) if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) @@ -99,6 +101,7 @@ func init() { rootCmd.Flags().String("taskId", "", "Task id") rootCmd.Flags().String("body", "", "Body message of commit") rootCmd.Flags().String("emoji", "", "Put emoji in commit message") + rootCmd.Flags().BoolP("short", "c", false, "Short description") } // interactiveMode is a function that runs the interactive mode @@ -110,7 +113,7 @@ func init() { // // message in the body // Resolve: #123 -func interactiveMode() (*entity.Commit, error) { +func interactiveMode(short bool) (*entity.Commit, error) { opts := entity.Commit{} @@ -131,6 +134,7 @@ func interactiveMode() (*entity.Commit, error) { if err != nil { return nil, fmt.Errorf("select prompt failed %v", err) } + opts.Option = opts.Option.FromString(result) opts.Choice = result // ENDS Commit type @@ -184,6 +188,11 @@ func interactiveMode() (*entity.Commit, error) { opts.Comment = commitBodyResult // ENDS body message + // Long description + if short { + return &opts, nil + } + // STARTS task status promptStatus := promptui.Select{ Label: "Select task status", diff --git a/go.mod b/go.mod index 8cbef19..e8484a3 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,14 @@ module github.com/Tomelin/go-convetional-commit go 1.23.1 require ( + github.com/google/go-github/v57 v57.0.0 github.com/manifoldco/promptui v0.9.0 github.com/spf13/cobra v1.8.1 ) require ( github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect diff --git a/go.sum b/go.sum index fe58bc3..23887e4 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,13 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs= +github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= @@ -16,5 +23,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=