Skip to content

Commit

Permalink
Improve repository name detection
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik committed Jul 1, 2024
1 parent 41535f3 commit bb15e9b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
21 changes: 17 additions & 4 deletions internal/cmd/stack/open_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ func getRepositoryName() (string, error) {
return "", err
}

result := strings.SplitN(string(out), ":", 2)
if len(result) != 2 {
return "", errors.New("could not parse result")
return cleanupRepositoryString(string(out))
}

func cleanupRepositoryString(s string) (string, error) {
var userRepo string

switch {
case strings.HasPrefix(s, "https://"):
userRepo = strings.TrimPrefix(s, "https://")
userRepo = userRepo[strings.Index(userRepo, "/")+1:]
case strings.HasPrefix(s, "git@"):
userRepo = strings.TrimPrefix(s, "git@")
userRepo = userRepo[strings.Index(userRepo, ":")+1:]
default:
return "", fmt.Errorf("unsupported repository string: %s", s)
}
return strings.TrimSuffix(strings.TrimSpace(result[1]), ".git"), nil

return strings.TrimSuffix(strings.TrimSpace(userRepo), ".git"), nil
}

func getGitCurrentBranch() (string, error) {
Expand Down
24 changes: 24 additions & 0 deletions internal/cmd/stack/open_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package stack

import "testing"

func Test_cleanupRepositoryString(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"https://github.com/username/tftest.git", "username/tftest"},
{"git@github.com:username/spacelift-local.git", "username/spacelift-local"},
{"https://gitlab.com/username/project.git", "username/project"},
{"git@gitlab.com:username/project.git", "username/project"},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
result, _ := cleanupRepositoryString(test.input)
if result != test.expected {
t.Errorf("expected %q, got %q", test.expected, result)
}
})
}
}

0 comments on commit bb15e9b

Please sign in to comment.