summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/repo/file.go
diff options
context:
space:
mode:
Diffstat (limited to 'routers/api/v1/repo/file.go')
-rw-r--r--routers/api/v1/repo/file.go274
1 files changed, 269 insertions, 5 deletions
diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go
index 3ce80d24f0..10108e11c3 100644
--- a/routers/api/v1/repo/file.go
+++ b/routers/api/v1/repo/file.go
@@ -6,10 +6,15 @@
package repo
import (
+ "encoding/base64"
+ "net/http"
+
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/repofiles"
"code.gitea.io/gitea/routers/repo"
+ api "code.gitea.io/sdk/gitea"
)
// GetRawFile get a file by path on a repository
@@ -48,12 +53,12 @@ func GetRawFile(ctx *context.APIContext) {
if git.IsErrNotExist(err) {
ctx.NotFound()
} else {
- ctx.Error(500, "GetBlobByPath", err)
+ ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
}
return
}
if err = repo.ServeBlob(ctx.Context, blob); err != nil {
- ctx.Error(500, "ServeBlob", err)
+ ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
}
}
@@ -86,7 +91,7 @@ func GetArchive(ctx *context.APIContext) {
repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
- ctx.Error(500, "OpenRepository", err)
+ ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return
}
ctx.Repo.GitRepo = gitRepo
@@ -125,7 +130,7 @@ func GetEditorconfig(ctx *context.APIContext) {
if git.IsErrNotExist(err) {
ctx.NotFound(err)
} else {
- ctx.Error(500, "GetEditorconfig", err)
+ ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
}
return
}
@@ -136,5 +141,264 @@ func GetEditorconfig(ctx *context.APIContext) {
ctx.NotFound(err)
return
}
- ctx.JSON(200, def)
+ ctx.JSON(http.StatusOK, def)
+}
+
+// CanWriteFiles returns true if repository is editable and user has proper access level.
+func CanWriteFiles(r *context.Repository) bool {
+ return r.Permission.CanWrite(models.UnitTypeCode) && !r.Repository.IsMirror && !r.Repository.IsArchived
+}
+
+// CanReadFiles returns true if repository is readable and user has proper access level.
+func CanReadFiles(r *context.Repository) bool {
+ return r.Permission.CanRead(models.UnitTypeCode)
+}
+
+// CreateFile handles API call for creating a file
+func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
+ // 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
+ // description: "'content' must be base64 encoded\n\n 'author' and 'committer' are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)\n\n If 'branch' is not given, default branch will be used\n\n 'sha' is the SHA for the file that already exists\n\n 'new_branch' (optional) will make a new branch from 'branch' before creating the file"
+ // schema:
+ // "$ref": "#/definitions/CreateFileOptions"
+ // responses:
+ // "201":
+ // "$ref": "#/responses/FileResponse"
+
+ 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,
+ },
+ }
+ if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
+ ctx.Error(http.StatusInternalServerError, "CreateFile", err)
+ } else {
+ ctx.JSON(http.StatusCreated, fileResponse)
+ }
+}
+
+// UpdateFile handles API call for updating a file
+func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
+ // 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
+ // description: "'content' must be base64 encoded\n\n 'sha' is the SHA for the file that already exists\n\n 'author' and 'committer' are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)\n\n If 'branch' is not given, default branch will be used\n\n 'new_branch' (optional) will make a new branch from 'branch' before updating the file"
+ // schema:
+ // "$ref": "#/definitions/UpdateFileOptions"
+ // responses:
+ // "200":
+ // "$ref": "#/responses/FileResponse"
+
+ 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,
+ },
+ }
+
+ if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
+ ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
+ } else {
+ ctx.JSON(http.StatusOK, fileResponse)
+ }
+}
+
+// Called from both CreateFile or UpdateFile to handle both
+func createOrUpdateFile(ctx *context.APIContext, opts *repofiles.UpdateRepoFileOptions) (*api.FileResponse, error) {
+ if !CanWriteFiles(ctx.Repo) {
+ 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
+func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
+ // 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
+ // description: "'sha' is the SHA for the file to be deleted\n\n 'author' and 'committer' are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)\n\n If 'branch' is not given, default branch will be used\n\n 'new_branch' (optional) will make a new branch from 'branch' before deleting the file"
+ // schema:
+ // "$ref": "#/definitions/DeleteFileOptions"
+ // responses:
+ // "200":
+ // "$ref": "#/responses/FileDeleteResponse"
+ if !CanWriteFiles(ctx.Repo) {
+ ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
+ UserID: ctx.User.ID,
+ RepoName: ctx.Repo.Repository.LowerName,
+ })
+ return
+ }
+
+ 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,
+ },
+ }
+
+ if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
+ ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
+ } else {
+ ctx.JSON(http.StatusOK, fileResponse)
+ }
+}
+
+// GetFileContents Get the contents of a fle in a repository
+func GetFileContents(ctx *context.APIContext) {
+ // swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetFileContents
+ // ---
+ // summary: Gets the contents of a file or directory 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: path of the file to delete
+ // type: string
+ // required: true
+ // - name: ref
+ // in: query
+ // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
+ // required: false
+ // type: string
+ // responses:
+ // "200":
+ // "$ref": "#/responses/FileContentResponse"
+
+ if !CanReadFiles(ctx.Repo) {
+ ctx.Error(http.StatusInternalServerError, "GetFileContents", models.ErrUserDoesNotHaveAccessToRepo{
+ UserID: ctx.User.ID,
+ RepoName: ctx.Repo.Repository.LowerName,
+ })
+ return
+ }
+
+ treePath := ctx.Params("*")
+ ref := ctx.QueryTrim("ref")
+
+ if fileContents, err := repofiles.GetFileContents(ctx.Repo.Repository, treePath, ref); err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetFileContents", err)
+ } else {
+ ctx.JSON(http.StatusOK, fileContents)
+ }
}