Skip to content

Commit cead819

Browse files
kdumontnulunny
andauthored
Implement delete release attachments and update release attachments' name (#14130) (#15666)
* Implement delete release attachment * Add attachments on release edit page * Fix bug * Finish del release attachments * Fix frontend lint * Fix tests * Support edit release attachments * Added tests * Remove the unnecessary parameter isCreate from UpdateReleaseOrCreatReleaseFromTag * Rename UpdateReleaseOrCreatReleaseFromTag to UpdateRelease * Fix middle align Co-authored-by: Lunny Xiao <[email protected]>
1 parent 4fa2804 commit cead819

File tree

11 files changed

+276
-56
lines changed

11 files changed

+276
-56
lines changed

models/attachment.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
125125
}
126126

127127
// GetAttachmentsByUUIDs returns attachment by given UUID list.
128-
func GetAttachmentsByUUIDs(uuids []string) ([]*Attachment, error) {
129-
return getAttachmentsByUUIDs(x, uuids)
128+
func GetAttachmentsByUUIDs(ctx DBContext, uuids []string) ([]*Attachment, error) {
129+
return getAttachmentsByUUIDs(ctx.e, uuids)
130130
}
131131

132132
func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
@@ -183,12 +183,12 @@ func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string
183183

184184
// DeleteAttachment deletes the given attachment and optionally the associated file.
185185
func DeleteAttachment(a *Attachment, remove bool) error {
186-
_, err := DeleteAttachments([]*Attachment{a}, remove)
186+
_, err := DeleteAttachments(DefaultDBContext(), []*Attachment{a}, remove)
187187
return err
188188
}
189189

190190
// DeleteAttachments deletes the given attachments and optionally the associated files.
191-
func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
191+
func DeleteAttachments(ctx DBContext, attachments []*Attachment, remove bool) (int, error) {
192192
if len(attachments) == 0 {
193193
return 0, nil
194194
}
@@ -198,7 +198,7 @@ func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
198198
ids = append(ids, a.ID)
199199
}
200200

201-
cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
201+
cnt, err := ctx.e.In("id", ids).NoAutoCondition().Delete(attachments[0])
202202
if err != nil {
203203
return 0, err
204204
}
@@ -220,7 +220,7 @@ func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
220220
return 0, err
221221
}
222222

223-
return DeleteAttachments(attachments, remove)
223+
return DeleteAttachments(DefaultDBContext(), attachments, remove)
224224
}
225225

226226
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
@@ -230,14 +230,23 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
230230
return 0, err
231231
}
232232

233-
return DeleteAttachments(attachments, remove)
233+
return DeleteAttachments(DefaultDBContext(), attachments, remove)
234234
}
235235

236236
// UpdateAttachment updates the given attachment in database
237237
func UpdateAttachment(atta *Attachment) error {
238238
return updateAttachment(x, atta)
239239
}
240240

