1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-01-04 09:17:43 +03:00

Compare commits

...

10 Commits

Author SHA1 Message Date
Chai-Shi
440753042c
Merge 48b4be2e64 into a96776b3cb 2024-12-29 19:07:49 +08:00
iknowright
48b4be2e64 adding profile test case 2024-12-29 02:54:34 +08:00
changchaishi
e95d7166ed Ignore view_as param when the profiles not both exist 2024-12-29 02:54:34 +08:00
changchaishi
86e698dc16 add rollback to get private profile when view as is not present 2024-12-29 02:54:34 +08:00
changchaishi
d71cba7620 add check icons and translation 2024-12-29 02:54:34 +08:00
changchaishi
4b3a8ac3f0 fix that drop down manu shows in repository tab 2024-12-29 02:54:34 +08:00
changchaishi
6496b3c128 Add hint for public and private profile repo name 2024-12-29 02:54:34 +08:00
Ben Chang
ef826a1d0c make sure the drop down appears only when criteria are met 2024-12-29 02:54:34 +08:00
Ben Chang
3682adb01c make query string right 2024-12-29 02:54:34 +08:00
Ben Chang
784bedd1b8 Use view as to get public and private profile repo 2024-12-29 02:54:34 +08:00
12 changed files with 266 additions and 25 deletions

View File

@ -1015,6 +1015,8 @@ new_repo_helper = A repository contains all project files, including revision hi
owner = Owner
owner_helper = Some organizations may not show up in the dropdown due to a maximum repository count limit.
repo_name = Repository Name
repo_name_public_profile_hint=.profile is a special repository that you can use to add a README.md to your personal or organization publiv profile. Make sure it's public and initialize it with a README to get started.
repo_name_private_profile_hint=.profile-private is a special repository that you can use to add a README.md to your organization member profile. Make sure it's private and initialize it with a README to get started.
repo_name_helper = Good repository names use short, memorable and unique keywords.
repo_size = Repository Size
template = Template

View File

@ -5,6 +5,7 @@ package org
import (
"fmt"
html_template "html/template"
"net/http"
"path"
"strings"
@ -111,8 +112,38 @@ func home(ctx *context.Context, viewRepositories bool) {
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0
if !prepareOrgProfileReadme(ctx, viewRepositories) {
ctx.Data["PageIsViewRepositories"] = true
currentURL := ctx.Req.URL
queryParams := currentURL.Query()
queryParams.Set("view_as", "member")
ctx.Data["QueryForMember"] = html_template.URL(queryParams.Encode())
queryParams.Set("view_as", "public")
ctx.Data["QueryForPublic"] = html_template.URL(queryParams.Encode())
err = shared_user.RenderOrgHeader(ctx)
if err != nil {
ctx.ServerError("RenderOrgHeader", err)
return
}
isBothProfilesExist := ctx.Data["HasPublicProfileReadme"] == true && ctx.Data["HasPrivateProfileReadme"] == true
isViewerMember := ctx.FormString("view_as")
ctx.Data["IsViewerMember"] = isViewerMember == "member"
profileType := "Public"
if isViewerMember == "member" {
profileType = "Private"
}
if !isBothProfilesExist {
if !prepareOrgProfileReadme(ctx, viewRepositories, "Public") {
if !prepareOrgProfileReadme(ctx, viewRepositories, "Private") {
ctx.Data["PageIsViewRepositories"] = true
}
}
} else {
if !prepareOrgProfileReadme(ctx, viewRepositories, profileType) {
ctx.Data["PageIsViewRepositories"] = true
}
}
var (
@ -168,28 +199,26 @@ func home(ctx *context.Context, viewRepositories bool) {
ctx.HTML(http.StatusOK, tplOrgHome)
}
func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool) bool {
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
func prepareOrgProfileReadme(ctx *context.Context, viewRepositories bool, profileType string) bool {
profileDbRepo, profileGitRepo, profileReadme, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, profileType)
defer profileClose()
ctx.Data["HasProfileReadme"] = profileReadme != nil
ctx.Data[fmt.Sprintf("Has%sProfileReadme", profileType)] = profileReadme != nil
if profileGitRepo == nil || profileReadme == nil || viewRepositories {
return false
}
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
log.Error("failed to GetBlobContent: %v", err)
log.Error("failed to GetBlobContent for %s profile readme: %v", profileType, 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: %v", err)
log.Error("failed to RenderString for %s profile readme: %v", profileType, err)
} else {
ctx.Data["ProfileReadme"] = profileContent
ctx.Data[fmt.Sprintf("%sProfileReadme", profileType)] = profileContent
}
}
ctx.Data["PageIsViewOverview"] = true
return true
}

View File

@ -102,8 +102,12 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
}
}
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ".profile")
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User, profileType string) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
profileName := ".profile"
if profileType != "Public" {
profileName = ".profile-private"
}
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, profileName)
if err == nil {
perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer)
if err == nil && !profileDbRepo.IsEmpty && perm.CanRead(unit.TypeCode) {
@ -130,9 +134,9 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile
func RenderUserHeader(ctx *context.Context) {
prepareContextForCommonProfile(ctx)
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
defer profileClose()
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
}
func LoadHeaderCount(ctx *context.Context) error {
@ -174,9 +178,13 @@ func RenderOrgHeader(ctx *context.Context) error {
return err
}
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer, "Public")
defer profileClose()
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
ctx.Data["HasPublicProfileReadme"] = profileReadmeBlob != nil
_, _, profileReadmeBlob, profileClose = FindUserProfileReadme(ctx, ctx.Doer, "Private")
defer profileClose()
ctx.Data["HasPrivateProfileReadme"] = profileReadmeBlob != nil
return nil
}

