From a192f3052ed9b59d1404fdcebf2b5c156d6d6969 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 5 Jan 2018 11:56:52 +0100 Subject: [PATCH] Serve pull request .diff files (#3293) * Serve pull request .diff files Closes #3259 * Add test for pull request redirection and .diff access * Typo * There's no need to test for pr.BaseRepo being nil after calling GetBaseRepo --- integrations/pull_create_test.go | 15 ++++++++++--- models/repo.go | 4 +++- routers/repo/pull.go | 38 +++++++++++++++++++++++++++++++- routers/routes/routes.go | 1 + 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/integrations/pull_create_test.go b/integrations/pull_create_test.go index c392ecc18..00a23a29e 100644 --- a/integrations/pull_create_test.go +++ b/integrations/pull_create_test.go @@ -39,8 +39,6 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin }) resp = session.MakeRequest(t, req, http.StatusFound) - //TODO check the redirected URL - return resp } @@ -49,5 +47,16 @@ func TestPullCreate(t *testing.T) { session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1") testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n") - testPullCreate(t, session, "user1", "repo1", "master") + resp := testPullCreate(t, session, "user1", "repo1", "master") + + // check the redirected URL + url := resp.HeaderMap.Get("Location") + assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url) + + // check .diff can be accessed and matches performed change + req := NewRequest(t, "GET", url+".diff") + resp = session.MakeRequest(t, req, http.StatusOK) + assert.Regexp(t, "\\+Hello, World \\(Edited\\)", resp.Body) + assert.Regexp(t, "^diff", resp.Body) + assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one } diff --git a/models/repo.go b/models/repo.go index f8057b751..29fd39ca9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -570,7 +570,9 @@ func (repo *Repository) GetMirror() (err error) { return err } -// GetBaseRepo returns the base repository +// GetBaseRepo populates repo.BaseRepo for a fork repository and +// returns an error on failure (NOTE: no error is returned for +// non-fork repositories, and BaseRepo will be left untouched) func (repo *Repository) GetBaseRepo() (err error) { if !repo.IsFork { return nil diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 5575009af..6162df965 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -1,4 +1,6 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2018 The Gitea Authors. +// Copyright 2014 The Gogs Authors. +// All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -991,3 +993,37 @@ func CleanUpPullRequest(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName)) } + +// DownloadPullDiff render a pull's raw diff +func DownloadPullDiff(ctx *context.Context) { + issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if models.IsErrIssueNotExist(err) { + ctx.Handle(404, "GetIssueByIndex", err) + } else { + ctx.Handle(500, "GetIssueByIndex", err) + } + return + } + + // Redirect elsewhere if it's not a pull request + if !issue.IsPull { + ctx.Handle(404, "DownloadPullDiff", + fmt.Errorf("Issue is not a pull request")) + return + } + + pr := issue.PullRequest + + if err = pr.GetBaseRepo(); err != nil { + ctx.Handle(500, "GetBaseRepo", err) + return + } + patch, err := pr.BaseRepo.PatchPath(pr.Index) + if err != nil { + ctx.Handle(500, "PatchPath", err) + return + } + + ctx.ServeFileContent(patch) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index cdbbd2280..4293cca6c 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -624,6 +624,7 @@ func RegisterRoutes(m *macaron.Macaron) { }, repo.MustBeNotBare, context.RepoRef(), context.CheckUnit(models.UnitTypeCode)) m.Group("/pulls/:index", func() { + m.Get(".diff", repo.DownloadPullDiff) m.Get("/commits", context.RepoRef(), repo.ViewPullCommits) m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles) m.Post("/merge", reqRepoWriter, repo.MergePullRequest)