From d4273dbcf35b7c966814afd2a19aad072b0f425b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 11 Jun 2019 15:52:57 +0200 Subject: [PATCH 01/15] Added API endpoint ListAllCommits (/repos/{owner}/{repo}/git/commits) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mike Schwörer --- integrations/api_repo_git_commits_test.go | 58 +++++++ routers/api/v1/api.go | 1 + routers/api/v1/repo/commits.go | 203 ++++++++++++++++++++++ templates/swagger/v1_json.tmpl | 83 +++++++++ 4 files changed, 345 insertions(+) diff --git a/integrations/api_repo_git_commits_test.go b/integrations/api_repo_git_commits_test.go index 587e9de5b2678..2f48249ddd9db 100644 --- a/integrations/api_repo_git_commits_test.go +++ b/integrations/api_repo_git_commits_test.go @@ -9,6 +9,9 @@ import ( "testing" "code.gitea.io/gitea/models" + api "code.gitea.io/sdk/gitea" + + "github.com/stretchr/testify/assert" ) func TestAPIReposGitCommits(t *testing.T) { @@ -30,3 +33,58 @@ func TestAPIReposGitCommits(t *testing.T) { req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/unknown?token="+token, user.Name) session.MakeRequest(t, req, http.StatusNotFound) } + +func TestAPIReposGitCommitList(t *testing.T) { + prepareTestEnv(t) + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + // Test getting commits (Page 1) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits?token="+token, user.Name) + resp := session.MakeRequest(t, req, http.StatusOK) + + var apiData []api.Commit + DecodeJSON(t, resp, &apiData) + + assert.Equal(t, 3, len(apiData)) + assert.Equal(t, "69554a64c1e6030f051e5c3f94bfbd773cd6a324", apiData[0].CommitMeta.SHA) + assert.Equal(t, "27566bd5738fc8b4e3fef3c5e72cce608537bd95", apiData[1].CommitMeta.SHA) + assert.Equal(t, "5099b81332712fe655e34e8dd63574f503f61811", apiData[2].CommitMeta.SHA) +} + +func TestAPIReposGitCommitListPage2Empty(t *testing.T) { + prepareTestEnv(t) + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + // Test getting commits (Page=2) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits?token="+token+"&page=2", user.Name) + resp := session.MakeRequest(t, req, http.StatusOK) + + var apiData []api.Commit + DecodeJSON(t, resp, &apiData) + + assert.Equal(t, 0, len(apiData)) +} + +func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { + prepareTestEnv(t) + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + // Test getting commits (Page=1, Branch=good-sign) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits?token="+token+"&sha=good-sign", user.Name) + resp := session.MakeRequest(t, req, http.StatusOK) + + var apiData []api.Commit + DecodeJSON(t, resp, &apiData) + + assert.Equal(t, 1, len(apiData)) + assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 2268c1be38e05..1899fae442ab8 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -757,6 +757,7 @@ func RegisterRoutes(m *macaron.Macaron) { }, reqRepoReader(models.UnitTypeCode)) m.Group("/git", func() { m.Group("/commits", func() { + m.Get("", repo.GetAllCommits) m.Get("/:sha", repo.GetSingleCommit) }) m.Get("/refs", repo.GetGitAllRefs) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 3f3bd132683e5..f883a0db6138b 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -6,6 +6,8 @@ package repo import ( + "math" + "strconv" "time" "code.gitea.io/gitea/models" @@ -120,3 +122,204 @@ func GetSingleCommit(ctx *context.APIContext) { Parents: apiParents, }) } + +// GetAllCommits get all commits via +func GetAllCommits(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/git/commits repository repoGetAllCommits + // --- + // summary: Get a list of all commits from a repository + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: sha + // in: query + // description: SHA or branch to start listing commits from (usually 'master') + // type: string + // - name: page + // in: query + // description: page number of requested commits + // type: integer + // responses: + // "200": + // "$ref": "#/responses/CommitList" + // "404": + // "$ref": "#/responses/notFound" + + gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) + if err != nil { + ctx.ServerError("OpenRepository", err) + return + } + + page := ctx.QueryInt("page") + if page <= 0 { + page = 1 + } + + sha := ctx.Query("sha") + + var baseCommit *git.Commit + if len(sha) == 0 { + // no sha supplied - use default branch + head, err := gitRepo.GetHEADBranch() + if err != nil { + ctx.ServerError("GetHEADBranch", err) + return + } + baseCommit, err = gitRepo.GetBranchCommit(head.Name) + if err != nil { + ctx.ServerError("GetCommit", err) + return + } + } else { + // get commit specified by sha + baseCommit, err = gitRepo.GetCommit(sha) + if err != nil { + ctx.ServerError("GetCommit", err) + return + } + } + + // Total commit count + commitsCountTotal, err := baseCommit.CommitsCount() + if err != nil { + ctx.ServerError("GetCommitsCount", err) + return + } + + pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(git.CommitsRangeSize))) + + // Query commits + commits, err := baseCommit.CommitsByRange(page) + if err != nil { + ctx.ServerError("CommitsByRange", err) + return + } + + userCache := make(map[string]*models.User) + + apiCommits := make([]*api.Commit, commits.Len()) + + i := 0 + for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() { + commit := commitPointer.Value.(*git.Commit) + + var apiAuthor, apiCommitter *api.User + + // Retrieve author and committer information + 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) { + ctx.ServerError("Get user by author email", err) + return + } else if err == nil { + apiAuthor = author.APIFormat() + userCache[commit.Author.Email] = author + } + } + 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) { + ctx.ServerError("Get user by committer email", err) + return + } else if err == nil { + apiCommitter = committer.APIFormat() + 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: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(), + SHA: sha.String(), + } + } + + // Create json struct + apiCommits[i] = &api.Commit{ + CommitMeta: &api.CommitMeta{ + URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + commit.ID.String(), + SHA: commit.ID.String(), + }, + HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(), + RepoCommit: &api.RepoCommit{ + URL: setting.AppURL + ctx.Link[1:], + 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.Summary(), + Tree: &api.CommitMeta{ + URL: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(), + SHA: commit.ID.String(), + }, + }, + Author: apiAuthor, + Committer: apiCommitter, + Parents: apiParents, + } + + i++ + } + + ctx.SetLinkHeader(int(commitsCountTotal), git.CommitsRangeSize) + + ctx.Header().Set("X-Page", strconv.Itoa(page)) + ctx.Header().Set("X-PerPage", strconv.Itoa(git.CommitsRangeSize)) + ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10)) + ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount)) + ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount)) + + ctx.JSON(200, &apiCommits) +} + +// CommitList +// swagger:response CommitList +type swaggerCommitList struct { + // The current page + Page int `json:"X-Page"` + + // Commits per page + PerPage int `json:"X-PerPage"` + + // Total commit count + Total int `json:"X-Total"` + + // Total number of pages + PageCount int `json:"X-PageCount"` + + // True if there is another page + HasMore bool `json:"X-HasMore"` + + //in: body + Body []api.Commit `json:"body"` +} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 2b40c89791a05..77baa5e52a6f4 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -1914,6 +1914,54 @@ } } }, + "/repos/{owner}/{repo}/git/commits": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get a list of all commits from a repository", + "operationId": "repoGetAllCommits", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "SHA or branch to start listing commits from (usually 'master')", + "name": "sha", + "in": "query" + }, + { + "type": "integer", + "description": "page number of requested commits", + "name": "page", + "in": "query" + } + ], + "responses": { + "200": { + "$ref": "#/responses/CommitList" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/git/commits/{sha}": { "get": { "produces": [ @@ -9876,6 +9924,41 @@ "$ref": "#/definitions/Commit" } }, + "CommitList": { + "description": "CommitList", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Commit" + } + }, + "headers": { + "X-HasMore": { + "type": "boolean", + "description": "True if there is another page" + }, + "X-Page": { + "type": "integer", + "format": "int64", + "description": "The current page" + }, + "X-PageCount": { + "type": "integer", + "format": "int64", + "description": "Total number of pages" + }, + "X-PerPage": { + "type": "integer", + "format": "int64", + "description": "Commits per page" + }, + "X-Total": { + "type": "integer", + "format": "int64", + "description": "Total commit count" + } + } + }, "DeployKey": { "description": "DeployKey", "schema": { From 6a2fe667a46aebca2c02c5a999ee54125b6d66b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 19 Jun 2019 14:14:05 +0200 Subject: [PATCH 02/15] Fixed failing drone build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mike Schwörer --- integrations/api_repo_git_commits_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/api_repo_git_commits_test.go b/integrations/api_repo_git_commits_test.go index 2f48249ddd9db..c7b50dad0ffc8 100644 --- a/integrations/api_repo_git_commits_test.go +++ b/integrations/api_repo_git_commits_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - api "code.gitea.io/sdk/gitea" + api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) From 413b290b58df0de24bdc3a1756500df49c9ebf72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 25 Jul 2019 18:20:46 +0200 Subject: [PATCH 03/15] Implemented requested changes (PR reviews) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mike Schwörer --- routers/api/v1/repo/commits.go | 30 ++++-------------------------- routers/api/v1/swagger/repo.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 7e15d595334a8..463583ff3b34c 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -260,9 +260,9 @@ func GetAllCommits(ctx *context.APIContext) { URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + commit.ID.String(), SHA: commit.ID.String(), }, - HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(), + HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(), RepoCommit: &api.RepoCommit{ - URL: setting.AppURL + ctx.Link[1:], + URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + commit.ID.String(), Author: &api.CommitUser{ Identity: api.Identity{ Name: commit.Committer.Name, @@ -279,7 +279,7 @@ func GetAllCommits(ctx *context.APIContext) { }, Message: commit.Summary(), Tree: &api.CommitMeta{ - URL: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(), + URL: ctx.Repo.Repository.APIURL() + "/git/trees/" + commit.ID.String(), SHA: commit.ID.String(), }, }, @@ -300,26 +300,4 @@ func GetAllCommits(ctx *context.APIContext) { ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount)) ctx.JSON(200, &apiCommits) -} - -// CommitList -// swagger:response CommitList -type swaggerCommitList struct { - // The current page - Page int `json:"X-Page"` - - // Commits per page - PerPage int `json:"X-PerPage"` - - // Total commit count - Total int `json:"X-Total"` - - // Total number of pages - PageCount int `json:"X-PageCount"` - - // True if there is another page - HasMore bool `json:"X-HasMore"` - - //in: body - Body []api.Commit `json:"body"` -} +} \ No newline at end of file diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 2cab5b0ed42f7..2ae67e7926118 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -190,6 +190,29 @@ type swaggerCommit struct { Body api.Commit `json:"body"` } +// CommitList +// swagger:response CommitList +type swaggerCommitList struct { + // The current page + Page int `json:"X-Page"` + + // Commits per page + PerPage int `json:"X-PerPage"` + + // Total commit count + Total int `json:"X-Total"` + + // Total number of pages + PageCount int `json:"X-PageCount"` + + // True if there is another page + HasMore bool `json:"X-HasMore"` + + //in: body + Body []api.Commit `json:"body"` +} + + // FileResponse // swagger:response FileResponse type swaggerFileResponse struct { From ab8afc47b3778f05f590fcccc185bc942ba4b2ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 25 Jul 2019 18:33:28 +0200 Subject: [PATCH 04/15] gofmt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mike Schwörer --- routers/api/v1/repo/commits.go | 2 +- routers/api/v1/swagger/repo.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 463583ff3b34c..5bb0941acd1ca 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -300,4 +300,4 @@ func GetAllCommits(ctx *context.APIContext) { ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount)) ctx.JSON(200, &apiCommits) -} \ No newline at end of file +} diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 2ae67e7926118..0833c6515e0ae 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -212,7 +212,6 @@ type swaggerCommitList struct { Body []api.Commit `json:"body"` } - // FileResponse // swagger:response FileResponse type swaggerFileResponse struct { From 9a3e66f83b05a875d953aa351aa47215f1f713d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 30 Jul 2019 17:01:29 +0200 Subject: [PATCH 05/15] Changed api route from "/repos/{owner}/{repo}/git/commits" to "/repos/{owner}/{repo}/commits" --- integrations/api_repo_git_commits_test.go | 6 +- routers/api/v1/api.go | 12 +-- routers/api/v1/repo/commits.go | 2 +- templates/swagger/v1_json.tmpl | 96 +++++++++++------------ 4 files changed, 59 insertions(+), 57 deletions(-) diff --git a/integrations/api_repo_git_commits_test.go b/integrations/api_repo_git_commits_test.go index c7b50dad0ffc8..16db1e871c727 100644 --- a/integrations/api_repo_git_commits_test.go +++ b/integrations/api_repo_git_commits_test.go @@ -42,7 +42,7 @@ func TestAPIReposGitCommitList(t *testing.T) { token := getTokenForLoggedInUser(t, session) // Test getting commits (Page 1) - req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits?token="+token, user.Name) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token, user.Name) resp := session.MakeRequest(t, req, http.StatusOK) var apiData []api.Commit @@ -62,7 +62,7 @@ func TestAPIReposGitCommitListPage2Empty(t *testing.T) { token := getTokenForLoggedInUser(t, session) // Test getting commits (Page=2) - req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits?token="+token+"&page=2", user.Name) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&page=2", user.Name) resp := session.MakeRequest(t, req, http.StatusOK) var apiData []api.Commit @@ -79,7 +79,7 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { token := getTokenForLoggedInUser(t, session) // Test getting commits (Page=1, Branch=good-sign) - req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits?token="+token+"&sha=good-sign", user.Name) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign", user.Name) resp := session.MakeRequest(t, req, http.StatusOK) var apiData []api.Commit diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0edf04863e257..9a6f901f21c5a 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -751,14 +751,16 @@ func RegisterRoutes(m *macaron.Macaron) { m.Combo("/:sha").Get(repo.GetCommitStatuses). Post(reqToken(), bind(api.CreateStatusOption{}), repo.NewCommitStatus) }, reqRepoReader(models.UnitTypeCode)) - m.Group("/commits/:ref", func() { - // TODO: Add m.Get("") for single commit (https://developer.github.com/v3/repos/commits/#get-a-single-commit) - m.Get("/status", repo.GetCombinedCommitStatusByRef) - m.Get("/statuses", repo.GetCommitStatusesByRef) + m.Group("/commits", func() { + m.Get("", repo.GetAllCommits) + m.Group("/:ref", func() { + // TODO: Add m.Get("") for single commit (https://developer.github.com/v3/repos/commits/#get-a-single-commit) + m.Get("/status", repo.GetCombinedCommitStatusByRef) + m.Get("/statuses", repo.GetCommitStatusesByRef) + }, reqRepoReader(models.UnitTypeCode)) }, reqRepoReader(models.UnitTypeCode)) m.Group("/git", func() { m.Group("/commits", func() { - m.Get("", repo.GetAllCommits) m.Get("/:sha", repo.GetSingleCommit) }) m.Get("/refs", repo.GetGitAllRefs) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 5bb0941acd1ca..48b81b9c820c8 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -125,7 +125,7 @@ func GetSingleCommit(ctx *context.APIContext) { // GetAllCommits get all commits via func GetAllCommits(ctx *context.APIContext) { - // swagger:operation GET /repos/{owner}/{repo}/git/commits repository repoGetAllCommits + // swagger:operation GET /repos/{owner}/{repo}/commits repository repoGetAllCommits // --- // summary: Get a list of all commits from a repository // produces: diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 6cde3ef1a4a77..d7700d2cc23e7 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -1530,6 +1530,54 @@ } } }, + "/repos/{owner}/{repo}/commits": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get a list of all commits from a repository", + "operationId": "repoGetAllCommits", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "SHA or branch to start listing commits from (usually 'master')", + "name": "sha", + "in": "query" + }, + { + "type": "integer", + "description": "page number of requested commits", + "name": "page", + "in": "query" + } + ], + "responses": { + "200": { + "$ref": "#/responses/CommitList" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/commits/{ref}/statuses": { "get": { "produces": [ @@ -1959,54 +2007,6 @@ } } }, - "/repos/{owner}/{repo}/git/commits": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "repository" - ], - "summary": "Get a list of all commits from a repository", - "operationId": "repoGetAllCommits", - "parameters": [ - { - "type": "string", - "description": "owner of the repo", - "name": "owner", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "name of the repo", - "name": "repo", - "in": "path", - "required": true - }, - { - "type": "string", - "description": "SHA or branch to start listing commits from (usually 'master')", - "name": "sha", - "in": "query" - }, - { - "type": "integer", - "description": "page number of requested commits", - "name": "page", - "in": "query" - } - ], - "responses": { - "200": { - "$ref": "#/responses/CommitList" - }, - "404": { - "$ref": "#/responses/notFound" - } - } - } - }, "/repos/{owner}/{repo}/git/commits/{sha}": { "get": { "produces": [ From 30e3006789032d0439f4c541f21b6782efd47855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 30 Jul 2019 17:47:26 +0200 Subject: [PATCH 06/15] Removed unnecessary line --- routers/api/v1/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 9a6f901f21c5a..66b45b3b96f8f 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -757,7 +757,7 @@ func RegisterRoutes(m *macaron.Macaron) { // TODO: Add m.Get("") for single commit (https://developer.github.com/v3/repos/commits/#get-a-single-commit) m.Get("/status", repo.GetCombinedCommitStatusByRef) m.Get("/statuses", repo.GetCommitStatusesByRef) - }, reqRepoReader(models.UnitTypeCode)) + }) }, reqRepoReader(models.UnitTypeCode)) m.Group("/git", func() { m.Group("/commits", func() { From d306b00ba5b8abb24df4af1dd1452dd7fd59faf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 2 Aug 2019 16:05:47 +0200 Subject: [PATCH 07/15] better error message when git repo is empty --- modules/structs/miscellaneous.go | 6 ++++++ routers/api/v1/repo/commits.go | 10 +++++++++- routers/api/v1/swagger/repo.go | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/structs/miscellaneous.go b/modules/structs/miscellaneous.go index 8eca90330e175..ee31888420034 100644 --- a/modules/structs/miscellaneous.go +++ b/modules/structs/miscellaneous.go @@ -44,3 +44,9 @@ type MarkdownRender string type ServerVersion struct { Version string `json:"version"` } + +// SearchError error of a failed search +type APIError struct { + Message string `json:"message"` + URL string `json:"url"` +} diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 48b81b9c820c8..894ca0c082ba9 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -154,6 +154,14 @@ func GetAllCommits(ctx *context.APIContext) { // "$ref": "#/responses/CommitList" // "404": // "$ref": "#/responses/notFound" + // "409": + // "$ref": "#/responses/EmptyRepository" + + if ctx.Repo.Repository.IsEmpty { + err409 := api.APIError{Message: "Git Repository is empty.", URL: setting.API.SwaggerURL} + ctx.JSON(409, err409) + return + } gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) if err != nil { @@ -176,10 +184,10 @@ func GetAllCommits(ctx *context.APIContext) { ctx.ServerError("GetHEADBranch", err) return } + baseCommit, err = gitRepo.GetBranchCommit(head.Name) if err != nil { ctx.ServerError("GetCommit", err) - return } } else { // get commit specified by sha diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 0833c6515e0ae..422cc0861cc10 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -212,6 +212,13 @@ type swaggerCommitList struct { Body []api.Commit `json:"body"` } +// EmptyRepository +// swagger:response EmptyRepository +type swaggerEmptyRepository struct { + //in: body + Body api.APIError `json:"body"` +} + // FileResponse // swagger:response FileResponse type swaggerFileResponse struct { From 37606256bce8af0b799bf52a15eb0907bde180d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 2 Aug 2019 16:50:03 +0200 Subject: [PATCH 08/15] make generate-swagger --- modules/structs/miscellaneous.go | 2 +- templates/swagger/v1_json.tmpl | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/structs/miscellaneous.go b/modules/structs/miscellaneous.go index ee31888420034..c21c466cb075d 100644 --- a/modules/structs/miscellaneous.go +++ b/modules/structs/miscellaneous.go @@ -45,7 +45,7 @@ type ServerVersion struct { Version string `json:"version"` } -// SearchError error of a failed search +// APIError is an api error with a message type APIError struct { Message string `json:"message"` URL string `json:"url"` diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index d7700d2cc23e7..55513732af9b4 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -1574,6 +1574,9 @@ }, "404": { "$ref": "#/responses/notFound" + }, + "409": { + "$ref": "#/responses/EmptyRepository" } } } @@ -6900,6 +6903,21 @@ } }, "definitions": { + "APIError": { + "description": "APIError is an api error with a message", + "type": "object", + "properties": { + "message": { + "type": "string", + "x-go-name": "Message" + }, + "url": { + "type": "string", + "x-go-name": "URL" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "AddCollaboratorOption": { "description": "AddCollaboratorOption options when adding a user as a collaborator of a repository", "type": "object", @@ -10140,6 +10158,12 @@ } } }, + "EmptyRepository": { + "description": "EmptyRepository", + "schema": { + "$ref": "#/definitions/APIError" + } + }, "FileDeleteResponse": { "description": "FileDeleteResponse", "schema": { From bf8315cce3abb2ff166b72b34ee9cc822406461e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 2 Aug 2019 20:41:57 +0200 Subject: [PATCH 09/15] fixed removed return --- routers/api/v1/repo/commits.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 894ca0c082ba9..2fd9c59bef4b0 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -188,6 +188,7 @@ func GetAllCommits(ctx *context.APIContext) { baseCommit, err = gitRepo.GetBranchCommit(head.Name) if err != nil { ctx.ServerError("GetCommit", err) + return } } else { // get commit specified by sha From f0aadfe801cb46d602f0a85f71e8c1298fd0ef3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 9 Aug 2019 12:02:46 +0200 Subject: [PATCH 10/15] Update routers/api/v1/repo/commits.go Co-Authored-By: Lauris BH --- routers/api/v1/repo/commits.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 2fd9c59bef4b0..ff054bf5d28cb 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -158,7 +158,10 @@ func GetAllCommits(ctx *context.APIContext) { // "$ref": "#/responses/EmptyRepository" if ctx.Repo.Repository.IsEmpty { - err409 := api.APIError{Message: "Git Repository is empty.", URL: setting.API.SwaggerURL} + ctx.JSON(409, api.APIError{ + Message: "Git Repository is empty.", + URL: setting.API.SwaggerURL, + } ctx.JSON(409, err409) return } From 6a05550b4512880d4a4c16549d8f4337b3d63874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 9 Aug 2019 12:07:50 +0200 Subject: [PATCH 11/15] Update routers/api/v1/repo/commits.go Co-Authored-By: Lauris BH --- routers/api/v1/repo/commits.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index ff054bf5d28cb..83491dec1777b 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -162,7 +162,6 @@ func GetAllCommits(ctx *context.APIContext) { Message: "Git Repository is empty.", URL: setting.API.SwaggerURL, } - ctx.JSON(409, err409) return } From 9cb32d6ba4f792edfca13b3533c17a9e0ba8eaf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 9 Aug 2019 12:21:02 +0200 Subject: [PATCH 12/15] go fmt --- routers/api/v1/repo/commits.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 83491dec1777b..cda7801bdefd7 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -160,8 +160,8 @@ func GetAllCommits(ctx *context.APIContext) { if ctx.Repo.Repository.IsEmpty { ctx.JSON(409, api.APIError{ Message: "Git Repository is empty.", - URL: setting.API.SwaggerURL, - } + URL: setting.API.SwaggerURL, + }) return } From b5c94355af4469131b2fc39b64da4f2d27bbe249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 26 Aug 2019 14:06:54 +0200 Subject: [PATCH 13/15] Refactored common code into ToCommit() --- routers/api/v1/repo/commits.go | 231 ++++++++++++++------------------- 1 file changed, 99 insertions(+), 132 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index cda7801bdefd7..6301ddb6f8078 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -57,70 +57,13 @@ func GetSingleCommit(ctx *context.APIContext) { return } - // Retrieve author and committer information - var apiAuthor, apiCommitter *api.User - author, err := models.GetUserByEmail(commit.Author.Email) - if err != nil && !models.IsErrUserNotExist(err) { - ctx.ServerError("Get user by author email", err) + json, err := ToCommit(ctx, ctx.Repo.Repository, commit, nil) + if err != nil { + ctx.ServerError("ToCommit", err) return - } 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 := models.GetUserByEmail(commit.Committer.Email) - if err != nil && !models.IsErrUserNotExist(err) { - ctx.ServerError("Get user by committer email", err) - return - } else if err == nil { - apiCommitter = committer.APIFormat() - } - } - - // 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: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(), - SHA: sha.String(), - } } - ctx.JSON(200, &api.Commit{ - CommitMeta: &api.CommitMeta{ - URL: setting.AppURL + ctx.Link[1:], - SHA: commit.ID.String(), - }, - HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(), - RepoCommit: &api.RepoCommit{ - URL: setting.AppURL + ctx.Link[1:], - Author: &api.CommitUser{ - Identity: api.Identity{ - Name: commit.Author.Name, - Email: commit.Author.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: ctx.Repo.Repository.APIURL() + "/git/trees/" + commit.ID.String(), - SHA: commit.ID.String(), - }, - }, - Author: apiAuthor, - Committer: apiCommitter, - Parents: apiParents, - }) + ctx.JSON(200, json) } // GetAllCommits get all commits via @@ -225,78 +168,11 @@ func GetAllCommits(ctx *context.APIContext) { for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() { commit := commitPointer.Value.(*git.Commit) - var apiAuthor, apiCommitter *api.User - - // Retrieve author and committer information - 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) { - ctx.ServerError("Get user by author email", err) - return - } else if err == nil { - apiAuthor = author.APIFormat() - userCache[commit.Author.Email] = author - } - } - 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) { - ctx.ServerError("Get user by committer email", err) - return - } else if err == nil { - apiCommitter = committer.APIFormat() - 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: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(), - SHA: sha.String(), - } - } - // Create json struct - apiCommits[i] = &api.Commit{ - CommitMeta: &api.CommitMeta{ - URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + commit.ID.String(), - SHA: commit.ID.String(), - }, - HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(), - RepoCommit: &api.RepoCommit{ - URL: ctx.Repo.Repository.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.Summary(), - Tree: &api.CommitMeta{ - URL: ctx.Repo.Repository.APIURL() + "/git/trees/" + commit.ID.String(), - SHA: commit.ID.String(), - }, - }, - Author: apiAuthor, - Committer: apiCommitter, - Parents: apiParents, + apiCommits[i], err = ToCommit(ctx, ctx.Repo.Repository, commit, userCache) + if err != nil { + ctx.ServerError("ToCommit", err) + return } i++ @@ -312,3 +188,94 @@ func GetAllCommits(ctx *context.APIContext) { ctx.JSON(200, &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() + 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() + 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.Summary(), + Tree: &api.CommitMeta{ + URL: repo.APIURL() + "/git/trees/" + commit.ID.String(), + SHA: commit.ID.String(), + }, + }, + Author: apiAuthor, + Committer: apiCommitter, + Parents: apiParents, + }, nil +} From 0852fa523649b86472812f7efe52eeadc20a585f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 26 Aug 2019 14:18:22 +0200 Subject: [PATCH 14/15] made toCommit not exported --- routers/api/v1/repo/commits.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 6301ddb6f8078..c02e32bb73027 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -57,9 +57,9 @@ func GetSingleCommit(ctx *context.APIContext) { return } - json, err := ToCommit(ctx, ctx.Repo.Repository, commit, nil) + json, err := toCommit(ctx, ctx.Repo.Repository, commit, nil) if err != nil { - ctx.ServerError("ToCommit", err) + ctx.ServerError("toCommit", err) return } @@ -169,9 +169,9 @@ 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 = toCommit(ctx, ctx.Repo.Repository, commit, userCache) if err != nil { - ctx.ServerError("ToCommit", err) + ctx.ServerError("toCommit", err) return } @@ -189,7 +189,7 @@ func GetAllCommits(ctx *context.APIContext) { ctx.JSON(200, &apiCommits) } -func ToCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) { +func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) { var apiAuthor, apiCommitter *api.User From e91dbbd2af241dad41750d100e34a19329faf862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 26 Aug 2019 14:41:33 +0200 Subject: [PATCH 15/15] added check for userCache == nil --- routers/api/v1/repo/commits.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index c02e32bb73027..0156aaaa05cd3 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -212,7 +212,9 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm return nil, err } else if err == nil { apiAuthor = author.APIFormat() - userCache[commit.Author.Email] = author + if userCache != nil { + userCache[commit.Author.Email] = author + } } } @@ -232,7 +234,9 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm return nil, err } else if err == nil { apiCommitter = committer.APIFormat() - userCache[commit.Committer.Email] = committer + if userCache != nil { + userCache[commit.Committer.Email] = committer + } } }