From d291936302f0414fd2fbf09f36e62c1452426a1b Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Mon, 3 Aug 2020 11:54:01 +0200 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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 }