Skip to content

Commit eb3054e

Browse files
committed
Merge branch 'patch-1' of github.com:snematoda/gitea into snematoda-patch-1
2 parents 3084c99 + 50049de commit eb3054e

File tree

13 files changed

+390
-21
lines changed

13 files changed

+390
-21
lines changed

models/repo/repo_list.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,31 @@ func GetUserRepositories(ctx context.Context, opts *SearchRepoOptions) (Reposito
745745
repos := make(RepositoryList, 0, opts.PageSize)
746746
return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
747747
}
748+
749+
func GetPrimaryRepoLanguageList(ctx context.Context, ownerID int64, user *user_model.User) (LanguageStatList, error) {
750+
languageList := make(LanguageStatList, 0)
751+
752+
q := db.GetEngine(ctx).
753+
Table("language_stat").
754+
Cols("language").
755+
Where(builder.Eq{"is_primary": true})
756+
757+
if ownerID > 0 {
758+
ownerIDs, err := FindUserCodeAccessibleOwnerRepoIDs(ctx, ownerID, user)
759+
if err != nil {
760+
return nil, err
761+
}
762+
763+
q = q.In("repo_id", ownerIDs)
764+
}
765+
766+
err := q.Distinct("language").
767+
OrderBy("language").
768+
Find(&languageList)
769+
if err != nil {
770+
return nil, err
771+
}
772+
773+
languageList.LoadAttributes()
774+
return languageList, nil
775+
}

modules/structs/miscellaneous.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ type LicenseTemplateInfo struct {
9494
Body string `json:"body"`
9595
}
9696

