From 0e9bc2d4108ae32611e4fe65af493913c20d0279 Mon Sep 17 00:00:00 2001
From: Unknwon
Date: Sun, 6 Mar 2016 23:57:46 -0500
Subject: [PATCH] Fix pull request availability check
---
README.md | 2 +-
gogs.go | 2 +-
modules/middleware/context.go | 13 ++++++-
modules/middleware/repo.go | 59 +++++++++++++++++--------------
routers/repo/issue.go | 16 +++++++--
routers/repo/pull.go | 2 +-
templates/.VERSION | 2 +-
templates/repo/home.tmpl | 4 +--
templates/repo/issue/list.tmpl | 2 +-
templates/repo/issue/view.tmpl | 2 +-
templates/repo/pulls/commits.tmpl | 2 +-
templates/repo/pulls/compare.tmpl | 4 +--
templates/repo/pulls/files.tmpl | 2 +-
13 files changed, 69 insertions(+), 43 deletions(-)
diff --git a/README.md b/README.md
index 290c9893a..9597da4ce 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
-##### Current version: 0.9.0
+##### Current version: 0.9.1
| Web | UI | Preview |
|:-------------:|:-------:|:-------:|
diff --git a/gogs.go b/gogs.go
index ff0dac92f..1a18d4a55 100644
--- a/gogs.go
+++ b/gogs.go
@@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)
-const APP_VER = "0.9.0.0306"
+const APP_VER = "0.9.1.0306"
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index cee5d1003..d2b7de4a2 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -27,6 +27,13 @@ import (
"github.com/gogits/gogs/modules/setting"
)
+type PullRequestContext struct {
+ BaseRepo *models.Repository
+ Allowed bool
+ SameRepo bool
+ HeadInfo string // [:]
+}
+
type RepoContext struct {
AccessMode models.AccessMode
IsWatching bool
@@ -46,6 +53,8 @@ type RepoContext struct {
CloneLink models.CloneLink
CommitsCount int64
Mirror *models.Mirror
+
+ PullRequest *PullRequestContext
}
// Context represents context of a request.
@@ -211,7 +220,9 @@ func Contexter() macaron.Handler {
csrf: x,
Flash: f,
Session: sess,
- Repo: &RepoContext{},
+ Repo: &RepoContext{
+ PullRequest: &PullRequestContext{},
+ },
}
// Compute current URL for real-time change language.
ctx.Data["Link"] = setting.AppSubUrl + strings.TrimSuffix(ctx.Req.URL.Path, "/")
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
index 3e1835f5c..f96b87d43 100644
--- a/modules/middleware/repo.go
+++ b/modules/middleware/repo.go
@@ -142,32 +142,6 @@ func RepoAssignment(args ...bool) macaron.Handler {
ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
ctx.Data["IsRepositoryWriter"] = ctx.Repo.IsWriter()
- if repo.IsFork {
- RetrieveBaseRepo(ctx, repo)
- if ctx.Written() {
- return
- }
- }
-
- // People who have push access and propose a new pull request.
- if ctx.Repo.IsWriter() {
- // Pull request is allowed if this is a fork repository
- // and base repository accepts pull requests.
- if repo.BaseRepo != nil {
- if repo.BaseRepo.AllowsPulls() {
- ctx.Data["CanPullRequest"] = true
- ctx.Data["BaseRepo"] = repo.BaseRepo
- }
- } else {
- // Or, this is repository accepts pull requests between branches.
- if repo.AllowsPulls() {
- ctx.Data["CanPullRequest"] = true
- ctx.Data["BaseRepo"] = repo
- ctx.Data["IsBetweenBranches"] = true
- }
- }
- }
-
ctx.Data["DisableSSH"] = setting.SSH.Disabled
ctx.Data["CloneLink"] = repo.CloneLink()
ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
@@ -209,10 +183,41 @@ func RepoAssignment(args ...bool) macaron.Handler {
ctx.Repo.BranchName = brs[0]
}
}
-
ctx.Data["BranchName"] = ctx.Repo.BranchName
ctx.Data["CommitID"] = ctx.Repo.CommitID
+ if repo.IsFork {
+ RetrieveBaseRepo(ctx, repo)
+ if ctx.Written() {
+ return
+ }
+ }
+
+ // People who have push access and propose a new pull request.
+ if ctx.Repo.IsWriter() {
+ // Pull request is allowed if this is a fork repository
+ // and base repository accepts pull requests.
+ if repo.BaseRepo != nil {
+ if repo.BaseRepo.AllowsPulls() {
+ ctx.Data["BaseRepo"] = repo.BaseRepo
+ ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
+ ctx.Repo.PullRequest.Allowed = true
+ ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
+ }
+ } else {
+ // Or, this is repository accepts pull requests between branches.
+ if repo.AllowsPulls() {
+ ctx.Data["BaseRepo"] = repo
+ ctx.Repo.PullRequest.BaseRepo = repo
+ ctx.Repo.PullRequest.Allowed = true
+ ctx.Repo.PullRequest.SameRepo = true
+ ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
+ }
+ }
+ }
+ fmt.Println(222222, ctx.Repo.PullRequest)
+ ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
+
if ctx.Query("go-get") == "1" {
ctx.Data["GoGetImport"] = path.Join(setting.Domain, setting.AppSubUrl, owner.Name, repo.Name)
prefix := setting.AppUrl + path.Join(owner.Name, repo.Name, "src", ctx.Repo.BranchName)
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 02ccd2f9d..3a1049e65 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -55,15 +55,21 @@ var (
func MustEnableIssues(ctx *middleware.Context) {
if !ctx.Repo.Repository.EnableIssues {
ctx.Handle(404, "MustEnableIssues", nil)
+ return
}
}
func MustAllowPulls(ctx *middleware.Context) {
if !ctx.Repo.Repository.AllowsPulls() {
ctx.Handle(404, "MustAllowPulls", nil)
+ return
}
- ctx.Data["HasForkedRepo"] = ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)
+ // User can send pull request if owns a forked repository.
+ if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) {
+ ctx.Repo.PullRequest.Allowed = true
+ ctx.Repo.PullRequest.HeadInfo = ctx.User.Name + ":" + ctx.Repo.BranchName
+ }
}
func RetrieveLabels(ctx *middleware.Context) {
@@ -560,14 +566,18 @@ func ViewIssue(ctx *middleware.Context) {
}
if issue.IsPull {
+ MustAllowPulls(ctx)
+ if ctx.Written() {
+ return
+ }
+ ctx.Data["PageIsPullList"] = true
+
if err = issue.GetPullRequest(); err != nil {
ctx.Handle(500, "GetPullRequest", err)
return
}
- ctx.Data["PageIsPullList"] = true
ctx.Data["PageIsPullConversation"] = true
- ctx.Data["HasForkedRepo"] = ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)
} else {
MustEnableIssues(ctx)
if ctx.Written() {
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index c9d92297f..fd4f6f69c 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -462,7 +462,7 @@ func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository
}
ctx.Data["HeadUser"] = headUser
ctx.Data["HeadBranch"] = headBranch
- ctx.Data["IsBetweenBranches"] = isSameRepo
+ ctx.Repo.PullRequest.SameRepo = isSameRepo
// Check if base branch is valid.
if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
diff --git a/templates/.VERSION b/templates/.VERSION
index 21142bce7..873dfc2a7 100644
--- a/templates/.VERSION
+++ b/templates/.VERSION
@@ -1 +1 @@
-0.9.0.0306
\ No newline at end of file
+0.9.1.0306
\ No newline at end of file
diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl
index 36752a6ea..fe5a62163 100644
--- a/templates/repo/home.tmpl
+++ b/templates/repo/home.tmpl
@@ -7,9 +7,9 @@
{{.Repository.Website}}
diff --git a/templates/repo/issue/view.tmpl b/templates/repo/issue/view.tmpl
index 4e2e69bbe..c289dbcb7 100644
--- a/templates/repo/issue/view.tmpl
+++ b/templates/repo/issue/view.tmpl
@@ -8,7 +8,7 @@
{{if .PageIsIssueList}}
{{.i18n.Tr "repo.issues.new"}}
{{else}}
- {{.i18n.Tr "repo.pulls.new"}}
+ {{.i18n.Tr "repo.pulls.new"}}
{{end}}
diff --git a/templates/repo/pulls/commits.tmpl b/templates/repo/pulls/commits.tmpl
index 469499ae8..99bac62cf 100644
--- a/templates/repo/pulls/commits.tmpl
+++ b/templates/repo/pulls/commits.tmpl
@@ -5,7 +5,7 @@
{{template "repo/issue/navbar" .}}
diff --git a/templates/repo/pulls/compare.tmpl b/templates/repo/pulls/compare.tmpl
index 30a2fd5b6..0d7b6f9b5 100644
--- a/templates/repo/pulls/compare.tmpl
+++ b/templates/repo/pulls/compare.tmpl
@@ -21,7 +21,7 @@
@@ -39,7 +39,7 @@
diff --git a/templates/repo/pulls/files.tmpl b/templates/repo/pulls/files.tmpl
index 14ef29037..19a75e076 100644
--- a/templates/repo/pulls/files.tmpl
+++ b/templates/repo/pulls/files.tmpl
@@ -5,7 +5,7 @@
{{template "repo/issue/navbar" .}}