View File

@ -74,7 +74,7 @@ func userProfile(ctx *context.Context) {
ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
}
profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
profileDbRepo, _ /*profileGitRepo*/, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer, "Public")
defer profileClose()
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
@ -96,7 +96,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
}
}
ctx.Data["TabName"] = tab
ctx.Data["HasProfileReadme"] = profileReadme != nil
ctx.Data["HasPublicProfileReadme"] = profileReadme != nil
page := ctx.FormInt("page")
if page <= 0 {
@ -254,7 +254,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
if profileContent, err := markdown.RenderString(rctx, bytes); err != nil {
log.Error("failed to RenderString: %v", err)
} else {
ctx.Data["ProfileReadme"] = profileContent
ctx.Data["PublicProfileReadme"] = profileContent
}
}
case "organizations":

View File

@ -5,8 +5,25 @@
<div class="ui container">
<div class="ui mobile reversed stackable grid">
<div class="ui {{if .ShowMemberAndTeamTab}}eleven wide{{end}} column">
{{if .ProfileReadme}}
<div id="readme_profile" class="markup">{{.ProfileReadme}}</div>
{{if or .PublicProfileReadme .PrivateProfileReadme}}
{{if and .ShowMemberAndTeamTab .HasPublicProfileReadme .HasPrivateProfileReadme}}
<div class="ui small secondary filter menu">
<div id="profile_view_as_dropdown" class="item ui small dropdown jump" style="padding-bottom: 1em;">
{{svg "octicon-eye" 14 "view as icon"}}<span class="text">View as: {{if not .IsViewerMember}}{{ctx.Locale.Tr "settings.visibility.public"}}{{else}}{{ctx.Locale.Tr "org.members.member"}}{{end}}</span>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
<a href="{{$.Org.HomeLink}}?{{.QueryForPublic}}" class="{{if not .IsViewerMember}}active {{end}}item"><input hidden type="radio" {{if not .IsViewerMember}}checked{{end}}> {{ctx.Locale.Tr "settings.visibility.public"}} {{if not .IsViewerMember}}{{svg "octicon-check" 14 "check icon"}}{{end}}</a>
<a href="{{$.Org.HomeLink}}?{{.QueryForMember}}" class="{{if .IsViewerMember}}active {{end}}item"><input hidden type="radio" {{if .IsViewerMember}}checked{{end}}> {{ctx.Locale.Tr "org.members.member"}} {{if .IsViewerMember}}{{svg "octicon-check" 14 "check icon"}}{{end}}</a>
</div>
</div>
</div>
{{end}}
{{end}}
{{if .PrivateProfileReadme}}
<div id="readme_profile" class="markup">{{.PrivateProfileReadme}}</div>
{{end}}
{{if .PublicProfileReadme}}
<div id="readme_profile" class="markup">{{.PublicProfileReadme}}</div>
{{end}}
{{template "shared/repo_search" .}}
{{template "explore/repo_list" .}}

