Hide activity contributors, recent commits and code frequrency left tabs if there is no code permission (#34053)

When a team have no code unit permission of a repository, the member of
the team should not view activity contributors, recent commits and code
frequrency.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
(cherry picked from commit 49899070cd600e7b7bd31a750f4d49de1722db23)
This commit is contained in:
Lunny Xiao 2025-03-28 21:04:40 -07:00 committed by Gusted
parent adc2a215c1
commit b296354873
No known key found for this signature in database
GPG key ID: FD821B732837125F
4 changed files with 15 additions and 26 deletions

View file

@ -34,7 +34,7 @@ func CodeFrequencyData(ctx *context.Context) {
ctx.Status(http.StatusAccepted) ctx.Status(http.StatusAccepted)
return return
} }
ctx.ServerError("GetCodeFrequencyData", err) ctx.ServerError("GetContributorStats", err)
} else { } else {
ctx.JSON(http.StatusOK, contributorStats["total"].Weeks) ctx.JSON(http.StatusOK, contributorStats["total"].Weeks)
} }

View file

@ -4,12 +4,10 @@
package repo package repo
import ( import (
"errors"
"net/http" "net/http"
"forgejo.org/modules/base" "forgejo.org/modules/base"
"forgejo.org/services/context" "forgejo.org/services/context"
contributors_service "forgejo.org/services/repository"
) )
const ( const (
@ -26,16 +24,3 @@ func RecentCommits(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplRecentCommits) ctx.HTML(http.StatusOK, tplRecentCommits)
} }
// RecentCommitsData returns JSON of recent commits data
func RecentCommitsData(ctx *context.Context) {
if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.CommitID); err != nil {
if errors.Is(err, contributors_service.ErrAwaitGeneration) {
ctx.Status(http.StatusAccepted)
return
}
ctx.ServerError("RecentCommitsData", err)
} else {
ctx.JSON(http.StatusOK, contributorStats["total"].Weeks)
}
}

View file

@ -1455,7 +1455,7 @@ func registerRoutes(m *web.Route) {
}, repo.MustBeNotEmpty, context.RequireRepoReaderOr(unit.TypeCode)) }, repo.MustBeNotEmpty, context.RequireRepoReaderOr(unit.TypeCode))
m.Group("/recent-commits", func() { m.Group("/recent-commits", func() {
m.Get("", repo.RecentCommits) m.Get("", repo.RecentCommits)
m.Get("/data", repo.RecentCommitsData) m.Get("/data", repo.CodeFrequencyData)
}, repo.MustBeNotEmpty, context.RequireRepoReaderOr(unit.TypeCode)) }, repo.MustBeNotEmpty, context.RequireRepoReaderOr(unit.TypeCode))
}, context.RepoRef(), context.RequireRepoReaderOr(unit.TypeCode, unit.TypePullRequests, unit.TypeIssues, unit.TypeReleases)) }, context.RepoRef(), context.RequireRepoReaderOr(unit.TypeCode, unit.TypePullRequests, unit.TypeIssues, unit.TypeReleases))

View file

@ -1,14 +1,18 @@
{{$canReadCode := $.Permission.CanRead $.UnitTypeCode}}
<div class="ui fluid vertical menu"> <div class="ui fluid vertical menu">
<a class="{{if .PageIsPulse}}active {{end}}item" href="{{.RepoLink}}/activity"> <a class="{{if .PageIsPulse}}active {{end}}item" href="{{.RepoLink}}/activity">
{{ctx.Locale.Tr "repo.activity.navbar.pulse"}} {{ctx.Locale.Tr "repo.activity.navbar.pulse"}}
</a> </a>
<a class="{{if .PageIsContributors}}active {{end}}item" href="{{.RepoLink}}/activity/contributors"> {{if $canReadCode}}
{{ctx.Locale.Tr "repo.activity.navbar.contributors"}} <a class="{{if .PageIsContributors}}active {{end}}item" href="{{.RepoLink}}/activity/contributors">
</a> {{ctx.Locale.Tr "repo.activity.navbar.contributors"}}
<a class="{{if .PageIsCodeFrequency}}active{{end}} item" href="{{.RepoLink}}/activity/code-frequency"> </a>
{{ctx.Locale.Tr "repo.activity.navbar.code_frequency"}} <a class="{{if .PageIsCodeFrequency}}active{{end}} item" href="{{.RepoLink}}/activity/code-frequency">
</a> {{ctx.Locale.Tr "repo.activity.navbar.code_frequency"}}
<a class="{{if .PageIsRecentCommits}}active{{end}} item" href="{{.RepoLink}}/activity/recent-commits"> </a>
{{ctx.Locale.Tr "repo.activity.navbar.recent_commits"}} <a class="{{if .PageIsRecentCommits}}active{{end}} item" href="{{.RepoLink}}/activity/recent-commits">
</a> {{ctx.Locale.Tr "repo.activity.navbar.recent_commits"}}
</a>
{{end}}
</div> </div>