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
This commit is contained in:
Sandro Santilli 2018-01-05 11:56:52 +01:00 committed by Lauris BH
parent ce7ae17b81
commit a192f3052e
4 changed files with 53 additions and 5 deletions

View file

@ -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)
}

View file

@ -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)