Skip to content

Commit

Permalink
feat: 90 create support to custom flow
Browse files Browse the repository at this point in the history
- Add support to read config file
- Add support to create PR based on flow
- Create branch based on flow
  • Loading branch information
Isaac Martinez committed Jul 23, 2023
1 parent 2464fa7 commit 1794664
Show file tree
Hide file tree
Showing 21 changed files with 204 additions and 329 deletions.
11 changes: 2 additions & 9 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ import (

var branchCmd = &cobra.Command{
Use: "branch",
Short: "Create a new branch. This is variant of git-flow by Minnek",
Short: "Create a new branch.",
Run: func(cmd *cobra.Command, args []string) {
pkg_action.Branch()
},
}

func init() {
branch.SetFlags()
branchCmd.AddCommand(branch.BranchFeatureCmd)
branchCmd.AddCommand(branch.BranchReleaseCmd)
branchCmd.AddCommand(branch.BranchHotfixCmd)
branchCmd.AddCommand(branch.BranchSupportCmd)
branchCmd.AddCommand(branch.BranchBugfixCmd)
branchCmd.AddCommand(branch.BranchTestCmd)
branchCmd.AddCommand(branch.BranchSyncCmd)
branch.SetCommands(branchCmd)
rootCmd.AddCommand(branchCmd)
}
19 changes: 0 additions & 19 deletions cmd/branch/bugfix.go

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/branch/feature.go

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/branch/hotfix.go

This file was deleted.

36 changes: 28 additions & 8 deletions cmd/branch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@ package branch

import (
"github.com/Minnek-Digital-Studio/cominnek/config"
"github.com/Minnek-Digital-Studio/cominnek/controllers/project"
pkg_action "github.com/Minnek-Digital-Studio/cominnek/pkg/cli/actions"
"github.com/spf13/cobra"
)

var stash bool

func SetFlags() {
addFlags(BranchFeatureCmd)
addFlags(BranchReleaseCmd)
addFlags(BranchHotfixCmd)
addFlags(BranchSupportCmd)
addFlags(BranchBugfixCmd)
addFlags(BranchTestCmd)
addFlags(BranchSyncCmd)
func SetCommands(Command *cobra.Command) {
project.ReadConfigFile()

for _, branch := range project.Config.Git.Branches {
if branch.Config.Hidden {
continue
}

branchName := branch.Name
branchData := branch
branchCmd := &cobra.Command{
Use: branchName,
Example: branchName + " <name>",
Short: "Create a new " + branchName + " branch",
Long: branch.Config.Description,
Run: func(cmd *cobra.Command, args []string) {
setTicket(args)
config.AppData.Branch.Stash = stash
config.AppData.Branch.Data = branchData
pkg_action.Branch()
},
}

addFlags(branchCmd)
Command.AddCommand(branchCmd)
}
}

func addFlags(cmd *cobra.Command) {
Expand Down
19 changes: 0 additions & 19 deletions cmd/branch/release.go

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/branch/support.go

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/branch/sync.go

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/branch/test.go

This file was deleted.

14 changes: 8 additions & 6 deletions config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"path/filepath"
"time"

"github.com/Minnek-Digital-Studio/cominnek/controllers/project"
)

type ICommit struct {
Expand Down Expand Up @@ -34,9 +36,9 @@ type IAppData struct {
Base string
}
Branch struct {
Type string
Ticket string
Data project.Branch
Stash bool
Ticket string
}
Stash struct {
Branch string
Expand All @@ -51,10 +53,10 @@ type IAppData struct {
Name string
}
Reset struct {
Type string
Commit string
Number string
Target string
Type string
Commit string
Number string
Target string
Confirm bool
}
}
Expand Down
39 changes: 6 additions & 33 deletions controllers/git/branch.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,15 @@
package git_controller

import "fmt"
import (
"fmt"
"strings"
)

func getBaseCmd(branch string) string {
return fmt.Sprintf("git branch %s; git checkout %s", branch, branch)
}

func Feature(ticket string) (string, string) {
branchName := "feature/" + ticket
return getBaseCmd(branchName), branchName
}

func Bugfix(ticket string) (string, string) {
branchName := "bugfix/" + ticket
return getBaseCmd(branchName), branchName
}

func Hotfix(ticket string) (string, string) {
branchName := "hotfix/" + ticket
return getBaseCmd(branchName), branchName
}

func Release(ticket string) (string, string) {
branchName := "release/" + ticket
return getBaseCmd(branchName), branchName
}

func Support(ticket string) (string, string) {
branchName := "support/" + ticket
return getBaseCmd(branchName), branchName
}

func Test(ticket string) (string, string) {
branchName := "test/" + ticket
return getBaseCmd(branchName), branchName
}

func Sync(ticket string) (string, string) {
branchName := "sync/" + ticket
func Custom(path string, ticket string) (string, string) {
branchName := strings.Replace(path, "*", ticket, 1)
return getBaseCmd(branchName), branchName
}
35 changes: 23 additions & 12 deletions controllers/git/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"strings"

"github.com/Minnek-Digital-Studio/cominnek/controllers/project"
"github.com/Minnek-Digital-Studio/cominnek/pkg/shell"
"github.com/fatih/color"
)
Expand All @@ -30,18 +31,28 @@ func GetCurrentBranch() string {
func GetTicketNumber() string {
currentBranch := GetCurrentBranch()

if strings.HasPrefix(currentBranch, "feature/") {
return strings.TrimPrefix(currentBranch, "feature/")
} else if strings.HasPrefix(currentBranch, "hotfix/") {
return strings.TrimPrefix(currentBranch, "hotfix/")
} else if strings.HasPrefix(currentBranch, "release/") {
return strings.TrimPrefix(currentBranch, "release/")
} else if strings.HasPrefix(currentBranch, "support/") {
return strings.TrimPrefix(currentBranch, "support/")
} else if strings.HasPrefix(currentBranch, "bugfix/") {
return strings.TrimPrefix(currentBranch, "bugfix/")
} else if strings.HasPrefix(currentBranch, "sync/") {
return strings.TrimPrefix(currentBranch, "sync/")
var paths []string

for _, path := range project.Config.Git.Branches {
paths = append(paths, strings.ReplaceAll(path.Path, "*", ""))
}

for _, path := range paths {
if strings.HasPrefix(currentBranch, path) {
return strings.TrimPrefix(currentBranch, path)
}
}

return ""
}

func GetBranchType() string {
currentBranch := GetCurrentBranch()

for _, path := range project.Config.Git.Branches {
if strings.HasPrefix(currentBranch, strings.ReplaceAll(path.Path, "*", "")) {
return path.Name
}
}

return ""
Expand Down
2 changes: 1 addition & 1 deletion controllers/git/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ func Switch(branch string) {
shell.ExecuteCommand("git checkout "+branch, false)
loading.Stop()
color.HiGreen(switchedMsg)
}
}
46 changes: 46 additions & 0 deletions controllers/project/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package project

import (
"encoding/json"

"github.com/Minnek-Digital-Studio/cominnek/controllers/files"
)

var Config Cominnek

func ReadConfigFile() bool {
fileNames := []string{
"mnk-config.json",
}

for _, fileName := range fileNames {
if files.CheckExist(fileName) {
configByte := files.Read(fileName)
Config = convertToJSON(configByte).Cominnek
return true
}
}

return false
}

func convertToJSON(data []byte) Project {
var project Project
err := json.Unmarshal([]byte(data), &project)

if err != nil {
panic(err)
}

return project
}

func GetConfigByName(name string) Branch {
for _, branch := range Config.Git.Branches {
if branch.Name == name {
return branch
}
}

return Branch{}
}
Loading

0 comments on commit 1794664

Please sign in to comment.