aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/attachment.go16
-rw-r--r--models/issue.go2
-rw-r--r--models/release.go57
3 files changed, 66 insertions, 9 deletions
diff --git a/models/attachment.go b/models/attachment.go
index a270b751..4ba6adcb 100644
--- a/models/attachment.go
+++ b/models/attachment.go
@@ -110,7 +110,7 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) {
}
func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) {
- attachments := make([]*Attachment, 0, 10)
+ attachments := make([]*Attachment, 0, 5)
return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments)
}
@@ -120,15 +120,25 @@ func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
}
func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
- attachments := make([]*Attachment, 0, 10)
+ attachments := make([]*Attachment, 0, 5)
return attachments, e.Where("comment_id=?", commentID).Find(&attachments)
}
-// GetAttachmentsByCommentID returns all attachments if comment by given ID.
+// GetAttachmentsByCommentID returns all attachments of a comment.
func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
return getAttachmentsByCommentID(x, commentID)
}
+func getAttachmentsByReleaseID(e Engine, releaseID int64) ([]*Attachment, error) {
+ attachments := make([]*Attachment, 0, 10)
+ return attachments, e.Where("release_id = ?", releaseID).Find(&attachments)
+}
+
+// GetAttachmentsByReleaseID returns all attachments of a release.
+func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) {
+ return getAttachmentsByReleaseID(x, releaseID)
+}
+
// DeleteAttachment deletes the given attachment and optionally the associated file.
func DeleteAttachment(a *Attachment, remove bool) error {
_, err := DeleteAttachments([]*Attachment{a}, remove)
diff --git a/models/issue.go b/models/issue.go
index 8bc5914c..3caa3b8f 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -741,7 +741,7 @@ func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
return opts.Issue.loadAttributes(e)
}
-// NewIssue creates new issue with labels for repository.
+// NewIssue creates new issue with labels and attachments for repository.
func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
sess := x.NewSession()
defer sessionRelease(sess)
diff --git a/models/release.go b/models/release.go
index 08192370..47771bc1 100644
--- a/models/release.go
+++ b/models/release.go
@@ -39,6 +39,8 @@ type Release struct {
Created time.Time `xorm:"-"`
CreatedUnix int64
+
+ Attachments []*Attachment `xorm:"-"`
}
func (r *Release) BeforeInsert() {
@@ -74,6 +76,13 @@ func (r *Release) loadAttributes(e Engine) (err error) {
}
}
+ if r.Attachments == nil {
+ r.Attachments, err = getAttachmentsByReleaseID(e, r.ID)
+ if err != nil {
+ return fmt.Errorf("getAttachmentsByReleaseID [%d]: %v", r.ID, err)
+ }
+ }
+
return nil
}
@@ -150,8 +159,8 @@ func (r *Release) preparePublishWebhooks() {
}
}
-// CreateRelease creates a new release of repository.
-func CreateRelease(gitRepo *git.Repository, r *Release) error {
+// NewRelease creates a new release with attachments for repository.
+func NewRelease(gitRepo *git.Repository, r *Release, uuids []string) error {
isExist, err := IsReleaseExist(r.RepoID, r.TagName)
if err != nil {
return err
@@ -163,10 +172,27 @@ func CreateRelease(gitRepo *git.Repository, r *Release) error {
return err
}
r.LowerTagName = strings.ToLower(r.TagName)
- if _, err = x.Insert(r); err != nil {
+
+ sess := x.NewSession()
+ defer sessionRelease(sess)
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
+ if _, err = sess.Insert(r); err != nil {
return fmt.Errorf("Insert: %v", err)
}
+ if len(uuids) > 0 {
+ if _, err = sess.In("uuid", uuids).Cols("release_id").Update(&Attachment{ReleaseID: r.ID}); err != nil {
+ return fmt.Errorf("link attachments: %v", err)
+ }
+ }
+
+ if err = sess.Commit(); err != nil {
+ return fmt.Errorf("Commit: %v", err)
+ }
+
// Only send webhook when actually published, skip drafts
if r.IsDraft {
return nil
@@ -254,15 +280,36 @@ func SortReleases(rels []*Release) {
}
// UpdateRelease updates information of a release.
-func UpdateRelease(doer *User, gitRepo *git.Repository, r *Release, isPublish bool) (err error) {
+func UpdateRelease(doer *User, gitRepo *git.Repository, r *Release, isPublish bool, uuids []string) (err error) {
if err = createTag(gitRepo, r); err != nil {
return fmt.Errorf("createTag: %v", err)
}
r.PublisherID = doer.ID
- if _, err = x.Id(r.ID).AllCols().Update(r); err != nil {
+
+ sess := x.NewSession()
+ defer sessionRelease(sess)
+ if err = sess.Begin(); err != nil {
return err
}
+ if _, err = sess.Id(r.ID).AllCols().Update(r); err != nil {
+ return fmt.Errorf("Update: %v", err)
+ }
+
+ // Unlink all current attachments and link back later if still valid
+ if _, err = sess.Exec("UPDATE attachment SET release_id = 0 WHERE release_id = ?", r.ID); err != nil {
+ return fmt.Errorf("unlink current attachments: %v", err)
+ }
+
+ if len(uuids) > 0 {
+ if _, err = sess.In("uuid", uuids).Cols("release_id").Update(&Attachment{ReleaseID: r.ID}); err != nil {
+ return fmt.Errorf("link attachments: %v", err)
+ }
+ }
+
+ if err = sess.Commit(); err != nil {
+ return fmt.Errorf("Commit: %v", err)
+ }
if !isPublish {
return nil