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 ;)
This commit is contained in:
parent
5995326d51
commit
1418288734
8 changed files with 230 additions and 228 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue