mirror of https://github.com/gogs/gogs.git
api: add `GET /api/v1/{owner}/{repo}/commits` endpoint (#6574)
This pull request targets issue #6573. It provides a new API endpoint: `/api/v1/repos/{org}/{repo}/commits?pageSize=<int>` with a default page size of 30 commits (the same as the UI). This implementation currently only focuses on the main/master branch of the repository, and does not provide the ability to return commit history for other branches. - Note: Since the logic for converting a git.Commit to api.Commit had to be used in `GetAllCommits` and `GetSingleCommit`, I decided to pull the code out into a helper function, ` gitCommitToAPICommit(commit, context)`.pull/6620/head
parent
b3eb33be0f
commit
b9a3626cad
|
@ -286,6 +286,7 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||||
})
|
})
|
||||||
m.Group("/commits", func() {
|
m.Group("/commits", func() {
|
||||||
m.Get("/:sha", repo.GetSingleCommit)
|
m.Get("/:sha", repo.GetSingleCommit)
|
||||||
|
m.Get("", repo.GetAllCommits)
|
||||||
m.Get("/*", repo.GetReferenceSHA)
|
m.Get("/*", repo.GetReferenceSHA)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,40 @@ import (
|
||||||
"gogs.io/gogs/internal/gitutil"
|
"gogs.io/gogs/internal/gitutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAllCommits returns a slice of commits starting from HEAD.
|
||||||
|
func GetAllCommits(c *context.APIContext) {
|
||||||
|
// Get pagesize, set default if it is not specified.
|
||||||
|
pageSize := c.QueryInt("pageSize")
|
||||||
|
if pageSize == 0 {
|
||||||
|
pageSize = 30
|
||||||
|
}
|
||||||
|
|
||||||
|
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
|
||||||
|
if err != nil {
|
||||||
|
c.Error(err, "open repository")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The response object returned as JSON
|
||||||
|
result := make([]*api.Commit, 0, pageSize)
|
||||||
|
commits, err := gitRepo.Log("HEAD", git.LogOptions{MaxCount: pageSize})
|
||||||
|
if err != nil {
|
||||||
|
c.Error(err, "git log")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, commit := range commits {
|
||||||
|
apiCommit, err := gitCommitToAPICommit(commit, c)
|
||||||
|
if err != nil {
|
||||||
|
c.Error(err, "convert git commit to api commit")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result = append(result, apiCommit)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSONSuccess(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSingleCommit will return a single Commit object based on the specified SHA.
|
||||||
func GetSingleCommit(c *context.APIContext) {
|
func GetSingleCommit(c *context.APIContext) {
|
||||||
if strings.Contains(c.Req.Header.Get("Accept"), api.MediaApplicationSHA) {
|
if strings.Contains(c.Req.Header.Get("Accept"), api.MediaApplicationSHA) {
|
||||||
c.SetParams("*", c.Params(":sha"))
|
c.SetParams("*", c.Params(":sha"))
|
||||||
|
@ -36,66 +70,11 @@ func GetSingleCommit(c *context.APIContext) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve author and committer information
|
apiCommit, err := gitCommitToAPICommit(commit, c)
|
||||||
var apiAuthor, apiCommitter *api.User
|
if err != nil {
|
||||||
author, err := db.GetUserByEmail(commit.Author.Email)
|
c.Error(err, "convert git commit to api commit")
|
||||||
if err != nil && !db.IsErrUserNotExist(err) {
|
|
||||||
c.Error(err, "get user by author email")
|
|
||||||
return
|
|
||||||
} else if err == nil {
|
|
||||||
apiAuthor = author.APIFormat()
|
|
||||||
}
|
}
|
||||||
// Save one query if the author is also the committer
|
c.JSONSuccess(apiCommit)
|
||||||
if commit.Committer.Email == commit.Author.Email {
|
|
||||||
apiCommitter = apiAuthor
|
|
||||||
} else {
|
|
||||||
committer, err := db.GetUserByEmail(commit.Committer.Email)
|
|
||||||
if err != nil && !db.IsErrUserNotExist(err) {
|
|
||||||
c.Error(err, "get user by committer email")
|
|
||||||
return
|
|
||||||
} else if err == nil {
|
|
||||||
apiCommitter = committer.APIFormat()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve parent(s) of the commit
|
|
||||||
apiParents := make([]*api.CommitMeta, commit.ParentsCount())
|
|
||||||
for i := 0; i < commit.ParentsCount(); i++ {
|
|
||||||
sha, _ := commit.ParentID(i)
|
|
||||||
apiParents[i] = &api.CommitMeta{
|
|
||||||
URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/commits/" + sha.String(),
|
|
||||||
SHA: sha.String(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSONSuccess(&api.Commit{
|
|
||||||
CommitMeta: &api.CommitMeta{
|
|
||||||
URL: conf.Server.ExternalURL + c.Link[1:],
|
|
||||||
SHA: commit.ID.String(),
|
|
||||||
},
|
|
||||||
HTMLURL: c.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
|
|
||||||
RepoCommit: &api.RepoCommit{
|
|
||||||
URL: conf.Server.ExternalURL + c.Link[1:],
|
|
||||||
Author: &api.CommitUser{
|
|
||||||
Name: commit.Author.Name,
|
|
||||||
Email: commit.Author.Email,
|
|
||||||
Date: commit.Author.When.Format(time.RFC3339),
|
|
||||||
},
|
|
||||||
Committer: &api.CommitUser{
|
|
||||||
Name: commit.Committer.Name,
|
|
||||||
Email: commit.Committer.Email,
|
|
||||||
Date: commit.Committer.When.Format(time.RFC3339),
|
|
||||||
},
|
|
||||||
Message: commit.Summary(),
|
|
||||||
Tree: &api.CommitMeta{
|
|
||||||
URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/tree/" + commit.ID.String(),
|
|
||||||
SHA: commit.ID.String(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Author: apiAuthor,
|
|
||||||
Committer: apiCommitter,
|
|
||||||
Parents: apiParents,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetReferenceSHA(c *context.APIContext) {
|
func GetReferenceSHA(c *context.APIContext) {
|
||||||
|
@ -136,3 +115,66 @@ func GetReferenceSHA(c *context.APIContext) {
|
||||||
}
|
}
|
||||||
c.PlainText(http.StatusOK, sha)
|
c.PlainText(http.StatusOK, sha)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gitCommitToApiCommit is a helper function to convert git commit object to API commit.
|
||||||
|
func gitCommitToAPICommit(commit *git.Commit, c *context.APIContext) (*api.Commit, error) {
|
||||||
|
// Retrieve author and committer information
|
||||||
|
var apiAuthor, apiCommitter *api.User
|
||||||
|
author, err := db.GetUserByEmail(commit.Author.Email)
|
||||||
|
if err != nil && !db.IsErrUserNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
} else if err == nil {
|
||||||
|
apiAuthor = author.APIFormat()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save one query if the author is also the committer
|
||||||
|
if commit.Committer.Email == commit.Author.Email {
|
||||||
|
apiCommitter = apiAuthor
|
||||||
|
} else {
|
||||||
|
committer, err := db.GetUserByEmail(commit.Committer.Email)
|
||||||
|
if err != nil && !db.IsErrUserNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
} else if err == nil {
|
||||||
|
apiCommitter = committer.APIFormat()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve parent(s) of the commit
|
||||||
|
apiParents := make([]*api.CommitMeta, commit.ParentsCount())
|
||||||
|
for i := 0; i < commit.ParentsCount(); i++ {
|
||||||
|
sha, _ := commit.ParentID(i)
|
||||||
|
apiParents[i] = &api.CommitMeta{
|
||||||
|
URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/commits/" + sha.String(),
|
||||||
|
SHA: sha.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &api.Commit{
|
||||||
|
CommitMeta: &api.CommitMeta{
|
||||||
|
URL: conf.Server.ExternalURL + c.Link[1:],
|
||||||
|
SHA: commit.ID.String(),
|
||||||
|
},
|
||||||
|
HTMLURL: c.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
|
||||||
|
RepoCommit: &api.RepoCommit{
|
||||||
|
URL: conf.Server.ExternalURL + c.Link[1:],
|
||||||
|
Author: &api.CommitUser{
|
||||||
|
Name: commit.Author.Name,
|
||||||
|
Email: commit.Author.Email,
|
||||||
|
Date: commit.Author.When.Format(time.RFC3339),
|
||||||
|
},
|
||||||
|
Committer: &api.CommitUser{
|
||||||
|
Name: commit.Committer.Name,
|
||||||
|
Email: commit.Committer.Email,
|
||||||
|
Date: commit.Committer.When.Format(time.RFC3339),
|
||||||
|
},
|
||||||
|
Message: commit.Summary(),
|
||||||
|
Tree: &api.CommitMeta{
|
||||||
|
URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/tree/" + commit.ID.String(),
|
||||||
|
SHA: commit.ID.String(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Author: apiAuthor,
|
||||||
|
Committer: apiCommitter,
|
||||||
|
Parents: apiParents,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue