Browse Source

repo: fix panic on pull request submit (#4572)

Unknwon 7 years ago
parent
commit
e02fac4968
5 changed files with 20 additions and 11 deletions
  1. 1 1
      gogs.go
  2. 6 3
      models/repo.go
  3. 1 1
      models/user.go
  4. 11 5
      routes/repo/pull.go
  5. 1 1
      templates/.VERSION

+ 1 - 1
gogs.go

@@ -16,7 +16,7 @@ import (
 	"github.com/gogits/gogs/pkg/setting"
 )
 
-const APP_VER = "0.11.25.0714"
+const APP_VER = "0.11.26.0714"
 
 func init() {
 	setting.AppVer = APP_VER

+ 6 - 3
models/repo.go

@@ -2257,10 +2257,13 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
 
 // HasForkedRepo checks if given user has already forked a repository.
 // When user has already forked, it returns true along with the repository.
-func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
+func HasForkedRepo(ownerID, repoID int64) (*Repository, bool, error) {
 	repo := new(Repository)
-	has, _ := x.Where("owner_id = ? AND fork_id = ?", ownerID, repoID).Get(repo)
-	return repo, has
+	has, err := x.Where("owner_id = ? AND fork_id = ?", ownerID, repoID).Get(repo)
+	if err != nil {
+		return nil, false, err
+	}
+	return repo, has, repo.LoadAttributes()
 }
 
 // ForkRepository creates a fork of target repository under another user domain.

+ 1 - 1
models/user.go

@@ -137,7 +137,7 @@ func (u *User) IsLocal() bool {
 
 // HasForkedRepo checks if user has already forked a repository with given ID.
 func (u *User) HasForkedRepo(repoID int64) bool {
-	_, has := HasForkedRepo(u.ID, repoID)
+	_, has, _ := HasForkedRepo(u.ID, repoID)
 	return has
 }
 

+ 11 - 5
routes/repo/pull.go

@@ -101,8 +101,11 @@ func ForkPost(c *context.Context, f form.CreateRepo) {
 		return
 	}
 
-	repo, has := models.HasForkedRepo(ctxUser.ID, baseRepo.ID)
-	if has {
+	repo, has, err := models.HasForkedRepo(ctxUser.ID, baseRepo.ID)
+	if err != nil {
+		c.ServerError("HasForkedRepo", err)
+		return
+	} else if has {
 		c.Redirect(repo.Link())
 		return
 	}
@@ -119,7 +122,7 @@ func ForkPost(c *context.Context, f form.CreateRepo) {
 		return
 	}
 
-	repo, err := models.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description)
+	repo, err = models.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description)
 	if err != nil {
 		c.Data["Err_RepoName"] = true
 		switch {
@@ -475,8 +478,11 @@ func ParseCompareInfo(c *context.Context) (*models.User, *models.Repository, *gi
 	// no need to check the fork relation.
 	if !isSameRepo {
 		var has bool
-		headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ID)
-		if !has {
+		headRepo, has, err = models.HasForkedRepo(headUser.ID, baseRepo.ID)
+		if err != nil {
+			c.ServerError("HasForkedRepo", err)
+			return nil, nil, nil, nil, "", ""
+		} else if !has {
 			log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have fork or in same repository", baseRepo.ID)
 			c.NotFound()
 			return nil, nil, nil, nil, "", ""

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.11.25.0714
+0.11.26.0714