Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single commit API support #5843

Merged
merged 8 commits into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions integrations/api_repo_git_commits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"net/http"
"testing"

"code.gitea.io/gitea/models"
)

func TestAPIReposGitCommits(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)

for _, ref := range [...]string{
"commits/master", // Branch
"commits/v1.1", // Tag
} {
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/%s?token="+token, user.Name, ref)
session.MakeRequest(t, req, http.StatusOK)
}

// Test getting non-existent refs
req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/unknown?token="+token, user.Name)
session.MakeRequest(t, req, http.StatusNotFound)
}
3 changes: 3 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/statuses", repo.GetCommitStatusesByRef)
}, reqRepoReader(models.UnitTypeCode))
m.Group("/git", func() {
m.Group("/commits", func() {
m.Get("/:sha", repo.GetSingleCommit)
})
m.Get("/refs", repo.GetGitAllRefs)
m.Get("/refs/*", repo.GetGitRefs)
m.Combo("/trees/:sha", context.RepoRef()).Get(repo.GetTree)
Expand Down
119 changes: 119 additions & 0 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2018 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repo

import (
"time"

"code.gitea.io/git"
api "code.gitea.io/sdk/gitea"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
)

// GetSingleCommit get a commit via
func GetSingleCommit(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
// ---
// summary: Get a single commit 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: path
// description: the commit hash
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Commit"
// "404":
// "$ref": "#/responses/notFound"

gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return
}
commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
if err != nil {
ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
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)
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() + "/commits/" + commit.ID.String(),
RepoCommit: &api.RepoCommit{
URL: setting.AppURL + ctx.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: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(),
SHA: commit.ID.String(),
},
},
Author: apiAuthor,
Committer: apiCommitter,
Parents: apiParents,
})
}
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ type swaggerGitTreeResponse struct {
//in: body
Body api.GitTreeResponse `json:"body"`
}

// Commit
// swagger:response Commit
type swaggerCommit struct {
//in: body
Body api.Commit `json:"body"`
}
142 changes: 142 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,49 @@
}
}
},
"/repos/{owner}/{repo}/git/commits/{sha}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get a single commit from a repository",
"operationId": "repoGetSingleCommit",
"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": "the commit hash",
"name": "sha",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/Commit"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/git/refs": {
"get": {
"produces": [
Expand Down Expand Up @@ -6174,6 +6217,75 @@
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
},
"Commit": {
"type": "object",
"title": "Commit contains information generated from a Git commit.",
"properties": {
"author": {
"$ref": "#/definitions/User"
},
"commit": {
"$ref": "#/definitions/RepoCommit"
},
"committer": {
"$ref": "#/definitions/User"
},
"html_url": {
"type": "string",
"x-go-name": "HTMLURL"
},
"parents": {
"type": "array",
"items": {
"$ref": "#/definitions/CommitMeta"
},
"x-go-name": "Parents"
},
"sha": {
"type": "string",
"x-go-name": "SHA"
},
"url": {
"type": "string",
"x-go-name": "URL"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
},
"CommitMeta": {
"type": "object",
"title": "CommitMeta contains meta information of a commit in terms of API.",
"properties": {
"sha": {
"type": "string",
"x-go-name": "SHA"
},
"url": {
"type": "string",
"x-go-name": "URL"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
},
"CommitUser": {
"type": "object",
"title": "CommitUser contains information of a user in the context of a commit.",
"properties": {
"date": {
"type": "string",
"x-go-name": "Date"
},
"email": {
"type": "string",
"x-go-name": "Email"
},
"name": {
"type": "string",
"x-go-name": "Name"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
},
"CreateEmailOption": {
"description": "CreateEmailOption options when creating email addresses",
"type": "object",
Expand Down Expand Up @@ -7952,6 +8064,30 @@
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
},
"RepoCommit": {
"type": "object",
"title": "RepoCommit contains information of a commit in the context of a repository.",
"properties": {
"author": {
"$ref": "#/definitions/CommitUser"
},
"committer": {
"$ref": "#/definitions/CommitUser"
},
"message": {
"type": "string",
"x-go-name": "Message"
},
"tree": {
"$ref": "#/definitions/CommitMeta"
},
"url": {
"type": "string",
"x-go-name": "URL"
}
},
"x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
},
"Repository": {
"description": "Repository represents a repository",
"type": "object",
Expand Down Expand Up @@ -8382,6 +8518,12 @@
}
}
},
"Commit": {
"description": "Commit",
"schema": {
"$ref": "#/definitions/Commit"
}
},
"DeployKey": {
"description": "DeployKey",
"schema": {
Expand Down
Loading