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 API to get commit diff/patch #17095

Merged
merged 11 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
16 changes: 16 additions & 0 deletions integrations/api_repo_git_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,19 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
}

func TestDownloadCommitDiffOrPatch(t *testing.T) {
defer prepareTestEnv(t)()
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
// Login as User2.
session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session)

// Test getting diff
reqDiff := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.diff?token="+token, user.Name)
session.MakeRequest(t, reqDiff, http.StatusOK)

// Test getting patch
reqPatch := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/git/commits/f27c2b2b03dcab38beaf89b0ab4ff61f6de63441.patch?token="+token, user.Name)
session.MakeRequest(t, reqPatch, http.StatusOK)
}
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
m.Group("/git", func() {
m.Group("/commits", func() {
m.Get("/{sha}", repo.GetSingleCommit)
m.Get("/{sha}.{diffType:diff|patch}", repo.DownloadCommitDiffOrPatch)
})
m.Get("/refs", repo.GetGitAllRefs)
m.Get("/refs/*", repo.GetGitRefs)
Expand Down
50 changes: 50 additions & 0 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,53 @@ func GetAllCommits(ctx *context.APIContext) {

ctx.JSON(http.StatusOK, &apiCommits)
}

// DownloadCommitDiffOrPatch render a commit's raw diff or patch
func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha}.{diffType} repository repoDownloadCommitDiffOrPatch
// ---
// summary: Get a commit's diff or patch
// produces:
// - text/plain
// 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: SHA of the commit to get
// type: string
// required: true
// - name: diffType
// in: path
// description: whether the output is diff or patch
// type: string
// enum: [diff, patch]
// required: true
// responses:
// "200":
// "$ref": "#/responses/string"
// "404":
// "$ref": "#/responses/notFound"
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
if err := git.GetRawDiff(
repoPath,
ctx.Params(":sha"),
git.RawDiffType(ctx.Params(":diffType")),
ctx.Resp,
); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound(ctx.Params(":sha"))
return
}
ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)
return
}
}
54 changes: 54 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3581,6 +3581,60 @@
}
}
},
"/repos/{owner}/{repo}/git/commits/{sha}.{diffType}": {
"get": {
"produces": [
"text/plain"
],
"tags": [
"repository"
],
"summary": "Get a commit's diff or patch",
"operationId": "repoDownloadCommitDiffOrPatch",
"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 of the commit to get",
"name": "sha",
"in": "path",
"required": true
},
{
"enum": [
"diff",
"patch"
],
"type": "string",
"description": "whether the output is diff or patch",
"name": "diffType",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/string"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/git/notes/{sha}": {
"get": {
"produces": [
Expand Down