View File

@ -1,12 +1,12 @@
<div class="ui container">
<overflow-menu class="ui secondary pointing tabular borderless menu tw-mb-4">
<div class="overflow-menu-items">
{{if .HasProfileReadme}}
{{if or .HasPublicProfileReadme .HasPrivateProfileReadme}}
<a class="{{if .PageIsViewOverview}}active {{end}}item" href="{{$.Org.HomeLink}}">
{{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}}
</a>
{{end}}
<a class="{{if .PageIsViewRepositories}}active {{end}}item" href="{{$.Org.HomeLink}}{{if .HasProfileReadme}}/-/repositories{{end}}">
<a class="{{if .PageIsViewRepositories}}active {{end}}item" href="{{$.Org.HomeLink}}{{if or .HasPublicProfileReadme .HasPrivateProfileReadme}}/-/repositories{{end}}">
{{svg "octicon-repo"}} {{ctx.Locale.Tr "user.repositories"}}
{{if .RepoCount}}
<div class="ui small label">{{.RepoCount}}</div>

View File

@ -44,6 +44,8 @@
<div class="inline required field {{if .Err_RepoName}}error{{end}}">
<label for="repo_name">{{ctx.Locale.Tr "repo.repo_name"}}</label>
<input id="repo_name" name="repo_name" value="{{.repo_name}}" autofocus required maxlength="100">
<span id="repo_name_public_profile_hint" style="display:none" class="help">{{ctx.Locale.Tr "repo.repo_name_public_profile_hint"}}</span>
<span id="repo_name_private_profile_hint" style="display:none" class="help">{{ctx.Locale.Tr "repo.repo_name_private_profile_hint"}}</span>
<span class="help">{{ctx.Locale.Tr "repo.repo_name_helper"}}</span>
</div>
<div class="inline field">

View File

@ -1,6 +1,6 @@
<overflow-menu class="ui secondary pointing tabular borderless menu">
<div class="overflow-menu-items">
{{if and .HasProfileReadme .ContextUser.IsIndividual}}
{{if and .HasPublicProfileReadme .ContextUser.IsIndividual}}
<a class="{{if eq .TabName "overview"}}active {{end}}item" href="{{.ContextUser.HomeLink}}?tab=overview">
{{svg "octicon-info"}} {{ctx.Locale.Tr "user.overview"}}
</a>

View File

@ -26,7 +26,7 @@
{{else if eq .TabName "followers"}}
{{template "repo/user_cards" .}}
{{else if eq .TabName "overview"}}
<div id="readme_profile" class="markup">{{.ProfileReadme}}</div>
<div id="readme_profile" class="markup">{{.PublicProfileReadme}}</div>
{{else if eq .TabName "organizations"}}
{{template "repo/user_cards" .}}
{{else}}

View File

