Skip to content

Commit

Permalink
Merge pull request #12 from muandane/feat/skipquestions
Browse files Browse the repository at this point in the history
feature ✨ (src): added ability to skip questions
  • Loading branch information
muandane authored Jul 10, 2023
2 parents 3995ab2 + 36f1208 commit b7860db
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .goji.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"emoji": "",
"code": ":sparkles:",
"description": "Introduce new features.",
"name": "feature"
"name": "feat"
},
{
"emoji": "🐛",
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ You can customize the `.goji.json` generated file to add or change the scopes, t
"emoji": "",
"code": ":sparkles:",
"description": "Introducing new features.",
"name": "feature"
"name": "feat"
},
{
"emoji": "🐛",
Expand All @@ -90,6 +90,10 @@ You can customize the `.goji.json` generated file to add or change the scopes, t
}
```

You can skip questions by adding them in `"SkipQuestions"`

Only `"Scopes"` question can be skipped since it's optional according to the [Commit Spec](https://www.conventionalcommits.org/en/v1.0.0/)

## License

Apache 2.0 license [Zine El Abidine Moualhi](https://www.linkedin.com/in/zinemoualhi/)
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/goji.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

func main() {
version := "0.0.1-rc3"
version := "0.0.1-rc4"
helpFlag := flag.Bool("h", false, "Display help information")
flag.BoolVar(helpFlag, "help", false, "display help")
versionFlag := flag.Bool("v", false, "Display version information")
Expand Down Expand Up @@ -108,7 +108,7 @@ type initConfig struct {

func AddCustomCommitTypes(gitmojis []Gitmoji) []Gitmoji {
customGitmojis := []Gitmoji{
{Emoji: "✨", Code: ":sparkles:", Description: "Introduce new features.", Name: "feature"},
{Emoji: "✨", Code: ":sparkles:", Description: "Introduce new features.", Name: "feat"},
{Emoji: "🐛", Code: ":bug:", Description: "Fix a bug.", Name: "fix"},
{Emoji: "📚", Code: ":books:", Description: "Documentation change.", Name: "docs"},
{Emoji: "🎨", Code: ":art:", Description: "Improve structure/format of the code.", Name: "refactor"},
Expand Down
8 changes: 3 additions & 5 deletions src/pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package config

import (
"github.com/stretchr/testify/mock"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/mock"
)

func TestLoadConfig(t *testing.T) {
Expand Down Expand Up @@ -255,7 +255,7 @@ func (m *MockExec) Command(name string, arg ...string) *exec.Cmd {
}
func TestLoadConfig_Failure1(t *testing.T) {
// Create a temporary directory for the test
tmpDir, err := ioutil.TempDir("", "test")
tmpDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatal(err)
}
Expand All @@ -265,12 +265,10 @@ func TestLoadConfig_Failure1(t *testing.T) {

// Change the working directory to the temporary directory
os.Chdir(tmpDir)

// Try to load a non-existent config file
_, err = LoadConfig("non_existent_file.json")
if err == nil {
t.Errorf("Expected error, got nil")
}

// The test will succeed if there is any error (not just a 'file does not exist' error)
}
32 changes: 25 additions & 7 deletions src/pkg/utils/askQuestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ func AskQuestions(config *config.Config) (string, error) {
}
}

promptScope := &survey.Input{
Message: "Enter the scope of the change:",
}
err = askOneFunc(promptScope, &commitScope)
if err != nil {
return "", err
// Only ask for commitScope if not in SkipQuestions
if !isInSkipQuestions("Scopes", config.SkipQuestions) {
promptScope := &survey.Input{
Message: "Enter the scope of the change:",
}
err = askOneFunc(promptScope, &commitScope)
if err != nil {
return "", err
}
}

promptSubject := &survey.Input{
Expand All @@ -49,6 +52,21 @@ func AskQuestions(config *config.Config) (string, error) {
return "", err
}

commitMessage := fmt.Sprintf("%s (%s): %s", commitType, commitScope, commitSubject)
var commitMessage string
if commitScope == "" {
commitMessage = fmt.Sprintf("%s: %s", commitType, commitSubject)
} else {
commitMessage = fmt.Sprintf("%s (%s): %s", commitType, commitScope, commitSubject)
}

return commitMessage, nil
}

func isInSkipQuestions(value string, list []string) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}
22 changes: 20 additions & 2 deletions src/pkg/utils/commitMessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package utils

import (
"errors"
"github.com/AlecAivazis/survey/v2"
"github.com/stretchr/testify/mock"
"goji/pkg/config"
"goji/pkg/models"
"testing"

"github.com/AlecAivazis/survey/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestAskQuestions(t *testing.T) {
Expand Down Expand Up @@ -125,3 +127,19 @@ func TestAskQuestions_Failure(t *testing.T) {
t.Errorf("Expected commit message '', got '%s'", commitMessage)
}
}
func TestIsInSkipQuestions(t *testing.T) {
testCases := []struct {
skipQuestions []string
value string
expected bool
}{
{[]string{"types", "scopes", "message"}, "scopes", true},
{[]string{"types", "scopes", "message"}, "notInList", false},
{[]string{}, "scopes", false},
}

for _, tc := range testCases {
result := isInSkipQuestions(tc.value, tc.skipQuestions)
assert.Equal(t, tc.expected, result)
}
}

0 comments on commit b7860db

Please sign in to comment.