diff options
author | 6543 <6543@obermui.de> | 2021-06-17 18:04:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-17 18:04:10 +0200 |
commit | f7cd394680f885061144d236abc3c25f30be3147 (patch) | |
tree | b0cceab982c18db7fb48489729dc44fdff8101af /routers | |
parent | 19dedc3fa521fdb6456bad2b080bb1aaa6722b24 (diff) | |
download | gitea-f7cd394680f885061144d236abc3c25f30be3147.tar.gz gitea-f7cd394680f885061144d236abc3c25f30be3147.zip |
[API] Add repoCreateTag (#16165)
* Add API CreateTag
* Add Test
* API: expose Tag Message
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 1 | ||||
-rw-r--r-- | routers/api/v1/repo/tag.go | 61 | ||||
-rw-r--r-- | routers/api/v1/swagger/options.go | 3 |
3 files changed, 65 insertions, 0 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0b47953e58..34cf80e072 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -775,6 +775,7 @@ func Routes() *web.Route { }, reqToken(), reqAdmin()) m.Group("/tags", func() { m.Get("", repo.ListTags) + m.Post("", reqRepoWriter(models.UnitTypeCode), bind(api.CreateTagOption{}), repo.CreateTag) m.Delete("/{tag}", repo.DeleteTag) }, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo(true)) m.Group("/keys", func() { diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index ec9b541bd4..51ba43ea89 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -6,12 +6,14 @@ package repo import ( "errors" + "fmt" "net/http" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" releaseservice "code.gitea.io/gitea/services/release" ) @@ -160,3 +162,62 @@ func DeleteTag(ctx *context.APIContext) { ctx.Status(http.StatusNoContent) } + +// CreateTag create a new git tag in a repository +func CreateTag(ctx *context.APIContext) { + // swagger:operation POST /repos/{owner}/{repo}/tags repository repoCreateTag + // --- + // summary: Create a new git tag 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: body + // in: body + // schema: + // "$ref": "#/definitions/CreateTagOption" + // responses: + // "200": + // "$ref": "#/responses/AnnotatedTag" + // "404": + // "$ref": "#/responses/notFound" + // "409": + // "$ref": "#/responses/conflict" + form := web.GetForm(ctx).(*api.CreateTagOption) + + // If target is not provided use default branch + if len(form.Target) == 0 { + form.Target = ctx.Repo.Repository.DefaultBranch + } + + commit, err := ctx.Repo.GitRepo.GetCommit(form.Target) + if err != nil { + ctx.Error(http.StatusNotFound, "target not found", fmt.Errorf("target not found: %v", err)) + return + } + + if err := releaseservice.CreateNewTag(ctx.User, ctx.Repo.Repository, commit.ID.String(), form.TagName, form.Message); err != nil { + if models.IsErrTagAlreadyExists(err) { + ctx.Error(http.StatusConflict, "tag exist", err) + return + } + ctx.InternalServerError(err) + return + } + + tag, err := ctx.Repo.GitRepo.GetTag(form.TagName) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.JSON(http.StatusCreated, convert.ToTag(ctx.Repo.Repository, tag)) +} diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go index dad025710d..11158fb86d 100644 --- a/routers/api/v1/swagger/options.go +++ b/routers/api/v1/swagger/options.go @@ -158,4 +158,7 @@ type swaggerParameterBodies struct { // in:body PullReviewRequestOptions api.PullReviewRequestOptions + + // in:body + CreateTagOption api.CreateTagOption } |