diff --git a/models/attachment.go b/models/attachment.go index 7fbf9dde992..a9032f1a86a 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -255,3 +255,9 @@ func updateAttachment(e Engine, atta *Attachment) error { _, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta) return err } + +// DeleteAttachmentsByRelease deletes all attachments associated with the given release. +func DeleteAttachmentsByRelease(releaseID int64) error { + _, err := x.Where("release_id = ?", releaseID).Delete(&Attachment{}) + return err +} diff --git a/models/release.go b/models/release.go index 85a758becfc..3729a6f6909 100644 --- a/models/release.go +++ b/models/release.go @@ -7,13 +7,10 @@ package models import ( "fmt" - "os" "sort" "strings" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -319,70 +316,10 @@ func SortReleases(rels []*Release) { sort.Sort(sorter) } -// DeleteReleaseByID deletes a release and corresponding Git tag by given ID. -func DeleteReleaseByID(id int64, doer *User, delTag bool) error { - rel, err := GetReleaseByID(id) - if err != nil { - return fmt.Errorf("GetReleaseByID: %v", err) - } - - repo, err := GetRepositoryByID(rel.RepoID) - if err != nil { - return fmt.Errorf("GetRepositoryByID: %v", err) - } - - if delTag { - _, stderr, err := process.GetManager().ExecDir(-1, repo.RepoPath(), - fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID), - git.GitExecutable, "tag", "-d", rel.TagName) - if err != nil && !strings.Contains(stderr, "not found") { - return fmt.Errorf("git tag -d: %v - %s", err, stderr) - } - - if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil { - return fmt.Errorf("Delete: %v", err) - } - } else { - rel.IsTag = true - rel.IsDraft = false - rel.IsPrerelease = false - rel.Title = "" - rel.Note = "" - - if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil { - return fmt.Errorf("Update: %v", err) - } - } - - rel.Repo = repo - if err = rel.LoadAttributes(); err != nil { - return fmt.Errorf("LoadAttributes: %v", err) - } - - if _, err := x.Delete(&Attachment{ReleaseID: id}); err != nil { - return err - } - - for i := range rel.Attachments { - attachment := rel.Attachments[i] - if err := os.RemoveAll(attachment.LocalPath()); err != nil { - return err - } - } - - mode, _ := AccessLevel(doer, rel.Repo) - if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{ - Action: api.HookReleaseDeleted, - Release: rel.APIFormat(), - Repository: rel.Repo.APIFormat(mode), - Sender: doer.APIFormat(), - }); err != nil { - log.Error("PrepareWebhooks: %v", err) - } else { - go HookQueue.Add(rel.Repo.ID) - } - - return nil +// DeleteReleaseByID deletes a release from database by given ID. +func DeleteReleaseByID(id int64) error { + _, err := x.ID(id).Delete(new(Release)) + return err } // SyncReleasesWithTags synchronizes release table with repository tags diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index da32b955fb8..8bcebf6b409 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -316,7 +316,7 @@ func DeleteRelease(ctx *context.APIContext) { ctx.NotFound() return } - if err := models.DeleteReleaseByID(id, ctx.User, false); err != nil { + if err := releaseservice.DeleteReleaseByID(id, ctx.User, false); err != nil { ctx.Error(500, "DeleteReleaseByID", err) return } diff --git a/routers/repo/release.go b/routers/repo/release.go index 95423a92b29..0ab8450bd55 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -291,7 +291,7 @@ func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) { // DeleteRelease delete a release func DeleteRelease(ctx *context.Context) { - if err := models.DeleteReleaseByID(ctx.QueryInt64("id"), ctx.User, true); err != nil { + if err := releaseservice.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")) diff --git a/services/release/release.go b/services/release/release.go index 246a784efe6..b4f814a77f3 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -6,11 +6,13 @@ package release import ( "fmt" + "os" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/process" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" ) @@ -132,3 +134,69 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea return err } + +// DeleteReleaseByID deletes a release and corresponding Git tag by given ID. +func DeleteReleaseByID(id int64, doer *models.User, delTag bool) error { + rel, err := models.GetReleaseByID(id) + if err != nil { + return fmt.Errorf("GetReleaseByID: %v", err) + } + + repo, err := models.GetRepositoryByID(rel.RepoID) + if err != nil { + return fmt.Errorf("GetRepositoryByID: %v", err) + } + + if delTag { + _, stderr, err := process.GetManager().ExecDir(-1, repo.RepoPath(), + fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID), + git.GitExecutable, "tag", "-d", rel.TagName) + if err != nil && !strings.Contains(stderr, "not found") { + return fmt.Errorf("git tag -d: %v - %s", err, stderr) + } + + if err := models.DeleteReleaseByID(id); err != nil { + return fmt.Errorf("DeleteReleaseByID: %v", err) + } + } else { + rel.IsTag = true + rel.IsDraft = false + rel.IsPrerelease = false + rel.Title = "" + rel.Note = "" + + if err = models.UpdateRelease(rel); err != nil { + return fmt.Errorf("Update: %v", err) + } + } + + rel.Repo = repo + if err = rel.LoadAttributes(); err != nil { + return fmt.Errorf("LoadAttributes: %v", err) + } + + if err := models.DeleteAttachmentsByRelease(rel.ID); err != nil { + return fmt.Errorf("DeleteAttachments: %v", err) + } + + for i := range rel.Attachments { + attachment := rel.Attachments[i] + if err := os.RemoveAll(attachment.LocalPath()); err != nil { + log.Error("Delete attachment %s of release %s failed: %v", attachment.UUID, rel.ID, err) + } + } + + mode, _ := models.AccessLevel(doer, rel.Repo) + if err := models.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{ + Action: api.HookReleaseDeleted, + Release: rel.APIFormat(), + Repository: rel.Repo.APIFormat(mode), + Sender: doer.APIFormat(), + }); err != nil { + log.Error("PrepareWebhooks: %v", err) + } else { + go models.HookQueue.Add(rel.Repo.ID) + } + + return nil +} diff --git a/services/release/release_test.go b/services/release/release_test.go index 91dd0a73b5a..97febeecb97 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -150,7 +150,7 @@ func TestRelease_MirrorDelete(t *testing.T) { release, err := models.GetRelease(repo.ID, "v0.2") assert.NoError(t, err) - assert.NoError(t, models.DeleteReleaseByID(release.ID, user, true)) + assert.NoError(t, DeleteReleaseByID(release.ID, user, true)) ok = models.RunMirrorSync(mirror.Mirror) assert.True(t, ok)