mirror of https://github.com/gogits/gogs.git
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.
357 lines
9.8 KiB
357 lines
9.8 KiB
9 years ago
|
// Copyright 2015 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 (
|
||
|
"strings"
|
||
|
|
||
|
"github.com/go-macaron/binding"
|
||
|
"gopkg.in/macaron.v1"
|
||
|
|
||
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
8 years ago
|
"github.com/gogits/gogs/routes/api/v1/admin"
|
||
|
"github.com/gogits/gogs/routes/api/v1/misc"
|
||
|
"github.com/gogits/gogs/routes/api/v1/org"
|
||
|
"github.com/gogits/gogs/routes/api/v1/repo"
|
||
|
"github.com/gogits/gogs/routes/api/v1/user"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
func repoAssignment() macaron.Handler {
|
||
8 years ago
|
return func(c *context.APIContext) {
|
||
|
userName := c.Params(":username")
|
||
|
repoName := c.Params(":reponame")
|
||
9 years ago
|
|
||
|
var (
|
||
|
owner *models.User
|
||
|
err error
|
||
|
)
|
||
|
|
||
|
// Check if the user is the same as the repository owner.
|
||
8 years ago
|
if c.IsLogged && c.User.LowerName == strings.ToLower(userName) {
|
||
|
owner = c.User
|
||
9 years ago
|
} else {
|
||
|
owner, err = models.GetUserByName(userName)
|
||
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetUserByName", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
}
|
||
8 years ago
|
c.Repo.Owner = owner
|
||
9 years ago
|
|
||
|
// Get repository.
|
||
9 years ago
|
repo, err := models.GetRepositoryByName(owner.ID, repoName)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsRepoNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetRepositoryByName", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
} else if err = repo.GetOwner(); err != nil {
|
||
8 years ago
|
c.Error(500, "GetOwner", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.IsLogged && c.User.IsAdmin {
|
||
|
c.Repo.AccessMode = models.ACCESS_MODE_OWNER
|
||
9 years ago
|
} else {
|
||
8 years ago
|
mode, err := models.AccessLevel(c.User.ID, repo)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "AccessLevel", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Repo.AccessMode = mode
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if !c.Repo.HasAccess() {
|
||
|
c.Status(404)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Repo.Repository = repo
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Contexter middleware already checks token for user sign in process.
|
||
9 years ago
|
func reqToken() macaron.Handler {
|
||
8 years ago
|
return func(c *context.Context) {
|
||
|
if !c.IsLogged {
|
||
|
c.Error(401)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func reqBasicAuth() macaron.Handler {
|
||
8 years ago
|
return func(c *context.Context) {
|
||
|
if !c.IsBasicAuth {
|
||
|
c.Error(401)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func reqAdmin() macaron.Handler {
|
||
8 years ago
|
return func(c *context.Context) {
|
||
|
if !c.IsLogged || !c.User.IsAdmin {
|
||
|
c.Error(403)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func reqRepoWriter() macaron.Handler {
|
||
8 years ago
|
return func(c *context.Context) {
|
||
|
if !c.Repo.IsWriter() {
|
||
|
c.Error(403)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func orgAssignment(args ...bool) macaron.Handler {
|
||
9 years ago
|
var (
|
||
9 years ago
|
assignOrg bool
|
||
9 years ago
|
assignTeam bool
|
||
|
)
|
||
|
if len(args) > 0 {
|
||
9 years ago
|
assignOrg = args[0]
|
||
|
}
|
||
|
if len(args) > 1 {
|
||
|
assignTeam = args[1]
|
||
9 years ago
|
}
|
||
8 years ago
|
return func(c *context.APIContext) {
|
||
|
c.Org = new(context.APIOrganization)
|
||
9 years ago
|
|
||
|
var err error
|
||
|
if assignOrg {
|
||
8 years ago
|
c.Org.Organization, err = models.GetUserByName(c.Params(":orgname"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetUserByName", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if assignTeam {
|
||
8 years ago
|
c.Org.Team, err = models.GetTeamByID(c.ParamsInt64(":teamid"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetTeamById", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func mustEnableIssues(c *context.APIContext) {
|
||
|
if !c.Repo.Repository.EnableIssues || c.Repo.Repository.EnableExternalTracker {
|
||
|
c.Status(404)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// RegisterRoutes registers all v1 APIs routes to web application.
|
||
|
// FIXME: custom form error response
|
||
|
func RegisterRoutes(m *macaron.Macaron) {
|
||
|
bind := binding.Bind
|
||
|
|
||
|
m.Group("/v1", func() {
|
||
8 years ago
|
// Handle preflight OPTIONS request
|
||
|
m.Options("/*", func() {})
|
||
|
|
||
9 years ago
|
// Miscellaneous
|
||
|
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
|
||
|
m.Post("/markdown/raw", misc.MarkdownRaw)
|
||
|
|
||
|
// Users
|
||
|
m.Group("/users", func() {
|
||
|
m.Get("/search", user.Search)
|
||
|
|
||
|
m.Group("/:username", func() {
|
||
|
m.Get("", user.GetInfo)
|
||
|
|
||
|
m.Group("/tokens", func() {
|
||
|
m.Combo("").Get(user.ListAccessTokens).
|
||
|
Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
|
||
9 years ago
|
}, reqBasicAuth())
|
||
9 years ago
|
})
|
||
|
})
|
||
|
|
||
|
m.Group("/users", func() {
|
||
|
m.Group("/:username", func() {
|
||
9 years ago
|
m.Get("/keys", user.ListPublicKeys)
|
||
9 years ago
|
|
||
|
m.Get("/followers", user.ListFollowers)
|
||
|
m.Group("/following", func() {
|
||
|
m.Get("", user.ListFollowing)
|
||
|
m.Get("/:target", user.CheckFollowing)
|
||
|
})
|
||
9 years ago
|
})
|
||
9 years ago
|
}, reqToken())
|
||
9 years ago
|
|
||
|
m.Group("/user", func() {
|
||
8 years ago
|
m.Get("", user.GetAuthenticatedUser)
|
||
9 years ago
|
m.Combo("/emails").Get(user.ListEmails).
|
||
|
Post(bind(api.CreateEmailOption{}), user.AddEmail).
|
||
|
Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
|
||
|
|
||
|
m.Get("/followers", user.ListMyFollowers)
|
||
|
m.Group("/following", func() {
|
||
|
m.Get("", user.ListMyFollowing)
|
||
|
m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
|
||
|
})
|
||
|
|
||
9 years ago
|
m.Group("/keys", func() {
|
||
|
m.Combo("").Get(user.ListMyPublicKeys).
|
||
|
Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
|
||
|
m.Combo("/:id").Get(user.GetPublicKey).
|
||
|
Delete(user.DeletePublicKey)
|
||
|
})
|
||
8 years ago
|
|
||
8 years ago
|
m.Combo("/issues").Get(repo.ListUserIssues)
|
||
9 years ago
|
}, reqToken())
|
||
9 years ago
|
|
||
|
// Repositories
|
||
8 years ago
|
m.Get("/users/:username/repos", reqToken(), repo.ListUserRepositories)
|
||
|
m.Get("/orgs/:org/repos", reqToken(), repo.ListOrgRepositories)
|
||
9 years ago
|
m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
|
||
9 years ago
|
Post(bind(api.CreateRepoOption{}), repo.Create)
|
||
9 years ago
|
m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
|
||
9 years ago
|
|
||
|
m.Group("/repos", func() {
|
||
|
m.Get("/search", repo.Search)
|
||
|
})
|
||
|
|
||
|
m.Group("/repos", func() {
|
||
8 years ago
|
m.Post("/migrate", bind(form.MigrateRepo{}), repo.Migrate)
|
||
8 years ago
|
m.Combo("/:username/:reponame", repoAssignment()).Get(repo.Get).
|
||
9 years ago
|
Delete(repo.Delete)
|
||
|
|
||
|
m.Group("/:username/:reponame", func() {
|
||
9 years ago
|
m.Group("/hooks", func() {
|
||
|
m.Combo("").Get(repo.ListHooks).
|
||
|
Post(bind(api.CreateHookOption{}), repo.CreateHook)
|
||
9 years ago
|
m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
|
||
9 years ago
|
Delete(repo.DeleteHook)
|
||
|
})
|
||
8 years ago
|
m.Group("/collaborators", func() {
|
||
|
m.Get("", repo.ListCollaborators)
|
||
|
m.Combo("/:collaborator").Get(repo.IsCollaborator).Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
|
||
|
Delete(repo.DeleteCollaborator)
|
||
|
})
|
||
9 years ago
|
m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
|
||
9 years ago
|
m.Get("/archive/*", repo.GetArchive)
|
||
8 years ago
|
m.Get("/forks", repo.ListForks)
|
||
9 years ago
|
m.Group("/branches", func() {
|
||
9 years ago
|
m.Get("", repo.ListBranches)
|
||
8 years ago
|
m.Get("/*", repo.GetBranch)
|
||
9 years ago
|
})
|
||
9 years ago
|
m.Group("/keys", func() {
|
||
|
m.Combo("").Get(repo.ListDeployKeys).
|
||
|
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
|
||
|
m.Combo("/:id").Get(repo.GetDeployKey).
|
||
|
Delete(repo.DeleteDeploykey)
|
||
|
})
|
||
9 years ago
|
m.Group("/issues", func() {
|
||
|
m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
|
||
8 years ago
|
m.Group("/comments", func() {
|
||
|
m.Get("", repo.ListRepoIssueComments)
|
||
|
m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
|
||
|
})
|
||
9 years ago
|
m.Group("/:index", func() {
|
||
|
m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
|
||
8 years ago
|
|
||
|
m.Group("/comments", func() {
|
||
|
m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
|
||
8 years ago
|
m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
|
||
|
Delete(repo.DeleteIssueComment)
|
||
8 years ago
|
})
|
||
|
|
||
9 years ago
|
m.Group("/labels", func() {
|
||
9 years ago
|
m.Combo("").Get(repo.ListIssueLabels).
|
||
9 years ago
|
Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
|
||
|
Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
|
||
|
Delete(repo.ClearIssueLabels)
|
||
|
m.Delete("/:id", repo.DeleteIssueLabel)
|
||
|
})
|
||
|
|
||
|
})
|
||
9 years ago
|
}, mustEnableIssues)
|
||
9 years ago
|
m.Group("/labels", func() {
|
||
|
m.Combo("").Get(repo.ListLabels).
|
||
9 years ago
|
Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
|
||
|
m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
|
||
9 years ago
|
Delete(repo.DeleteLabel)
|
||
9 years ago
|
})
|
||
8 years ago
|
m.Group("/milestones", func() {
|
||
|
m.Combo("").Get(repo.ListMilestones).
|
||
8 years ago
|
Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
|
||
|
m.Combo("/:id").Get(repo.GetMilestone).
|
||
|
Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
|
||
|
Delete(reqRepoWriter(), repo.DeleteMilestone)
|
||
8 years ago
|
})
|
||
8 years ago
|
m.Post("/mirror-sync", repo.MirrorSync)
|
||
8 years ago
|
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
|
||
9 years ago
|
}, repoAssignment())
|
||
|
}, reqToken())
|
||
9 years ago
|
|
||
8 years ago
|
m.Get("/issues", reqToken(), repo.ListUserIssues)
|
||
|
|
||
9 years ago
|
// Organizations
|
||
9 years ago
|
m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
|
||
9 years ago
|
m.Get("/users/:username/orgs", org.ListUserOrgs)
|
||
9 years ago
|
m.Group("/orgs/:orgname", func() {
|
||
|
m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
|
||
|
m.Combo("/teams").Get(org.ListTeams)
|
||
9 years ago
|
}, orgAssignment(true))
|
||
9 years ago
|
|
||
8 years ago
|
m.Any("/*", func(c *context.Context) {
|
||
|
c.Error(404)
|
||
9 years ago
|
})
|
||
9 years ago
|
|
||
|
m.Group("/admin", func() {
|
||
|
m.Group("/users", func() {
|
||
|
m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
|
||
|
|
||
|
m.Group("/:username", func() {
|
||
|
m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
|
||
|
Delete(admin.DeleteUser)
|
||
9 years ago
|
m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
|
||
9 years ago
|
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
|
||
9 years ago
|
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
|
||
9 years ago
|
})
|
||
|
})
|
||
9 years ago
|
|
||
|
m.Group("/orgs/:orgname", func() {
|
||
9 years ago
|
m.Group("/teams", func() {
|
||
9 years ago
|
m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
|
||
9 years ago
|
})
|
||
9 years ago
|
})
|
||
9 years ago
|
m.Group("/teams", func() {
|
||
|
m.Group("/:teamid", func() {
|
||
|
m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
|
||
|
m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
|
||
9 years ago
|
}, orgAssignment(false, true))
|
||
9 years ago
|
})
|
||
9 years ago
|
}, reqAdmin())
|
||
9 years ago
|
}, context.APIContexter())
|
||
9 years ago
|
}
|