diff options
author | Lauris BH <lauris@nix.lv> | 2020-06-07 14:48:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-07 14:48:41 +0300 |
commit | 2874ab54bccd7a4ebd6b8a51367eb8c1d9b16720 (patch) | |
tree | 2fd5ff4f7351afeb60361c1aeec9ff437e5d6b2e /routers/api | |
parent | 94f60e199bc504c6cfb7b853889e3ceb2a837adc (diff) | |
download | gitea-2874ab54bccd7a4ebd6b8a51367eb8c1d9b16720.tar.gz gitea-2874ab54bccd7a4ebd6b8a51367eb8c1d9b16720.zip |
Add language statistics API endpoint (#11737)
* Add language statistics API
* Add tests
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/api.go | 1 | ||||
-rw-r--r-- | routers/api/v1/repo/language.go | 84 | ||||
-rw-r--r-- | routers/api/v1/swagger/repo.go | 7 |
3 files changed, 92 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 1ae4e7a58f..0567c3560c 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -855,6 +855,7 @@ func RegisterRoutes(m *macaron.Macaron) { Delete(reqToken(), repo.DeleteTopic) }, reqAdmin()) }, reqAnyRepoReader()) + m.Get("/languages", reqRepoReader(models.UnitTypeCode), repo.GetLanguages) }, repoAssignment()) }) diff --git a/routers/api/v1/repo/language.go b/routers/api/v1/repo/language.go new file mode 100644 index 0000000000..c45911ee66 --- /dev/null +++ b/routers/api/v1/repo/language.go @@ -0,0 +1,84 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "bytes" + "net/http" + "strconv" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" +) + +type languageResponse []*models.LanguageStat + +func (l languageResponse) MarshalJSON() ([]byte, error) { + var buf bytes.Buffer + if _, err := buf.WriteString("{"); err != nil { + return nil, err + } + for i, lang := range l { + if i > 0 { + if _, err := buf.WriteString(","); err != nil { + return nil, err + } + } + if _, err := buf.WriteString(strconv.Quote(lang.Language)); err != nil { + return nil, err + } + if _, err := buf.WriteString(":"); err != nil { + return nil, err + } + if _, err := buf.WriteString(strconv.FormatInt(lang.Size, 10)); err != nil { + return nil, err + } + } + if _, err := buf.WriteString("}"); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +// GetLanguages returns languages and number of bytes of code written +func GetLanguages(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/languages repository repoGetLanguages + // --- + // summary: Get languages and number of bytes of code written + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // responses: + // "404": + // "$ref": "#/responses/notFound" + // "200": + // "$ref": "#/responses/LanguageStatistics" + + langs, err := ctx.Repo.Repository.GetLanguageStats() + if err != nil { + log.Error("GetLanguageStats failed: %v", err) + ctx.InternalServerError(err) + return + } + + resp := make(languageResponse, len(langs)) + for i, v := range langs { + resp[i] = v + } + + ctx.JSON(http.StatusOK, resp) +} diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index bcbc2b5fa9..bce9e45c37 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -302,3 +302,10 @@ type swaggerTopicNames struct { // in: body Body api.TopicName `json:"body"` } + +// LanguageStatistics +// swagger:response LanguageStatistics +type swaggerLanguageStatistics struct { + // in: body + Body map[string]int64 `json:"body"` +} |