241+
// UpdateAttachmentByUUID Updates attachment via uuid
242+
func UpdateAttachmentByUUID(ctx DBContext, attach *Attachment, cols ...string) error {
243+
if attach.UUID == "" {
244+
return fmt.Errorf("Attachement uuid should not blank")
245+
}
246+
_, err := ctx.e.Where("uuid=?", attach.UUID).Cols(cols...).Update(attach)
247+
return err
248+
}
249+
241250
func updateAttachment(e Engine, atta *Attachment) error {
242251
var sess *xorm.Session
243252
if atta.ID != 0 && atta.UUID == "" {

models/attachment_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestUpdateAttachment(t *testing.T) {
120120
func TestGetAttachmentsByUUIDs(t *testing.T) {
121121
assert.NoError(t, PrepareTestDatabase())
122122

123-
attachList, err := GetAttachmentsByUUIDs([]string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
123+
attachList, err := GetAttachmentsByUUIDs(DefaultDBContext(), []string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
124124
assert.NoError(t, err)
125125
assert.Equal(t, 2, len(attachList))
126126
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)

models/release.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package models
77

88
import (
9+
"errors"
910
"fmt"
1011
"sort"
1112
"strings"
@@ -117,17 +118,20 @@ func UpdateRelease(ctx DBContext, rel *Release) error {
117118
}
118119

119120
// AddReleaseAttachments adds a release attachments
120-
func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
121+
func AddReleaseAttachments(ctx DBContext, releaseID int64, attachmentUUIDs []string) (err error) {
121122
// Check attachments
122-
attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
123+
attachments, err := getAttachmentsByUUIDs(ctx.e, attachmentUUIDs)
123124
if err != nil {
124125
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
125126
}
126127

127128
for i := range attachments {
129+
if attachments[i].ReleaseID != 0 {
130+
return errors.New("release permission denied")
131+
}
128132
attachments[i].ReleaseID = releaseID
129133
// No assign value could be 0, so ignore AllCols().
130-
if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
134+
if _, err = ctx.e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
131135
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
132136
}
133137
}

routers/api/v1/repo/release.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ func CreateRelease(ctx *context.APIContext) {
202202
rel.Repo = ctx.Repo.Repository
203203
rel.Publisher = ctx.User
204204

205-
if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, nil, true); err != nil {
206-
ctx.Error(http.StatusInternalServerError, "UpdateReleaseOrCreatReleaseFromTag", err)
205+
if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil {
206+
ctx.Error(http.StatusInternalServerError, "UpdateRelease", err)
207207
return
208208
}
209209
}
@@ -277,8 +277,8 @@ func EditRelease(ctx *context.APIContext) {
277277
if form.IsPrerelease != nil {
278278
rel.IsPrerelease = *form.IsPrerelease
279279
}
280-
if err := releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, nil, false); err != nil {
281-
ctx.Error(http.StatusInternalServerError, "UpdateReleaseOrCreatReleaseFromTag", err)
280+
if err := releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, nil, nil, nil); err != nil {
281+
ctx.Error(http.StatusInternalServerError, "UpdateRelease", err)
282282
return
283283
}
284284

routers/repo/release.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77

88
import (
99
"fmt"
10+
"strings"
1011

1112
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/base"
@@ -206,7 +207,7 @@ func LatestRelease(ctx *context.Context) {
206207
ctx.Redirect(release.HTMLURL())
207208
}
208209

209-
// NewRelease render creating release page
210+
// NewRelease render creating or edit release page
210211
func NewRelease(ctx *context.Context) {
211212
ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
212213
ctx.Data["PageIsReleaseList"] = true
@@ -221,10 +222,17 @@ func NewRelease(ctx *context.Context) {
221222
}
222223

223224
if rel != nil {
225+
rel.Repo = ctx.Repo.Repository
226+
if err := rel.LoadAttributes(); err != nil {
227+
ctx.ServerError("LoadAttributes", err)
228+
return
229+
}
230+
224231
ctx.Data["tag_name"] = rel.TagName
225232
ctx.Data["tag_target"] = rel.Target
226233
ctx.Data["title"] = rel.Title
227234
ctx.Data["content"] = rel.Note
235+
ctx.Data["attachments"] = rel.Attachments
228236
}
229237
}
230238
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
@@ -324,9 +332,9 @@ func NewReleasePost(ctx *context.Context) {
324332
rel.PublisherID = ctx.User.ID
325333
rel.IsTag = false
326334

327-
if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, true); err != nil {
335+
if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, nil, nil); err != nil {
328336
ctx.Data["Err_TagName"] = true
329-
ctx.ServerError("UpdateReleaseOrCreatReleaseFromTag", err)
337+
ctx.ServerError("UpdateRelease", err)
330338
return
331339
}
332340
}
@@ -363,6 +371,13 @@ func EditRelease(ctx *context.Context) {
363371
ctx.Data["prerelease"] = rel.IsPrerelease
364372
ctx.Data["IsDraft"] = rel.IsDraft
365373

374+
rel.Repo = ctx.Repo.Repository
375+
if err := rel.LoadAttributes(); err != nil {
376+
ctx.ServerError("LoadAttributes", err)
377+
return
378+
}
379+
ctx.Data["attachments"] = rel.Attachments
380+
366381
ctx.HTML(200, tplReleaseNew)
367382
}
368383

@@ -400,16 +415,27 @@ func EditReleasePost(ctx *context.Context) {
400415
return
401416
}
402417

403-
var attachmentUUIDs []string
418+
const delPrefix = "attachment-del-"
419+
const editPrefix = "attachment-edit-"
420+
var addAttachmentUUIDs, delAttachmentUUIDs []string
421+
var editAttachments = make(map[string]string) // uuid -> new name
404422
if setting.Attachment.Enabled {
405-
attachmentUUIDs = form.Files
423+
addAttachmentUUIDs = form.Files
424+
for k, v := range ctx.Req.Form {
425+
if strings.HasPrefix(k, delPrefix) && v[0] == "true" {
426+
delAttachmentUUIDs = append(delAttachmentUUIDs, k[len(delPrefix):])
427+
} else if strings.HasPrefix(k, editPrefix) {
428+
editAttachments[k[len(editPrefix):]] = v[0]
429+
}
430+
}
406431
}
407432

408433
rel.Title = form.Title
409434
rel.Note = form.Content
410435
rel.IsDraft = len(form.Draft) > 0
411436
rel.IsPrerelease = form.Prerelease
412-
if err = releaseservice.UpdateReleaseOrCreatReleaseFromTag(ctx.User, ctx.Repo.GitRepo, rel, attachmentUUIDs, false); err != nil {
437+
if err = releaseservice.UpdateRelease(ctx.User, ctx.Repo.GitRepo,
438+
rel, addAttachmentUUIDs, delAttachmentUUIDs, editAttachments); err != nil {
413439
ctx.ServerError("UpdateRelease", err)
414440
return
415441
}

0 commit comments

Comments
 (0)