Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cmd/cli/pr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
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)
}

Check warning on line 39 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L29-L39

Added lines #L29 - L39 were not covered by tests

title, err := cmd.Flags().GetString("title")
if err != nil {
fmt.Println("Erro ao obter a flag 'title':", err.Error())
os.Exit(1)
}

Check warning on line 45 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L41-L45

Added lines #L41 - L45 were not covered by tests

if title == "" {
fmt.Println("A flag 'title' é obrigatória")
os.Exit(1)
}

Check warning on line 50 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L47-L50

Added lines #L47 - L50 were not covered by tests

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)
}

Check warning on line 60 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L52-L60

Added lines #L52 - L60 were not covered by tests

head, err := cmd.Flags().GetString("head")
if err != nil {
fmt.Println("Erro ao obter a flag 'head':", err.Error())
os.Exit(1)
}

Check warning on line 66 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L62-L66

Added lines #L62 - L66 were not covered by tests

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)

Check warning on line 79 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L68-L79

Added lines #L68 - L79 were not covered by tests
},
}

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")

Check warning on line 90 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L83-L90

Added lines #L83 - L90 were not covered by tests
}

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)
}

Check warning on line 107 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L93-L107

Added lines #L93 - L107 were not covered by tests

log.Println(response)
fmt.Printf("Pull Request criado com sucesso! URL: %s\n", pr.GetHTMLURL())

Check warning on line 110 in cmd/cli/pr.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/pr.go#L109-L110

Added lines #L109 - L110 were not covered by tests

}
13 changes: 11 additions & 2 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
os.Exit(0)
}

shortDesc, _ := cmd.Flags().GetBool("short")

Check warning on line 41 in cmd/cli/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/root.go#L40-L41

Added lines #L40 - L41 were not covered by tests
interactive, _ := cmd.Flags().GetBool("interactive")
if interactive {
interactive, err := interactiveMode()
interactive, err := interactiveMode(shortDesc)

Check warning on line 44 in cmd/cli/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/root.go#L44

Added line #L44 was not covered by tests
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
Expand Down Expand Up @@ -99,6 +101,7 @@
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")

Check warning on line 104 in cmd/cli/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/root.go#L104

Added line #L104 was not covered by tests
}

// interactiveMode is a function that runs the interactive mode
Expand All @@ -110,7 +113,7 @@
//
// message in the body
// Resolve: #123
func interactiveMode() (*entity.Commit, error) {
func interactiveMode(short bool) (*entity.Commit, error) {

Check warning on line 116 in cmd/cli/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/root.go#L116

Added line #L116 was not covered by tests

opts := entity.Commit{}

Expand All @@ -131,6 +134,7 @@
if err != nil {
return nil, fmt.Errorf("select prompt failed %v", err)
}

opts.Option = opts.Option.FromString(result)
opts.Choice = result
// ENDS Commit type
Expand Down Expand Up @@ -184,6 +188,11 @@
opts.Comment = commitBodyResult
// ENDS body message

// Long description
if short {
return &opts, nil
}

Check warning on line 194 in cmd/cli/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/root.go#L191-L194

Added lines #L191 - L194 were not covered by tests

// STARTS task status
promptStatus := promptui.Select{
Label: "Select task status",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=