2020-06-07 07:48:41 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-07 07:48:41 -04:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-06-07 07:48:41 -04:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
type languageResponse []*repo_model.LanguageStat
|
2020-06-07 07:48:41 -04:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
langs, err := repo_model.GetLanguageStats(ctx, ctx.Repo.Repository)
|
2020-06-07 07:48:41 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetLanguageStats failed: %v", err)
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := make(languageResponse, len(langs))
|
2022-04-30 19:53:56 -04:00
|
|
|
copy(resp, langs)
|
2020-06-07 07:48:41 -04:00
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
}
|