Skip to content

Commit

Permalink
feat: add users branch
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Dec 2, 2021
1 parent 8629a68 commit dde57df
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
64 changes: 37 additions & 27 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var branchCmd = &cobra.Command{
Long: `
If you don't give any argument, the command will look for issue in pre-configured issues provider.
The issue ID will be used as a prefix.
If type=user, a prefix with your git username will be added to the branch name.
If type=user(s), a prefix with your git username will be added to the branch name.
`,
Example: `
# Create branch feat/my-feature from current branch
Expand Down Expand Up @@ -84,6 +84,28 @@ func runBranchCmd(cmd *cobra.Command, args []string) {
}
}

func promptProviderBranch(repo *git.Repository) (nb format.TugBranch, err error) {
providers, err := integrations.ProvidersFrom(repo)
if err != nil {
return nb, err
}
issues := []integrations.IssueDescription{}
for _, p := range providers {
// TODO concurrent search
pIssues, err := p.Search()
if err != nil {
return nb, err
}
issues = append(issues, pIssues...)
}
issue, err := integrations.SelectIssue(issues, false)
if err != nil {
return nb, err
}

return issue.ToBranch(format.DefaultTypeRewrite), nil
}

func parseBranchCmd(cmd *cobra.Command, args []string) (*BranchCmdOption, error) {
// Find repo
repo, err := tugit.Getrepo()
Expand All @@ -94,40 +116,28 @@ func parseBranchCmd(cmd *cobra.Command, args []string) (*BranchCmdOption, error)
var nb format.TugBranch

if len(args) == 0 {
providers, err := integrations.ProvidersFrom(repo)
nb, err = promptProviderBranch(repo)
if err != nil {
return nil, err
}
issues := []integrations.IssueDescription{}
for _, p := range providers {
// TODO concurrent search
pIssues, err := p.Search()
if err != nil {
return nil, err
}
issues = append(issues, pIssues...)
}
issue, err := integrations.SelectIssue(issues, false)
if err != nil {
return nil, err
}
nb = issue.ToBranch(format.DefaultTypeRewrite)
} else {
nb = format.TugBranch{
Description: strings.Join(args[1:], " "),
}.WithType(args[0], format.DefaultTypeRewrite)
if nb.Type == "user" {
// Get user name from config
config, err := repo.Config()
if err != nil {
return nil, err
}
username, _ := config.LookupString("user.name")
if username == "" {
return nil, errors.New("You need to configure your username before creating a user branch.")
}
nb.Prefix = username
}

// User(s) branch
if nb.Type == "user" || nb.Type == "users" {
// Get user name from config
config, err := repo.Config()
if err != nil {
return nil, err
}
username, _ := config.LookupString("user.name")
if username == "" {
return nil, errors.New("You need to configure your username before creating a user branch.")
}
nb.Prefix = username
}

return &BranchCmdOption{
Expand Down
8 changes: 8 additions & 0 deletions cmd/branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ func TestParseBranchCmd(t *testing.T) {
Repo: r,
}
assert.Equal(t, expected, *bco)
// Users branch
bco, err = parseBranchCmd(cmd, []string{"users", "my", "branch"})
assert.NoError(t, err)
expected = BranchCmdOption{
NewBranch: format.TugBranch{Type: "users", Prefix: test.GIT_USERNAME, Description: "my branch"},
Repo: r,
}
assert.Equal(t, expected, *bco)
// Classic branch
bco, err = parseBranchCmd(cmd, []string{"feat", "foo", "bar"})
assert.NoError(t, err)
Expand Down

0 comments on commit dde57df

Please sign in to comment.