From 5ff626ecdba8f5e010507c3871ce7d3a5c0b8a61 Mon Sep 17 00:00:00 2001 From: Anton Bracke Date: Mon, 10 Jan 2022 17:20:42 +0100 Subject: [PATCH 01/20] add api endpoint to get changed files of a pr --- modules/convert/convert.go | 8 +++ modules/structs/pull.go | 5 ++ routers/api/v1/api.go | 1 + routers/api/v1/repo/pull.go | 127 +++++++++++++++++++++++++++++++++ routers/api/v1/swagger/repo.go | 22 ++++++ 5 files changed, 163 insertions(+) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 41a044c6d74e2..34e9c6a1094a6 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/services/gitdiff" webhook_service "code.gitea.io/gitea/services/webhook" ) @@ -372,3 +373,10 @@ func ToLFSLock(l *models.LFSLock) *api.LFSLock { }, } } + +// ToChangedFile convert a gitdiff.DiffFile to api.ChangedFile +func ToChangedFile(f *gitdiff.DiffFile) *api.ChangedFile { + return &api.ChangedFile{ + Filename: f.Name, + } +} diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 653091b2f4321..a28be5513fda6 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -93,3 +93,8 @@ type EditPullRequestOption struct { Deadline *time.Time `json:"due_date"` RemoveDeadline *bool `json:"unset_due_date"` } + +// ChangedFile store information about files affected by the pull request +type ChangedFile struct { + Filename string `json:"filename"` +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 7a2347650a05b..09f07095cffe8 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -930,6 +930,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch) m.Post("/update", reqToken(), repo.UpdatePullRequest) m.Get("/commits", repo.GetPullRequestCommits) + m.Get("/files", repo.GetPullRequestFiles) m.Combo("/merge").Get(repo.IsPullRequestMerged). Post(reqToken(), mustNotBeArchived, bind(forms.MergePullRequestForm{}), repo.MergePullRequest) m.Group("/reviews", func() { diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 8297e35a17765..7809e0cad10df 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -22,12 +22,14 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" asymkey_service "code.gitea.io/gitea/services/asymkey" "code.gitea.io/gitea/services/forms" + "code.gitea.io/gitea/services/gitdiff" issue_service "code.gitea.io/gitea/services/issue" pull_service "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" @@ -1255,3 +1257,128 @@ func GetPullRequestCommits(ctx *context.APIContext) { ctx.JSON(http.StatusOK, &apiCommits) } + +// GetPullRequestFiles gets all changed files associated with a given PR +func GetPullRequestFiles(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/files repository repoGetPullRequestFiles + // --- + // summary: Get changed files for a pull request + // 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: index + // in: path + // description: index of the pull request to get + // type: integer + // format: int64 + // required: true + // - name: page + // in: query + // description: page number of results to return (1-based) + // type: integer + // - name: limit + // in: query + // description: page size of results + // type: integer + // responses: + // "200": + // "$ref": "#/responses/ChangedFileList" + // "404": + // "$ref": "#/responses/notFound" + + pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if models.IsErrPullRequestNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err) + } + return + } + + if err := pr.LoadBaseRepo(); err != nil { + ctx.InternalServerError(err) + return + } + + var prInfo *git.CompareInfo + baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) + if err != nil { + ctx.ServerError("OpenRepository", err) + return + } + defer baseGitRepo.Close() + if pr.HasMerged { + prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) + } else { + prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), true, false) + } + if err != nil { + ctx.ServerError("GetCompareInfo", err) + return + } + + headCommitID, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName()) + if err != nil { + ctx.ServerError("GetRefCommitID", err) + return + } + + startCommitID := prInfo.MergeBase + endCommitID := headCommitID + + maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles + + diff, err := gitdiff.GetDiff(baseGitRepo, + &gitdiff.DiffOptions{ + BeforeCommitID: startCommitID, + AfterCommitID: endCommitID, + SkipTo: ctx.FormString("skip-to"), + MaxLines: maxLines, + MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, + MaxFiles: maxFiles, + WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)), + }, ctx.FormStrings("files")...) + if err != nil { + ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err) + return + } + + listOptions := utils.GetListOptions(ctx) + + totalNumberOfFiles := diff.NumFiles + totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize))) + + start, end := listOptions.GetStartEnd() + + if end > totalNumberOfFiles { + end = totalNumberOfFiles + } + + apiFiles := make([]*api.ChangedFile, 0, end-start) + for i := start; i < end; i++ { + apiFile := convert.ToChangedFile(diff.Files[i]) + apiFiles = append(apiFiles, apiFile) + } + + ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) + ctx.SetTotalCountHeader(int64(totalNumberOfFiles)) + + ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page)) + ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) + ctx.RespHeader().Set("X-PageCount", strconv.Itoa(totalNumberOfPages)) + ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) + ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") + + ctx.JSON(http.StatusOK, &apiFiles) +} diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 40aeca677de83..b42cc6b0a7826 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -254,6 +254,28 @@ type swaggerCommitList struct { Body []api.Commit `json:"body"` } +// ChangedFileList +// swagger:response ChangedFileList +type swaggerChangedFileList 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 + Files []api.ChangedFile `json:"files"` +} + // Note // swagger:response Note type swaggerNote struct { From 0c807d12fe6eab73db754ea4840559ef89d6d4b4 Mon Sep 17 00:00:00 2001 From: Anton Bracke Date: Mon, 10 Jan 2022 17:55:24 +0100 Subject: [PATCH 02/20] update swagger definitions --- templates/swagger/v1_json.tmpl | 102 +++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 7588261256390..487766f2a9347 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -7567,6 +7567,62 @@ } } }, + "/repos/{owner}/{repo}/pulls/{index}/files": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get changed files for a pull request", + "operationId": "repoGetPullRequestFiles", + "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": "integer", + "format": "int64", + "description": "index of the pull request to get", + "name": "index", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "page number of results to return (1-based)", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "page size of results", + "name": "limit", + "in": "query" + } + ], + "responses": { + "200": { + "$ref": "#/responses/ChangedFileList" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/pulls/{index}/merge": { "get": { "produces": [ @@ -12914,6 +12970,17 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "ChangedFile": { + "description": "ChangedFile store information about files affected by the pull request", + "type": "object", + "properties": { + "filename": { + "type": "string", + "x-go-name": "Filename" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CombinedStatus": { "description": "CombinedStatus holds the combined state of several statuses for a single commit", "type": "object", @@ -18181,6 +18248,41 @@ } } }, + "ChangedFileList": { + "description": "ChangedFileList", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/ChangedFile" + } + }, + "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" + } + } + }, "CombinedStatus": { "description": "CombinedStatus", "schema": { From 4f39f56941748c7fdd96184dfd34874a35672ec0 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Thu, 15 Sep 2022 20:42:49 +0200 Subject: [PATCH 03/20] Iimprovements to changed files API --- build/generate-go-licenses.go | 4 ++-- modules/convert/convert.go | 21 ++++++++++++++++++--- modules/structs/pull.go | 8 +++++++- routers/api/v1/api.go | 2 +- routers/api/v1/repo/pull.go | 28 ++++++++++++++++------------ tests/e2e/e2e_test.go | 3 ++- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/build/generate-go-licenses.go b/build/generate-go-licenses.go index fedfdc315e43a..87c773ed8b0cd 100644 --- a/build/generate-go-licenses.go +++ b/build/generate-go-licenses.go @@ -64,8 +64,8 @@ func main() { } entries = append(entries, LicenseEntry{ - Name: name, - Path: path, + Name: name, + Path: path, LicenseText: string(licenseText), }) } diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 4c33a674a604a..5d29300660bda 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -417,8 +417,23 @@ func ToLFSLock(l *git_model.LFSLock) *api.LFSLock { } // ToChangedFile convert a gitdiff.DiffFile to api.ChangedFile -func ToChangedFile(f *gitdiff.DiffFile) *api.ChangedFile { - return &api.ChangedFile{ - Filename: f.Name, +func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit string) *api.ChangedFile { + status := "changed" + if f.IsDeleted { + status = "deleted" } + file := &api.ChangedFile{ + NameHash: f.NameHash, + Filename: f.Name, + Status: status, + Additions: f.Addition, + Deletions: f.Deletion, + } + + if f.Name != "" { + file.HTMLURL = fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", f.GetDiffFileName()) + file.ContentsURL = fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", f.GetDiffFileName()) + } + + return file } diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 13e69e6bf0e65..495047ac6cc46 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -98,5 +98,11 @@ type EditPullRequestOption struct { // ChangedFile store information about files affected by the pull request type ChangedFile struct { - Filename string `json:"filename"` + NameHash string `json:"name_hash"` + Filename string `json:"filename"` + Status string `json:"status"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + HTMLURL string `json:"html_url,omitempty"` + ContentsURL string `json:"contents_url,omitempty"` } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0d11674aa9971..7f60e6737e954 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1002,7 +1002,7 @@ func Routes(ctx gocontext.Context) *web.Route { m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch) m.Post("/update", reqToken(), repo.UpdatePullRequest) m.Get("/commits", repo.GetPullRequestCommits) - m.Get("/files", repo.GetPullRequestFiles) + m.Get("/files", context.ReferencesGitRepo(false), repo.GetPullRequestFiles) m.Combo("/merge").Get(repo.IsPullRequestMerged). Post(reqToken(), mustNotBeArchived, bind(forms.MergePullRequestForm{}), repo.MergePullRequest). Delete(reqToken(), mustNotBeArchived, repo.CancelScheduledAutoMerge) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index c8d6ba2b9dc0c..9c8fce0e0ec55 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1350,6 +1350,15 @@ func GetPullRequestFiles(ctx *context.APIContext) { // type: integer // format: int64 // required: true + // - name: skip-to + // in: query + // description: skip to given file + // type: string + // - name: whitespace + // in: query + // description: whitespace behavior + // type: string + // enum: [ignore-all, ignore-change, ignore-eol, show-all] // - name: page // in: query // description: page number of results to return (1-based) @@ -1364,9 +1373,9 @@ func GetPullRequestFiles(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { - if models.IsErrPullRequestNotExist(err) { + if issues_model.IsErrPullRequestNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err) @@ -1379,13 +1388,9 @@ func GetPullRequestFiles(ctx *context.APIContext) { return } + baseGitRepo := ctx.Repo.GitRepo + var prInfo *git.CompareInfo - baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath()) - if err != nil { - ctx.ServerError("OpenRepository", err) - return - } - defer baseGitRepo.Close() if pr.HasMerged { prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) } else { @@ -1415,10 +1420,10 @@ func GetPullRequestFiles(ctx *context.APIContext) { MaxLines: maxLines, MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: maxFiles, - WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)), + WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.FormString("whitespace")), }, ctx.FormStrings("files")...) if err != nil { - ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err) + ctx.ServerError("GetDiff", err) return } @@ -1435,8 +1440,7 @@ func GetPullRequestFiles(ctx *context.APIContext) { apiFiles := make([]*api.ChangedFile, 0, end-start) for i := start; i < end; i++ { - apiFile := convert.ToChangedFile(diff.Files[i]) - apiFiles = append(apiFiles, apiFile) + apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, endCommitID)) } ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index c77c071181e38..1de630d0d43e5 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -73,7 +73,8 @@ func TestMain(m *testing.M) { } // This should be the only test e2e necessary. It will collect all "*.test.e2e.js" -// files in this directory and build a test for each. +// +// files in this directory and build a test for each. func TestE2e(t *testing.T) { // Find the paths of all e2e test files in test test directory. searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.js") From 69569775f44fe48aba28ced3d76cc3ff72c3b00e Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sun, 18 Sep 2022 08:32:11 +0200 Subject: [PATCH 04/20] Undo change --- tests/e2e/e2e_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 1de630d0d43e5..c77c071181e38 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -73,8 +73,7 @@ func TestMain(m *testing.M) { } // This should be the only test e2e necessary. It will collect all "*.test.e2e.js" -// -// files in this directory and build a test for each. +// files in this directory and build a test for each. func TestE2e(t *testing.T) { // Find the paths of all e2e test files in test test directory. searchGlob := filepath.Join(filepath.Dir(setting.AppPath), "tests", "e2e", "*.test.e2e.js") From 01b73fc03fded2e7f47a7de2261abe66c9b3d4f4 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sun, 18 Sep 2022 20:04:17 +0200 Subject: [PATCH 05/20] Update swagger struct --- routers/api/v1/swagger/repo.go | 2 +- templates/swagger/v1_json.tmpl | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index c57a2ac146be5..642b1b7b91023 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -273,7 +273,7 @@ type swaggerChangedFileList struct { HasMore bool `json:"X-HasMore"` // in: body - Files []api.ChangedFile `json:"files"` + Body []api.ChangedFile `json:"body"` } // Note diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 9af6c70631461..8b87f0c72cabf 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8052,6 +8052,24 @@ "in": "path", "required": true }, + { + "type": "string", + "description": "skip to given file", + "name": "skip-to", + "in": "query" + }, + { + "enum": [ + "ignore-all", + "ignore-change", + "ignore-eol", + "show-all" + ], + "type": "string", + "description": "whitespace behavior", + "name": "whitespace", + "in": "query" + }, { "type": "integer", "description": "page number of results to return (1-based)", @@ -13775,9 +13793,35 @@ "description": "ChangedFile store information about files affected by the pull request", "type": "object", "properties": { + "additions": { + "type": "integer", + "format": "int64", + "x-go-name": "Additions" + }, + "contents_url": { + "type": "string", + "x-go-name": "ContentsURL" + }, + "deletions": { + "type": "integer", + "format": "int64", + "x-go-name": "Deletions" + }, "filename": { "type": "string", "x-go-name": "Filename" + }, + "html_url": { + "type": "string", + "x-go-name": "HTMLURL" + }, + "name_hash": { + "type": "string", + "x-go-name": "NameHash" + }, + "status": { + "type": "string", + "x-go-name": "Status" } }, "x-go-package": "code.gitea.io/gitea/modules/structs" From a6ec85d7de96cf1f19a73f8a234b8ae40e25fb61 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 14:39:53 +0200 Subject: [PATCH 06/20] support status "added" --- modules/convert/convert.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 5d29300660bda..d1c4c85e35cd3 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -421,7 +421,10 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri status := "changed" if f.IsDeleted { status = "deleted" + } else if f.IsCreated { + status = "added" } + file := &api.ChangedFile{ NameHash: f.NameHash, Filename: f.Name, From 65792363f5347a657bc145ea7ea0f82855ab20f1 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 14:45:18 +0200 Subject: [PATCH 07/20] no duble call --- 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 7f60e6737e954..0d11674aa9971 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1002,7 +1002,7 @@ func Routes(ctx gocontext.Context) *web.Route { m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch) m.Post("/update", reqToken(), repo.UpdatePullRequest) m.Get("/commits", repo.GetPullRequestCommits) - m.Get("/files", context.ReferencesGitRepo(false), repo.GetPullRequestFiles) + m.Get("/files", repo.GetPullRequestFiles) m.Combo("/merge").Get(repo.IsPullRequestMerged). Post(reqToken(), mustNotBeArchived, bind(forms.MergePullRequestForm{}), repo.MergePullRequest). Delete(reqToken(), mustNotBeArchived, repo.CancelScheduledAutoMerge) From ea6644c8cab066f0591a643a100ed434585f0178 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 14:47:43 +0200 Subject: [PATCH 08/20] rm NameHash as it's just sha1(Filename) --- modules/convert/convert.go | 1 - modules/structs/pull.go | 1 - 2 files changed, 2 deletions(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index d1c4c85e35cd3..b1fff34181b19 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -426,7 +426,6 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri } file := &api.ChangedFile{ - NameHash: f.NameHash, Filename: f.Name, Status: status, Additions: f.Addition, diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 495047ac6cc46..73fa7cd1149d5 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -98,7 +98,6 @@ type EditPullRequestOption struct { // ChangedFile store information about files affected by the pull request type ChangedFile struct { - NameHash string `json:"name_hash"` Filename string `json:"filename"` Status string `json:"status"` Additions int `json:"additions"` From af1a79878588c10f9f050c917c0258b1c503dd00 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 19 Sep 2022 19:17:29 +0200 Subject: [PATCH 09/20] Remove `files` opt --- routers/api/v1/repo/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 9c8fce0e0ec55..c298230a5a0a2 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1421,7 +1421,7 @@ func GetPullRequestFiles(ctx *context.APIContext) { MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, MaxFiles: maxFiles, WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.FormString("whitespace")), - }, ctx.FormStrings("files")...) + }) if err != nil { ctx.ServerError("GetDiff", err) return From 8ce1e20948166e2420713424ff273de8dfd147d3 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 19 Sep 2022 19:44:24 +0200 Subject: [PATCH 10/20] Add simple test and add renamed support --- modules/convert/convert.go | 2 ++ tests/integration/api_pull_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index b1fff34181b19..b9fb662f44646 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -423,6 +423,8 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri status = "deleted" } else if f.IsCreated { status = "added" + } else if f.IsRenamed { + status = "renamed" } file := &api.ChangedFile{ diff --git a/tests/integration/api_pull_test.go b/tests/integration/api_pull_test.go index 032912a07360a..2408d5c4af721 100644 --- a/tests/integration/api_pull_test.go +++ b/tests/integration/api_pull_test.go @@ -183,3 +183,33 @@ func TestAPIEditPull(t *testing.T) { }) session.MakeRequest(t, req, http.StatusNotFound) } + +func TestAPIGetPullFiles(t *testing.T) { + defer tests.PrepareTestEnv(t)() + repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) + owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID}) + + session := loginUser(t, owner10.Name) + token := getTokenForLoggedInUser(t, session) + createFileOptions := getCreateFileOptions() + createFileOptions.NewBranchName = "get-pull-files-test-branch" + req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", owner10.Name, repo10.Name, "new/file1.txt", token), &createFileOptions) + session.MakeRequest(t, req, http.StatusCreated) + req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner10.Name, repo10.Name, token), &api.CreatePullRequestOption{ + Head: "get-pull-files-test-branch", + Base: "master", + Title: "create a success pr", + }) + pull := new(api.PullRequest) + resp := session.MakeRequest(t, req, http.StatusCreated) + DecodeJSON(t, resp, pull) + assert.EqualValues(t, "master", pull.Base.Name) + + req = NewRequest(t, http.MethodGet, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/files?token=%s", owner10.Name, repo10.Name, pull.Index, token)) + resp = session.MakeRequest(t, req, http.StatusOK) + files := make([]*api.ChangedFile, 0) + DecodeJSON(t, resp, &files) + assert.Len(t, files, 0) + assert.EqualValues(t, "new/file1.txt", files[0].Filename) + assert.EqualValues(t, "added", files[0].Status) +} From 6fbb0edae45e50a33fe06adcf0caa57bc5511fc2 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 19 Sep 2022 19:47:34 +0200 Subject: [PATCH 11/20] Escape in converter --- modules/convert/convert.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index b9fb662f44646..5a023aad7f016 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -435,8 +435,8 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri } if f.Name != "" { - file.HTMLURL = fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", f.GetDiffFileName()) - file.ContentsURL = fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", f.GetDiffFileName()) + file.HTMLURL = fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.Name)) + file.ContentsURL = fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.Name)) } return file From 26af5f7e1a99adbeeff943b3815df38f5c84cc1d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 19 Sep 2022 22:13:59 +0200 Subject: [PATCH 12/20] fix lint --- templates/swagger/v1_json.tmpl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 8b87f0c72cabf..d5e29573f7259 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13815,10 +13815,6 @@ "type": "string", "x-go-name": "HTMLURL" }, - "name_hash": { - "type": "string", - "x-go-name": "NameHash" - }, "status": { "type": "string", "x-go-name": "Status" From 5d1691d8dfddcce2fd0a80b3183e8b428ca81bd9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 20 Sep 2022 10:01:35 +0800 Subject: [PATCH 13/20] Update convert.go --- modules/convert/convert.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 5a023aad7f016..e4f2c3f5f9213 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -423,7 +423,9 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri status = "deleted" } else if f.IsCreated { status = "added" - } else if f.IsRenamed { + } else if f.IsRenamed && f.Type == gitdiff.DiffFileCopy { + status = "copied" + } else if f.IsRenamed && f.Type == gitdiff.DiffFileRename { status = "renamed" } From 777f1144f855b582a57e2c1007c7101628a6899b Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sat, 24 Sep 2022 18:26:08 +0200 Subject: [PATCH 14/20] Always include urls; add previous_filename --- modules/convert/convert.go | 15 ++++++++------- modules/structs/pull.go | 13 +++++++------ templates/swagger/v1_json.tmpl | 4 ++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index e4f2c3f5f9213..8f74085125521 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -430,15 +430,16 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri } file := &api.ChangedFile{ - Filename: f.Name, - Status: status, - Additions: f.Addition, - Deletions: f.Deletion, + Filename: f.GetDiffFileName(), + Status: status, + Additions: f.Addition, + Deletions: f.Deletion, + HTMLURL: fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), + ContentsURL: fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), } - if f.Name != "" { - file.HTMLURL = fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.Name)) - file.ContentsURL = fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.Name)) + if status == "rename" { + file.PreviousFilename = f.OldName } return file diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 73fa7cd1149d5..c66d0dddce740 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -98,10 +98,11 @@ type EditPullRequestOption struct { // ChangedFile store information about files affected by the pull request type ChangedFile struct { - Filename string `json:"filename"` - Status string `json:"status"` - Additions int `json:"additions"` - Deletions int `json:"deletions"` - HTMLURL string `json:"html_url,omitempty"` - ContentsURL string `json:"contents_url,omitempty"` + Filename string `json:"filename"` + PreviousFilename string `json:"previous_filename,omitempty"` + Status string `json:"status"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + HTMLURL string `json:"html_url,omitempty"` + ContentsURL string `json:"contents_url,omitempty"` } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index d5e29573f7259..970cc1ea1d432 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13815,6 +13815,10 @@ "type": "string", "x-go-name": "HTMLURL" }, + "previous_filename": { + "type": "string", + "x-go-name": "PreviousFilename" + }, "status": { "type": "string", "x-go-name": "Status" From 5c268aed6aeec6c57e1cde66a3c614429901962c Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sat, 24 Sep 2022 18:47:43 +0200 Subject: [PATCH 15/20] Use head instead of base repo --- routers/api/v1/repo/pull.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index c298230a5a0a2..2cf30e7c47b82 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1388,6 +1388,11 @@ func GetPullRequestFiles(ctx *context.APIContext) { return } + if err := pr.LoadHeadRepo(); err != nil { + ctx.InternalServerError(err) + return + } + baseGitRepo := ctx.Repo.GitRepo var prInfo *git.CompareInfo @@ -1440,7 +1445,7 @@ func GetPullRequestFiles(ctx *context.APIContext) { apiFiles := make([]*api.ChangedFile, 0, end-start) for i := start; i < end; i++ { - apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, endCommitID)) + apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], pr.HeadRepo, endCommitID)) } ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) From b884d2edc055345ba6ce1e73b38d70e00f145d26 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 28 Sep 2022 19:42:12 +0200 Subject: [PATCH 16/20] Add `Changes` and `unchaged` status --- modules/convert/convert.go | 3 +++ modules/structs/pull.go | 1 + templates/swagger/v1_json.tmpl | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 8f74085125521..0fd6cd48ce213 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -427,6 +427,8 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri status = "copied" } else if f.IsRenamed && f.Type == gitdiff.DiffFileRename { status = "renamed" + } else if f.Addition == 0 && f.Deletion == 0 { + status = "unchanged" } file := &api.ChangedFile{ @@ -434,6 +436,7 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri Status: status, Additions: f.Addition, Deletions: f.Deletion, + Changes: f.Addition + f.Deletion, HTMLURL: fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), ContentsURL: fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), } diff --git a/modules/structs/pull.go b/modules/structs/pull.go index c66d0dddce740..8e87b691dadc8 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -103,6 +103,7 @@ type ChangedFile struct { Status string `json:"status"` Additions int `json:"additions"` Deletions int `json:"deletions"` + Changes int `json:"changes"` HTMLURL string `json:"html_url,omitempty"` ContentsURL string `json:"contents_url,omitempty"` } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 711c72fe4efa1..28267ff967a2a 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13798,6 +13798,11 @@ "format": "int64", "x-go-name": "Additions" }, + "changes": { + "type": "integer", + "format": "int64", + "x-go-name": "Changes" + }, "contents_url": { "type": "string", "x-go-name": "ContentsURL" From d41fd552be5f4fed2d94eb5e729eae4612a53a73 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Wed, 28 Sep 2022 20:34:56 +0200 Subject: [PATCH 17/20] Update URLs behavior --- modules/convert/convert.go | 3 ++- modules/structs/pull.go | 1 + templates/swagger/v1_json.tmpl | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 0fd6cd48ce213..0e67563077131 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -438,7 +438,8 @@ func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit stri Deletions: f.Deletion, Changes: f.Addition + f.Deletion, HTMLURL: fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), - ContentsURL: fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), + ContentsURL: fmt.Sprint(repo.APIURL(), "/contents/", util.PathEscapeSegments(f.GetDiffFileName()), "?ref=", commit), + RawURL: fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), } if status == "rename" { diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 8e87b691dadc8..f627241b26307 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -106,4 +106,5 @@ type ChangedFile struct { Changes int `json:"changes"` HTMLURL string `json:"html_url,omitempty"` ContentsURL string `json:"contents_url,omitempty"` + RawURL string `json:"raw_url,omitempty"` } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 28267ff967a2a..52553e2e89919 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13824,6 +13824,10 @@ "type": "string", "x-go-name": "PreviousFilename" }, + "raw_url": { + "type": "string", + "x-go-name": "RawURL" + }, "status": { "type": "string", "x-go-name": "Status" From ac865e1bff4cdff67e82375305882312d8db6972 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 29 Sep 2022 03:31:14 +0200 Subject: [PATCH 18/20] fix fixtures to match gitea-repositories-meta/user2/repo1.git/refs --- models/fixtures/pull_request.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/fixtures/pull_request.yml b/models/fixtures/pull_request.yml index d45baa711c6e7..165437f0328dd 100644 --- a/models/fixtures/pull_request.yml +++ b/models/fixtures/pull_request.yml @@ -60,8 +60,8 @@ head_repo_id: 1 base_repo_id: 1 head_branch: pr-to-update - base_branch: branch1 - merge_base: 1234567890abcdef + base_branch: branch2 + merge_base: 985f0301dba5e7b34be866819cd15ad3d8f508ee has_merged: false - From 929a279b89749631a28a4148db4aa10b02858f15 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 29 Sep 2022 03:38:15 +0200 Subject: [PATCH 19/20] wow --- tests/integration/api_pull_test.go | 63 ++++++++++++++++-------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/tests/integration/api_pull_test.go b/tests/integration/api_pull_test.go index 2408d5c4af721..ad24d1e26d193 100644 --- a/tests/integration/api_pull_test.go +++ b/tests/integration/api_pull_test.go @@ -6,6 +6,7 @@ package integration import ( "fmt" + "io/ioutil" "net/http" "testing" @@ -27,15 +28,31 @@ func TestAPIViewPulls(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) - session := loginUser(t, "user2") - token := getTokenForLoggedInUser(t, session) - req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls?state=all&token="+token, owner.Name, repo.Name) - resp := session.MakeRequest(t, req, http.StatusOK) + ctx := NewAPITestContext(t, "user2", repo.Name) + + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls?state=all&token="+ctx.Token, owner.Name, repo.Name) + resp := ctx.Session.MakeRequest(t, req, http.StatusOK) var pulls []*api.PullRequest DecodeJSON(t, resp, &pulls) expectedLen := unittest.GetCount(t, &issues_model.Issue{RepoID: repo.ID}, unittest.Cond("is_pull = ?", true)) assert.Len(t, pulls, expectedLen) + + pull := pulls[0] + if assert.EqualValues(t, 5, pull.ID) { + resp = ctx.Session.MakeRequest(t, NewRequest(t, "GET", pull.DiffURL), http.StatusOK) + _, err := ioutil.ReadAll(resp.Body) + assert.NoError(t, err) + // TODO: use diff to generate stats to test against + + t.Run(fmt.Sprintf("APIGetPullFiles_%d", pull.ID), func(t *testing.T) { + doAPIGetPullFiles(ctx, pull, func(files []*api.ChangedFile) { + if assert.Len(t, files, 2) { + assert.EqualValues(t, "", files) + } + }) + }) + } } // TestAPIMergePullWIP ensures that we can't merge a WIP pull request @@ -184,32 +201,18 @@ func TestAPIEditPull(t *testing.T) { session.MakeRequest(t, req, http.StatusNotFound) } -func TestAPIGetPullFiles(t *testing.T) { - defer tests.PrepareTestEnv(t)() - repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) - owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID}) +func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, checkFiles func([]*api.ChangedFile)) func(*testing.T) { + return func(t *testing.T) { + url := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/files?token=%s", ctx.Username, ctx.Reponame, pr.Index, ctx.Token) - session := loginUser(t, owner10.Name) - token := getTokenForLoggedInUser(t, session) - createFileOptions := getCreateFileOptions() - createFileOptions.NewBranchName = "get-pull-files-test-branch" - req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", owner10.Name, repo10.Name, "new/file1.txt", token), &createFileOptions) - session.MakeRequest(t, req, http.StatusCreated) - req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s", owner10.Name, repo10.Name, token), &api.CreatePullRequestOption{ - Head: "get-pull-files-test-branch", - Base: "master", - Title: "create a success pr", - }) - pull := new(api.PullRequest) - resp := session.MakeRequest(t, req, http.StatusCreated) - DecodeJSON(t, resp, pull) - assert.EqualValues(t, "master", pull.Base.Name) + req := NewRequest(t, http.MethodGet, url) + if ctx.ExpectedCode == 0 { + ctx.ExpectedCode = http.StatusOK + } + resp := ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) - req = NewRequest(t, http.MethodGet, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/files?token=%s", owner10.Name, repo10.Name, pull.Index, token)) - resp = session.MakeRequest(t, req, http.StatusOK) - files := make([]*api.ChangedFile, 0) - DecodeJSON(t, resp, &files) - assert.Len(t, files, 0) - assert.EqualValues(t, "new/file1.txt", files[0].Filename) - assert.EqualValues(t, "added", files[0].Status) + files := make([]*api.ChangedFile, 0, 1) + DecodeJSON(t, resp, &files) + checkFiles(files) + } } From 96dcc0a6d75c81daab4997be3c59f682cc6ba90e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 29 Sep 2022 03:53:47 +0200 Subject: [PATCH 20/20] start with declarative tests --- tests/integration/api_pull_test.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/integration/api_pull_test.go b/tests/integration/api_pull_test.go index ad24d1e26d193..8ce92f3d4a622 100644 --- a/tests/integration/api_pull_test.go +++ b/tests/integration/api_pull_test.go @@ -45,13 +45,17 @@ func TestAPIViewPulls(t *testing.T) { assert.NoError(t, err) // TODO: use diff to generate stats to test against - t.Run(fmt.Sprintf("APIGetPullFiles_%d", pull.ID), func(t *testing.T) { - doAPIGetPullFiles(ctx, pull, func(files []*api.ChangedFile) { - if assert.Len(t, files, 2) { - assert.EqualValues(t, "", files) + t.Run(fmt.Sprintf("APIGetPullFiles_%d", pull.ID), + doAPIGetPullFiles(ctx, pull, func(t *testing.T, files []*api.ChangedFile) { + if assert.Len(t, files, 1) { + assert.EqualValues(t, "File-WoW", files[0].Filename) + assert.EqualValues(t, "", files[0].PreviousFilename) + assert.EqualValues(t, 1, files[0].Additions) + assert.EqualValues(t, 1, files[0].Changes) + assert.EqualValues(t, 0, files[0].Deletions) + assert.EqualValues(t, "added", files[0].Status) } - }) - }) + })) } } @@ -201,7 +205,7 @@ func TestAPIEditPull(t *testing.T) { session.MakeRequest(t, req, http.StatusNotFound) } -func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, checkFiles func([]*api.ChangedFile)) func(*testing.T) { +func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, callback func(*testing.T, []*api.ChangedFile)) func(*testing.T) { return func(t *testing.T) { url := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/files?token=%s", ctx.Username, ctx.Reponame, pr.Index, ctx.Token) @@ -213,6 +217,9 @@ func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, checkFiles func( files := make([]*api.ChangedFile, 0, 1) DecodeJSON(t, resp, &files) - checkFiles(files) + + if callback != nil { + callback(t, files) + } } }