Skip to content

Add language statistics API endpoint #11737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions integrations/api_repo_languages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 integrations

import (
"net/http"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRepoLanguages(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
session := loginUser(t, "user2")

// Request editor page
req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
resp := session.MakeRequest(t, req, http.StatusOK)

doc := NewHTMLParser(t, resp.Body)
lastCommit := doc.GetInputValueByName("last_commit")
assert.NotEmpty(t, lastCommit)

// Save new file to master branch
req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
"_csrf": doc.GetCSRF(),
"last_commit": lastCommit,
"tree_path": "test.go",
"content": "package main",
"commit_choice": "direct",
})
session.MakeRequest(t, req, http.StatusFound)

// Save new file to master branch
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/languages")
resp = session.MakeRequest(t, req, http.StatusOK)

var languages map[string]int64
DecodeJSON(t, resp, &languages)

assert.InDeltaMapValues(t, map[string]int64{"Go": 12}, languages, 0)
})
}
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ func RegisterRoutes(m *macaron.Macaron) {
Delete(reqToken(), repo.DeleteTopic)
}, reqAdmin())
}, reqAnyRepoReader())
m.Get("/languages", reqRepoReader(models.UnitTypeCode), repo.GetLanguages)
}, repoAssignment())
})

Expand Down
84 changes: 84 additions & 0 deletions routers/api/v1/repo/language.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
46 changes: 46 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6049,6 +6049,42 @@
}
}
},
"/repos/{owner}/{repo}/languages": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get languages and number of bytes of code written",
"operationId": "repoGetLanguages",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/LanguageStatistics"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/milestones": {
"get": {
"produces": [
Expand Down Expand Up @@ -14917,6 +14953,16 @@
}
}
},
"LanguageStatistics": {
"description": "LanguageStatistics",
"schema": {
"type": "object",
"additionalProperties": {
"type": "integer",
"format": "int64"
}
}
},
"MarkdownRender": {
"description": "MarkdownRender is a rendered markdown document",
"schema": {
Expand Down