Skip to content

Commit c3d31e5

Browse files
sapkzeripath
authored andcommitted
refactor(models/attachement): use getAttachmentsByUUIDs (#9317)
1 parent 50da9f7 commit c3d31e5

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

models/attachment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
133133
return attach, nil
134134
}
135135

136+
// GetAttachmentsByUUIDs returns attachment by given UUID list.
137+
func GetAttachmentsByUUIDs(uuids []string) ([]*Attachment, error) {
138+
return getAttachmentsByUUIDs(x, uuids)
139+
}
140+
136141
func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
137142
if len(uuids) == 0 {
138143
return []*Attachment{}, nil

models/attachment_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,15 @@ func TestUpdateAttachment(t *testing.T) {
116116

117117
AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
118118
}
119+
120+
func TestGetAttachmentsByUUIDs(t *testing.T) {
121+
assert.NoError(t, PrepareTestDatabase())
122+
123+
attachList, err := GetAttachmentsByUUIDs([]string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
124+
assert.NoError(t, err)
125+
assert.Equal(t, 2, len(attachList))
126+
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)
127+
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", attachList[1].UUID)
128+
assert.Equal(t, int64(1), attachList[0].IssueID)
129+
assert.Equal(t, int64(5), attachList[1].IssueID)
130+
}

models/issue_comment.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -567,16 +567,9 @@ func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Co
567567
}
568568

569569
// Check attachments
570-
attachments := make([]*Attachment, 0, len(opts.Attachments))
571-
for _, uuid := range opts.Attachments {
572-
attach, err := getAttachmentByUUID(e, uuid)
573-
if err != nil {
574-
if IsErrAttachmentNotExist(err) {
575-
continue
576-
}
577-
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
578-
}
579-
attachments = append(attachments, attach)
570+
attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
571+
if err != nil {
572+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
580573
}
581574

582575
for i := range attachments {

models/release.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,9 @@ func UpdateRelease(rel *Release) error {
129129
// AddReleaseAttachments adds a release attachments
130130
func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
131131
// Check attachments
132-
var attachments = make([]*Attachment, 0)
133-
for _, uuid := range attachmentUUIDs {
134-
attach, err := getAttachmentByUUID(x, uuid)
135-
if err != nil {
136-
if IsErrAttachmentNotExist(err) {
137-
continue
138-
}
139-
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
140-
}
141-
attachments = append(attachments, attach)
132+
attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
133+
if err != nil {
134+
return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
142135
}
143136

144137
for i := range attachments {

0 commit comments

Comments
 (0)