Extract actions on deletereleasebyid from models to release service (#8219)
* extract actions on deletereleasebyid from models to release service * fix tests
This commit is contained in:
parent
ecd4bfd284
commit
3249c0ccba
6 changed files with 81 additions and 70 deletions
|
@ -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)
|
_, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -7,13 +7,10 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/log"
|
|
||||||
"code.gitea.io/gitea/modules/process"
|
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
|
@ -319,72 +316,12 @@ func SortReleases(rels []*Release) {
|
||||||
sort.Sort(sorter)
|
sort.Sort(sorter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
|
// DeleteReleaseByID deletes a release from database by given ID.
|
||||||
func DeleteReleaseByID(id int64, doer *User, delTag bool) error {
|
func DeleteReleaseByID(id int64) error {
|
||||||
rel, err := GetReleaseByID(id)
|
_, err := x.ID(id).Delete(new(Release))
|
||||||
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
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncReleasesWithTags synchronizes release table with repository tags
|
// SyncReleasesWithTags synchronizes release table with repository tags
|
||||||
func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
|
func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
|
||||||
existingRelTags := make(map[string]struct{})
|
existingRelTags := make(map[string]struct{})
|
||||||
|
|
|
@ -316,7 +316,7 @@ func DeleteRelease(ctx *context.APIContext) {
|
||||||
ctx.NotFound()
|
ctx.NotFound()
|
||||||
return
|
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)
|
ctx.Error(500, "DeleteReleaseByID", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -291,7 +291,7 @@ func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) {
|
||||||
|
|
||||||
// DeleteRelease delete a release
|
// DeleteRelease delete a release
|
||||||
func DeleteRelease(ctx *context.Context) {
|
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())
|
ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
|
ctx.Flash.Success(ctx.Tr("repo.release.deletion_success"))
|
||||||
|
|
|
@ -6,11 +6,13 @@ package release
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models"
|
"code.gitea.io/gitea/models"
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/process"
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
)
|
)
|
||||||
|
@ -132,3 +134,69 @@ func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Relea
|
||||||
|
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -150,7 +150,7 @@ func TestRelease_MirrorDelete(t *testing.T) {
|
||||||
|
|
||||||
release, err := models.GetRelease(repo.ID, "v0.2")
|
release, err := models.GetRelease(repo.ID, "v0.2")
|
||||||
assert.NoError(t, err)
|
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)
|
ok = models.RunMirrorSync(mirror.Mirror)
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
|
|
Loading…
Reference in a new issue