|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package repo |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models" |
| 12 | + "code.gitea.io/gitea/modules/context" |
| 13 | + "code.gitea.io/gitea/modules/git" |
| 14 | + api "code.gitea.io/gitea/modules/structs" |
| 15 | + "code.gitea.io/gitea/modules/web" |
| 16 | + "code.gitea.io/gitea/services/repository/files" |
| 17 | +) |
| 18 | + |
| 19 | +// ApplyDiffPatch handles API call for applying a patch |
| 20 | +func ApplyDiffPatch(ctx *context.APIContext) { |
| 21 | + // swagger:operation POST /repos/{owner}/{repo}/diffpatch repository repoApplyDiffPatch |
| 22 | + // --- |
| 23 | + // summary: Apply diff patch to repository |
| 24 | + // consumes: |
| 25 | + // - application/json |
| 26 | + // produces: |
| 27 | + // - application/json |
| 28 | + // parameters: |
| 29 | + // - name: owner |
| 30 | + // in: path |
| 31 | + // description: owner of the repo |
| 32 | + // type: string |
| 33 | + // required: true |
| 34 | + // - name: repo |
| 35 | + // in: path |
| 36 | + // description: name of the repo |
| 37 | + // type: string |
| 38 | + // required: true |
| 39 | + // - name: body |
| 40 | + // in: body |
| 41 | + // required: true |
| 42 | + // schema: |
| 43 | + // "$ref": "#/definitions/UpdateFileOptions" |
| 44 | + // responses: |
| 45 | + // "200": |
| 46 | + // "$ref": "#/responses/FileResponse" |
| 47 | + apiOpts := web.GetForm(ctx).(*api.ApplyDiffPatchFileOptions) |
| 48 | + |
| 49 | + opts := &files.ApplyDiffPatchOptions{ |
| 50 | + Content: apiOpts.Content, |
| 51 | + SHA: apiOpts.SHA, |
| 52 | + Message: apiOpts.Message, |
| 53 | + OldBranch: apiOpts.BranchName, |
| 54 | + NewBranch: apiOpts.NewBranchName, |
| 55 | + Committer: &files.IdentityOptions{ |
| 56 | + Name: apiOpts.Committer.Name, |
| 57 | + Email: apiOpts.Committer.Email, |
| 58 | + }, |
| 59 | + Author: &files.IdentityOptions{ |
| 60 | + Name: apiOpts.Author.Name, |
| 61 | + Email: apiOpts.Author.Email, |
| 62 | + }, |
| 63 | + Dates: &files.CommitDateOptions{ |
| 64 | + Author: apiOpts.Dates.Author, |
| 65 | + Committer: apiOpts.Dates.Committer, |
| 66 | + }, |
| 67 | + Signoff: apiOpts.Signoff, |
| 68 | + } |
| 69 | + if opts.Dates.Author.IsZero() { |
| 70 | + opts.Dates.Author = time.Now() |
| 71 | + } |
| 72 | + if opts.Dates.Committer.IsZero() { |
| 73 | + opts.Dates.Committer = time.Now() |
| 74 | + } |
| 75 | + |
| 76 | + if opts.Message == "" { |
| 77 | + opts.Message = "apply-patch" |
| 78 | + } |
| 79 | + |
| 80 | + if !canWriteFiles(ctx.Repo) { |
| 81 | + ctx.Error(http.StatusInternalServerError, "ApplyPatch", models.ErrUserDoesNotHaveAccessToRepo{ |
| 82 | + UserID: ctx.User.ID, |
| 83 | + RepoName: ctx.Repo.Repository.LowerName, |
| 84 | + }) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + fileResponse, err := files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.User, opts) |
| 89 | + if err != nil { |
| 90 | + if models.IsErrUserCannotCommit(err) || models.IsErrFilePathProtected(err) { |
| 91 | + ctx.Error(http.StatusForbidden, "Access", err) |
| 92 | + return |
| 93 | + } |
| 94 | + if models.IsErrBranchAlreadyExists(err) || models.IsErrFilenameInvalid(err) || models.IsErrSHADoesNotMatch(err) || |
| 95 | + models.IsErrFilePathInvalid(err) || models.IsErrRepoFileAlreadyExists(err) { |
| 96 | + ctx.Error(http.StatusUnprocessableEntity, "Invalid", err) |
| 97 | + return |
| 98 | + } |
| 99 | + if models.IsErrBranchDoesNotExist(err) || git.IsErrBranchNotExist(err) { |
| 100 | + ctx.Error(http.StatusNotFound, "BranchDoesNotExist", err) |
| 101 | + return |
| 102 | + } |
| 103 | + ctx.Error(http.StatusInternalServerError, "ApplyPatch", err) |
| 104 | + } else { |
| 105 | + ctx.JSON(http.StatusCreated, fileResponse) |
| 106 | + } |
| 107 | +} |
0 commit comments