97+
// LanguagesInfo contains information about a Language
98+
type LanguageInfo struct {
99+
Name string `json:"name"`
100+
Color string `json:"color"`
101+
}
102+
97103
// APIError is an api error with a message
98104
type APIError struct {
99105
Message string `json:"message"`

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ func Routes() *web.Route {
907907
m.Get("/heatmap", user.GetUserHeatmapData)
908908
}
909909

910+
m.Get("/languages", repo.GetUserPrimaryLanguageList)
910911
m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignIn(), user.ListUserRepos)
911912
m.Group("/tokens", func() {
912913
m.Combo("").Get(user.ListAccessTokens).
@@ -1044,6 +1045,7 @@ func Routes() *web.Route {
10441045
// Repos (requires repo scope)
10451046
m.Group("/repos", func() {
10461047
m.Get("/search", repo.Search)
1048+
m.Get("/languages", repo.ListPrimaryLanguages)
10471049

10481050
// (repo scope)
10491051
m.Post("/migrate", reqToken(), bind(api.MigrateRepoOptions{}), repo.Migrate)
@@ -1446,6 +1448,7 @@ func Routes() *web.Route {
14461448
m.Get("/registration-token", reqToken(), reqOrgOwnership(), org.GetRegistrationToken)
14471449
})
14481450
})
1451+
m.Get("/languages", repo.GetOrgPrimaryLanguageList)
14491452
m.Group("/public_members", func() {
14501453
m.Get("", org.ListPublicMembers)
14511454
m.Combo("/{username}").Get(org.IsPublicMember).

routers/api/v1/repo/languages.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
"net/http"
8+
9+
repo_model "code.gitea.io/gitea/models/repo"
10+
"code.gitea.io/gitea/modules/context"
11+
api "code.gitea.io/gitea/modules/structs"
12+
)
13+
14+
func SendPrimaryLanguageList(ctx *context.APIContext, ownerID int64) {
15+
langs, err := repo_model.GetPrimaryRepoLanguageList(ctx, ownerID, ctx.Doer)
16+
if err != nil {
17+
ctx.InternalServerError(err)
18+
}
19+
20+
list := []api.LanguageInfo{}
21+
22+
for _, i := range langs {
23+
list = append(list, api.LanguageInfo{
24+
Name: i.Language,
25+
Color: i.Color,
26+
})
27+
}
28+
29+
ctx.JSON(http.StatusOK, list)
30+
}
31+
32+
// List all primary languages
33+
func ListPrimaryLanguages(ctx *context.APIContext) {
34+
// swagger:operation GET /repos/languages repository repoListPrimaryLanguages
35+
// ---
36+
// summary: Get primary languages and their color
37+
// produces:
38+
// - application/json
39+
// responses:
40+
// "200":
41+
// "$ref": "#/responses/PrimaryLanguageList"
42+
43+
SendPrimaryLanguageList(ctx, 0)
44+
}
45+
46+
// List user primary languages
47+
func GetUserPrimaryLanguageList(ctx *context.APIContext) {
48+
// swagger:operation GET /users/{username}/languages user userListPrimaryLanguages
49+
// ---
50+
// summary: Get primary languages and their color
51+
// produces:
52+
// - application/json
53+
// parameters:
54+
// - name: username
55+
// in: path
56+
// description: username of user to get
57+
// type: string
58+
// required: true
59+
// responses:
60+
// "404":
61+
// "$ref": "#/responses/notFound"
62+
// "200":
63+
// "$ref": "#/responses/PrimaryLanguageList"
64+
65+
SendPrimaryLanguageList(ctx, ctx.ContextUser.ID)
66+
}
67+
68+
// List org primary languages
69+
func GetOrgPrimaryLanguageList(ctx *context.APIContext) {
70+
// swagger:operation GET /orgs/{org}/languages organization orgListPrimaryLanguages
71+
// ---
72+
// summary: Get primary languages and their color
73+
// produces:
74+
// - application/json
75+
// parameters:
76+
// - name: org
77+
// in: path
78+
// description: name of the organization to get
79+
// type: string
80+
// required: true
81+
// responses:
82+
// "404":
83+
// "$ref": "#/responses/notFound"
84+
// "200":
85+
// "$ref": "#/responses/PrimaryLanguageList"
86+
87+
SendPrimaryLanguageList(ctx, ctx.Org.Organization.ID)
88+
}

routers/api/v1/swagger/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ type swaggerLanguageStatistics struct {
345345
Body map[string]int64 `json:"body"`
346346
}
347347

348+
// PrimaryLanguageList
349+
// swagger:response PrimaryLanguageList
350+
type swaggerPrimaryLanguageList struct {
351+
// in: body
352+
Body []api.LanguageInfo `json:"body"`
353+
}
354+
348355
// CombinedStatus
349356
// swagger:response CombinedStatus
350357
type swaggerCombinedStatus struct {

routers/web/explore/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,17 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
142142
return
143143
}
144144

145+
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, 0, ctx.Doer)
146+
if err != nil {
147+
ctx.ServerError("GetPrimaryRepoLanguageList", err)
148+
return
149+
}
150+
145151
ctx.Data["Keyword"] = keyword
146152
ctx.Data["Total"] = count
147153
ctx.Data["Repos"] = repos
148154
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
155+
ctx.Data["ProgramLanguages"] = programLanguages
149156

150157
pager := context.NewPagination(int(count), opts.PageSize, page, 5)
151158
pager.SetDefaultParams(ctx)

routers/web/org/home.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,20 @@ func Home(ctx *context.Context) {
136136
isFollowing = user_model.IsFollowing(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
137137
}
138138

139+
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, org.ID, ctx.Doer)
140+
if err != nil {
141+
ctx.ServerError("GetPrimaryRepoLanguageList", err)
142+
return
143+
}
144+
139145
ctx.Data["Repos"] = repos
140146
ctx.Data["Total"] = count
141147
ctx.Data["Members"] = members
142148
ctx.Data["Teams"] = ctx.Org.Teams
143149
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
144150
ctx.Data["PageIsViewRepositories"] = true
145151
ctx.Data["IsFollowing"] = isFollowing
152+
ctx.Data["ProgramLanguages"] = programLanguages
146153

147154
err = shared_user.LoadHeaderCount(ctx)
148155
if err != nil {

routers/web/user/profile.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
275275
}
276276

277277
total = int(count)
278+
279+
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, ctx.ContextUser.ID, ctx.Doer)
280+
if err != nil {
281+
ctx.ServerError("GetPrimaryRepoLanguageList", err)
282+
return
283+
}
284+
ctx.Data["ProgramLanguages"] = programLanguages
278285
}
279286
ctx.Data["Repos"] = repos
280287
ctx.Data["Total"] = total

