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

Improve repository name detection #233

Merged
merged 1 commit into from
Jul 1, 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
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"},
{"[email protected]:username/spacelift-local.git", "username/spacelift-local"},
{"https://gitlab.com/username/project.git", "username/project"},
{"[email protected]: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)
}
})
}
}
Loading