summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2022-01-25 08:33:40 +0200
committerGitHub <noreply@github.com>2022-01-25 08:33:40 +0200
commit6392f4691af7db20a2ad81ec19408fa9377a5cd1 (patch)
tree0553b384cdde77323b40f8d70daa38ba8d4a5280
parent9628a76ce6f4fea8508f79747d78007907e3ea76 (diff)
downloadgitea-6392f4691af7db20a2ad81ec19408fa9377a5cd1.tar.gz
gitea-6392f4691af7db20a2ad81ec19408fa9377a5cd1.zip
API: Return primary language and repository language stats API URL (#18396)
-rw-r--r--models/repo/repo.go24
-rw-r--r--models/repo_list.go4
-rw-r--r--modules/convert/repository.go9
-rw-r--r--modules/structs/repo.go2
-rw-r--r--routers/api/v1/repo/repo.go5
-rw-r--r--routers/api/v1/user/repo.go5
-rw-r--r--templates/swagger/v1_json.tmpl8
7 files changed, 55 insertions, 2 deletions
diff --git a/models/repo/repo.go b/models/repo/repo.go
index a78d287315..353d707e60 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -222,6 +222,30 @@ func (repo *Repository) MustOwner() *user_model.User {
return repo.mustOwner(db.DefaultContext)
}
+// LoadAttributes loads attributes of the repository.
+func (repo *Repository) LoadAttributes(ctx context.Context) error {
+ // Load owner
+ if err := repo.GetOwner(ctx); err != nil {
+ return fmt.Errorf("load owner: %w", err)
+ }
+
+ // Load primary language
+ stats := make(LanguageStatList, 0, 1)
+ if err := db.GetEngine(ctx).
+ Where("`repo_id` = ? AND `is_primary` = ? AND `language` != ?", repo.ID, true, "other").
+ Find(&stats); err != nil {
+ return fmt.Errorf("find primary languages: %w", err)
+ }
+ stats.LoadAttributes()
+ for _, st := range stats {
+ if st.RepoID == repo.ID {
+ repo.PrimaryLanguage = st
+ break
+ }
+ }
+ return nil
+}
+
// FullName returns the repository full name
func (repo *Repository) FullName() string {
return repo.OwnerName + "/" + repo.Name
diff --git a/models/repo_list.go b/models/repo_list.go
index 9cb7a163fc..290919bb6d 100644
--- a/models/repo_list.go
+++ b/models/repo_list.go
@@ -623,7 +623,7 @@ func FindUserAccessibleRepoIDs(user *user_model.User) ([]int64, error) {
}
// GetUserRepositories returns a list of repositories of given user.
-func GetUserRepositories(opts *SearchRepoOptions) ([]*repo_model.Repository, int64, error) {
+func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error) {
if len(opts.OrderBy) == 0 {
opts.OrderBy = "updated_unix DESC"
}
@@ -646,6 +646,6 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*repo_model.Repository, int
}
sess = sess.Where(cond).OrderBy(opts.OrderBy.String())
- repos := make([]*repo_model.Repository, 0, opts.PageSize)
+ repos := make(RepositoryList, 0, opts.PageSize)
return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
}
diff --git a/modules/convert/repository.go b/modules/convert/repository.go
index a356925539..1f11fda7ac 100644
--- a/modules/convert/repository.go
+++ b/modules/convert/repository.go
@@ -125,6 +125,13 @@ func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent boo
}
}
+ var language string
+ if repo.PrimaryLanguage != nil {
+ language = repo.PrimaryLanguage.Language
+ }
+
+ repoAPIURL := repo.APIURL()
+
return &api.Repository{
ID: repo.ID,
Owner: ToUserWithAccessMode(repo.Owner, mode),
@@ -144,6 +151,8 @@ func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent boo
CloneURL: cloneLink.HTTPS,
OriginalURL: repo.SanitizedOriginalURL(),
Website: repo.Website,
+ Language: language,
+ LanguagesURL: repoAPIURL + "/languages",
Stars: repo.NumStars,
Forks: repo.NumForks,
Watchers: repo.NumWatches,
diff --git a/modules/structs/repo.go b/modules/structs/repo.go
index 671885f20a..5a1e99e36b 100644
--- a/modules/structs/repo.go
+++ b/modules/structs/repo.go
@@ -59,6 +59,8 @@ type Repository struct {
Parent *Repository `json:"parent"`
Mirror bool `json:"mirror"`
Size int `json:"size"`
+ Language string `json:"language"`
+ LanguagesURL string `json:"languages_url"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index c2dfc4f193..7a7fe218e8 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -533,6 +533,11 @@ func Get(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/Repository"
+ if err := ctx.Repo.Repository.LoadAttributes(ctx); err != nil {
+ ctx.Error(http.StatusInternalServerError, "Repository.LoadAttributes", err)
+ return
+ }
+
ctx.JSON(http.StatusOK, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
}
diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go
index 3d5c841856..109548ec76 100644
--- a/routers/api/v1/user/repo.go
+++ b/routers/api/v1/user/repo.go
@@ -32,6 +32,11 @@ func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) {
return
}
+ if err := repos.LoadAttributes(); err != nil {
+ ctx.Error(http.StatusInternalServerError, "RepositoryList.LoadAttributes", err)
+ return
+ }
+
apiRepos := make([]*api.Repository, 0, len(repos))
for i := range repos {
access, err := models.AccessLevel(ctx.User, repos[i])
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index 768c4c69ee..497636e781 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -17188,6 +17188,14 @@
"internal_tracker": {
"$ref": "#/definitions/InternalTracker"
},
+ "language": {
+ "type": "string",
+ "x-go-name": "Language"
+ },
+ "languages_url": {
+ "type": "string",
+ "x-go-name": "LanguagesURL"
+ },
"mirror": {
"type": "boolean",
"x-go-name": "Mirror"