Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amend commit #2

Merged
merged 5 commits into from
Nov 22, 2024
Merged
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
35 changes: 21 additions & 14 deletions cmd/commit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"bufio"

Check failure on line 4 in cmd/commit.go

View workflow job for this annotation

GitHub Actions / build

"bufio" imported and not used

Check failure on line 4 in cmd/commit.go

View workflow job for this annotation

GitHub Actions / build

"bufio" imported and not used) (typecheck)

Check failure on line 4 in cmd/commit.go

View workflow job for this annotation

GitHub Actions / build

"bufio" imported and not used (typecheck)

Check failure on line 4 in cmd/commit.go

View workflow job for this annotation

GitHub Actions / test

"bufio" imported and not used
"bytes"
"encoding/json"
"fmt"
Expand All @@ -10,11 +10,24 @@
"os"
"os/exec"

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

var openAIAPIKey = os.Getenv("OPENAI_TOKEN")

const systemMessage = `You are intelligent, helpful and an expert developer, who always gives the correct answer and only does what instructed. You always answer truthfully and don't make things up. (When responding to the following prompt, please make sure to properly style your response using Github Flavored Markdown. Use markdown syntax for things like headings, lists, colored text, code blocks, highlights etc. Make sure not to mention markdown or styling in your actual response.)`

const userMessage = `Suggest a precise and informative commit message based on the following diff. Do not use markdown syntax in your response.

The commit message should have description with a short title that follows emoji commit message format like <emoji> <description>.

Examples:
- :refactor: Change log format for better visibility
- :sparkles: Introduce new logging class

Diff: `

// commitCmd represents the commit command
var commitCmd = &cobra.Command{
Use: "commit",
Expand All @@ -33,23 +46,17 @@
return
}

fmt.Println("Select a commit message:")
for i, msg := range commitMessages {
fmt.Printf("%d: %s\n", i+1, msg)
prompt := promptui.Select{
Label: "Select a commit message",
Items: commitMessages,
}

reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter the number of the commit message you want to use: ")
choice, _ := reader.ReadString('\n')

commitIndex := -1
fmt.Sscanf(choice, "%d", &commitIndex)
if commitIndex < 1 || commitIndex > len(commitMessages) {
fmt.Println("Invalid choice.")
_, selectedMessage, err := prompt.Run()
if err != nil {
fmt.Println("Prompt failed:", err)
return
}

selectedMessage := commitMessages[commitIndex-1]
commitChanges(selectedMessage)
},
}
Expand All @@ -70,8 +77,8 @@
requestBody, _ := json.Marshal(map[string]interface{}{
"model": "gpt-3.5-turbo",
"messages": []map[string]string{
{"role": "system", "content": "You are a helpful assistant that generates commit messages using the emoji commit structure."},
{"role": "user", "content": fmt.Sprintf("Generate a commit messages for the following diff:\n%s", string(diff))},
{"role": "system", "content": systemMessage},
{"role": "user", "content": userMessage + string(diff)},
},
"max_tokens": 150,
"n": 5,
Expand Down
38 changes: 38 additions & 0 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"bytes"
"os/exec"
"testing"
)

func TestCommitCmd(t *testing.T) {
// Mock the exec.Command function
originalCommand := execCommand
defer func() { execCommand = originalCommand }()
execCommand = mockExecCommand

// Capture the output
var output bytes.Buffer
rootCmd.SetOut(&output)

// Execute the commit command
rootCmd.SetArgs([]string{"commit"})
if err := rootCmd.Execute(); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Check the output
expectedOutput := "mocked git diff output\n"
if output.String() != expectedOutput {
t.Fatalf("Expected output to be %q, got %q", expectedOutput, output.String())
}
}

// Mock exec.Command function
var execCommand = exec.Command

func mockExecCommand(command string, args ...string) *exec.Cmd {
cmd := exec.Command("echo", "mocked git diff output")
return cmd
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ go 1.23.3
require github.com/spf13/cobra v1.8.1

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
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/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading