2014-11-17 06:57:04 +05:30
|
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-11-28 16:56:14 +05:30
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-11-17 06:57:04 +05:30
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2015-12-05 03:46:42 +05:30
|
|
|
|
package repo
|
2014-11-17 08:02:26 +05:30
|
|
|
|
|
|
|
|
|
import (
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"encoding/base64"
|
2020-06-01 02:29:34 +05:30
|
|
|
|
"fmt"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"net/http"
|
2019-12-24 08:03:52 +05:30
|
|
|
|
"time"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 15:03:00 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/repofiles"
|
2019-05-11 15:51:34 +05:30
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 21:06:53 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/web"
|
2016-11-10 21:54:48 +05:30
|
|
|
|
"code.gitea.io/gitea/routers/repo"
|
2014-11-17 08:02:26 +05:30
|
|
|
|
)
|
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
|
// GetRawFile get a file by path on a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
|
func GetRawFile(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Get a file from a repository
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: filepath of the file to get
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
2021-02-09 05:45:47 +05:30
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
|
|
|
|
// required: false
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// responses:
|
2018-06-01 11:21:49 +05:30
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2019-12-20 22:37:12 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2019-01-18 05:31:04 +05:30
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound()
|
2017-06-11 08:27:28 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 05:45:47 +05:30
|
|
|
|
commit := ctx.Repo.Commit
|
|
|
|
|
|
|
|
|
|
if ref := ctx.QueryTrim("ref"); len(ref) > 0 {
|
|
|
|
|
var err error
|
|
|
|
|
commit, err = ctx.Repo.GitRepo.GetCommit(ref)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
|
ctx.NotFound()
|
|
|
|
|
} else {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blob, err := commit.GetBlobByPath(ctx.Repo.TreePath)
|
2014-11-17 08:02:26 +05:30
|
|
|
|
if err != nil {
|
2015-12-10 07:16:05 +05:30
|
|
|
|
if git.IsErrNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound()
|
2014-11-17 08:02:26 +05:30
|
|
|
|
} else {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
|
2014-11-17 08:02:26 +05:30
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-03-14 04:19:16 +05:30
|
|
|
|
if err = repo.ServeBlob(ctx.Context, blob); err != nil {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
|
2014-11-17 08:02:26 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-02 19:24:35 +05:30
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
|
// GetArchive get archive of a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
|
func GetArchive(ctx *context.APIContext) {
|
2018-06-12 20:29:22 +05:30
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/archive/{archive} repository repoGetArchive
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// ---
|
|
|
|
|
// summary: Get an archive of a repository
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: archive
|
|
|
|
|
// in: path
|
2020-09-06 21:53:47 +05:30
|
|
|
|
// description: the git reference for download with attached archive format (e.g. master.zip)
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
2018-06-01 11:21:49 +05:30
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2019-12-20 22:37:12 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2015-09-02 19:24:35 +05:30
|
|
|
|
repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
|
|
|
|
|
gitRepo, err := git.OpenRepository(repoPath)
|
|
|
|
|
if err != nil {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
2015-09-02 19:24:35 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Repo.GitRepo = gitRepo
|
2019-11-13 12:31:19 +05:30
|
|
|
|
defer gitRepo.Close()
|
2015-09-02 19:24:35 +05:30
|
|
|
|
|
2016-03-14 04:19:16 +05:30
|
|
|
|
repo.Download(ctx.Context)
|
2015-09-02 19:24:35 +05:30
|
|
|
|
}
|
2016-08-31 04:48:40 +05:30
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
|
// GetEditorconfig get editor config of a repository
|
2016-08-31 04:48:40 +05:30
|
|
|
|
func GetEditorconfig(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/editorconfig/{filepath} repository repoGetEditorConfig
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Get the EditorConfig definitions of a file in a repository
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: filepath of file to get
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
2018-06-01 11:21:49 +05:30
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2019-12-20 22:37:12 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2016-08-31 04:48:40 +05:30
|
|
|
|
ec, err := ctx.Repo.GetEditorconfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if git.IsErrNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound(err)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
} else {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := ctx.Params("filename")
|
2019-10-17 05:45:02 +05:30
|
|
|
|
def, err := ec.GetDefinitionForFilename(fileName)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
if def == nil {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound(err)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, def)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 21:50:22 +05:30
|
|
|
|
// canWriteFiles returns true if repository is editable and user has proper access level.
|
|
|
|
|
func canWriteFiles(r *context.Repository) bool {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return r.Permission.CanWrite(models.UnitTypeCode) && !r.Repository.IsMirror && !r.Repository.IsArchived
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 21:50:22 +05:30
|
|
|
|
// canReadFiles returns true if repository is readable and user has proper access level.
|
|
|
|
|
func canReadFiles(r *context.Repository) bool {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return r.Permission.CanRead(models.UnitTypeCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateFile handles API call for creating a file
|
2021-01-26 21:06:53 +05:30
|
|
|
|
func CreateFile(ctx *context.APIContext) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/contents/{filepath} repository repoCreateFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Create a file in a repository
|
|
|
|
|
// consumes:
|
|
|
|
|
// - application/json
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to create
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
2019-05-30 23:27:55 +05:30
|
|
|
|
// required: true
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/CreateFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "201":
|
|
|
|
|
// "$ref": "#/responses/FileResponse"
|
2020-06-01 02:29:34 +05:30
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
// "422":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.CreateFileOptions)
|
2020-06-01 02:29:34 +05:30
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "RepoIsEmpty", fmt.Errorf("repo is empty"))
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2020-04-20 22:17:05 +05:30
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
opts := &repofiles.UpdateRepoFileOptions{
|
|
|
|
|
Content: apiOpts.Content,
|
|
|
|
|
IsNewFile: true,
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
Committer: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Dates: &repofiles.CommitDateOptions{
|
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff: apiOpts.Signoff,
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2019-06-29 20:49:24 +05:30
|
|
|
|
|
|
|
|
|
if opts.Message == "" {
|
|
|
|
|
opts.Message = ctx.Tr("repo.editor.add", opts.TreePath)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
|
2020-06-01 02:29:34 +05:30
|
|
|
|
handleCreateOrUpdateFileError(ctx, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusCreated, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateFile handles API call for updating a file
|
2021-01-26 21:06:53 +05:30
|
|
|
|
func UpdateFile(ctx *context.APIContext) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/contents/{filepath} repository repoUpdateFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Update a file in a repository
|
|
|
|
|
// consumes:
|
|
|
|
|
// - application/json
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to update
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
2019-05-30 23:27:55 +05:30
|
|
|
|
// required: true
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/UpdateFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileResponse"
|
2020-06-01 02:29:34 +05:30
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
// "422":
|
|
|
|
|
// "$ref": "#/responses/error"
|
2021-01-26 21:06:53 +05:30
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.UpdateFileOptions)
|
2020-06-01 02:29:34 +05:30
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "RepoIsEmpty", fmt.Errorf("repo is empty"))
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2020-04-20 22:17:05 +05:30
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
opts := &repofiles.UpdateRepoFileOptions{
|
|
|
|
|
Content: apiOpts.Content,
|
|
|
|
|
SHA: apiOpts.SHA,
|
|
|
|
|
IsNewFile: false,
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
FromTreePath: apiOpts.FromPath,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
Committer: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Dates: &repofiles.CommitDateOptions{
|
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff: apiOpts.Signoff,
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:49:24 +05:30
|
|
|
|
if opts.Message == "" {
|
|
|
|
|
opts.Message = ctx.Tr("repo.editor.update", opts.TreePath)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
|
2020-06-01 02:29:34 +05:30
|
|
|
|
handleCreateOrUpdateFileError(ctx, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusOK, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 02:29:34 +05:30
|
|
|
|
func handleCreateOrUpdateFileError(ctx *context.APIContext, err error) {
|
|
|
|
|
if models.IsErrUserCannotCommit(err) || models.IsErrFilePathProtected(err) {
|
|
|
|
|
ctx.Error(http.StatusForbidden, "Access", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if models.IsErrBranchAlreadyExists(err) || models.IsErrFilenameInvalid(err) || models.IsErrSHADoesNotMatch(err) ||
|
|
|
|
|
models.IsErrFilePathInvalid(err) || models.IsErrRepoFileAlreadyExists(err) {
|
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Invalid", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-06-07 23:00:58 +05:30
|
|
|
|
if models.IsErrBranchDoesNotExist(err) || git.IsErrBranchNotExist(err) {
|
|
|
|
|
ctx.Error(http.StatusNotFound, "BranchDoesNotExist", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-06-01 02:29:34 +05:30
|
|
|
|
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// Called from both CreateFile or UpdateFile to handle both
|
|
|
|
|
func createOrUpdateFile(ctx *context.APIContext, opts *repofiles.UpdateRepoFileOptions) (*api.FileResponse, error) {
|
2020-04-24 21:50:22 +05:30
|
|
|
|
if !canWriteFiles(ctx.Repo) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return nil, models.ErrUserDoesNotHaveAccessToRepo{
|
|
|
|
|
UserID: ctx.User.ID,
|
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content, err := base64.StdEncoding.DecodeString(opts.Content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
opts.Content = string(content)
|
|
|
|
|
|
|
|
|
|
return repofiles.CreateOrUpdateRepoFile(ctx.Repo.Repository, ctx.User, opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteFile Delete a fle in a repository
|
2021-01-26 21:06:53 +05:30
|
|
|
|
func DeleteFile(ctx *context.APIContext) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/contents/{filepath} repository repoDeleteFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Delete a file in a repository
|
|
|
|
|
// consumes:
|
|
|
|
|
// - application/json
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to delete
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
2019-05-30 23:27:55 +05:30
|
|
|
|
// required: true
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/DeleteFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileDeleteResponse"
|
2020-04-15 10:48:51 +05:30
|
|
|
|
// "400":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/error"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.DeleteFileOptions)
|
2020-04-24 21:50:22 +05:30
|
|
|
|
if !canWriteFiles(ctx.Repo) {
|
2020-04-15 10:48:51 +05:30
|
|
|
|
ctx.Error(http.StatusForbidden, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
UserID: ctx.User.ID,
|
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 22:17:05 +05:30
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
opts := &repofiles.DeleteRepoFileOptions{
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
SHA: apiOpts.SHA,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
Committer: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Dates: &repofiles.CommitDateOptions{
|
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff: apiOpts.Signoff,
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:49:24 +05:30
|
|
|
|
if opts.Message == "" {
|
|
|
|
|
opts.Message = ctx.Tr("repo.editor.delete", opts.TreePath)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
|
2020-04-15 10:48:51 +05:30
|
|
|
|
if git.IsErrBranchNotExist(err) || models.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) {
|
|
|
|
|
ctx.Error(http.StatusNotFound, "DeleteFile", err)
|
|
|
|
|
return
|
|
|
|
|
} else if models.IsErrBranchAlreadyExists(err) ||
|
|
|
|
|
models.IsErrFilenameInvalid(err) ||
|
|
|
|
|
models.IsErrSHADoesNotMatch(err) ||
|
|
|
|
|
models.IsErrCommitIDDoesNotMatch(err) ||
|
|
|
|
|
models.IsErrSHAOrCommitIDNotProvided(err) {
|
|
|
|
|
ctx.Error(http.StatusBadRequest, "DeleteFile", err)
|
|
|
|
|
return
|
|
|
|
|
} else if models.IsErrUserCannotCommit(err) {
|
|
|
|
|
ctx.Error(http.StatusForbidden, "DeleteFile", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
|
|
|
|
|
} else {
|
2020-04-15 10:48:51 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, fileResponse) // FIXME on APIv2: return http.StatusNoContent
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
|
|
|
|
|
func GetContents(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// ---
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// summary: Gets the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: filepath
|
|
|
|
|
// in: path
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// description: path of the dir, file, symlink or submodule in the repo
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// required: false
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// "$ref": "#/responses/ContentsResponse"
|
2020-04-15 10:48:51 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2020-04-24 21:50:22 +05:30
|
|
|
|
if !canReadFiles(ctx.Repo) {
|
2019-06-30 02:21:10 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
UserID: ctx.User.ID,
|
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
treePath := ctx.Params("*")
|
|
|
|
|
ref := ctx.QueryTrim("ref")
|
|
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
|
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
|
2020-04-15 10:48:51 +05:30
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
|
ctx.NotFound("GetContentsOrList", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
} else {
|
2019-06-30 02:21:10 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, fileList)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2016-08-31 04:48:40 +05:30
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
|
|
|
|
|
|
// GetContentsList Get the metadata of all the entries of the root dir
|
|
|
|
|
func GetContentsList(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/contents repository repoGetContentsList
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Gets the metadata of all the entries of the root dir
|
|
|
|
|
// produces:
|
|
|
|
|
// - application/json
|
|
|
|
|
// parameters:
|
|
|
|
|
// - name: owner
|
|
|
|
|
// in: path
|
|
|
|
|
// description: owner of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: repo
|
|
|
|
|
// in: path
|
|
|
|
|
// description: name of the repo
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
|
|
|
|
// required: false
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/ContentsListResponse"
|
2020-04-15 10:48:51 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-06-30 02:21:10 +05:30
|
|
|
|
|
|
|
|
|
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
|
|
|
|
|
GetContents(ctx)
|
|
|
|
|
}
|