Skip to content

Commit

Permalink
Merge pull request #315 from drone/ks/CODE-2072
Browse files Browse the repository at this point in the history
Setting azure devops repo visibility based on the project's visibility
  • Loading branch information
karansaraswat19 authored Jul 6, 2024
2 parents b8beb99 + 8df99c4 commit ecdc44a
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 80 deletions.
23 changes: 13 additions & 10 deletions scm/driver/azure/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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),
}
}

Expand Down
4 changes: 3 additions & 1 deletion scm/driver/azure/testdata/repo.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]/tphoney/test_project/_git/test_project"
"Clone": "https://[email protected]/tphoney/test_project/_git/test_project",
"Private": true,
"Visibility": 3
}
2 changes: 1 addition & 1 deletion scm/driver/azure/testdata/repos.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions scm/driver/azure/testdata/repos.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
"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://[email protected]/tphoney/test_project/_git/test_project"
"Clone": "https://[email protected]/tphoney/test_project/_git/test_project",
"Private": true,
"Visibility": 3
},
{
"ID": "fde2d21f-13b9-4864-a995-83329045289a",
"Name": "test_repo2",
"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://[email protected]/tphoney/test_project/_git/test_repo2"
"Clone": "https://[email protected]/tphoney/test_project/_git/test_repo2",
"Private": false,
"Visibility": 1
}
]
15 changes: 1 addition & 14 deletions scm/driver/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"`
Expand Down
14 changes: 7 additions & 7 deletions scm/driver/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 4 additions & 26 deletions scm/driver/gitlab/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 0 additions & 19 deletions scm/driver/gitlab/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions scm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
39 changes: 39 additions & 0 deletions scm/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit ecdc44a

Please sign in to comment.