You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
10 KiB

// 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 v1
import (
"path"
"github.com/Unknwon/com"
api "github.com/kiliit/go-gogs-client"
"github.com/gogits/gogs/models"
11 years ago
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting"
9 years ago
"github.com/gogits/gogs/modules/mailer"
)
// ToApiRepository converts repository to API format.
func ToApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
cl, err := repo.CloneLink()
if err != nil {
log.Error(4, "CloneLink: %v", err)
}
return &api.Repository{
Id: repo.ID,
Owner: *ToApiUser(owner),
FullName: owner.Name + "/" + repo.Name,
Private: repo.IsPrivate,
Fork: repo.IsFork,
HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
CloneUrl: cl.HTTPS,
SshUrl: cl.SSH,
Permissions: permission,
}
}
func SearchRepos(ctx *middleware.Context) {
opt := models.SearchOption{
Keyword: path.Base(ctx.Query("q")),
Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
Limit: com.StrTo(ctx.Query("limit")).MustInt(),
}
if opt.Limit == 0 {
opt.Limit = 10
}
10 years ago
// Check visibility.
if ctx.IsSigned && opt.Uid > 0 {
if ctx.User.Id == opt.Uid {
opt.Private = true
} else {
u, err := models.GetUserByID(opt.Uid)
10 years ago
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
10 years ago
opt.Private = true
}
// FIXME: how about collaborators?
}
}
repos, err := models.SearchRepositoryByName(opt)
if err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results := make([]*api.Repository, len(repos))
for i := range repos {
if err = repos[i].GetOwner(); err != nil {
ctx.JSON(500, map[string]interface{}{
"ok": false,
"error": err.Error(),
})
return
}
results[i] = &api.Repository{
Id: repos[i].ID,
FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
}
}
ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})
}
11 years ago
// https://github.com/kiliit/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *middleware.Context) {
ownRepos, err := models.GetRepositories(ctx.User.Id, true)
if err != nil {
ctx.APIError(500, "GetRepositories", err)
return
}
numOwnRepos := len(ownRepos)
9 years ago
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
if err != nil {
9 years ago
ctx.APIError(500, "GetRepositoryAccesses", err)
return
}
repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
for i := range ownRepos {
repos[i] = ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
}
i := numOwnRepos
for repo, access := range accessibleRepos {
repos[i] = ToApiRepository(repo.Owner, repo, api.Permission{
Admin: access >= models.ACCESS_MODE_ADMIN,
Push: access >= models.ACCESS_MODE_WRITE,
Pull: true,
})
i++
}
ctx.JSON(200, &repos)
}
func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) {
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
Name: opt.Name,
Description: opt.Description,
Gitignores: opt.Gitignores,
License: opt.License,
Readme: opt.Readme,
IsPrivate: opt.Private,
AutoInit: opt.AutoInit,
})
if err != nil {
if models.IsErrRepoAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.APIError(422, "", err)
} else {
if repo != nil {
if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil {
log.Error(4, "DeleteRepository: %v", err)
}
}
ctx.APIError(500, "CreateRepository", err)
}
return
}
ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true}))
}
// https://github.com/kiliit/go-gogs-client/wiki/Repositories#create
func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
// Shouldn't reach this condition, but just in case.
if ctx.User.IsOrganization() {
ctx.APIError(422, "", "not allowed creating repository for organization")
return
}
createRepo(ctx, ctx.User, opt)
}
func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
org, err := models.GetOrgByName(ctx.Params(":org"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
} else {
ctx.APIError(500, "GetOrgByName", err)
}
return
}
if !org.IsOwnedBy(ctx.User.Id) {
ctx.APIError(403, "", "Given user is not owner of organization.")
return
}
createRepo(ctx, org, opt)
}
func ForkRepo(ctx *middleware.Context, opt api.ForkRepoOption) {
if opt.TargetUser != "" {
if ctx.User.Name == opt.TargetUser {
forkRepoTo(ctx, ctx.User, opt.Name, opt.Description)
} else {
if ctx.User.IsAdmin {
targetUser, err :=models.GetUserByName(opt.TargetUser)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
} else {
ctx.APIError(500, "GetUserByName", err)
}
return
}
forkRepoTo(ctx, targetUser, opt.Name, opt.Description)
} else {
ctx.APIError(403, "", "You do not have access to " + opt.TargetUser + ".")
return
}
}
} else {
forkRepoTo(ctx, ctx.User, opt.Name, opt.Description)
}
}
func forkRepoTo(ctx *middleware.Context, targetUser *models.User, name string, description string) {
forkedRepo, err := models.ForkRepository(targetUser, ctx.Repo.Repository, name, description)
if err != nil {
if models.IsErrRepoAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.APIError(422, "ForkRepository", err)
} else {
log.Error(4, "ForkRepository: %v", err)
if forkedRepo != nil {
if err = models.DeleteRepository(ctx.User.Id, forkedRepo.ID); err != nil {
log.Error(4, "DeleteRepository: %v", err)
}
}
ctx.Error(500)
}
return
}
ctx.JSON(201, ToApiRepository(ctx.User, forkedRepo, api.Permission{true, true, true}))
}
9 years ago
func AddCollaborator(ctx *middleware.Context, opt api.CollaboratorOption) {
u, err := models.GetUserByName(opt.UserName)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
} else {
ctx.Handle(500, "GetUserByName", err)
}
return
}
// Check if user is organization member.
if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
ctx.APIError(422, "", "User is organization member")
return
}
if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
ctx.Handle(500, "AddCollaborator", err)
return
}
if setting.Service.EnableNotifyMail {
if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
ctx.Handle(500, "SendCollaboratorMail", err)
return
}
}
ctx.Status(201)
}
func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
9 years ago
ctxUser := ctx.User
// Not equal means context user is an organization,
// or is another user/organization if current user is admin.
9 years ago
if form.Uid != ctxUser.Id {
org, err := models.GetUserByID(form.Uid)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
} else {
ctx.APIError(500, "GetUserByID", err)
}
11 years ago
return
}
ctxUser = org
}
if ctx.HasError() {
ctx.APIError(422, "", ctx.GetErrMsg())
11 years ago
return
}
if ctxUser.IsOrganization() && !ctx.User.IsAdmin {
11 years ago
// Check ownership of organization.
9 years ago
if !ctxUser.IsOwnedBy(ctx.User.Id) {
ctx.APIError(403, "", "Given user is not owner of organization.")
11 years ago
return
}
}
remoteAddr, err := form.ParseRemoteAddr(ctx.User)
if err != nil {
if models.IsErrInvalidCloneAddr(err) {
addrErr := err.(models.ErrInvalidCloneAddr)
switch {
case addrErr.IsURLError:
ctx.APIError(422, "", err)
case addrErr.IsPermissionDenied:
ctx.APIError(422, "", "You are not allowed to import local repositories.")
case addrErr.IsInvalidPath:
ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.")
default:
ctx.APIError(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
}
} else {
ctx.APIError(500, "ParseRemoteAddr", err)
}
11 years ago
return
}
repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{
Name: form.RepoName,
Description: form.Description,
IsPrivate: form.Private || setting.Repository.ForcePrivate,
IsMirror: form.Mirror,
RemoteAddr: remoteAddr,
})
if err != nil {
if repo != nil {
if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
log.Error(4, "DeleteRepository: %v", errDelete)
}
11 years ago
}
ctx.APIError(500, "MigrateRepository", err)
return
11 years ago
}
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
9 years ago
ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
11 years ago
}
9 years ago
func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
owner, err := models.GetUserByName(ctx.Params(":username"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
} else {
ctx.APIError(500, "GetUserByName", err)
}
9 years ago
return nil, nil
}
9 years ago
repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
if err != nil {
if models.IsErrRepoNotExist(err) {
ctx.Error(404)
} else {
ctx.APIError(500, "GetRepositoryByName", err)
}
9 years ago
return nil, nil
}
return owner, repo
}
func GetRepo(ctx *middleware.Context) {
owner, repo := parseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
ctx.JSON(200, ToApiRepository(owner, repo, api.Permission{true, true, true}))
}
func DeleteRepo(ctx *middleware.Context) {
owner, repo := parseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
9 years ago
if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.Id) {
ctx.APIError(403, "", "Given user is not owner of organization.")
return
}
9 years ago
if err := models.DeleteRepository(owner.Id, repo.ID); err != nil {
ctx.APIError(500, "DeleteRepository", err)
return
}
9 years ago
log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
ctx.Status(204)
}