api: support put content (#7114)

Co-authored-by: Joe Chen <jc@unknwon.io>
pull/7202/head
Mateusz Reszka 2022-10-22 17:52:48 +02:00 committed by GitHub
parent a7299bbb8d
commit 742bc36edd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 174 additions and 88 deletions

View File

@ -8,6 +8,7 @@ All notable changes to Gogs are documented in this file.
- Support using personal access token in the password field. [#3866](https://github.com/gogs/gogs/issues/3866)
- An unlisted option is added when create or migrate a repository. Unlisted repositories are public but not being listed for users without direct access in the UI. [#5733](https://github.com/gogs/gogs/issues/5733)
- New API endpoint `PUT /repos/:owner/:repo/contents/:path` for creating and update repository contents. [#5967](https://github.com/gogs/gogs/issues/5967)
- New configuration option `[git.timeout] DIFF` for customizing operation timeout of `git diff`. [#6315](https://github.com/gogs/gogs/issues/6315)
- New configuration option `[server] SSH_SERVER_MACS` for setting list of accepted MACs for connections to builtin SSH server. [#6434](https://github.com/gogs/gogs/issues/6434)
- Support specifying custom schema for PostgreSQL. [#6695](https://github.com/gogs/gogs/pull/6695)

View File

@ -9,6 +9,7 @@ import (
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/unknwon/paginater"
"gopkg.in/macaron.v1"
log "unknwon.dev/clog/v2"
@ -49,8 +50,11 @@ func (c *APIContext) ErrorStatus(status int, err error) {
// Error renders the 500 response.
func (c *APIContext) Error(err error, msg string) {
log.ErrorDepth(5, "%s: %v", msg, err)
c.ErrorStatus(http.StatusInternalServerError, err)
log.ErrorDepth(4, "%s: %v", msg, err)
c.ErrorStatus(
http.StatusInternalServerError,
errors.New("Something went wrong, please check the server logs for more information."),
)
}
// Errorf renders the 500 response with formatted message.

View File

@ -273,7 +273,9 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
m.Group("/contents", func() {
m.Get("", repo.GetContents)
m.Get("/*", repo.GetContents)
m.Combo("/*").
Get(repo.GetContents).
Put(bind(repo.PutContentsRequest{}), repo.PutContents)
})
m.Get("/archive/*", repo.GetArchive)
m.Group("/git", func() {

View File

@ -7,45 +7,24 @@ package repo
import (
"encoding/base64"
"fmt"
"net/http"
"path"
"github.com/gogs/git-module"
"github.com/pkg/errors"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/gitutil"
"gogs.io/gogs/internal/repoutil"
)
func GetContents(c *context.APIContext) {
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
c.Error(err, "open repository")
return
}
ref := c.Query("ref")
if ref == "" {
ref = c.Repo.Repository.DefaultBranch
}
commit, err := gitRepo.CatFileCommit(ref)
if err != nil {
c.NotFoundOrError(gitutil.NewError(err), "get commit")
return
}
treePath := c.Params("*")
entry, err := commit.TreeEntry(treePath)
if err != nil {
c.NotFoundOrError(gitutil.NewError(err), "get tree entry")
return
}
type links struct {
Git string `json:"git"`
Self string `json:"self"`
HTML string `json:"html"`
}
type repoContent struct {
Type string `json:"type"`
Target string `json:"target,omitempty"`
@ -63,11 +42,11 @@ func GetContents(c *context.APIContext) {
Links links `json:"_links"`
}
toRepoContent := func(subpath string, entry *git.TreeEntry) (*repoContent, error) {
func toRepoContent(c *context.APIContext, ref, subpath string, commit *git.Commit, entry *git.TreeEntry) (*repoContent, error) {
repoURL := fmt.Sprintf("%s/repos/%s/%s", c.BaseURL, c.Params(":username"), c.Params(":reponame"))
selfURL := fmt.Sprintf("%s/contents/%s", repoURL, subpath)
htmlURL := fmt.Sprintf("%s/src/%s/%s", c.Repo.Repository.HTMLURL(), ref, entry.Name())
downloadURL := fmt.Sprintf("%s/raw/%s/%s", c.Repo.Repository.HTMLURL(), ref, entry.Name())
htmlURL := fmt.Sprintf("%s/src/%s/%s", repoutil.HTMLURL(c.Repo.Owner.Name, c.Repo.Repository.Name), ref, entry.Name())
downloadURL := fmt.Sprintf("%s/raw/%s/%s", repoutil.HTMLURL(c.Repo.Owner.Name, c.Repo.Repository.Name), ref, entry.Name())
content := &repoContent{
Size: entry.Size(),
@ -119,12 +98,37 @@ func GetContents(c *context.APIContext) {
}
content.Links.Git = content.GitURL
return content, nil
}
func GetContents(c *context.APIContext) {
repoPath := repoutil.RepositoryPath(c.Params(":username"), c.Params(":reponame"))
gitRepo, err := git.Open(repoPath)
if err != nil {
c.Error(err, "open repository")
return
}
ref := c.Query("ref")
if ref == "" {
ref = c.Repo.Repository.DefaultBranch
}
commit, err := gitRepo.CatFileCommit(ref)
if err != nil {
c.NotFoundOrError(gitutil.NewError(err), "get commit")
return
}
treePath := c.Params("*")
entry, err := commit.TreeEntry(treePath)
if err != nil {
c.NotFoundOrError(gitutil.NewError(err), "get tree entry")
return
}
if !entry.IsTree() {
content, err := toRepoContent(treePath, entry)
content, err := toRepoContent(c, ref, treePath, commit, entry)
if err != nil {
c.Errorf(err, "convert %q to repoContent", treePath)
return
@ -155,7 +159,7 @@ func GetContents(c *context.APIContext) {
contents := make([]*repoContent, 0, len(entries))
for _, entry := range entries {
subpath := path.Join(treePath, entry.Name())
content, err := toRepoContent(subpath, entry)
content, err := toRepoContent(c, ref, subpath, commit, entry)
if err != nil {
c.Errorf(err, "convert %q to repoContent", subpath)
return
@ -165,3 +169,78 @@ func GetContents(c *context.APIContext) {
}
c.JSONSuccess(contents)
}
// PutContentsRequest is the API message for creating or updating a file.
type PutContentsRequest struct {
Message string `json:"message" binding:"Required"`
Content string `json:"content" binding:"Required"`
Branch string `json:"branch"`
}
// PUT /repos/:username/:reponame/contents/*
func PutContents(c *context.APIContext, r PutContentsRequest) {
content, err := base64.StdEncoding.DecodeString(r.Content)
if err != nil {
c.Error(err, "decoding base64")
return
}
if r.Branch == "" {
r.Branch = c.Repo.Repository.DefaultBranch
}
treePath := c.Params("*")
err = c.Repo.Repository.UpdateRepoFile(
c.User,
db.UpdateRepoFileOptions{
OldBranch: c.Repo.Repository.DefaultBranch,
NewBranch: r.Branch,
OldTreeName: treePath,
NewTreeName: treePath,
Message: r.Message,
Content: string(content),
},
)
if err != nil {
c.Error(err, "updating repository file")
return
}
repoPath := repoutil.RepositoryPath(c.Params(":username"), c.Params(":reponame"))
gitRepo, err := git.Open(repoPath)
if err != nil {
c.Error(err, "open repository")
return
}
commit, err := gitRepo.CatFileCommit(r.Branch)
if err != nil {
c.Error(err, "get file commit")
return
}
entry, err := commit.TreeEntry(treePath)
if err != nil {
c.Error(err, "get tree entry")
return
}
apiContent, err := toRepoContent(c, r.Branch, treePath, commit, entry)
if err != nil {
c.Error(err, "convert to *repoContent")
return
}
apiCommit, err := gitCommitToAPICommit(commit, c)
if err != nil {
c.Error(err, "convert to *api.Commit")
return
}
c.JSON(
http.StatusCreated,
map[string]any{
"content": apiContent,
"commit": apiCommit,
},
)
}