Skip to content

Commit a192f30

Browse files
strklafriks
authored andcommitted
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
1 parent ce7ae17 commit a192f30

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

integrations/pull_create_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ func testPullCreate(t *testing.T, session *TestSession, user, repo, branch strin
3939
})
4040
resp = session.MakeRequest(t, req, http.StatusFound)
4141

42-
//TODO check the redirected URL
43-
4442
return resp
4543
}
4644

@@ -49,5 +47,16 @@ func TestPullCreate(t *testing.T) {
4947
session := loginUser(t, "user1")
5048
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
5149
testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
52-
testPullCreate(t, session, "user1", "repo1", "master")
50+
resp := testPullCreate(t, session, "user1", "repo1", "master")
51+
52+
// check the redirected URL
53+
url := resp.HeaderMap.Get("Location")
54+
assert.Regexp(t, "^/user2/repo1/pulls/[0-9]*$", url)
55+
56+
// check .diff can be accessed and matches performed change
57+
req := NewRequest(t, "GET", url+".diff")
58+
resp = session.MakeRequest(t, req, http.StatusOK)
59+
assert.Regexp(t, "\\+Hello, World \\(Edited\\)", resp.Body)
60+
assert.Regexp(t, "^diff", resp.Body)
61+
assert.NotRegexp(t, "diff.*diff", resp.Body) // not two diffs, just one
5362
}

models/repo.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ func (repo *Repository) GetMirror() (err error) {
570570
return err
571571
}
572572

573-
// GetBaseRepo returns the base repository
573+
// GetBaseRepo populates repo.BaseRepo for a fork repository and
574+
// returns an error on failure (NOTE: no error is returned for
575+
// non-fork repositories, and BaseRepo will be left untouched)
574576
func (repo *Repository) GetBaseRepo() (err error) {
575577
if !repo.IsFork {
576578
return nil

routers/repo/pull.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Copyright 2014 The Gogs Authors. All rights reserved.
1+
// Copyright 2018 The Gitea Authors.
2+
// Copyright 2014 The Gogs Authors.
3+
// All rights reserved.
24
// Use of this source code is governed by a MIT-style
35
// license that can be found in the LICENSE file.
46

@@ -991,3 +993,37 @@ func CleanUpPullRequest(ctx *context.Context) {
991993

992994
ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
993995
}
996+
997+
// DownloadPullDiff render a pull's raw diff
998+
func DownloadPullDiff(ctx *context.Context) {
999+
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
1000+
if err != nil {
1001+
if models.IsErrIssueNotExist(err) {
1002+
ctx.Handle(404, "GetIssueByIndex", err)
1003+
} else {
1004+
ctx.Handle(500, "GetIssueByIndex", err)
1005+
}
1006+
return
1007+
}
1008+
1009+
// Redirect elsewhere if it's not a pull request
1010+
if !issue.IsPull {
1011+
ctx.Handle(404, "DownloadPullDiff",
1012+
fmt.Errorf("Issue is not a pull request"))
1013+
return
1014+
}
1015+
1016+
pr := issue.PullRequest
1017+
1018+
if err = pr.GetBaseRepo(); err != nil {
1019+
ctx.Handle(500, "GetBaseRepo", err)
1020+
return
1021+
}
1022+
patch, err := pr.BaseRepo.PatchPath(pr.Index)
1023+
if err != nil {
1024+
ctx.Handle(500, "PatchPath", err)
1025+
return
1026+
}
1027+
1028+
ctx.ServeFileContent(patch)
1029+
}

routers/routes/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ func RegisterRoutes(m *macaron.Macaron) {
624624
}, repo.MustBeNotBare, context.RepoRef(), context.CheckUnit(models.UnitTypeCode))
625625

626626
m.Group("/pulls/:index", func() {
627+
m.Get(".diff", repo.DownloadPullDiff)
627628
m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
628629
m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles)
629630
m.Post("/merge", reqRepoWriter, repo.MergePullRequest)

0 commit comments

Comments
 (0)