Skip to content

Commit 2a8d0c8

Browse files
committed
Improved api and db calls
1 parent 3794e37 commit 2a8d0c8

File tree

10 files changed

+162
-106
lines changed

10 files changed

+162
-106
lines changed

models/repo/repo_list.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -746,21 +746,24 @@ func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error)
746746
return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
747747
}
748748

749-
func GetPrimaryRepoLanguageList(ctx context.Context, repos RepositoryList) (LanguageStatList, error) {
749+
func GetPrimaryRepoLanguageList(ctx context.Context, ownerID int64, private bool) (LanguageStatList, error) {
750750
languageList := make(LanguageStatList, 0)
751751

752752
q := db.GetEngine(ctx).
753753
Table("language_stat").
754754
Cols("language").
755755
Where(builder.Eq{"is_primary": true})
756756

757-
if repos != nil {
758-
repoIDs := make([]int64, len(repos))
759-
for i := range repos {
760-
repoIDs[i] = repos[i].ID
757+
if ownerID > 0 {
758+
ids, err := SearchRepositoryIDsByCondition(ctx, builder.NewCond().And(
759+
builder.Eq{"owner_id": ownerID},
760+
builder.Eq{"is_private": private},
761+
))
762+
if err != nil {
763+
return nil, err
761764
}
762765

763-
q = q.In("repo_id", repoIDs)
766+
q = q.In("repo_id", ids)
764767
}
765768

766769
err := q.Distinct("language").

modules/context/base.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,6 @@ func (b *Base) FormInt64(key string) int64 {
198198
return v
199199
}
200200

201-
// FormInt64s returns a int64 slice for the provided key from the form
202-
func (b *Base) FormInt64s(key string) (v []int64) {
203-
str := b.FormStrings(key)
204-
if str == nil {
205-
return nil
206-
}
207-
208-
for _, s := range str {
209-
i, _ := strconv.ParseInt(s, 10, 64)
210-
v = append(v, i)
211-
}
212-
return v
213-
}
214-
215201
// FormBool returns true if the value for the provided key in the form is "1", "true" or "on"
216202
func (b *Base) FormBool(key string) bool {
217203
s := b.Req.FormValue(key)

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ func Routes() *web.Route {
795795
m.Get("/heatmap", user.GetUserHeatmapData)
796796
}
797797

798+
m.Get("/languages", repo.GetUserPrimaryLanguageList)
798799
m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignIn(), user.ListUserRepos)
799800
m.Group("/tokens", func() {
800801
m.Combo("").Get(user.ListAccessTokens).
@@ -919,7 +920,7 @@ func Routes() *web.Route {
919920
// Repos (requires repo scope)
920921
m.Group("/repos", func() {
921922
m.Get("/search", repo.Search)
922-
m.Get("/languages", repo.GetPrimaryLanguageList)
923+
m.Get("/languages", repo.ListPrimaryLanguages)
923924

924925
// (repo scope)
925926
m.Post("/migrate", reqToken(), bind(api.MigrateRepoOptions{}), repo.Migrate)
@@ -1299,6 +1300,7 @@ func Routes() *web.Route {
12991300
m.Combo("/{username}").Get(reqToken(), org.IsMember).
13001301
Delete(reqToken(), reqOrgOwnership(), org.DeleteMember)
13011302
})
1303+
m.Get("/languages", repo.GetOrgPrimaryLanguageList)
13021304
m.Group("/actions/secrets", func() {
13031305
m.Get("", reqToken(), reqOrgOwnership(), org.ListActionsSecrets)
13041306
m.Post("", reqToken(), reqOrgOwnership(), bind(api.CreateSecretOption{}), org.CreateOrgSecret)

routers/api/v1/repo/languages.go

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,83 @@ package repo
66
import (
77
"net/http"
88

9-
"code.gitea.io/gitea/models/db"
109
repo_model "code.gitea.io/gitea/models/repo"
11-
user_model "code.gitea.io/gitea/models/user"
1210
"code.gitea.io/gitea/modules/context"
11+
api "code.gitea.io/gitea/modules/structs"
1312
)
1413

15-
// GetPrimaryLanguageList returns a list of primary languages along with their color
16-
func GetPrimaryLanguageList(ctx *context.APIContext) {
17-
// swagger:operation GET /repos/languages repository repoGetPrimaryLanguages
14+
func SendPrimaryLanguageList(ctx *context.APIContext, ownerID int64) {
15+
langs, err := repo_model.GetPrimaryRepoLanguageList(ctx, ownerID, ctx.IsSigned)
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
1835
// ---
1936
// summary: Get primary languages and their color
2037
// produces:
2138
// - application/json
22-
// parameters:
23-
// - name: ids
24-
// in: query
25-
// description: Filter by Repo IDs (empty for all repos)
26-
// type: array
27-
// collectionFormat: multi
28-
// items:
29-
// type: integer
30-
// - name: ownerId
31-
// in: query
32-
// description: Filter by the the recent languages used by a user/org (ignored if filtered by Repo IDs)
33-
// type: integer
3439
// responses:
3540
// "200":
3641
// "$ref": "#/responses/PrimaryLanguageList"
3742

38-
ids := ctx.FormInt64s("ids")
39-
var repos repo_model.RepositoryList
40-
41-
ownerID := ctx.FormInt64("ownerId")
42-
43-
if ownerID > 0 && len(ids) == 0 {
44-
userRepos, _, err := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{
45-
Actor: &user_model.User{
46-
ID: ownerID,
47-
},
48-
Private: ctx.IsSigned,
49-
ListOptions: db.ListOptions{
50-
Page: 0,
51-
PageSize: 20,
52-
},
53-
})
54-
if err != nil {
55-
ctx.InternalServerError(err)
56-
}
57-
58-
repos = userRepos
59-
}
60-
61-
if len(repos) == 0 {
62-
if len(ids) > 0 {
63-
repos = make([]*repo_model.Repository, len(ids))
64-
for i, id := range ids {
65-
repos[i] = &repo_model.Repository{ID: id}
66-
}
67-
} else {
68-
repos = nil
69-
}
70-
}
43+
SendPrimaryLanguageList(ctx, 0)
44+
}
7145

72-
langs, err := repo_model.GetPrimaryRepoLanguageList(ctx, repos)
73-
if err != nil {
74-
ctx.InternalServerError(err)
75-
}
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"
7664

77-
list := map[string]string{}
65+
SendPrimaryLanguageList(ctx, ctx.ContextUser.ID)
66+
}
7867

79-
for _, i := range langs {
80-
list[i.Language] = i.Color
81-
}
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"
8286

83-
ctx.JSON(http.StatusOK, list)
87+
SendPrimaryLanguageList(ctx, ctx.Org.Organization.ID)
8488
}

routers/api/v1/swagger/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ type swaggerLanguageStatistics struct {
349349
// swagger:response PrimaryLanguageList
350350
type swaggerPrimaryLanguageList struct {
351351
// in: body
352-
Body map[string]string `json:"body"`
352+
Body []api.LanguageInfo `json:"body"`
353353
}
354354

355355
// CombinedStatus

routers/web/explore/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
137137
return
138138
}
139139

140-
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, nil)
140+
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, opts.OwnerID, opts.Private)
141141
if err != nil {
142142
ctx.ServerError("GetPrimaryRepoLanguageList", err)
143143
return

routers/web/org/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func Home(ctx *context.Context) {
134134
isFollowing = user_model.IsFollowing(ctx.Doer.ID, ctx.ContextUser.ID)
135135
}
136136

137-
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, repos)
137+
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, org.ID, ctx.IsSigned)
138138
if err != nil {
139139
ctx.ServerError("GetPrimaryRepoLanguageList", err)
140140
return

routers/web/user/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGi
266266

267267
total = int(count)
268268

269-
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, repos)
269+
programLanguages, err := repo_model.GetPrimaryRepoLanguageList(ctx, ctx.ContextUser.ID, ctx.IsSigned)
270270
if err != nil {
271271
ctx.ServerError("GetPrimaryRepoLanguageList", err)
272272
return

0 commit comments

Comments
 (0)