diff options
author | Lauris BH <lauris@nix.lv> | 2017-09-20 08:26:49 +0300 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-09-20 13:26:49 +0800 |
commit | 7a0297819d3aececb8b633de5a2995c40b7bfcae (patch) | |
tree | 7aed967ad21f95df2b16359ec1551662d653a1fd /routers | |
parent | 8b6236d67b21f2a3291207ef77d103a3ce7476ba (diff) | |
download | gitea-7a0297819d3aececb8b633de5a2995c40b7bfcae.tar.gz gitea-7a0297819d3aececb8b633de5a2995c40b7bfcae.zip |
Sync releases table with tags on push and for mirrors (#2459)
* Sync releases table with tags on push and for mirrors
* Code style fixes
* Fix api to return only releases
* Optimize release creation and update
Minimize posibility of race conditions
* Fix release lower tag name updating
* handle tag reference update by addionally comparing commit id
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/release.go | 89 | ||||
-rw-r--r-- | routers/repo/release.go | 98 |
2 files changed, 101 insertions, 86 deletions
diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 302be8dbab..78289a6a2a 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -5,8 +5,6 @@ package repo import ( - "strings" - api "code.gitea.io/sdk/gitea" "code.gitea.io/gitea/models" @@ -36,6 +34,7 @@ func GetRelease(ctx *context.APIContext) { func ListReleases(ctx *context.APIContext) { releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite, + IncludeTags: false, }, 1, 2147483647) if err != nil { ctx.Error(500, "GetReleasesByRepoID", err) @@ -62,43 +61,49 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) { ctx.Status(404) return } - tag, err := ctx.Repo.GitRepo.GetTag(form.TagName) - if err != nil { - ctx.Error(500, "GetTag", err) - return - } - commit, err := tag.Commit() - if err != nil { - ctx.Error(500, "Commit", err) - return - } - commitsCount, err := commit.CommitsCount() + rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName) if err != nil { - ctx.Error(500, "CommitsCount", err) - return - } - rel := &models.Release{ - RepoID: ctx.Repo.Repository.ID, - PublisherID: ctx.User.ID, - Publisher: ctx.User, - TagName: form.TagName, - LowerTagName: strings.ToLower(form.TagName), - Target: form.Target, - Title: form.Title, - Sha1: commit.ID.String(), - NumCommits: commitsCount, - Note: form.Note, - IsDraft: form.IsDraft, - IsPrerelease: form.IsPrerelease, - CreatedUnix: commit.Author.When.Unix(), - } - if err := models.CreateRelease(ctx.Repo.GitRepo, rel, nil); err != nil { - if models.IsErrReleaseAlreadyExist(err) { + if !models.IsErrReleaseNotExist(err) { + ctx.Handle(500, "GetRelease", err) + return + } + rel = &models.Release{ + RepoID: ctx.Repo.Repository.ID, + PublisherID: ctx.User.ID, + Publisher: ctx.User, + TagName: form.TagName, + Target: form.Target, + Title: form.Title, + Note: form.Note, + IsDraft: form.IsDraft, + IsPrerelease: form.IsPrerelease, + IsTag: false, + } + if err := models.CreateRelease(ctx.Repo.GitRepo, rel, nil); err != nil { + if models.IsErrReleaseAlreadyExist(err) { + ctx.Status(409) + } else { + ctx.Error(500, "CreateRelease", err) + } + return + } + } else { + if !rel.IsTag { ctx.Status(409) - } else { - ctx.Error(500, "CreateRelease", err) + return + } + + rel.Title = form.Title + rel.Note = form.Note + rel.IsDraft = form.IsDraft + rel.IsPrerelease = form.IsPrerelease + rel.PublisherID = ctx.User.ID + rel.IsTag = false + + if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, nil); err != nil { + ctx.Handle(500, "UpdateRelease", err) + return } - return } ctx.JSON(201, rel.APIFormat()) } @@ -111,11 +116,12 @@ func EditRelease(ctx *context.APIContext, form api.EditReleaseOption) { } id := ctx.ParamsInt64(":id") rel, err := models.GetReleaseByID(id) - if err != nil { + if err != nil && !models.IsErrReleaseNotExist(err) { ctx.Error(500, "GetReleaseByID", err) return } - if rel.RepoID != ctx.Repo.Repository.ID { + if err != nil && models.IsErrReleaseNotExist(err) || + rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID { ctx.Status(404) return } @@ -162,12 +168,13 @@ func DeleteRelease(ctx *context.APIContext) { return } id := ctx.ParamsInt64(":id") - release, err := models.GetReleaseByID(id) - if err != nil { + rel, err := models.GetReleaseByID(id) + if err != nil && !models.IsErrReleaseNotExist(err) { ctx.Error(500, "GetReleaseByID", err) return } - if release.RepoID != ctx.Repo.Repository.ID { + if err != nil && models.IsErrReleaseNotExist(err) || + rel.IsTag || rel.RepoID != ctx.Repo.Repository.ID { ctx.Status(404) return } diff --git a/routers/repo/release.go b/routers/repo/release.go index d9cc967801..fe68f1b6f1 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -67,6 +67,7 @@ func Releases(ctx *context.Context) { opts := models.FindReleasesOptions{ IncludeDrafts: ctx.Repo.IsWriter(), + IncludeTags: true, } releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts, page, limit) @@ -145,57 +146,61 @@ func NewReleasePost(ctx *context.Context, form auth.NewReleaseForm) { return } - var tagCreatedUnix int64 - tag, err := ctx.Repo.GitRepo.GetTag(form.TagName) - if err == nil { - commit, err := tag.Commit() - if err == nil { - tagCreatedUnix = commit.Author.When.Unix() - } + var attachmentUUIDs []string + if setting.AttachmentEnabled { + attachmentUUIDs = form.Files } - commit, err := ctx.Repo.GitRepo.GetBranchCommit(form.Target) + rel, err := models.GetRelease(ctx.Repo.Repository.ID, form.TagName) if err != nil { - ctx.Handle(500, "GetBranchCommit", err) - return - } + if !models.IsErrReleaseNotExist(err) { + ctx.Handle(500, "GetRelease", err) + return + } - commitsCount, err := commit.CommitsCount() - if err != nil { - ctx.Handle(500, "CommitsCount", err) - return - } + rel := &models.Release{ + RepoID: ctx.Repo.Repository.ID, + PublisherID: ctx.User.ID, + Title: form.Title, + TagName: form.TagName, + Target: form.Target, + Note: form.Content, + IsDraft: len(form.Draft) > 0, + IsPrerelease: form.Prerelease, + IsTag: false, + } - rel := &models.Release{ - RepoID: ctx.Repo.Repository.ID, - PublisherID: ctx.User.ID, - Title: form.Title, - TagName: form.TagName, - Target: form.Target, - Sha1: commit.ID.String(), - NumCommits: commitsCount, - Note: form.Content, - IsDraft: len(form.Draft) > 0, - IsPrerelease: form.Prerelease, - CreatedUnix: tagCreatedUnix, - } + if err = models.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil { + ctx.Data["Err_TagName"] = true + switch { + case models.IsErrReleaseAlreadyExist(err): + ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form) + case models.IsErrInvalidTagName(err): + ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form) + default: + ctx.Handle(500, "CreateRelease", err) + } + return + } + } else { + if !rel.IsTag { + ctx.Data["Err_TagName"] = true + ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form) + return + } - var attachmentUUIDs []string - if setting.AttachmentEnabled { - attachmentUUIDs = form.Files - } + rel.Title = form.Title + rel.Note = form.Content + rel.IsDraft = len(form.Draft) > 0 + rel.IsPrerelease = form.Prerelease + rel.PublisherID = ctx.User.ID + rel.IsTag = false - if err = models.CreateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil { - ctx.Data["Err_TagName"] = true - switch { - case models.IsErrReleaseAlreadyExist(err): - ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_already_exist"), tplReleaseNew, &form) - case models.IsErrInvalidTagName(err): - ctx.RenderWithErr(ctx.Tr("repo.release.tag_name_invalid"), tplReleaseNew, &form) - default: - ctx.Handle(500, "CreateRelease", err) + if err = models.UpdateRelease(ctx.Repo.GitRepo, rel, attachmentUUIDs); err != nil { + ctx.Data["Err_TagName"] = true + ctx.Handle(500, "UpdateRelease", err) + return } - return } log.Trace("Release created: %s/%s:%s", ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName) @@ -246,6 +251,10 @@ func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) { } return } + if rel.IsTag { + ctx.Handle(404, "GetRelease", err) + return + } ctx.Data["tag_name"] = rel.TagName ctx.Data["tag_target"] = rel.Target ctx.Data["title"] = rel.Title @@ -275,8 +284,7 @@ func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) { // DeleteRelease delete a release func DeleteRelease(ctx *context.Context) { - delTag := ctx.QueryBool("delTag") - if err := models.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, delTag); err != nil { + if err := models.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil { ctx.Flash.Error("DeleteReleaseByID: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("repo.release.deletion_success")) |