summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-03-23 00:09:51 +0800
committerGitHub <noreply@github.com>2021-03-23 00:09:51 +0800
commit8567cba0d978e6ab68c337c0a80244704a15718a (patch)
treedb8794db6a32057adadda9460bf5923aee9ae89f /models
parent1a03fa7a4f353eb2f965cdcac39f630c281eca1e (diff)
downloadgitea-8567cba0d978e6ab68c337c0a80244704a15718a.tar.gz
gitea-8567cba0d978e6ab68c337c0a80244704a15718a.zip
Implement delete release attachments and update release attachments' name (#14130)
* 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
Diffstat (limited to 'models')
-rw-r--r--models/attachment.go23
-rw-r--r--models/attachment_test.go2
-rw-r--r--models/release.go10
3 files changed, 24 insertions, 11 deletions
diff --git a/models/attachment.go b/models/attachment.go
index 478b4cd0d2..2126e6d773 100644
--- a/models/attachment.go
+++ b/models/attachment.go
@@ -125,8 +125,8 @@ func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
}
// GetAttachmentsByUUIDs returns attachment by given UUID list.
-func GetAttachmentsByUUIDs(uuids []string) ([]*Attachment, error) {
- return getAttachmentsByUUIDs(x, uuids)
+func GetAttachmentsByUUIDs(ctx DBContext, uuids []string) ([]*Attachment, error) {
+ return getAttachmentsByUUIDs(ctx.e, uuids)
}
func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
@@ -183,12 +183,12 @@ func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string
// DeleteAttachment deletes the given attachment and optionally the associated file.
func DeleteAttachment(a *Attachment, remove bool) error {
- _, err := DeleteAttachments([]*Attachment{a}, remove)
+ _, err := DeleteAttachments(DefaultDBContext(), []*Attachment{a}, remove)
return err
}
// DeleteAttachments deletes the given attachments and optionally the associated files.
-func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
+func DeleteAttachments(ctx DBContext, attachments []*Attachment, remove bool) (int, error) {
if len(attachments) == 0 {
return 0, nil
}
@@ -198,7 +198,7 @@ func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
ids = append(ids, a.ID)
}
- cnt, err := x.In("id", ids).NoAutoCondition().Delete(attachments[0])
+ cnt, err := ctx.e.In("id", ids).NoAutoCondition().Delete(attachments[0])
if err != nil {
return 0, err
}
@@ -220,7 +220,7 @@ func DeleteAttachmentsByIssue(issueID int64, remove bool) (int, error) {
return 0, err
}
- return DeleteAttachments(attachments, remove)
+ return DeleteAttachments(DefaultDBContext(), attachments, remove)
}
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
@@ -230,7 +230,7 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
return 0, err
}
- return DeleteAttachments(attachments, remove)
+ return DeleteAttachments(DefaultDBContext(), attachments, remove)
}
// UpdateAttachment updates the given attachment in database
@@ -238,6 +238,15 @@ func UpdateAttachment(atta *Attachment) error {
return updateAttachment(x, atta)
}
+// UpdateAttachmentByUUID Updates attachment via uuid
+func UpdateAttachmentByUUID(ctx DBContext, attach *Attachment, cols ...string) error {
+ if attach.UUID == "" {
+ return fmt.Errorf("Attachement uuid should not blank")
+ }
+ _, err := ctx.e.Where("uuid=?", attach.UUID).Cols(cols...).Update(attach)
+ return err
+}
+
func updateAttachment(e Engine, atta *Attachment) error {
var sess *xorm.Session
if atta.ID != 0 && atta.UUID == "" {
diff --git a/models/attachment_test.go b/models/attachment_test.go
index e36a5977ee..fa7fd3471b 100644
--- a/models/attachment_test.go
+++ b/models/attachment_test.go
@@ -120,7 +120,7 @@ func TestUpdateAttachment(t *testing.T) {
func TestGetAttachmentsByUUIDs(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- attachList, err := GetAttachmentsByUUIDs([]string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
+ attachList, err := GetAttachmentsByUUIDs(DefaultDBContext(), []string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
assert.NoError(t, err)
assert.Equal(t, 2, len(attachList))
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)
diff --git a/models/release.go b/models/release.go
index 751d08d4d8..13b8f17218 100644
--- a/models/release.go
+++ b/models/release.go
@@ -6,6 +6,7 @@
package models
import (
+ "errors"
"fmt"
"sort"
"strings"
@@ -117,17 +118,20 @@ func UpdateRelease(ctx DBContext, rel *Release) error {
}
// AddReleaseAttachments adds a release attachments
-func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
+func AddReleaseAttachments(ctx DBContext, releaseID int64, attachmentUUIDs []string) (err error) {
// Check attachments
- attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
+ attachments, err := getAttachmentsByUUIDs(ctx.e, attachmentUUIDs)
if err != nil {
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
}
for i := range attachments {
+ if attachments[i].ReleaseID != 0 {
+ return errors.New("release permission denied")
+ }
attachments[i].ReleaseID = releaseID
// No assign value could be 0, so ignore AllCols().
- if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
+ if _, err = ctx.e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
}
}