@ -0,0 +1,163 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"testing"
"time"
auth_model "code.gitea.io/gitea/models/auth"
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
)
func getCreateProfileReadmeFileOptions(profileType string) api.CreateFileOptions {
content := fmt.Sprintf("# %s", profileType)
contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
return api.CreateFileOptions{
FileOptions: api.FileOptions{
BranchName: "main",
NewBranchName: "main",
Message: "create the profile README.md",
Dates: api.CommitDateOptions{
Author: time.Unix(946684810, 0),
Committer: time.Unix(978307190, 0),
},
},
ContentBase64: contentEncoded,
}
}
func createTestProfile(t *testing.T, orgName, profileType string) {
repoName := ".profile"
isPrivate := false
if profileType == "Private" {
repoName = ".profile-private"
isPrivate = true
}
ctx := NewAPITestContext(t, "user1", repoName, auth_model.AccessTokenScopeAll)
session := loginUser(t, "user1")
tokenAdmin := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
// create repo
t.Run("CreateOrganization"+profileType+"ProfileRepo", doAPICreateOrganizationRepository(ctx, orgName, &api.CreateRepoOption{
Name: repoName,
Private: isPrivate,
}))
// create readme
createFileOptions := getCreateProfileReadmeFileOptions(profileType)
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", orgName, repoName, "README.md"), &createFileOptions).
AddTokenAuth(tokenAdmin)
MakeRequest(t, req, http.StatusCreated)
}
func TestOrgProfile(t *testing.T) {
onGiteaRun(t, testOrgProfile)
}
func testOrgProfile(t *testing.T, u *url.URL) {
// html #user-content-public (markdown title of public profile)
// html #user-content-private (markdown title of private profile)
// html #profile_view_as_dropdown (indicate whether the view as dropdown menu is present)
// PART 1: Test Both Private and Public
createTestProfile(t, "org3", "Public")
createTestProfile(t, "org3", "Private")
// Anonymous User
req := NewRequest(t, "GET", "org3")
resp := MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
profileDivs := htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv := htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
// Logged in but not member
session := loginUser(t, "user24")
req = NewRequest(t, "GET", "org3")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
// Site Admin
session = loginUser(t, "user1")
req = NewRequest(t, "GET", "org3")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 1, dropDownDiv.Length())
req = NewRequest(t, "GET", "/org3?view_as=member")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-private")
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")
assert.EqualValues(t, 1, profileDivs.Length())
// PART 2: Each org has either one of private pr public profile
createTestProfile(t, "org41", "Public")
createTestProfile(t, "org42", "Private")
// Anonymous User
req = NewRequest(t, "GET", "/org41")
resp = MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
req = NewRequest(t, "GET", "/org42")
resp = MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 0, profileDivs.Length())
profileDivs = htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 0, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
// Site Admin
req = NewRequest(t, "GET", "/org41")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-public")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
req = NewRequest(t, "GET", "/org42")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
profileDivs = htmlDoc.doc.Find("#user-content-private")
assert.EqualValues(t, 1, profileDivs.Length())
dropDownDiv = htmlDoc.doc.Find("#profile_view_as_dropdown")
assert.EqualValues(t, 0, dropDownDiv.Length())
}

View File

@ -0,0 +1,18 @@
const repoName = document.querySelector<HTMLInputElement>('#repo_name');
const repoPublicHint = document.querySelector<HTMLInputElement>('#repo_name_public_profile_hint');
const repoPrivateHint = document.querySelector<HTMLInputElement>('#repo_name_private_profile_hint');
export function initRepoCreate() {
repoName?.addEventListener('input', () => {
if (repoName?.value === '.profile') {
repoPublicHint.style.display = 'inline-block';
} else {
repoPublicHint.style.display = 'none';
}
if (repoName?.value === '.profile-private') {
repoPrivateHint.style.display = 'inline-block';
} else {
repoPrivateHint.style.display = 'none';
}
});
}

View File

@ -35,6 +35,7 @@ import {initRepoEllipsisButton, initCommitStatuses} from './features/repo-commit
import {initRepoTopicBar} from './features/repo-home.ts';
import {initAdminCommon} from './features/admin/common.ts';
import {initRepoTemplateSearch} from './features/repo-template.ts';
import {initRepoCreate} from './features/repo-create.ts';
import {initRepoCodeView} from './features/repo-code.ts';
import {initSshKeyFormParser} from './features/sshkey-helper.ts';
import {initUserSettings} from './features/user-settings.ts';
@ -173,6 +174,7 @@ onDomReady(() => {
initRepoActivityTopAuthorsChart,
initRepoArchiveLinks,
initRepoBranchButton,
initRepoCreate,
initRepoCodeView,
initBranchSelectorTabs,
initRepoEllipsisButton,