diff options
Diffstat (limited to 'services/release')
-rw-r--r-- | services/release/release.go | 68 | ||||
-rw-r--r-- | services/release/release_test.go | 2 |
2 files changed, 69 insertions, 1 deletions
diff --git a/services/release/release.go b/services/release/release.go index 246a784efe..b4f814a77f 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 91dd0a73b5..97febeecb9 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) |