diff --git a/scm/driver/azure/repo.go b/scm/driver/azure/repo.go index 25be94b8..64eead88 100644 --- a/scm/driver/azure/repo.go +++ b/scm/driver/azure/repo.go @@ -201,10 +201,11 @@ type repository struct { ID string `json:"id"` Name string `json:"name"` Project struct { - ID string `json:"id"` - Name string `json:"name"` - State string `json:"state"` - URL string `json:"url"` + ID string `json:"id"` + Name string `json:"name"` + State string `json:"state"` + URL string `json:"url"` + Visibility string `json:"visibility"` } `json:"project"` RemoteURL string `json:"remoteUrl"` URL string `json:"url"` @@ -283,12 +284,14 @@ func convertRepositoryList(from *repositories, owner string) []*scm.Repository { func convertRepository(from *repository, owner string) *scm.Repository { namespace := []string{owner, from.Project.Name} return &scm.Repository{ - ID: from.ID, - Name: from.Name, - Namespace: strings.Join(namespace, "/"), - Link: from.URL, - Branch: scm.TrimRef(from.DefaultBranch), - Clone: from.RemoteURL, + ID: from.ID, + Name: from.Name, + Namespace: strings.Join(namespace, "/"), + Link: from.URL, + Branch: scm.TrimRef(from.DefaultBranch), + Clone: from.RemoteURL, + Private: scm.ConvertPrivate(from.Project.Visibility), + Visibility: scm.ConvertVisibility(from.Project.Visibility), } } diff --git a/scm/driver/azure/testdata/repo.json.golden b/scm/driver/azure/testdata/repo.json.golden index c642b51e..c65a2d23 100644 --- a/scm/driver/azure/testdata/repo.json.golden +++ b/scm/driver/azure/testdata/repo.json.golden @@ -4,5 +4,7 @@ "Namespace": "ORG/test_project", "Branch": "main", "Link": "https://dev.azure.com/tphoney/d350c9c0-7749-4ff8-a78f-f9c1f0e56729/_apis/git/repositories/91f0d4cb-4c36-49a5-b28d-2d72da089c4d", - "Clone": "https://tphoney@dev.azure.com/tphoney/test_project/_git/test_project" + "Clone": "https://tphoney@dev.azure.com/tphoney/test_project/_git/test_project", + "Private": true, + "Visibility": 3 } \ No newline at end of file diff --git a/scm/driver/azure/testdata/repos.json b/scm/driver/azure/testdata/repos.json index a05b6ce1..e5d7c6a2 100644 --- a/scm/driver/azure/testdata/repos.json +++ b/scm/driver/azure/testdata/repos.json @@ -30,7 +30,7 @@ "url": "https://dev.azure.com/tphoney/_apis/projects/d350c9c0-7749-4ff8-a78f-f9c1f0e56729", "state": "wellFormed", "revision": 11, - "visibility": "private", + "visibility": "public", "lastUpdateTime": "2022-02-24T15:31:27.89Z" }, "defaultBranch": "refs/heads/main", diff --git a/scm/driver/azure/testdata/repos.json.golden b/scm/driver/azure/testdata/repos.json.golden index d4c8b140..137b7366 100644 --- a/scm/driver/azure/testdata/repos.json.golden +++ b/scm/driver/azure/testdata/repos.json.golden @@ -5,7 +5,9 @@ "Namespace": "ORG/test_project", "Branch": "main", "Link": "https://dev.azure.com/tphoney/d350c9c0-7749-4ff8-a78f-f9c1f0e56729/_apis/git/repositories/91f0d4cb-4c36-49a5-b28d-2d72da089c4d", - "Clone": "https://tphoney@dev.azure.com/tphoney/test_project/_git/test_project" + "Clone": "https://tphoney@dev.azure.com/tphoney/test_project/_git/test_project", + "Private": true, + "Visibility": 3 }, { "ID": "fde2d21f-13b9-4864-a995-83329045289a", @@ -13,6 +15,8 @@ "Namespace": "ORG/test_project", "Branch": "main", "Link": "https://dev.azure.com/tphoney/d350c9c0-7749-4ff8-a78f-f9c1f0e56729/_apis/git/repositories/fde2d21f-13b9-4864-a995-83329045289a", - "Clone": "https://tphoney@dev.azure.com/tphoney/test_project/_git/test_repo2" + "Clone": "https://tphoney@dev.azure.com/tphoney/test_project/_git/test_repo2", + "Private": false, + "Visibility": 1 } ] \ No newline at end of file diff --git a/scm/driver/github/repo.go b/scm/driver/github/repo.go index b7b6e336..cad7f917 100644 --- a/scm/driver/github/repo.go +++ b/scm/driver/github/repo.go @@ -261,7 +261,7 @@ func convertRepository(from *repository) *scm.Repository { Branch: from.DefaultBranch, Archived: from.Archived, Private: from.Private, - Visibility: convertVisibility(from.Visibility), + Visibility: scm.ConvertVisibility(from.Visibility), Clone: from.CloneURL, CloneSSH: from.SSHURL, Created: from.CreatedAt, @@ -314,19 +314,6 @@ func convertFromHookEvents(from scm.HookEvents) []string { return events } -func convertVisibility(from string) scm.Visibility { - switch from { - case "public": - return scm.VisibilityPublic - case "private": - return scm.VisibilityPrivate - case "internal": - return scm.VisibilityInternal - default: - return scm.VisibilityUndefined - } -} - type status struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` diff --git a/scm/driver/github/webhook.go b/scm/driver/github/webhook.go index 6c01a711..2fbdae93 100644 --- a/scm/driver/github/webhook.go +++ b/scm/driver/github/webhook.go @@ -408,7 +408,7 @@ func convertPushHook(src *pushHook) *scm.PushHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, @@ -435,7 +435,7 @@ func convertBranchHook(src *createDeleteHook) *scm.BranchHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, @@ -455,7 +455,7 @@ func convertTagHook(src *createDeleteHook) *scm.TagHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, @@ -473,7 +473,7 @@ func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, @@ -499,7 +499,7 @@ func convertDeploymentHook(src *deploymentHook) *scm.DeployHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, @@ -533,7 +533,7 @@ func convertIssueCommentHook(src *issueCommentHook) *scm.IssueCommentHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, @@ -571,7 +571,7 @@ func convertReleaseHook(src *releaseHook) *scm.ReleaseHook { Name: src.Repository.Name, Branch: src.Repository.DefaultBranch, Private: src.Repository.Private, - Visibility: convertVisibility(src.Repository.Visibility), + Visibility: scm.ConvertVisibility(src.Repository.Visibility), Clone: src.Repository.CloneURL, CloneSSH: src.Repository.SSHURL, Link: src.Repository.HTMLURL, diff --git a/scm/driver/gitlab/repo.go b/scm/driver/gitlab/repo.go index d4177614..9a44bc3b 100644 --- a/scm/driver/gitlab/repo.go +++ b/scm/driver/gitlab/repo.go @@ -178,7 +178,7 @@ func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id stri return s.client.do(ctx, "DELETE", path, nil, nil) } -// helper function to convert from the gogs repository list to +// helper function to convert from the gitlab repository list to // the common repository structure. func convertRepositoryList(from []*repository) []*scm.Repository { to := []*scm.Repository{} @@ -188,7 +188,7 @@ func convertRepositoryList(from []*repository) []*scm.Repository { return to } -// helper function to convert from the gogs repository structure +// helper function to convert from the gitlab repository structure // to the common repository structure. func convertRepository(from *repository) *scm.Repository { to := &scm.Repository{ @@ -197,8 +197,8 @@ func convertRepository(from *repository) *scm.Repository { Name: from.Path, Branch: from.DefaultBranch, Archived: from.Archived, - Private: convertPrivate(from.Visibility), - Visibility: convertVisibility(from.Visibility), + Private: scm.ConvertPrivate(from.Visibility), + Visibility: scm.ConvertVisibility(from.Visibility), Clone: from.HTTPURL, CloneSSH: from.SSHURL, Link: from.WebURL, @@ -317,28 +317,6 @@ func convertFromState(from scm.State) string { } } -func convertPrivate(from string) bool { - switch from { - case "public", "": - return false - default: - return true - } -} - -func convertVisibility(from string) scm.Visibility { - switch from { - case "public": - return scm.VisibilityPublic - case "private": - return scm.VisibilityPrivate - case "internal": - return scm.VisibilityInternal - default: - return scm.VisibilityUndefined - } -} - func canPush(proj *repository) bool { switch { case proj.Permissions.ProjectAccess.AccessLevel >= 30: diff --git a/scm/driver/gitlab/repo_test.go b/scm/driver/gitlab/repo_test.go index 293e523f..65c17517 100644 --- a/scm/driver/gitlab/repo_test.go +++ b/scm/driver/gitlab/repo_test.go @@ -521,25 +521,6 @@ func TestConvertFromState(t *testing.T) { } } -func TestConvertPrivate(t *testing.T) { - tests := []struct { - in string - out bool - }{ - {"public", false}, - {"", false}, - {"private", true}, - {"internal", true}, - {"invalid", true}, - } - - for _, test := range tests { - if got, want := convertPrivate(test.in), test.out; got != want { - t.Errorf("Want private %v, got %v", want, got) - } - } -} - func TestCanPush(t *testing.T) { tests := []struct { in *repository diff --git a/scm/util.go b/scm/util.go index 0a8c3a2c..6aa49683 100644 --- a/scm/util.go +++ b/scm/util.go @@ -89,3 +89,25 @@ func IsPullRequest(ref string) bool { func IsHash(s string) bool { return sha1.MatchString(s) || sha256.MatchString(s) } + +func ConvertVisibility(from string) Visibility { + switch from { + case "public": + return VisibilityPublic + case "private": + return VisibilityPrivate + case "internal": + return VisibilityInternal + default: + return VisibilityUndefined + } +} + +func ConvertPrivate(from string) bool { + switch from { + case "public", "": + return false + default: + return true + } +} diff --git a/scm/util_test.go b/scm/util_test.go index 41332276..c2b87ae7 100644 --- a/scm/util_test.go +++ b/scm/util_test.go @@ -239,3 +239,42 @@ func TestIsHash(t *testing.T) { } } } + +func TestConvertVisibility(t *testing.T) { + tests := []struct { + in string + out Visibility + }{ + {"public", 1}, + {"", 0}, + {"private", 3}, + {"internal", 2}, + {"invalid", 0}, + {"unknown", 0}, + } + + for _, test := range tests { + if got, want := ConvertVisibility(test.in), test.out; got != want { + t.Errorf("Want %d for %v type, got %d", want, test.in, got) + } + } +} + +func TestConvertPrivate(t *testing.T) { + tests := []struct { + in string + out bool + }{ + {"public", false}, + {"", false}, + {"private", true}, + {"internal", true}, + {"invalid", true}, + } + + for _, test := range tests { + if got, want := ConvertPrivate(test.in), test.out; got != want { + t.Errorf("Want private %v, got %v", want, got) + } + } +}