gitea/routers/repo/pull.go

115 lines
2.9 KiB
Go
Raw Normal View History

2014-03-24 03:25:15 -07:00
// 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.
package repo
import (
2015-08-08 02:10:34 -07:00
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
2014-06-22 20:11:12 -07:00
"github.com/gogits/gogs/modules/base"
2015-08-08 02:10:34 -07:00
"github.com/gogits/gogs/modules/log"
2014-03-24 03:25:15 -07:00
"github.com/gogits/gogs/modules/middleware"
2015-08-08 02:10:34 -07:00
"github.com/gogits/gogs/modules/setting"
2014-03-24 03:25:15 -07:00
)
2014-06-22 20:11:12 -07:00
const (
2015-08-08 02:10:34 -07:00
FORK base.TplName = "repo/pulls/fork"
2014-06-22 20:11:12 -07:00
PULLS base.TplName = "repo/pulls"
)
2015-08-08 02:10:34 -07:00
func getForkRepository(ctx *middleware.Context) *models.Repository {
forkRepo, err := models.GetRepositoryById(ctx.ParamsInt64(":repoid"))
if err != nil {
if models.IsErrRepoNotExist(err) {
ctx.Handle(404, "GetRepositoryById", nil)
} else {
ctx.Handle(500, "GetRepositoryById", err)
}
return nil
}
ctx.Data["repo_name"] = forkRepo.Name
ctx.Data["desc"] = forkRepo.Description
ctx.Data["IsPrivate"] = forkRepo.IsPrivate
if err = forkRepo.GetOwner(); err != nil {
ctx.Handle(500, "GetOwner", err)
return nil
}
ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
if err := ctx.User.GetOrganizations(); err != nil {
ctx.Handle(500, "GetOrganizations", err)
return nil
}
ctx.Data["Orgs"] = ctx.User.Orgs
return forkRepo
}
func Fork(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("new_fork")
getForkRepository(ctx)
if ctx.Written() {
return
}
ctx.Data["ContextUser"] = ctx.User
ctx.HTML(200, FORK)
}
func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
ctx.Data["Title"] = ctx.Tr("new_fork")
forkRepo := getForkRepository(ctx)
if ctx.Written() {
return
}
ctxUser := checkContextUser(ctx, form.Uid)
if ctx.Written() {
return
}
ctx.Data["ContextUser"] = ctxUser
if ctx.HasError() {
ctx.HTML(200, FORK)
return
}
// Check ownership of organization.
if ctxUser.IsOrganization() {
if !ctxUser.IsOwnedBy(ctx.User.Id) {
ctx.Error(403)
return
}
}
repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
if err != nil {
switch {
case models.IsErrRepoAlreadyExist(err):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
case models.IsErrNameReserved(err):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
case models.IsErrNamePatternNotAllowed(err):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
default:
ctx.Handle(500, "ForkPost", err)
}
return
}
log.Trace("Repository forked[%d]: %s/%s", forkRepo.Id, ctxUser.Name, repo.Name)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
}
2014-07-25 21:24:27 -07:00
func Pulls(ctx *middleware.Context) {
2014-03-24 03:25:15 -07:00
ctx.Data["IsRepoToolbarPulls"] = true
2014-06-22 20:11:12 -07:00
ctx.HTML(200, PULLS)
2014-03-24 03:25:15 -07:00
}