Skip to content
Closed
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
6 changes: 5 additions & 1 deletion server/forge/forgejo/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func toRepo(from *forgejo.Repository) *model.Repo {
from.HTMLURL,
from.Owner.AvatarURL,
)
return &model.Repo{
repo := &model.Repo{
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
Name: name,
Owner: from.Owner.UserName,
Expand All @@ -48,6 +48,10 @@ func toRepo(from *forgejo.Repository) *model.Repo {
Perm: toPerm(from.Permissions),
PREnabled: from.HasPullRequests,
}
// Repo.Update only propagates visibility when it is set,
// so derive it from the privacy flag here.
repo.ResetVisibility()
return repo
}

// toPerm converts a Forgejo permission to a Woodpecker permission.
Expand Down
52 changes: 52 additions & 0 deletions server/forge/forgejo/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,58 @@ func TestToRepo(t *testing.T) {
assert.True(t, repo.Perm.Admin)
}

func TestToRepoVisibility(t *testing.T) {
tests := []struct {
name string
private bool
ownerVis forgejo.VisibleType
wantVis model.RepoVisibility
wantPrivate bool
}{
{"public repo, public owner", false, forgejo.VisibleTypePublic, model.VisibilityPublic, false},
{"private repo, public owner", true, forgejo.VisibleTypePublic, model.VisibilityPrivate, true},
{"public repo, private owner", false, forgejo.VisibleTypePrivate, model.VisibilityPrivate, true},
{"public repo, limited owner", false, forgejo.VisibleTypeLimited, model.VisibilityPrivate, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
from := forgejo.Repository{
FullName: "gophers/hello-world",
Owner: &forgejo.User{
UserName: "gordon",
Visibility: tt.ownerVis,
},
Private: tt.private,
}
repo := toRepo(&from)
assert.Equal(t, tt.wantPrivate, repo.IsSCMPrivate)
// Repo.Update only propagates visibility if it is set here.
assert.Equal(t, tt.wantVis, repo.Visibility)
})
}
}

func TestToRepoVisibilityUpdatePropagation(t *testing.T) {
// a repo that became private on the forge must turn private in the store
stored := &model.Repo{Visibility: model.VisibilityPublic}
stored.Update(toRepo(&forgejo.Repository{
FullName: "gophers/hello-world",
Owner: &forgejo.User{UserName: "gordon", Visibility: forgejo.VisibleTypePublic},
Private: true,
}))
assert.Equal(t, model.VisibilityPrivate, stored.Visibility)
assert.True(t, stored.IsSCMPrivate)

// and a repo that became public again must propagate back
stored.Update(toRepo(&forgejo.Repository{
FullName: "gophers/hello-world",
Owner: &forgejo.User{UserName: "gordon", Visibility: forgejo.VisibleTypePublic},
Private: false,
}))
assert.Equal(t, model.VisibilityPublic, stored.Visibility)
assert.False(t, stored.IsSCMPrivate)
}

func TestFixMalformedAvatar(t *testing.T) {
urls := []struct {
Before string
Expand Down
19 changes: 19 additions & 0 deletions server/forge/forgejo/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "git@codeberg.org:meisam/woodpecktester.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -88,6 +89,7 @@ func TestForgejoParser(t *testing.T) {
ForgeURL: "http://forgejo.golang.org/gordon/hello-world",
Clone: "http://forgejo.golang.org/gordon/hello-world.git",
CloneSSH: "git@forgejo.golang.org:gordon/hello-world.git",
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -123,6 +125,7 @@ func TestForgejoParser(t *testing.T) {
Clone: "http://127.0.0.1:3000/Test-CI/multi-line-secrets.git",
CloneSSH: "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
Branch: "main",
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -157,6 +160,7 @@ func TestForgejoParser(t *testing.T) {
Clone: "http://forgejo.golang.org/gordon/hello-world.git",
CloneSSH: "git@forgejo.golang.org:gordon/hello-world.git",
Branch: "main",
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -190,6 +194,7 @@ func TestForgejoParser(t *testing.T) {
Clone: "https://forgejo.golang.org/gordon/hello-world.git",
CloneSSH: "",
Branch: "main",
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -228,6 +233,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
IsSCMPrivate: false,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -266,6 +272,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
IsSCMPrivate: false,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -307,6 +314,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "git@forgejo.com:anbraten/test-repo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -345,6 +353,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "git@forgejo.com:anbraten/test-repo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -382,6 +391,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "git@forgejo.com:anbraten/test-repo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -419,6 +429,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@git.xxx:22/anbraten/demo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -456,6 +467,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -495,6 +507,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -535,6 +548,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -575,6 +589,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -615,6 +630,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -655,6 +671,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -695,6 +712,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -735,6 +753,7 @@ func TestForgejoParser(t *testing.T) {
CloneSSH: "ssh://git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down
6 changes: 5 additions & 1 deletion server/forge/gitea/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func toRepo(from *gitea.Repository) *model.Repo {
from.HTMLURL,
from.Owner.AvatarURL,
)
return &model.Repo{
repo := &model.Repo{
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
Name: name,
Owner: from.Owner.UserName,
Expand All @@ -52,6 +52,10 @@ func toRepo(from *gitea.Repository) *model.Repo {
Perm: toPerm(from.Permissions),
PREnabled: from.HasPullRequests,
}
// Repo.Update only propagates visibility when it is set,
// so derive it from the privacy flag here.
repo.ResetVisibility()
return repo
}

// toPerm converts a Gitea permission to a Woodpecker permission.
Expand Down
52 changes: 52 additions & 0 deletions server/forge/gitea/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,58 @@ func TestToRepo(t *testing.T) {
assert.True(t, repo.Perm.Admin)
}

func TestToRepoVisibility(t *testing.T) {
tests := []struct {
name string
private bool
ownerVis gitea.VisibleType
wantVis model.RepoVisibility
wantPrivate bool
}{
{"public repo, public owner", false, gitea.VisibleTypePublic, model.VisibilityPublic, false},
{"private repo, public owner", true, gitea.VisibleTypePublic, model.VisibilityPrivate, true},
{"public repo, private owner", false, gitea.VisibleTypePrivate, model.VisibilityPrivate, true},
{"public repo, limited owner", false, gitea.VisibleTypeLimited, model.VisibilityPrivate, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
from := gitea.Repository{
FullName: "gophers/hello-world",
Owner: &gitea.User{
UserName: "gordon",
Visibility: tt.ownerVis,
},
Private: tt.private,
}
repo := toRepo(&from)
assert.Equal(t, tt.wantPrivate, repo.IsSCMPrivate)
// Repo.Update only propagates visibility if it is set here.
assert.Equal(t, tt.wantVis, repo.Visibility)
})
}
}

func TestToRepoVisibilityUpdatePropagation(t *testing.T) {
// a repo that became private on the forge must turn private in the store
stored := &model.Repo{Visibility: model.VisibilityPublic}
stored.Update(toRepo(&gitea.Repository{
FullName: "gophers/hello-world",
Owner: &gitea.User{UserName: "gordon", Visibility: gitea.VisibleTypePublic},
Private: true,
}))
assert.Equal(t, model.VisibilityPrivate, stored.Visibility)
assert.True(t, stored.IsSCMPrivate)

// and a repo that became public again must propagate back
stored.Update(toRepo(&gitea.Repository{
FullName: "gophers/hello-world",
Owner: &gitea.User{UserName: "gordon", Visibility: gitea.VisibleTypePublic},
Private: false,
}))
assert.Equal(t, model.VisibilityPublic, stored.Visibility)
assert.False(t, stored.IsSCMPrivate)
}

func TestFixMalformedAvatar(t *testing.T) {
urls := []struct {
Before string
Expand Down
11 changes: 11 additions & 0 deletions server/forge/gitea/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "ssh://git@gitea.com:3344/a_nice_user/hello_world_ci.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -74,6 +75,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "git@codeberg.org:meisam/woodpecktester.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -107,6 +109,7 @@ func TestGiteaParser(t *testing.T) {
ForgeURL: "http://gitea.golang.org/gordon/hello-world",
Clone: "http://gitea.golang.org/gordon/hello-world.git",
CloneSSH: "git@gitea.golang.org:gordon/hello-world.git",
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -142,6 +145,7 @@ func TestGiteaParser(t *testing.T) {
Clone: "http://127.0.0.1:3000/Test-CI/multi-line-secrets.git",
CloneSSH: "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
Branch: "main",
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -176,6 +180,7 @@ func TestGiteaParser(t *testing.T) {
Clone: "http://gitea.golang.org/gordon/hello-world.git",
CloneSSH: "git@gitea.golang.org:gordon/hello-world.git",
Branch: "main",
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -209,6 +214,7 @@ func TestGiteaParser(t *testing.T) {
Clone: "https://gitea.golang.org/gordon/hello-world.git",
CloneSSH: "",
Branch: "main",
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -247,6 +253,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "git@codeberg.org/test_it/test_ci_thing.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
IsSCMPrivate: false,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -285,6 +292,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "ssh://git@127.0.0.1:2200/Test-CI/multi-line-secrets.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
IsSCMPrivate: false,
Perm: &model.Perm{
Pull: true,
Expand Down Expand Up @@ -326,6 +334,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "git@gitea.com:anbraten/test-repo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -606,6 +615,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "git@gitea.com:anbraten/test-repo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPublic,
Perm: &model.Perm{
Pull: true,
Push: true,
Expand Down Expand Up @@ -643,6 +653,7 @@ func TestGiteaParser(t *testing.T) {
CloneSSH: "ssh://git@git.xxx:22/anbraten/demo.git",
Branch: "main",
PREnabled: true,
Visibility: model.VisibilityPrivate,
IsSCMPrivate: true,
Perm: &model.Perm{
Pull: true,
Expand Down