Files
Gogs/internal/route/api/v1/api.go

411 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 (
admin2 "gogs.io/gogs/internal/route/api/v1/admin"
misc2 "gogs.io/gogs/internal/route/api/v1/misc"
org2 "gogs.io/gogs/internal/route/api/v1/org"
repo2 "gogs.io/gogs/internal/route/api/v1/repo"
user2 "gogs.io/gogs/internal/route/api/v1/user"
"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/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/form"
)
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 *db.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 {
owner, err = db.GetUserByName(username)
if err != nil {
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return
}
}
2017-06-03 07:26:09 -04:00
c.Repo.Owner = owner
r, err := db.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 {
c.Repo.AccessMode = db.ACCESS_MODE_OWNER
} else {
mode, err := db.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 = db.GetUserByName(c.Params(":orgname"))
2019-08-08 23:53:43 -07:00
if err != nil {
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return
}
}
if assignTeam {
c.Org.Team, err = db.GetTeamByID(c.ParamsInt64(":teamid"))
2019-08-08 23:53:43 -07:00
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
}
}
// RegisterRoutes registers all route 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{}), misc2.Markdown)
m.Post("/markdown/raw", misc2.MarkdownRaw)
// Users
m.Group("/users", func() {
m.Get("/search", user2.Search)
m.Group("/:username", func() {
m.Get("", user2.GetInfo)
m.Group("/tokens", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(user2.ListAccessTokens).
Post(bind(api.CreateAccessTokenOption{}), user2.CreateAccessToken)
}, reqBasicAuth())
})
})
m.Group("/users", func() {
m.Group("/:username", func() {
m.Get("/keys", user2.ListPublicKeys)
m.Get("/followers", user2.ListFollowers)
m.Group("/following", func() {
m.Get("", user2.ListFollowing)
m.Get("/:target", user2.CheckFollowing)
})
})
}, reqToken())
m.Group("/user", func() {
m.Get("", user2.GetAuthenticatedUser)
2019-08-08 23:53:43 -07:00
m.Combo("/emails").
Get(user2.ListEmails).
Post(bind(api.CreateEmailOption{}), user2.AddEmail).
Delete(bind(api.CreateEmailOption{}), user2.DeleteEmail)
m.Get("/followers", user2.ListMyFollowers)
m.Group("/following", func() {
m.Get("", user2.ListMyFollowing)
2019-08-10 13:40:48 -07:00
m.Combo("/:username").
Get(user2.CheckMyFollowing).
Put(user2.Follow).
Delete(user2.Unfollow)
})
m.Group("/keys", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(user2.ListMyPublicKeys).
Post(bind(api.CreateKeyOption{}), user2.CreatePublicKey)
2019-08-08 23:53:43 -07:00
m.Combo("/:id").
Get(user2.GetPublicKey).
Delete(user2.DeletePublicKey)
})
2017-02-13 01:42:28 +01:00
m.Get("/issues", repo2.ListUserIssues)
}, reqToken())
// Repositories
m.Get("/users/:username/repos", reqToken(), repo2.ListUserRepositories)
m.Get("/orgs/:org/repos", reqToken(), repo2.ListOrgRepositories)
2019-08-08 23:53:43 -07:00
m.Combo("/user/repos", reqToken()).
Get(repo2.ListMyRepos).
Post(bind(api.CreateRepoOption{}), repo2.Create)
m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo2.CreateOrgRepo)
m.Group("/repos", func() {
m.Get("/search", repo2.Search)
m.Get("/:username/:reponame", repoAssignment(), repo2.Get)
})
m.Group("/repos", func() {
m.Post("/migrate", bind(form.MigrateRepo{}), repo2.Migrate)
m.Delete("/:username/:reponame", repoAssignment(), repo2.Delete)
m.Group("/:username/:reponame", func() {
m.Group("/hooks", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(repo2.ListHooks).
Post(bind(api.CreateHookOption{}), repo2.CreateHook)
2019-08-08 23:53:43 -07:00
m.Combo("/:id").
Patch(bind(api.EditHookOption{}), repo2.EditHook).
Delete(repo2.DeleteHook)
}, reqRepoAdmin())
2019-08-08 23:53:43 -07:00
m.Group("/collaborators", func() {
m.Get("", repo2.ListCollaborators)
2019-08-08 23:53:43 -07:00
m.Combo("/:collaborator").
Get(repo2.IsCollaborator).
Put(bind(api.AddCollaboratorOption{}), repo2.AddCollaborator).
Delete(repo2.DeleteCollaborator)
}, reqRepoAdmin())
2019-08-08 23:53:43 -07:00
m.Get("/raw/*", context.RepoRef(), repo2.GetRawFile)
api: `GET /repos/:owner/:repo/contents/:path` (#5963) * support API `GET /repos/:owner/:repo/contents/:path` This PR adds support to #5949: `GET /repos/:owner/:repo/contents/:path` Curl: ```bash curl -H "Authorization: token REDACTED" http://localhost:3000/api/v1/repos/root/testrepo/contents//master/README.md -X GET | jq . ``` Curl Response: ```bash { "type": "blob", "size": 12, "name": "README.md", "path": "README.md", "sha": "70fcb456d436f08462602f26df6fb7e167e7a916", "url": "http://localhost:3000/api/v1/repos/root/testrepo/contents/README.md", "git_url": "http://localhost:3000/api/v1/repos/root/testrepo/trees/70fcb456d436f08462602f26df6fb7e167e7a916", "html_url": "http://localhost:3000/api/v1/repos/root/testrepo/tree/70fcb456d436f08462602f26df6fb7e167e7a916", "download_url": "http://localhost:3000/api/v1/root/testrepo/raw/README.md", "_links": { "git": "http://localhost:3000/api/v1/repos/root/testrepo/trees/70fcb456d436f08462602f26df6fb7e167e7a916", "self": "http://localhost:3000/api/v1/repos/root/testrepo/contents/README.md", "html": "http://localhost:3000/api/v1/repos/root/testrepo/tree/70fcb456d436f08462602f26df6fb7e167e7a916" }, "content": "IyB0ZXN0cmVwbwoK" } ``` * rename - path.go to contents.go * reorder imports Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io> * rename struct to repoContents and fix field order Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io> * rename variable Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io> * rename GetPathContents to GetContents Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io> * return on server error Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io> * resolve conflicts introduced via git web ui * make constants as method variables * handle dir type case last * fix func and var names * implement suggested changes in review * refactor smaller funcs to be part of GetContent * fix content type check for blob after refactoring * changes based on suggestions * read full file, return empty json array * don't set submoduleURL * set server err msg to method name * set target to be blob data for symlinks * Update contents.go Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
2020-03-05 16:15:38 +08:00
m.Get("/contents/*", context.RepoRef(), repo2.GetContents)
m.Get("/archive/*", repo2.GetArchive)
api: support getting repository Git tree (#5934) (#5937) * add basic git repository tree api (#5934) This PR adds the tree api endpoint to gogs api: `GET/repos/:owner/:repo/git/trees/:tree_sha` This new api endpoint that is being added is in conformance to the GitHub REST API v3 specification. Documentation can be found here: developer.github.com/v3/git/trees/#get-a-tree For a given user, repo and sha value, this api (currently) returns a single tree using the SHA1 value for that tree. - Recursive implementation is yet to be implemented. - Creating a Tree using POST is yet to be implemented. Example curl: ``` l curl -H "Authorization: token REDACTED" http://localhost:3000/api/v1/repos/root/testrepo/git/trees/c59441ded1549b149def0d4c54594d31a7f3718f -X GET | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 940 100 940 0 0 12034 0 --:--:-- --:--:-- --:--:-- 12051 [ { "sha": "c59441ded1549b149def0d4c54594d31a7f3718f", "tree": [ { "mode": "120000", "path": "/home/bharatnc/gogs-repositories/root/testrepo.git", "sha": "472ac2361b65136b393d652de25341e2ea44f299", "size": 1077, "type": "blob", "url": "http://localhost:3000/api/v1/repos/root/testrepo/git/trees/472ac2361b65136b393d652de25341e2ea44f299" }, { "mode": "120000", "path": "/home/bharatnc/gogs-repositories/root/testrepo.git", "sha": "70fcb456d436f08462602f26df6fb7e167e7a916", "size": 12, "type": "blob", "url": "http://localhost:3000/api/v1/repos/root/testrepo/git/trees/70fcb456d436f08462602f26df6fb7e167e7a916" }, { "mode": "120000", "path": "/home/bharatnc/gogs-repositories/root/testrepo.git", "sha": "092c58d4b63df5779a4d020b1fdbb762421bbb4f", "size": 380, "type": "blob", "url": "http://localhost:3000/api/v1/repos/root/testrepo/git/trees/092c58d4b63df5779a4d020b1fdbb762421bbb4f" } ], "url": "http://localhost:3000/api/v1/repos/root/testrepo/git/trees/c59441ded1549b149def0d4c54594d31a7f3718f" } ] ``` * remove vertical space * make go.mod to be same as in master * rename structs to sound better * simplify expressions and fix error msg * Update tree.go * Update tree.go * display file name instead of repo path * Update tree.go Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
2020-02-25 06:19:42 -08:00
m.Group("/git/trees", func() {
m.Get("/:sha", context.RepoRef(), repo2.GetRepoGitTree)
})
m.Get("/forks", repo2.ListForks)
2016-01-15 19:24:03 +01:00
m.Group("/branches", func() {
m.Get("", repo2.ListBranches)
m.Get("/*", repo2.GetBranch)
2016-01-15 19:24:03 +01:00
})
2018-12-15 00:24:41 -05:00
m.Group("/commits", func() {
m.Get("/:sha", repo2.GetSingleCommit)
m.Get("/*", repo2.GetReferenceSHA)
2018-12-15 00:24:41 -05:00
})
m.Group("/keys", func() {
2019-08-08 23:53:43 -07:00
m.Combo("").
Get(repo2.ListDeployKeys).
Post(bind(api.CreateKeyOption{}), repo2.CreateDeployKey)
2019-08-08 23:53:43 -07:00
m.Combo("/:id").
Get(repo2.GetDeployKey).
Delete(repo2.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(repo2.ListIssues).
Post(bind(api.CreateIssueOption{}), repo2.CreateIssue)
m.Group("/comments", func() {
m.Get("", repo2.ListRepoIssueComments)
m.Patch("/:id", bind(api.EditIssueCommentOption{}), repo2.EditIssueComment)
})
m.Group("/:index", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Get(repo2.GetIssue).
Patch(bind(api.EditIssueOption{}), repo2.EditIssue)
m.Group("/comments", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Get(repo2.ListIssueComments).
Post(bind(api.CreateIssueCommentOption{}), repo2.CreateIssueComment)
2019-08-10 13:40:48 -07:00
m.Combo("/:id").
Patch(bind(api.EditIssueCommentOption{}), repo2.EditIssueComment).
Delete(repo2.DeleteIssueComment)
})
m.Get("/labels", repo2.ListIssueLabels)
m.Group("/labels", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Post(bind(api.IssueLabelsOption{}), repo2.AddIssueLabels).
Put(bind(api.IssueLabelsOption{}), repo2.ReplaceIssueLabels).
Delete(repo2.ClearIssueLabels)
m.Delete("/:id", repo2.DeleteIssueLabel)
2019-08-10 13:40:48 -07:00
}, reqRepoWriter())
})
}, mustEnableIssues)
2019-08-10 13:40:48 -07:00
m.Group("/labels", func() {
m.Get("", repo2.ListLabels)
m.Get("/:id", repo2.GetLabel)
})
2019-08-10 13:40:48 -07:00
m.Group("/labels", func() {
m.Post("", bind(api.CreateLabelOption{}), repo2.CreateLabel)
2019-08-10 13:40:48 -07:00
m.Combo("/:id").
Patch(bind(api.EditLabelOption{}), repo2.EditLabel).
Delete(repo2.DeleteLabel)
2019-08-10 13:40:48 -07:00
}, reqRepoWriter())
m.Group("/milestones", func() {
m.Get("", repo2.ListMilestones)
m.Get("/:id", repo2.GetMilestone)
})
2019-08-10 13:40:48 -07:00
m.Group("/milestones", func() {
m.Post("", bind(api.CreateMilestoneOption{}), repo2.CreateMilestone)
2019-08-10 13:40:48 -07:00
m.Combo("/:id").
Patch(bind(api.EditMilestoneOption{}), repo2.EditMilestone).
Delete(repo2.DeleteMilestone)
2019-08-10 13:40:48 -07:00
}, reqRepoWriter())
m.Patch("/issue-tracker", reqRepoWriter(), bind(api.EditIssueTrackerOption{}), repo2.IssueTracker)
m.Post("/mirror-sync", reqRepoWriter(), repo2.MirrorSync)
m.Get("/editorconfig/:filename", context.RepoRef(), repo2.GetEditorconfig)
}, repoAssignment())
}, reqToken())
m.Get("/issues", reqToken(), repo2.ListUserIssues)
2017-02-13 01:42:28 +01:00
2015-12-17 02:28:47 -05:00
// Organizations
2019-08-10 13:40:48 -07:00
m.Combo("/user/orgs", reqToken()).
Get(org2.ListMyOrgs).
Post(bind(api.CreateOrgOption{}), org2.CreateMyOrg)
m.Get("/users/:username/orgs", org2.ListUserOrgs)
m.Group("/orgs/:orgname", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Get(org2.Get).
Patch(bind(api.EditOrgOption{}), org2.Edit)
m.Get("/teams", org2.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{}), admin2.CreateUser)
2015-12-05 17:13:13 -05:00
m.Group("/:username", func() {
2019-08-10 13:40:48 -07:00
m.Combo("").
Patch(bind(api.EditUserOption{}), admin2.EditUser).
Delete(admin2.DeleteUser)
m.Post("/keys", bind(api.CreateKeyOption{}), admin2.CreatePublicKey)
m.Post("/orgs", bind(api.CreateOrgOption{}), admin2.CreateOrg)
m.Post("/repos", bind(api.CreateRepoOption{}), admin2.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{}), admin2.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(admin2.AddTeamMember).
Delete(admin2.RemoveTeamMember)
2019-08-10 13:40:48 -07:00
m.Combo("/repos/:reponame").
Put(admin2.AddTeamRepository).
Delete(admin2.RemoveTeamRepository)
}, orgAssignment(false, true))
})
}, reqAdmin())
2019-08-10 13:40:48 -07:00
m.Any("/*", func(c *context.Context) {
c.NotFound()
})
}, context.APIContexter())
}