templates/explore/repo_search.tmpl

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div class="ui secondary filter menu gt-ac gt-mx-0">
2-
<form class="ui form ignore-dirty gt-f1">
1+
<div class="ui secondary filter menu explore-menu gt-ac gt-mx-0">
2+
<form class="ui form ignore-dirty gt-f1 explore-form">
33
<input type="hidden" name="sort" value="{{$.SortType}}">
44
<input type="hidden" name="language" value="{{$.Language}}">
55
<div class="ui fluid action input">
@@ -12,25 +12,50 @@
1212
<button class="ui primary button">{{ctx.Locale.Tr "explore.search"}}</button>
1313
</div>
1414
</form>
15-
<!-- Sort -->
16-
<div class="ui dropdown type jump item gt-mr-0">
17-
<span class="text">
18-
{{ctx.Locale.Tr "repo.issues.filter_sort"}}
19-
</span>
20-
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
21-
<div class="menu">
22-
<a class="{{if eq .SortType "newest"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=newest&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.latest"}}</a>
23-
<a class="{{if eq .SortType "oldest"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=oldest&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.oldest"}}</a>
24-
<a class="{{if eq .SortType "alphabetically"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=alphabetically&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
25-
<a class="{{if eq .SortType "reversealphabetically"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=reversealphabetically&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
26-
<a class="{{if eq .SortType "recentupdate"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=recentupdate&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.recentupdate"}}</a>
27-
<a class="{{if eq .SortType "leastupdate"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=leastupdate&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.leastupdate"}}</a>
28-
{{if not .DisableStars}}
29-
<a class="{{if eq .SortType "moststars"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=moststars&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.moststars"}}</a>
30-
<a class="{{if eq .SortType "feweststars"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=feweststars&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.feweststars"}}</a>
31-
{{end}}
32-
<a class="{{if eq .SortType "mostforks"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=mostforks&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.mostforks"}}</a>
33-
<a class="{{if eq .SortType "fewestforks"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=fewestforks&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.fewestforks"}}</a>
15+
16+
<div class="explore-filters gt-df">
17+
<!-- Language -->
18+
{{if .ProgramLanguages}}
19+
<div class="ui dropdown type jump item gt-mx-0">
20+
<span class="text">{{.locale.Tr "repo.repo_lang"}}</span>
21+
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
22+
<div class="menu short">
23+
<div class="ui icon search input">
24+
<i class="icon">{{svg "octicon-search" 16}}</i>
25+
<input type="text" placeholder="{{.locale.Tr "repo.repo_lang"}}">
26+
</div>
27+
<a class="{{if not $.Language}}active {{end}}item" href="{{$.Link}}?q={{$.Keyword}}&sort={{$.SortType}}">
28+
<span class="gt-df gt-ac gt-gap-3"><i class="color-icon gt-border-secondary" style="background-color: inherit;"></i>{{.locale.Tr "all"}}</span>
29+
</a>
30+
{{range .ProgramLanguages}}
31+
<a class="{{if eq $.Language .Language}}active {{end}}item" href="{{$.Link}}?q={{$.Keyword}}&language={{.Language}}&sort={{$.SortType}}">
32+
<span class="gt-df gt-ac gt-gap-3"><i class="color-icon" style="background-color: {{.Color}}"></i>{{.Language}}</span>
33+
</a>
34+
{{end}}
35+
</div>
36+
</div>
37+
{{end}}
38+
39+
<!-- Sort -->
40+
<div class="ui dropdown type jump item gt-mr-0">
41+
<span class="text">
42+
{{ctx.Locale.Tr "repo.issues.filter_sort"}}
43+
</span>
44+
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
45+
<div class="menu">
46+
<a class="{{if eq .SortType "newest"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=newest&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.latest"}}</a>
47+
<a class="{{if eq .SortType "oldest"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=oldest&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.oldest"}}</a>
48+
<a class="{{if eq .SortType "alphabetically"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=alphabetically&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.label.filter_sort.alphabetically"}}</a>
49+
<a class="{{if eq .SortType "reversealphabetically"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=reversealphabetically&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.label.filter_sort.reverse_alphabetically"}}</a>
50+
<a class="{{if eq .SortType "recentupdate"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=recentupdate&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.recentupdate"}}</a>
51+
<a class="{{if eq .SortType "leastupdate"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=leastupdate&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.leastupdate"}}</a>
52+
{{if not .DisableStars}}
53+
<a class="{{if eq .SortType "moststars"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=moststars&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.moststars"}}</a>
54+
<a class="{{if eq .SortType "feweststars"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=feweststars&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.feweststars"}}</a>
55+
{{end}}
56+
<a class="{{if eq .SortType "mostforks"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=mostforks&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.mostforks"}}</a>
57+
<a class="{{if eq .SortType "fewestforks"}}active {{end}}item" href="{{$.Link}}?tab={{$.TabName}}&sort=fewestforks&q={{$.Keyword}}&language={{$.Language}}">{{ctx.Locale.Tr "repo.issues.filter_sort.fewestforks"}}</a>
58+
</div>
3459
</div>
3560
</div>
3661
</div>

0 commit comments

Comments
 (0)