新增:commit详情加载分支信息

This commit is contained in:
yystopf 2022-12-26 10:10:06 +08:00
parent 5fa04a24db
commit f1088ea158
4 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package convert
import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
gitea_convert "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
)
func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*hat_api.Commit, error) {
giteaApiCommit, err := gitea_convert.ToCommit(repo, gitRepo, commit, userCache, stat)
if err != nil {
return nil, err
}
err = commit.LoadBranchName()
if err != nil {
return nil, err
}
return &hat_api.Commit{
Commit: giteaApiCommit,
Branch: commit.Branch,
}, nil
}

View File

@ -0,0 +1,10 @@
package structs
import (
gitea_api "code.gitea.io/gitea/modules/structs"
)
type Commit struct {
*gitea_api.Commit
Branch string `json:"branch"`
}

View File

@ -118,6 +118,11 @@ func Routers(ctx gocontext.Context) *web.Route {
m.Get("", repo.GetContentsList)
m.Get("/*", repo.GetContents)
}, reqRepoReader(unit.TypeCode))
m.Group("/git", func() {
m.Group("/commits", func() {
m.Get("/{sha}", repo.GetSingleCommit)
})
}, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode))
}, repoAssignment())
})
m.Group("/users", func() {

View File

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert"
hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git"
)
@ -207,3 +208,32 @@ func GetFileAllCommits(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, apiCommits)
}
func GetSingleCommit(ctx *context.APIContext) {
sha := ctx.Params(":sha")
if !git.IsValidRefPattern(sha) {
ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
return
}
getCommit(ctx, sha)
}
func getCommit(ctx *context.APIContext, indentifier string) {
commit, err := ctx.Repo.GitRepo.GetCommit(indentifier)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound(indentifier)
return
}
ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
return
}
json, err := hat_convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, true)
if err != nil {
ctx.Error(http.StatusInternalServerError, "toCommit", err)
return
}
ctx.JSON(http.StatusOK, json)
}