From 9563008e94f6ada6c75d2126d8e68758736d18de Mon Sep 17 00:00:00 2001 From: "Navid Khan (Ali)" <60997541+Sun-Wukhan@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:18:57 -0500 Subject: [PATCH] Set Visibility in Gitea and Forgejo repo converters Fixes #7584 Signed-off-by: Navid Khan (Ali) <60997541+Sun-Wukhan@users.noreply.github.com> --- server/forge/forgejo/helper.go | 6 +++- server/forge/forgejo/helper_test.go | 52 +++++++++++++++++++++++++++++ server/forge/forgejo/parse_test.go | 19 +++++++++++ server/forge/gitea/helper.go | 6 +++- server/forge/gitea/helper_test.go | 52 +++++++++++++++++++++++++++++ server/forge/gitea/parse_test.go | 11 ++++++ 6 files changed, 144 insertions(+), 2 deletions(-) diff --git a/server/forge/forgejo/helper.go b/server/forge/forgejo/helper.go index bf8b2e24ff8..692d2b6618b 100644 --- a/server/forge/forgejo/helper.go +++ b/server/forge/forgejo/helper.go @@ -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, @@ -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. diff --git a/server/forge/forgejo/helper_test.go b/server/forge/forgejo/helper_test.go index bf02415b713..a32067ed437 100644 --- a/server/forge/forgejo/helper_test.go +++ b/server/forge/forgejo/helper_test.go @@ -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 diff --git a/server/forge/forgejo/parse_test.go b/server/forge/forgejo/parse_test.go index 19c0e4dec7b..910c9367575 100644 --- a/server/forge/forgejo/parse_test.go +++ b/server/forge/forgejo/parse_test.go @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/server/forge/gitea/helper.go b/server/forge/gitea/helper.go index 2d6b69f422d..bec9cb170a4 100644 --- a/server/forge/gitea/helper.go +++ b/server/forge/gitea/helper.go @@ -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, @@ -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. diff --git a/server/forge/gitea/helper_test.go b/server/forge/gitea/helper_test.go index fb3b40dcf9f..3551dd5eeec 100644 --- a/server/forge/gitea/helper_test.go +++ b/server/forge/gitea/helper_test.go @@ -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 diff --git a/server/forge/gitea/parse_test.go b/server/forge/gitea/parse_test.go index 980003f4f31..98ee18e3ab1 100644 --- a/server/forge/gitea/parse_test.go +++ b/server/forge/gitea/parse_test.go @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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,