aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-09-18 14:09:26 +0200
committerGitHub <noreply@github.com>2020-09-18 20:09:26 +0800
commit1418288734b430330023e4e17747175e4fea2f41 (patch)
tree270f7bdbb6aee5f732a6d1295ce3d021d3e98874 /routers/api
parent5995326d51ab42014c6d77d3313233641c258318 (diff)
downloadgitea-1418288734b430330023e4e17747175e4fea2f41.tar.gz
gitea-1418288734b430330023e4e17747175e4fea2f41.zip
Refactor: move Commit To APIFormat Code & Lot of StopWatch related things (#12729)
* move GitCommit to APIFormat convertion into convert package * rename Commit convert functions * move stopwatch to api convertion into convert package & rm unused code & extend test * fix compare time * Gitea not Gogs ;)
Diffstat (limited to 'routers/api')
-rw-r--r--routers/api/v1/repo/commits.go101
-rw-r--r--routers/api/v1/repo/hook.go2
-rw-r--r--routers/api/v1/repo/issue_stopwatch.go3
3 files changed, 6 insertions, 100 deletions
diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go
index 8b2dc28bdd..220bb0fd74 100644
--- a/routers/api/v1/repo/commits.go
+++ b/routers/api/v1/repo/commits.go
@@ -10,10 +10,10 @@ import (
"math"
"net/http"
"strconv"
- "time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
@@ -73,7 +73,7 @@ func getCommit(ctx *context.APIContext, identifier string) {
return
}
- json, err := toCommit(ctx, ctx.Repo.Repository, commit, nil)
+ json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil)
if err != nil {
ctx.ServerError("toCommit", err)
return
@@ -193,7 +193,7 @@ func GetAllCommits(ctx *context.APIContext) {
commit := commitPointer.Value.(*git.Commit)
// Create json struct
- apiCommits[i], err = toCommit(ctx, ctx.Repo.Repository, commit, userCache)
+ apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
if err != nil {
ctx.ServerError("toCommit", err)
return
@@ -215,98 +215,3 @@ func GetAllCommits(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, &apiCommits)
}
-
-func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) {
-
- var apiAuthor, apiCommitter *api.User
-
- // Retrieve author and committer information
-
- var cacheAuthor *models.User
- var ok bool
- if userCache == nil {
- cacheAuthor = ((*models.User)(nil))
- ok = false
- } else {
- cacheAuthor, ok = userCache[commit.Author.Email]
- }
-
- if ok {
- apiAuthor = cacheAuthor.APIFormat()
- } else {
- author, err := models.GetUserByEmail(commit.Author.Email)
- if err != nil && !models.IsErrUserNotExist(err) {
- return nil, err
- } else if err == nil {
- apiAuthor = author.APIFormat()
- if userCache != nil {
- userCache[commit.Author.Email] = author
- }
- }
- }
-
- var cacheCommitter *models.User
- if userCache == nil {
- cacheCommitter = ((*models.User)(nil))
- ok = false
- } else {
- cacheCommitter, ok = userCache[commit.Committer.Email]
- }
-
- if ok {
- apiCommitter = cacheCommitter.APIFormat()
- } else {
- committer, err := models.GetUserByEmail(commit.Committer.Email)
- if err != nil && !models.IsErrUserNotExist(err) {
- return nil, err
- } else if err == nil {
- apiCommitter = committer.APIFormat()
- if userCache != nil {
- userCache[commit.Committer.Email] = committer
- }
- }
- }
-
- // Retrieve parent(s) of the commit
- apiParents := make([]*api.CommitMeta, commit.ParentCount())
- for i := 0; i < commit.ParentCount(); i++ {
- sha, _ := commit.ParentID(i)
- apiParents[i] = &api.CommitMeta{
- URL: repo.APIURL() + "/git/commits/" + sha.String(),
- SHA: sha.String(),
- }
- }
-
- return &api.Commit{
- CommitMeta: &api.CommitMeta{
- URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
- SHA: commit.ID.String(),
- },
- HTMLURL: repo.HTMLURL() + "/commit/" + commit.ID.String(),
- RepoCommit: &api.RepoCommit{
- URL: repo.APIURL() + "/git/commits/" + commit.ID.String(),
- Author: &api.CommitUser{
- Identity: api.Identity{
- Name: commit.Committer.Name,
- Email: commit.Committer.Email,
- },
- Date: commit.Author.When.Format(time.RFC3339),
- },
- Committer: &api.CommitUser{
- Identity: api.Identity{
- Name: commit.Committer.Name,
- Email: commit.Committer.Email,
- },
- Date: commit.Committer.When.Format(time.RFC3339),
- },
- Message: commit.Message(),
- Tree: &api.CommitMeta{
- URL: repo.APIURL() + "/git/trees/" + commit.ID.String(),
- SHA: commit.ID.String(),
- },
- },
- Author: apiAuthor,
- Committer: apiCommitter,
- Parents: apiParents,
- }, nil
-}
diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go
index 3758493344..1a38fc3018 100644
--- a/routers/api/v1/repo/hook.go
+++ b/routers/api/v1/repo/hook.go
@@ -144,7 +144,7 @@ func TestHook(ctx *context.APIContext) {
Before: ctx.Repo.Commit.ID.String(),
After: ctx.Repo.Commit.ID.String(),
Commits: []*api.PayloadCommit{
- convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
+ convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit),
},
Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
Pusher: convert.ToUser(ctx.User, ctx.IsSigned, false),
diff --git a/routers/api/v1/repo/issue_stopwatch.go b/routers/api/v1/repo/issue_stopwatch.go
index ca1593619e..a4a2261b9a 100644
--- a/routers/api/v1/repo/issue_stopwatch.go
+++ b/routers/api/v1/repo/issue_stopwatch.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/routers/api/v1/utils"
)
@@ -224,7 +225,7 @@ func GetStopwatches(ctx *context.APIContext) {
return
}
- apiSWs, err := sws.APIFormat()
+ apiSWs, err := convert.ToStopWatches(sws)
if err != nil {
ctx.Error(http.StatusInternalServerError, "APIFormat", err)
return