Skip to content

Commit 6f6cb61

Browse files
committed
optimize attachement linking
1 parent 674a27a commit 6f6cb61

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

models/attachment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) {
153153
return getAttachmentByUUID(x, uuid)
154154
}
155155

156+
// GetAttachmentsByUUIDs returns attachment by given UUID list.
157+
func GetAttachmentsByUUIDs(uuids []string) ([]*Attachment, error) {
158+
return getAttachmentsByUUIDs(x, uuids)
159+
}
160+
156161
// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
157162
func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) {
158163
return getAttachmentByReleaseIDFileName(x, releaseID, fileName)

models/issue.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,21 +1164,23 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
11641164
return err
11651165
}
11661166

1167-
/* TODO set this at upload
11681167
if len(opts.Attachments) > 0 {
11691168
attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
11701169
if err != nil {
11711170
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
11721171
}
11731172

11741173
for i := 0; i < len(attachments); i++ {
1174+
if !attachments[i].IsNotAttached(){
1175+
log.Error("newIssue [%s]: skipping already linked attachement", attachments[i].UUID)
1176+
continue
1177+
}
11751178
attachments[i].IssueID = opts.Issue.ID
11761179
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
11771180
return fmt.Errorf("update attachment [id: %d]: %v", attachments[i].ID, err)
11781181
}
11791182
}
11801183
}
1181-
*/
11821184

11831185
return opts.Issue.loadAttributes(e)
11841186
}

models/issue_comment.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -604,29 +604,26 @@ func sendCreateCommentAction(e *xorm.Session, opts *CreateCommentOptions, commen
604604
if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
605605
return err
606606
}
607-
/* TODO set it at upload
607+
608608
// Check attachments
609-
attachments := make([]*Attachment, 0, len(opts.Attachments))
610-
for _, uuid := range opts.Attachments {
611-
attach, err := getAttachmentByUUID(e, uuid)
612-
if err != nil {
613-
if IsErrAttachmentNotExist(err) {
614-
continue
615-
}
616-
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
617-
}
618-
attachments = append(attachments, attach)
609+
attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
610+
if err != nil {
611+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
619612
}
620613

621614
for i := range attachments {
615+
if !attachments[i].IsNotAttached(){
616+
log.Error("sendCreateCommentAction [%s]: skipping already linked attachement", attachments[i].UUID)
617+
continue
618+
}
622619
attachments[i].IssueID = opts.Issue.ID
623620
attachments[i].CommentID = comment.ID
624621
// No assign value could be 0, so ignore AllCols().
625622
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
626623
return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
627624
}
628625
}
629-
*/
626+
630627
case CommentTypeReopen:
631628
act.OpType = ActionReopenIssue
632629
if opts.Issue.IsPull {

models/release.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,16 @@ func createTag(gitRepo *git.Repository, rel *Release) error {
150150

151151
func linkReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
152152
// Check attachments
153-
var attachments = make([]*Attachment, 0)
154-
for _, uuid := range attachmentUUIDs {
155-
attach, err := getAttachmentByUUID(x, uuid)
153+
attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
156154
if err != nil {
157-
if IsErrAttachmentNotExist(err) {
158-
continue
159-
}
160-
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
161-
}
162-
if !attach.IsNotAttached(){
163-
log.Error("getAttachmentByUUID [%s]: skipping already linked attachement", uuid)
164-
continue
155+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
165156
}
166-
attachments = append(attachments, attach)
167-
}
168157

169158
for i := range attachments {
159+
if !attachments[i].IsNotAttached(){
160+
log.Error("linkReleaseAttachments [%s]: skipping already linked attachement", attachments[i].UUID)
161+
continue
162+
}
170163
attachments[i].ReleaseID = releaseID
171164
// No assign value could be 0, so ignore AllCols().
172165
if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {

0 commit comments

Comments
 (0)