From ae3627fd2aaedc1b53ecbff5389d85433d123869 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 30 Dec 2024 21:56:43 +0800 Subject: [PATCH] clarify HasOrgProfileReadme vs HasUserProfileReadme, fix tests --- routers/web/org/home.go | 84 ++++++++++++--------------- routers/web/org/members.go | 4 +- routers/web/org/teams.go | 4 +- routers/web/shared/user/header.go | 32 +++++----- routers/web/user/profile.go | 2 +- templates/org/home.tmpl | 16 ++--- templates/org/menu.tmpl | 4 +- templates/user/overview/header.tmpl | 2 +- tests/integration/org_profile_test.go | 20 +++---- 9 files changed, 83 insertions(+), 85 deletions(-) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 037e53e214..800df79943 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/renderhelper" repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" @@ -21,9 +22,7 @@ import ( "code.gitea.io/gitea/services/context" ) -const ( - tplOrgHome templates.TplName = "org/home" -) +const tplOrgHome templates.TplName = "org/home" // Home show organization home page func Home(ctx *context.Context) { @@ -110,29 +109,13 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0 - err = shared_user.RenderOrgHeader(ctx) + prepareResult, err := shared_user.PrepareOrgHeader(ctx) if err != nil { - ctx.ServerError("RenderOrgHeader", err) + ctx.ServerError("PrepareOrgHeader", err) return } - isBothProfilesExist := ctx.Data["HasPublicProfileReadme"] == true && ctx.Data["HasPrivateProfileReadme"] == true - isViewerMember := ctx.FormString("view_as") - ctx.Data["IsViewerMember"] = isViewerMember == "member" - - viewAsPrivate := isViewerMember == "member" - - if !isBothProfilesExist { - if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories}) { - if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories, viewAsPrivate: true}) { - ctx.Data["PageIsViewRepositories"] = true - } - } - } else { - if !prepareOrgProfileReadme(ctx, prepareOrgProfileReadmeOptions{viewRepositories: viewRepositories, viewAsPrivate: viewAsPrivate}) { - ctx.Data["PageIsViewRepositories"] = true - } - } + ctx.Data["HasPrivateProfileReadme"] = viewRepositories || !prepareOrgProfileReadme(ctx, prepareResult) var ( repos []*repo_model.Repository @@ -171,36 +154,45 @@ func home(ctx *context.Context, viewRepositories bool) { ctx.HTML(http.StatusOK, tplOrgHome) } -type prepareOrgProfileReadmeOptions struct { - viewRepositories bool - viewAsPrivate bool -} +func prepareOrgProfileReadme(ctx *context.Context, prepareResult *shared_user.PrepareOrgHeaderResult) bool { + viewAs := ctx.FormString("view_as") + viewAsMember := viewAs == "member" -func prepareOrgProfileReadme(ctx *context.Context, opts prepareOrgProfileReadmeOptions) bool { - profileRepoName := util.Iif(opts.viewAsPrivate, shared_user.RepoNameProfilePrivate, shared_user.RepoNameProfile) - profileDbRepo, profileReadme := shared_user.FindOwnerProfileReadme(ctx, ctx.Doer, profileRepoName) - - if opts.viewAsPrivate { - ctx.Data["HasPrivateProfileReadme"] = profileReadme != nil + var profileRepo *repo_model.Repository + var readmeBlob *git.Blob + if viewAsMember { + if prepareResult.ProfilePrivateReadmeBlob != nil { + profileRepo, readmeBlob = prepareResult.ProfilePrivateRepo, prepareResult.ProfilePrivateReadmeBlob + } else { + profileRepo, readmeBlob = prepareResult.ProfilePublicRepo, prepareResult.ProfilePublicReadmeBlob + viewAsMember = false + } } else { - ctx.Data["HasPublicProfileReadme"] = profileReadme != nil + if prepareResult.ProfilePublicReadmeBlob != nil { + profileRepo, readmeBlob = prepareResult.ProfilePublicRepo, prepareResult.ProfilePublicReadmeBlob + } else { + profileRepo, readmeBlob = prepareResult.ProfilePrivateRepo, prepareResult.ProfilePrivateReadmeBlob + viewAsMember = true + } } - - if profileReadme == nil || opts.viewRepositories { + if readmeBlob == nil { return false } - if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { - log.Error("failed to GetBlobContent for %s profile readme: %v", profileRepoName, err) - } else { - rctx := renderhelper.NewRenderContextRepoFile(ctx, profileDbRepo, renderhelper.RepoFileOptions{ - CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), - }) - if profileContent, err := markdown.RenderString(rctx, bytes); err != nil { - log.Error("failed to RenderString for %s profile readme: %v", profileRepoName, err) - } else { - ctx.Data["ProfileReadmeContent"] = profileContent - } + readmeBytes, err := readmeBlob.GetBlobContent(setting.UI.MaxDisplayFileSize) + if err != nil { + log.Error("failed to GetBlobContent for %q profile (view as %q) readme: %v", ctx.ContextUser.Name, viewAs, err) + return false } + + rctx := renderhelper.NewRenderContextRepoFile(ctx, profileRepo, renderhelper.RepoFileOptions{ + CurrentRefPath: path.Join("branch", util.PathEscapeSegments(profileRepo.DefaultBranch)), + }) + ctx.Data["ProfileReadmeContent"], err = markdown.RenderString(rctx, readmeBytes) + if err != nil { + log.Error("failed to GetBlobContent for %q profile (view as %q) readme: %v", ctx.ContextUser.Name, viewAs, err) + return false + } + ctx.Data["IsViewingOrgAsMember"] = viewAsMember return true } diff --git a/routers/web/org/members.go b/routers/web/org/members.go index 5a134caecb..1665a12302 100644 --- a/routers/web/org/members.go +++ b/routers/web/org/members.go @@ -54,9 +54,9 @@ func Members(ctx *context.Context) { return } - err = shared_user.RenderOrgHeader(ctx) + _, err = shared_user.PrepareOrgHeader(ctx) if err != nil { - ctx.ServerError("RenderOrgHeader", err) + ctx.ServerError("PrepareOrgHeader", err) return } diff --git a/routers/web/org/teams.go b/routers/web/org/teams.go index 0137f2cc96..26031029d6 100644 --- a/routers/web/org/teams.go +++ b/routers/web/org/teams.go @@ -58,9 +58,9 @@ func Teams(ctx *context.Context) { } ctx.Data["Teams"] = ctx.Org.Teams - err := shared_user.RenderOrgHeader(ctx) + _, err := shared_user.PrepareOrgHeader(ctx) if err != nil { - ctx.ServerError("RenderOrgHeader", err) + ctx.ServerError("PrepareOrgHeader", err) return } diff --git a/routers/web/shared/user/header.go b/routers/web/shared/user/header.go index c9af52c7f0..ef36bcb108 100644 --- a/routers/web/shared/user/header.go +++ b/routers/web/shared/user/header.go @@ -142,7 +142,7 @@ func RenderUserHeader(ctx *context.Context) { prepareContextForCommonProfile(ctx) _, profileReadmeBlob := FindOwnerProfileReadme(ctx, ctx.Doer) - ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil + ctx.Data["HasUserProfileReadme"] = profileReadmeBlob != nil } func LoadHeaderCount(ctx *context.Context) error { @@ -184,18 +184,24 @@ const ( RepoNameProfile = ".profile" ) -func RenderOrgHeader(ctx *context.Context) error { - if err := LoadHeaderCount(ctx); err != nil { - return err +type PrepareOrgHeaderResult struct { + ProfilePublicRepo *repo_model.Repository + ProfilePublicReadmeBlob *git.Blob + ProfilePrivateRepo *repo_model.Repository + ProfilePrivateReadmeBlob *git.Blob + HasOrgProfileReadme bool +} + +func PrepareOrgHeader(ctx *context.Context) (result *PrepareOrgHeaderResult, err error) { + if err = LoadHeaderCount(ctx); err != nil { + return nil, err } - // FIXME: only do database query, do not open it - _, profileReadmeBlob := FindOwnerProfileReadme(ctx, ctx.Doer) - ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil - - // FIXME: only do database query, do not open it - _, profileReadmeBlob = FindOwnerProfileReadme(ctx, ctx.Doer, RepoNameProfilePrivate) - ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil - - return nil + result = &PrepareOrgHeaderResult{} + result.ProfilePublicRepo, result.ProfilePublicReadmeBlob = FindOwnerProfileReadme(ctx, ctx.Doer) + result.ProfilePrivateRepo, result.ProfilePrivateReadmeBlob = FindOwnerProfileReadme(ctx, ctx.Doer, RepoNameProfilePrivate) + result.HasOrgProfileReadme = result.ProfilePublicReadmeBlob != nil || result.ProfilePrivateReadmeBlob != nil + ctx.Data["HasOrgProfileReadme"] = result.HasOrgProfileReadme // many pages need it to show the "overview" tab + ctx.Data["ShowOrgProfileReadmeSelector"] = result.ProfilePublicReadmeBlob != nil && result.ProfilePrivateReadmeBlob != nil + return result, nil } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index e11a0c86ea..006ffdcf7e 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -95,7 +95,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb } } ctx.Data["TabName"] = tab - ctx.Data["HasPublicProfileReadme"] = profileReadme != nil + ctx.Data["HasUserProfileReadme"] = profileReadme != nil page := ctx.FormInt("page") if page <= 0 { diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 0a2d6f0b97..c5a7ae01f1 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -6,7 +6,7 @@
{{if .ProfileReadmeContent}} -
{{.ProfileReadmeContent}}
+
{{.ProfileReadmeContent}}
{{end}} {{template "shared/repo_search" .}} {{template "explore/repo_list" .}} @@ -25,26 +25,26 @@
{{end}} - {{if and .ShowMemberAndTeamTab .HasPublicProfileReadme .HasPrivateProfileReadme}} + {{if and .ShowMemberAndTeamTab .ShowOrgProfileReadmeSelector}}
- {{if .IsViewerMember}}{{ctx.Locale.Tr "org.view_as_member_hint"}}{{else}}{{ctx.Locale.Tr "org.view_as_public_hint"}}{{end}} + {{if .IsViewingOrgAsMember}}{{ctx.Locale.Tr "org.view_as_member_hint"}}{{else}}{{ctx.Locale.Tr "org.view_as_public_hint"}}{{end}}
{{end}} diff --git a/templates/org/menu.tmpl b/templates/org/menu.tmpl index a9b925ed78..4a8aee68a7 100644 --- a/templates/org/menu.tmpl +++ b/templates/org/menu.tmpl @@ -1,12 +1,12 @@
- {{if or .HasPublicProfileReadme .HasPrivateProfileReadme}} + {{if .HasOrgProfileReadme}} {{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}} {{end}} - + {{svg "octicon-repo"}} {{ctx.Locale.Tr "user.repositories"}} {{if .RepoCount}}
{{.RepoCount}}
diff --git a/templates/user/overview/header.tmpl b/templates/user/overview/header.tmpl index ee28f32ed1..f4664c704d 100644 --- a/templates/user/overview/header.tmpl +++ b/templates/user/overview/header.tmpl @@ -1,6 +1,6 @@
- {{if and .HasPublicProfileReadme .ContextUser.IsIndividual}} + {{if and .HasUserProfileReadme .ContextUser.IsIndividual}} {{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}} diff --git a/tests/integration/org_profile_test.go b/tests/integration/org_profile_test.go index 5b7aa8b7c2..f56955c2c9 100644 --- a/tests/integration/org_profile_test.go +++ b/tests/integration/org_profile_test.go @@ -69,7 +69,7 @@ func testOrgProfile(t *testing.T, u *url.URL) { resp := MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) - profileDivs := htmlDoc.doc.Find("#user-content-public") + profileDivs := htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 1, profileDivs.Length()) dropDownDiv := htmlDoc.doc.Find("#org-home-view-as-dropdown") @@ -81,7 +81,7 @@ func testOrgProfile(t *testing.T, u *url.URL) { resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 1, profileDivs.Length()) dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown") @@ -93,7 +93,7 @@ func testOrgProfile(t *testing.T, u *url.URL) { resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 1, profileDivs.Length()) dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown") @@ -103,14 +103,14 @@ func testOrgProfile(t *testing.T, u *url.URL) { resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-private") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=true]") assert.EqualValues(t, 1, profileDivs.Length()) req = NewRequest(t, "GET", "/org3?view_as=public") resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 1, profileDivs.Length()) // PART 2: Each org has either one of private pr public profile @@ -121,7 +121,7 @@ func testOrgProfile(t *testing.T, u *url.URL) { req = NewRequest(t, "GET", "/org41") resp = MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 1, profileDivs.Length()) dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown") assert.EqualValues(t, 0, dropDownDiv.Length()) @@ -129,9 +129,9 @@ func testOrgProfile(t *testing.T, u *url.URL) { req = NewRequest(t, "GET", "/org42") resp = MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 0, profileDivs.Length()) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 0, profileDivs.Length()) dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown") assert.EqualValues(t, 0, dropDownDiv.Length()) @@ -140,7 +140,7 @@ func testOrgProfile(t *testing.T, u *url.URL) { req = NewRequest(t, "GET", "/org41") resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-public") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=false]") assert.EqualValues(t, 1, profileDivs.Length()) dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown") assert.EqualValues(t, 0, dropDownDiv.Length()) @@ -148,7 +148,7 @@ func testOrgProfile(t *testing.T, u *url.URL) { req = NewRequest(t, "GET", "/org42") resp = session.MakeRequest(t, req, http.StatusOK) htmlDoc = NewHTMLParser(t, resp.Body) - profileDivs = htmlDoc.doc.Find("#user-content-private") + profileDivs = htmlDoc.doc.Find("[data-profile-view-as-member=true]") assert.EqualValues(t, 1, profileDivs.Length()) dropDownDiv = htmlDoc.doc.Find("#org-home-view-as-dropdown") assert.EqualValues(t, 0, dropDownDiv.Length())