From d291936302f0414fd2fbf09f36e62c1452426a1b Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Mon, 3 Aug 2020 11:54:01 +0200 Subject: [PATCH 01/11] Add files affected by a commit to gitea API -- similar to github --- modules/structs/repo_commit.go | 1 + routers/api/v1/repo/commits.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go index 9cde2873d4cde..1e75b0be91a5c 100644 --- a/modules/structs/repo_commit.go +++ b/modules/structs/repo_commit.go @@ -41,4 +41,5 @@ type Commit struct { Author *User `json:"author"` Committer *User `json:"committer"` Parents []*CommitMeta `json:"parents"` + Files []string `json:"files"` } diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 163a06a95e8e1..a09b9a265bc49 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -252,6 +252,14 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm } } + // Retrieve files affected by the commit + fileStatus, err := git.GetCommitFileStatus(repo.RepoPath(), commit.ID.String()) + if err != nil { + return nil, err + } + affectedFiles := append(fileStatus.Added, fileStatus.Removed...) + affectedFiles = append(affectedFiles, fileStatus.Modified...) + return &api.Commit{ CommitMeta: &api.CommitMeta{ URL: repo.APIURL() + "/git/commits/" + commit.ID.String(), @@ -283,5 +291,6 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm Author: apiAuthor, Committer: apiCommitter, Parents: apiParents, + Files: affectedFiles, }, nil } From bcbca82d44a83a77f1686a38a8a1d0d49bb83e53 Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Thu, 17 Sep 2020 10:48:40 +0200 Subject: [PATCH 02/11] Add files affected by a commit to gitea API --- modules/structs/repo_commit.go | 4 ++++ routers/api/v1/repo/commits.go | 16 +++++++++++----- templates/swagger/v1_json.tmpl | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go index 6083862c00b1f..13c17ca2a9357 100644 --- a/modules/structs/repo_commit.go +++ b/modules/structs/repo_commit.go @@ -55,3 +55,7 @@ type CommitDateOptions struct { // swagger:strfmt date-time Committer time.Time `json:"committer"` } + +type CommitAffectedFiles struct { + Filename string `json:"filename"` +} diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 1112c53e05ce8..3f5be799af8cb 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -278,11 +278,17 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm // Retrieve files affected by the commit fileStatus, err := git.GetCommitFileStatus(repo.RepoPath(), commit.ID.String()) - if err != nil { - return nil, err - } - affectedFiles := append(fileStatus.Added, fileStatus.Removed...) + if err != nil { + return nil, err + } + affectedFiles := append(fileStatus.Added, fileStatus.Removed...) affectedFiles = append(affectedFiles, fileStatus.Modified...) + affectedFileList := make([]*api.CommitAffectedFiles, len(affectedFiles)) + for i := 0; i < len(affectedFiles); i++ { + affectedFileList[i] = &api.CommitAffectedFiles{ + Filename: affectedFiles[i], + } + } return &api.Commit{ CommitMeta: &api.CommitMeta{ @@ -315,6 +321,6 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm Author: apiAuthor, Committer: apiCommitter, Parents: apiParents, - Files: affectedFiles, + Files: affectedFiles, }, nil } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index c601809a757bc..4837464ded960 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -10958,6 +10958,13 @@ }, "x-go-name": "Parents" }, + "files": { + "type": "array", + "items": { + "type": "#/definitions/CommitAffectedFiles" + }, + "x-go-name": "Files" + }, "sha": { "type": "string", "x-go-name": "SHA" @@ -11001,6 +11008,16 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "CommitAffectedFiles": { + "type": "object", + "title": "CommitAffectedFiles contains the list of files affected by the commit", + "properties": { + "filename": { + "type": "string", + "x-go-name": "Filename" + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CommitUser": { "type": "object", "title": "CommitUser contains information of a user in the context of a commit.", From cc98a4bb91343c8ebf350eda3634970b05934edc Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Thu, 17 Sep 2020 14:42:20 +0200 Subject: [PATCH 03/11] Fix stupid error --- modules/structs/repo_commit.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go index 13c17ca2a9357..832a08b3518b6 100644 --- a/modules/structs/repo_commit.go +++ b/modules/structs/repo_commit.go @@ -40,12 +40,12 @@ type RepoCommit struct { // Commit contains information generated from a Git commit. type Commit struct { *CommitMeta - HTMLURL string `json:"html_url"` - RepoCommit *RepoCommit `json:"commit"` - Author *User `json:"author"` - Committer *User `json:"committer"` - Parents []*CommitMeta `json:"parents"` - Files []string `json:"files"` + HTMLURL string `json:"html_url"` + RepoCommit *RepoCommit `json:"commit"` + Author *User `json:"author"` + Committer *User `json:"committer"` + Parents []*CommitMeta `json:"parents"` + Files []*CommitAffectedFiles `json:"files"` } // CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE @@ -56,6 +56,7 @@ type CommitDateOptions struct { Committer time.Time `json:"committer"` } +// CommitAffectedFiles store information about files affected by the commit type CommitAffectedFiles struct { Filename string `json:"filename"` } From ab2a4c650ae716323bfaabc3b52bf57c1b685a9b Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Thu, 17 Sep 2020 14:59:39 +0200 Subject: [PATCH 04/11] Fix other stupid typo --- routers/api/v1/repo/commits.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 3f5be799af8cb..b4a89fe1b4dc2 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -321,6 +321,6 @@ func toCommit(ctx *context.APIContext, repo *models.Repository, commit *git.Comm Author: apiAuthor, Committer: apiCommitter, Parents: apiParents, - Files: affectedFiles, + Files: affectedFileList, }, nil } From 3be1a8d1c4c8f27cbf5087f3e3ec47299cecfeb2 Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Thu, 17 Sep 2020 18:06:17 +0200 Subject: [PATCH 05/11] Generate swagger tmpl --- templates/swagger/v1_json.tmpl | 35 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 4837464ded960..7d05d419af1fa 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -10947,6 +10947,13 @@ "committer": { "$ref": "#/definitions/User" }, + "files": { + "type": "array", + "items": { + "$ref": "#/definitions/CommitAffectedFiles" + }, + "x-go-name": "Files" + }, "html_url": { "type": "string", "x-go-name": "HTMLURL" @@ -10958,13 +10965,6 @@ }, "x-go-name": "Parents" }, - "files": { - "type": "array", - "items": { - "type": "#/definitions/CommitAffectedFiles" - }, - "x-go-name": "Files" - }, "sha": { "type": "string", "x-go-name": "SHA" @@ -10976,6 +10976,17 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "CommitAffectedFiles": { + "description": "CommitAffectedFiles store information about files affected by the commit", + "type": "object", + "properties": { + "filename": { + "type": "string", + "x-go-name": "Filename" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CommitDateOptions": { "description": "CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE", "type": "object", @@ -11008,16 +11019,6 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, - "CommitAffectedFiles": { - "type": "object", - "title": "CommitAffectedFiles contains the list of files affected by the commit", - "properties": { - "filename": { - "type": "string", - "x-go-name": "Filename" - }, - "x-go-package": "code.gitea.io/gitea/modules/structs" - }, "CommitUser": { "type": "object", "title": "CommitUser contains information of a user in the context of a commit.", From 3cbcd39bbc2b57c6bb6cfe12858946e998b10ecb Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Tue, 22 Sep 2020 19:35:21 +0200 Subject: [PATCH 06/11] Comply with convert to git commit refacto --- modules/convert/git_commit.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index 55cbb3af80e39..cc27031479f9c 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -130,6 +130,20 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } } + // Retrieve files affected by the commit + fileStatus, err := git.GetCommitFileStatus(repo.RepoPath(), commit.ID.String()) + if err != nil { + return nil, err + } + affectedFiles := append(fileStatus.Added, fileStatus.Removed...) + affectedFiles = append(affectedFiles, fileStatus.Modified...) + affectedFileList := make([]*api.CommitAffectedFiles, len(affectedFiles)) + for i := 0; i < len(affectedFiles); i++ { + affectedFileList[i] = &api.CommitAffectedFiles{ + Filename: affectedFiles[i], + } + } + return &api.Commit{ CommitMeta: &api.CommitMeta{ URL: repo.APIURL() + "/git/commits/" + commit.ID.String(), @@ -161,5 +175,6 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] Author: apiAuthor, Committer: apiCommitter, Parents: apiParents, + Files: affectedFileList, }, nil } From 94c8353541237ceb80b9679fe066b1d9c4af9c09 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Feb 2021 18:53:22 +0100 Subject: [PATCH 07/11] update swagger docs --- templates/swagger/v1_json.tmpl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index fd760a28e6c47..5a3be37b4aec4 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -11821,6 +11821,13 @@ "format": "date-time", "x-go-name": "Created" }, + "files": { + "type": "array", + "items": { + "$ref": "#/definitions/CommitAffectedFiles" + }, + "x-go-name": "Files" + }, "html_url": { "type": "string", "x-go-name": "HTMLURL" @@ -11843,6 +11850,17 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "CommitAffectedFiles": { + "description": "CommitAffectedFiles store information about files affected by the commit", + "type": "object", + "properties": { + "filename": { + "type": "string", + "x-go-name": "Filename" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CommitDateOptions": { "description": "CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE", "type": "object", From a8098dfc846f82e5f4740fd733b32a27bf1887b9 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Feb 2021 19:31:36 +0100 Subject: [PATCH 08/11] extend test --- integrations/api_repo_git_commits_test.go | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/integrations/api_repo_git_commits_test.go b/integrations/api_repo_git_commits_test.go index 5b0f82e854121..f302ee2eb5d05 100644 --- a/integrations/api_repo_git_commits_test.go +++ b/integrations/api_repo_git_commits_test.go @@ -14,6 +14,14 @@ import ( "github.com/stretchr/testify/assert" ) +func compareCommitFiles(t *testing.T, expect []string, files []*api.CommitAffectedFiles) { + var actual []string + for i := range files { + actual = append(actual, files[i].Filename) + } + assert.ElementsMatch(t, expect, actual) +} + func TestAPIReposGitCommits(t *testing.T) { defer prepareTestEnv(t)() user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) @@ -56,10 +64,13 @@ func TestAPIReposGitCommitList(t *testing.T) { 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) + assert.Len(t, apiData, 3) + assert.EqualValues(t, "69554a64c1e6030f051e5c3f94bfbd773cd6a324", apiData[0].CommitMeta.SHA) + compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files) + assert.EqualValues(t, "27566bd5738fc8b4e3fef3c5e72cce608537bd95", apiData[1].CommitMeta.SHA) + compareCommitFiles(t, []string{"readme.md"}, apiData[1].Files) + assert.EqualValues(t, "5099b81332712fe655e34e8dd63574f503f61811", apiData[2].CommitMeta.SHA) + compareCommitFiles(t, []string{"readme.md"}, apiData[2].Files) } func TestAPIReposGitCommitListPage2Empty(t *testing.T) { @@ -76,7 +87,7 @@ func TestAPIReposGitCommitListPage2Empty(t *testing.T) { var apiData []api.Commit DecodeJSON(t, resp, &apiData) - assert.Equal(t, 0, len(apiData)) + assert.Len(t, apiData, 0) } func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { @@ -93,6 +104,7 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { var apiData []api.Commit DecodeJSON(t, resp, &apiData) - assert.Equal(t, 1, len(apiData)) + assert.Len(t, apiData, 1) assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) + compareCommitFiles(t, []string{"readme.md"}, apiData[2].Files) } From 0f01438f464c8b99cdd8c04c16e5c1d0fd876146 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Feb 2021 19:32:17 +0100 Subject: [PATCH 09/11] format code --- modules/convert/git_commit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index 223fb30e9d6a0..e2d5d14ae7d4f 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -139,7 +139,7 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] affectedFiles := append(fileStatus.Added, fileStatus.Removed...) affectedFiles = append(affectedFiles, fileStatus.Modified...) affectedFileList := make([]*api.CommitAffectedFiles, len(affectedFiles)) - for i := 0; i < len(affectedFiles); i++ { + for i := range affectedFiles { affectedFileList[i] = &api.CommitAffectedFiles{ Filename: affectedFiles[i], } From df914399b68c3a5a0adeb3b8ee034d0bfcaa721d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 5 Feb 2021 22:10:38 +0100 Subject: [PATCH 10/11] Update integrations/api_repo_git_commits_test.go --- 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 f302ee2eb5d05..d6bd5fc62e6bd 100644 --- a/integrations/api_repo_git_commits_test.go +++ b/integrations/api_repo_git_commits_test.go @@ -106,5 +106,5 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { assert.Len(t, apiData, 1) assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) - compareCommitFiles(t, []string{"readme.md"}, apiData[2].Files) + compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files) } From 8d8def95be0401a3d3ddcea8a782a60c431516d4 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 7 Feb 2021 13:10:44 +0000 Subject: [PATCH 11/11] Update modules/convert/git_commit.go --- modules/convert/git_commit.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index e2d5d14ae7d4f..4e30ec2c0b334 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -136,12 +136,12 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] if err != nil { return nil, err } - affectedFiles := append(fileStatus.Added, fileStatus.Removed...) - affectedFiles = append(affectedFiles, fileStatus.Modified...) - affectedFileList := make([]*api.CommitAffectedFiles, len(affectedFiles)) - for i := range affectedFiles { - affectedFileList[i] = &api.CommitAffectedFiles{ - Filename: affectedFiles[i], + affectedFileList := make([]*api.CommitAffectedFiles, 0, len(fileStatus.Added)+len(fileStatus.Removed)+len(fileStatus.Modified)) + for _, files := range [][]string{fileStatus.Added, fileStatus.Removed, fileStatus.Modified} { + for _, filename := range files { + affectedFileList = append(affectedFileList, &api.CommitAffectedFiles{ + Filename: filename, + }) } }