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

feat: allow parsing of Azure DevOps #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
58 changes: 47 additions & 11 deletions vcsurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ type Host string

// Supported VCS host provider.
const (
GitHub Host = "github.com"
Bitbucket Host = "bitbucket.org"
GitLab Host = "gitlab.com"
GitHub Host = "github.com"
Bitbucket Host = "bitbucket.org"
GitLab Host = "gitlab.com"
AzureLegacy Host = "vs-ssh.visualstudio.com"
Azure Host = "ssh.dev.azure.com"

gitHubAPI Host = "api.github.com"
)
Expand All @@ -49,16 +51,20 @@ const (
)

var knownHosts = map[Host]struct{}{
GitHub: struct{}{},
GitLab: struct{}{},
Bitbucket: struct{}{},
GitHub: struct{}{},
GitLab: struct{}{},
Bitbucket: struct{}{},
AzureLegacy: struct{}{},
Azure: struct{}{},
}

var kindByHost = map[Host]Kind{
GitHub: Git,
gitHubAPI: Git,
GitLab: Git,
Bitbucket: Git,
GitHub: Git,
gitHubAPI: Git,
GitLab: Git,
Bitbucket: Git,
AzureLegacy: Git,
Azure: Git,
}

// VCS describes a VCS repository.
Expand All @@ -84,7 +90,7 @@ type VCS struct {

var (
removeDotGit = regexp.MustCompile(`\.git$`)
gitPreprocessRE = regexp.MustCompile("^git@([a-zA-Z0-9-_\\.]+)\\:(.*)$")
gitPreprocessRE = regexp.MustCompile("^[a-zA-Z0-9-_]+@([a-zA-Z0-9-_\\.]+)\\:(.*)$")
)

// Parse parses a string that resembles a VCS repository URL. See TestParse for
Expand Down Expand Up @@ -123,6 +129,8 @@ func Parse(raw string) (*VCS, error) {
err = vcs.parseBitbucket(parsedURL)
case GitLab:
err = vcs.parseGitlab(parsedURL)
case Azure, AzureLegacy:
err = vcs.parseAzure(parsedURL)
default:
err = vcs.parseDefault(parsedURL)
}
Expand Down Expand Up @@ -226,6 +234,34 @@ func (v *VCS) parseGitlab(url *url.URL) error {
return nil
}

func (v *VCS) parseAzure(url *url.URL) error {
parts := strings.Split(url.Path, "/")
if len(parts) < 4 {
return ErrUnknownURL
}

var last int
for _, p := range parts {
if p == "-" {
break
}
last++
}

v.Username = strings.Join(parts[2:last-1], "/")
v.Name = removeDotGit.ReplaceAllLiteralString(parts[last-1], "")
v.FullName = v.Username + "/" + v.Name

if len(parts) >= (last + 2) {
object := parts[last+1]
if object == "tags" || object == "commit" || object == "tree" {
v.Committish = strings.Join(parts[last+2:], "/")
}
}

return nil
}

func (v *VCS) parseDefault(url *url.URL) error {
path := url.Path
if len(path) == 0 {
Expand Down
36 changes: 36 additions & 0 deletions vcsurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,42 @@ func TestParse_GitlabSubGroup(t *testing.T) {
}
}

func TestParse_AzureLegacy(t *testing.T) {
urls := []string{
"[email protected]:v3/org/Project/RepoName",
}

for _, url := range urls {
t.Run(url, func(t *testing.T) {
vcs, err := vcsurl.Parse(url)
require.NoError(t, err)
require.Equal(t, vcs.Kind, vcsurl.Git)
require.Equal(t, vcs.Host, vcsurl.AzureLegacy)
require.Equal(t, vcs.Username, "org/Project")
require.Equal(t, vcs.Name, "RepoName")
require.Equal(t, vcs.FullName, "org/Project/RepoName")
})
}
}

func TestParse_Azure(t *testing.T) {
urls := []string{
"[email protected]:v3/org/Project/RepoName",
}

for _, url := range urls {
t.Run(url, func(t *testing.T) {
vcs, err := vcsurl.Parse(url)
require.NoError(t, err)
require.Equal(t, vcs.Kind, vcsurl.Git)
require.Equal(t, vcs.Host, vcsurl.Azure)
require.Equal(t, vcs.Username, "org/Project")
require.Equal(t, vcs.Name, "RepoName")
require.Equal(t, vcs.FullName, "org/Project/RepoName")
})
}
}

func TestParse_GitlabCommittish(t *testing.T) {
urls := []string{
"https://gitlab.com/foo/qux/bar/-/commit/baz",
Expand Down