Files
Gogs/routes/api/v1/api.go

407 lines
11 KiB
Go
Raw Normal View History

// 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 (
"net/http"
"strings"
"github.com/go-macaron/binding"
"gopkg.in/macaron.v1"
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/models"
"gogs.io/gogs/models/errors"
"gogs.io/gogs/pkg/context"
"gogs.io/gogs/pkg/form"
"gogs.io/gogs/routes/api/v1/admin"
"gogs.io/gogs/routes/api/v1/misc"
"gogs.io/gogs/routes/api/v1/org"
"gogs.io/gogs/routes/api/v1/repo"
"gogs.io/gogs/routes/api/v1/user"
)
2019-08-08 23:53:43 -07:00
// repoAssignment extracts information from URL parameters to retrieve the repository,
// and makes sure the context user has at least the read access to the repository.
func repoAssignment() macaron.Handler {
2017-06-03 07:26:09 -04:00
return func(c *context.APIContext) {
2019-08-08 23:53:43 -07:00
username := c.Params(":username")
reponame := c.Params(":reponame")
2019-08-08 23:53:43 -07:00
var err error
var owner *models.User
2019-08-08 23:53:43 -07:00
// Check if the context user is the repository owner.
if c.IsLogged && c.User.LowerName == strings.ToLower(username) {
2017-06-03 07:26:09 -04:00
owner = c.User
} else {
2019-08-08 23:53:43 -07:00
owner, err = models.GetUserByName(username)
if err != nil {
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return
}
}
2017-06-03 07:26:09 -04:00
c.Repo.Owner = owner
2019-08-08 23:53:43 -07:00
r, err := models.GetRepositoryByName(owner.ID, reponame)
if err != nil {
c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
return
2019-08-08 23:53:43 -07:00
} else if err = r.GetOwner(); err != nil {
c.ServerError("GetOwner", err)
return
}
if c.IsTokenAuth && c.User.IsAdmin {
2017-06-03 07:26:09 -04:00
c.Repo.AccessMode = models.ACCESS_MODE_OWNER
} else {
2019-08-08 23:53:43 -07:00
mode, err := models.UserAccessMode(c.UserID(), r)
if err != nil {
2019-08-08 23:53:43 -07:00
c.ServerError("UserAccessMode", err)
return
}
2017-06-03 07:26:09 -04:00
c.Repo.AccessMode = mode
}
2017-06-03 07:26:09 -04:00
if !c.Repo.HasAccess() {
c.NotFound()
return
}
2019-08-08 23:53:43 -07:00
c.Repo.Repository = r
}
}
// orgAssignment extracts information from URL parameters to retrieve the organization or team.
func orgAssignment(args ...bool) macaron.Handler {
var (
assignOrg bool
assignTeam bool
)
if len(args) > 0 {
assignOrg = args[0]
}
if len(args) > 1 {
assignTeam = args[1]
}
return func(c *context.APIContext) {
c.Org = new(context.APIOrganization)
var err error
if assignOrg {
c.Org.Organization, err = models.GetUserByName(c.Params(":orgname"))
if err != nil {
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return
}
}
if assignTeam {
c.Org.Team, err = models.GetTeamByID(c.ParamsInt64(":teamid"))
if err != nil {
c.NotFoundOrServerError("GetTeamByID", errors.IsTeamNotExist, err)
return
}
}
}
}
2019-08-08 23:53:43 -07:00
// reqToken makes sure the context user is authorized via access token.
func reqToken() macaron.Handler {
2017-06-03 07:26:09 -04:00
return func(c *context.Context) {
if !c.IsTokenAuth {
c.Error(http.StatusUnauthorized)
return
}
}
}
2019-08-08 23:53:43 -07:00
// reqBasicAuth makes sure the context user is authorized via HTTP Basic Auth.
func reqBasicAuth() macaron.Handler {
2017-06-03 07:26:09 -04:00
return func(c *context.Context) {
if !c.IsBasicAuth {
c.Error(http.StatusUnauthorized)
return
}
}
}
2019-08-08 23:53:43 -07:00
// reqAdmin makes sure the context user is a site admin.
func reqAdmin() macaron.Handler {
2017-06-03 07:26:09 -04:00
return func(c *context.Context) {
if !c.IsLogged || !c.User.IsAdmin {
c.Error(http.StatusForbidden)
return
}
}
}
2019-08-08 23:53:43 -07:00
// reqRepoWriter makes sure the context user has at least write access to the repository.
2016-08-24 16:05:56 -07:00
func reqRepoWriter() macaron.Handler {
2017-06-03 07:26:09 -04:00
return func(c *context.Context) {
if !c.Repo.IsWriter() {
c.Error(http.StatusForbidden)
2016-08-24 16:05:56 -07:00
return
}
}
}
2019-08-08 23:53:43 -07:00
// reqRepoWriter makes sure the context user has at least admin access to the repository.
func reqRepoAdmin() macaron.Handler {
return func(c *context.Context) {
if !c.Repo.IsAdmin() {
c.Error(http.StatusForbidden)
return
}
}
}
2017-06-03 07:26:09 -04:00
func mustEnableIssues(c *context.APIContext) {
if !c.Repo.Repository.EnableIssues || c.Repo.Repository.EnableExternalTracker {
c.NotFound()
return
}
}
2019-08-08 23:53:43 -07:00
// RegisterRoutes registers all routes in API v1 to the web application.
// FIXME: custom form error response
func RegisterRoutes(m *macaron.Macaron) {
bind := binding.Bind
m.Group("/v1", func() {
// Handle preflight OPTIONS request
m.Options("/*", func() {})
// 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() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(user.ListAccessTokens).
Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
}, reqBasicAuth())
})
})
m.Group("/users", func() {
m.Group("/:username", func() {
2015-12-05 17:13:13 -05:00
m.Get("/keys", user.ListPublicKeys)
m.Get("/followers", user.ListFollowers)
m.Group("/following", func() {
m.Get("", user.ListFollowing)
m.Get("/:target", user.CheckFollowing)
})
})
}, reqToken())
m.Group("/user", func() {
2016-08-11 15:29:39 -07:00
m.Get("", user.GetAuthenticatedUser)
2019-08-08 23:53:43 -07:00
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)
2019-08-10 13:40:48 -07:00
m.Combo("/:username").
Get(user.CheckMyFollowing).
Put(user.Follow).
Delete(user.Unfollow)
})
m.Group("/keys", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(user.ListMyPublicKeys).
Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
2019-08-08 23:53:43 -07:00
m.Combo("/:id").
Get(user.GetPublicKey).
Delete(user.DeletePublicKey)
})
2017-02-13 01:42:28 +01:00
2019-08-10 13:40:48 -07:00
m.Get("/issues", repo.ListUserIssues)
}, reqToken())
// Repositories
m.Get("/users/:username/repos", reqToken(), repo.ListUserRepositories)
m.Get("/orgs/:org/repos", reqToken(), repo.ListOrgRepositories)
2019-08-08 23:53:43 -07:00
m.Combo("/user/repos", reqToken()).
Get(repo.ListMyRepos).
Post(bind(api.CreateRepoOption{}), repo.Create)
m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
m.Group("/repos", func() {
m.Get("/search", repo.Search)
m.Get("/:username/:reponame", repoAssignment(), repo.Get)
})
m.Group("/repos", func() {
m.Post("/migrate", bind(form.MigrateRepo{}), repo.Migrate)
m.Delete("/:username/:reponame", repoAssignment(), repo.Delete)
m.Group("/:username/:reponame", func() {
m.Group("/hooks", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(repo.ListHooks).
Post(bind(api.CreateHookOption{}), repo.CreateHook)
2019-08-08 23:53:43 -07:00
m.Combo("/:id").
Patch(bind(api.EditHookOption{}), repo.EditHook).
Delete(repo.DeleteHook)
}, reqRepoAdmin())
2019-08-08 23:53:43 -07:00
m.Group("/collaborators", func() {
m.Get("", repo.ListCollaborators)
2019-08-08 23:53:43 -07:00
m.Combo("/:collaborator").
Get(repo.IsCollaborator).
Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
Delete(repo.DeleteCollaborator)
}, reqRepoAdmin())
2019-08-08 23:53:43 -07:00
2016-03-11 11:56:52 -05:00
m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
m.Get("/archive/*", repo.GetArchive)
m.Get("/forks", repo.ListForks)
2016-01-15 19:24:03 +01:00
m.Group("/branches", func() {
2016-03-11 11:56:52 -05:00
m.Get("", repo.ListBranches)
m.Get("/*", repo.GetBranch)
2016-01-15 19:24:03 +01:00
})
2018-12-15 00:24:41 -05:00
m.Group("/commits", func() {
m.Get("/:sha", repo.GetSingleCommit)
2018-12-16 19:47:32 -05:00
m.Get("/*", repo.GetReferenceSHA)
2018-12-15 00:24:41 -05:00
})
m.Group("/keys", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(repo.ListDeployKeys).
Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
2019-08-08 23:53:43 -07:00
m.Combo("/:id").
Get(repo.GetDeployKey).
Delete(repo.DeleteDeploykey)
}, reqRepoAdmin())
2019-08-08 23:53:43 -07:00
m.Group("/issues", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(repo.ListIssues).
Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
m.Group("/comments", func() {
m.Get("", repo.ListRepoIssueComments)
2019-08-10 13:40:48 -07:00
m.Patch("/:id", bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
})
m.Group("/:index", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Get(repo.GetIssue).
Patch(bind(api.EditIssueOption{}), repo.EditIssue)
m.Group("/comments", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Get(repo.ListIssueComments).
Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
m.Combo("/:id").
Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
Delete(repo.DeleteIssueComment)
})
2019-08-10 13:40:48 -07:00
m.Get("/labels", repo.ListIssueLabels)
m.Group("/labels", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
Delete(repo.ClearIssueLabels)
m.Delete("/:id", repo.DeleteIssueLabel)
2019-08-10 13:40:48 -07:00
}, reqRepoWriter())
})
}, mustEnableIssues)
2019-08-10 13:40:48 -07:00
m.Group("/labels", func() {
2019-08-10 13:40:48 -07:00
m.Get("", repo.ListLabels)
m.Get("/:id", repo.GetLabel)
})
2019-08-10 13:40:48 -07:00
m.Group("/labels", func() {
m.Post("", bind(api.CreateLabelOption{}), repo.CreateLabel)
m.Combo("/:id").
Patch(bind(api.EditLabelOption{}), repo.EditLabel).
Delete(repo.DeleteLabel)
}, reqRepoWriter())
m.Group("/milestones", func() {
2019-08-10 13:40:48 -07:00
m.Get("", repo.ListMilestones)
m.Get("/:id", repo.GetMilestone)
})
2019-08-10 13:40:48 -07:00
m.Group("/milestones", func() {
m.Post("", bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
m.Combo("/:id").
Patch(bind(api.EditMilestoneOption{}), repo.EditMilestone).
Delete(repo.DeleteMilestone)
}, reqRepoWriter())
m.Patch("/issue-tracker", reqRepoWriter(), bind(api.EditIssueTrackerOption{}), repo.IssueTracker)
m.Post("/mirror-sync", reqRepoWriter(), repo.MirrorSync)
2016-08-30 20:18:40 -03:00
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
}, repoAssignment())
}, reqToken())
2017-02-13 01:42:28 +01:00
m.Get("/issues", reqToken(), repo.ListUserIssues)
2015-12-17 02:28:47 -05:00
// Organizations
2019-08-10 13:40:48 -07:00
m.Combo("/user/orgs", reqToken()).
Get(org.ListMyOrgs).
Post(bind(api.CreateOrgOption{}), org.CreateMyOrg)
2015-12-17 02:28:47 -05:00
m.Get("/users/:username/orgs", org.ListUserOrgs)
m.Group("/orgs/:orgname", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Get(org.Get).
Patch(bind(api.EditOrgOption{}), org.Edit)
m.Get("/teams", org.ListTeams)
}, orgAssignment(true))
2015-12-17 02:28:47 -05:00
2015-12-05 17:13:13 -05:00
m.Group("/admin", func() {
m.Group("/users", func() {
m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
m.Group("/:username", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Patch(bind(api.EditUserOption{}), admin.EditUser).
2015-12-05 17:13:13 -05:00
Delete(admin.DeleteUser)
2016-01-08 08:49:03 +08:00
m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
2015-12-17 02:28:47 -05:00
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
2015-12-17 22:57:41 -05:00
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
2015-12-05 17:13:13 -05:00
})
})
m.Group("/orgs/:orgname", func() {
m.Group("/teams", func() {
m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
})
})
2019-08-10 13:40:48 -07:00
m.Group("/teams", func() {
m.Group("/:teamid", func() {
2019-08-10 13:40:48 -07:00
m.Combo("/members/:username").
Put(admin.AddTeamMember).
Delete(admin.RemoveTeamMember)
m.Combo("/repos/:reponame").
Put(admin.AddTeamRepository).
Delete(admin.RemoveTeamRepository)
}, orgAssignment(false, true))
})
}, reqAdmin())
2019-08-10 13:40:48 -07:00
m.Any("/*", func(c *context.Context) {
c.NotFound()
})
}, context.APIContexter())
}