From 23f072887935a6aa81634ef487432ff2b5fa250a Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Thu, 29 Jun 2017 10:30:58 -0400 Subject: [PATCH 01/26] Don't ignore gravatar error --- modules/base/tool.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/base/tool.go b/modules/base/tool.go index 8952e7a8c959a..0f730868ebad9 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -197,8 +197,10 @@ func HashEmail(email string) string { // to return full URL if user enables Gravatar-like service. func AvatarLink(email string) string { if setting.EnableFederatedAvatar && setting.LibravatarService != nil { - // TODO: This doesn't check any error. AvatarLink should return (string, error) - url, _ := setting.LibravatarService.FromEmail(email) + url, err := setting.LibravatarService.FromEmail(email) + if err != nil { + log.Error(4, "LibravatarService.FromEmail(email=%s): error %v", email, err) + } return url } From 05e37264154bbcb375b358798c7a8f9b499c4881 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Thu, 29 Jun 2017 12:10:33 -0400 Subject: [PATCH 02/26] Use default avatar on error --- models/user.go | 5 ++--- modules/base/tool.go | 7 ++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/models/user.go b/models/user.go index 6484c8cf696f8..2a4fb557db76c 100644 --- a/models/user.go +++ b/models/user.go @@ -333,15 +333,14 @@ func (u *User) generateRandomAvatar(e Engine) error { // which includes app sub-url as prefix. However, it is possible // to return full URL if user enables Gravatar-like service. func (u *User) RelAvatarLink() string { - defaultImgURL := setting.AppSubURL + "/img/avatar_default.png" if u.ID == -1 { - return defaultImgURL + return base.DefaultAvatarLink() } switch { case u.UseCustomAvatar: if !com.IsFile(u.CustomAvatarPath()) { - return defaultImgURL + return base.DefaultAvatarLink() } return setting.AppSubURL + "/avatars/" + u.Avatar case setting.DisableGravatar, setting.OfflineMode: diff --git a/modules/base/tool.go b/modules/base/tool.go index 0f730868ebad9..1bf1ce3951387 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -192,6 +192,10 @@ func HashEmail(email string) string { return EncodeMD5(strings.ToLower(strings.TrimSpace(email))) } +func DefaultAvatarLink() string { + return setting.AppSubURL + "/img/avatar_default.png" +} + // AvatarLink returns relative avatar link to the site domain by given email, // which includes app sub-url as prefix. However, it is possible // to return full URL if user enables Gravatar-like service. @@ -200,6 +204,7 @@ func AvatarLink(email string) string { url, err := setting.LibravatarService.FromEmail(email) if err != nil { log.Error(4, "LibravatarService.FromEmail(email=%s): error %v", email, err) + return DefaultAvatarLink() } return url } @@ -208,7 +213,7 @@ func AvatarLink(email string) string { return setting.GravatarSource + HashEmail(email) } - return setting.AppSubURL + "/img/avatar_default.png" + return DefaultAvatarLink() } // Seconds-based time units From 2b410e44b2813648d670feb2fd543638bd8a4c80 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Thu, 29 Jun 2017 12:11:34 -0400 Subject: [PATCH 03/26] lint --- modules/base/tool.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/base/tool.go b/modules/base/tool.go index 1bf1ce3951387..543775e0dfdc7 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -192,6 +192,7 @@ func HashEmail(email string) string { return EncodeMD5(strings.ToLower(strings.TrimSpace(email))) } +// DefaultAvatarLink the default avatar link func DefaultAvatarLink() string { return setting.AppSubURL + "/img/avatar_default.png" } From 49f5ddca5b9e32b62bc1e92b2695b86dd43684d4 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 00:44:43 +0300 Subject: [PATCH 04/26] Added attachments to the releases API --- models/attachment.go | 13 ++++++++++ models/release.go | 9 +++++++ vendor/code.gitea.io/sdk/gitea/attachment.go | 16 ++++++++++++ vendor/code.gitea.io/sdk/gitea/release.go | 27 ++++++++++---------- 4 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 vendor/code.gitea.io/sdk/gitea/attachment.go diff --git a/models/attachment.go b/models/attachment.go index 3b3521bf60364..cee4c83028a81 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -12,6 +12,7 @@ import ( "path" "time" + api "code.gitea.io/sdk/gitea" "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" @@ -69,6 +70,18 @@ func (a *Attachment) LocalPath() string { return AttachmentLocalPath(a.UUID) } +// APIFormat converts a Attachment to an api.Attachment +func (a *Attachment) APIFormat() *api.Attachment { + return &api.Attachment{ + ID: a.ID, + Created: a.Created, + Name: a.Name, + UUID: a.UUID, + DownloadURL: setting.AppURL + "/attachments/" + a.UUID, + DownloadCount: a.DownloadCount, + } +} + // NewAttachment creates a new attachment object. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) { attach := &Attachment{ diff --git a/models/release.go b/models/release.go index 783f1f04757a5..56e920c29b00a 100644 --- a/models/release.go +++ b/models/release.go @@ -71,6 +71,10 @@ func (r *Release) loadAttributes(e Engine) error { return err } } + // load the attachments of this release + if r.Attachments == nil { + GetReleaseAttachments(r) + } return nil } @@ -97,6 +101,10 @@ func (r *Release) TarURL() string { // APIFormat convert a Release to api.Release func (r *Release) APIFormat() *api.Release { + apiAttachments := make([]*api.Attachment, len(r.Attachments)) + for i := range r.Attachments { + apiAttachments[i] = r.Attachments[i].APIFormat() + } return &api.Release{ ID: r.ID, TagName: r.TagName, @@ -110,6 +118,7 @@ func (r *Release) APIFormat() *api.Release { CreatedAt: r.Created, PublishedAt: r.Created, Publisher: r.Publisher.APIFormat(), + Attachments: apiAttachments, } } diff --git a/vendor/code.gitea.io/sdk/gitea/attachment.go b/vendor/code.gitea.io/sdk/gitea/attachment.go new file mode 100644 index 0000000000000..f422493e4d4a0 --- /dev/null +++ b/vendor/code.gitea.io/sdk/gitea/attachment.go @@ -0,0 +1,16 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gitea // import "code.gitea.io/sdk/gitea" +import "time" + +// a generic attachment +type Attachment struct { + ID int64 `json:"id"` + Created time.Time `json:"created_at"` + Name string `json:"name"` + UUID string `json:"uuid"` + DownloadURL string `json:"download_url"` + DownloadCount int64 `json:"download_count"` +} diff --git a/vendor/code.gitea.io/sdk/gitea/release.go b/vendor/code.gitea.io/sdk/gitea/release.go index cc841466d8eda..108269419fefd 100644 --- a/vendor/code.gitea.io/sdk/gitea/release.go +++ b/vendor/code.gitea.io/sdk/gitea/release.go @@ -13,19 +13,20 @@ import ( // Release represents a repository release type Release struct { - ID int64 `json:"id"` - TagName string `json:"tag_name"` - Target string `json:"target_commitish"` - Title string `json:"name"` - Note string `json:"body"` - URL string `json:"url"` - TarURL string `json:"tarball_url"` - ZipURL string `json:"zipball_url"` - IsDraft bool `json:"draft"` - IsPrerelease bool `json:"prerelease"` - CreatedAt time.Time `json:"created_at"` - PublishedAt time.Time `json:"published_at"` - Publisher *User `json:"author"` + ID int64 `json:"id"` + TagName string `json:"tag_name"` + Target string `json:"target_commitish"` + Title string `json:"name"` + Note string `json:"body"` + URL string `json:"url"` + TarURL string `json:"tarball_url"` + ZipURL string `json:"zipball_url"` + IsDraft bool `json:"draft"` + IsPrerelease bool `json:"prerelease"` + CreatedAt time.Time `json:"created_at"` + PublishedAt time.Time `json:"published_at"` + Publisher *User `json:"author"` + Attachments []*Attachment `json:"attachments"` } // ListReleases list releases of a repository From 789188f13c93e47b8a758c379bd463f7c2792f13 Mon Sep 17 00:00:00 2001 From: Dryusdan Date: Fri, 30 Jun 2017 02:58:57 +0200 Subject: [PATCH 05/26] Reduce number of layer (#2078) Somes layer are created and aren't usefull, so I compress this :) --- Dockerfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index cc9ec311be592..745642d30993f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,8 +13,8 @@ RUN apk --no-cache add \ s6 \ curl \ openssh \ - tzdata -RUN addgroup \ + tzdata \ + && addgroup \ -S -g 1000 \ git && \ adduser \ @@ -26,9 +26,9 @@ RUN addgroup \ git && \ echo "git:$(date +%s | sha256sum | base64 | head -c 32)" | chpasswd -ENV USER git -ENV GITEA_CUSTOM /data/gitea -ENV GODEBUG=netdns=go +ENV USER git \ + GITEA_CUSTOM /data/gitea \ + GODEBUG=netdns=go VOLUME ["/data"] From b187b791a71287f2ece19efa0b311b83e0fa13fd Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 09:08:40 +0300 Subject: [PATCH 06/26] Revert changes done to vendor package gitea-sdk Signed-off-by: Petrisor Lacatus --- vendor/code.gitea.io/sdk/gitea/attachment.go | 16 ------------ vendor/code.gitea.io/sdk/gitea/release.go | 27 ++++++++++---------- 2 files changed, 13 insertions(+), 30 deletions(-) delete mode 100644 vendor/code.gitea.io/sdk/gitea/attachment.go diff --git a/vendor/code.gitea.io/sdk/gitea/attachment.go b/vendor/code.gitea.io/sdk/gitea/attachment.go deleted file mode 100644 index f422493e4d4a0..0000000000000 --- a/vendor/code.gitea.io/sdk/gitea/attachment.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package gitea // import "code.gitea.io/sdk/gitea" -import "time" - -// a generic attachment -type Attachment struct { - ID int64 `json:"id"` - Created time.Time `json:"created_at"` - Name string `json:"name"` - UUID string `json:"uuid"` - DownloadURL string `json:"download_url"` - DownloadCount int64 `json:"download_count"` -} diff --git a/vendor/code.gitea.io/sdk/gitea/release.go b/vendor/code.gitea.io/sdk/gitea/release.go index 108269419fefd..cc841466d8eda 100644 --- a/vendor/code.gitea.io/sdk/gitea/release.go +++ b/vendor/code.gitea.io/sdk/gitea/release.go @@ -13,20 +13,19 @@ import ( // Release represents a repository release type Release struct { - ID int64 `json:"id"` - TagName string `json:"tag_name"` - Target string `json:"target_commitish"` - Title string `json:"name"` - Note string `json:"body"` - URL string `json:"url"` - TarURL string `json:"tarball_url"` - ZipURL string `json:"zipball_url"` - IsDraft bool `json:"draft"` - IsPrerelease bool `json:"prerelease"` - CreatedAt time.Time `json:"created_at"` - PublishedAt time.Time `json:"published_at"` - Publisher *User `json:"author"` - Attachments []*Attachment `json:"attachments"` + ID int64 `json:"id"` + TagName string `json:"tag_name"` + Target string `json:"target_commitish"` + Title string `json:"name"` + Note string `json:"body"` + URL string `json:"url"` + TarURL string `json:"tarball_url"` + ZipURL string `json:"zipball_url"` + IsDraft bool `json:"draft"` + IsPrerelease bool `json:"prerelease"` + CreatedAt time.Time `json:"created_at"` + PublishedAt time.Time `json:"published_at"` + Publisher *User `json:"author"` } // ListReleases list releases of a repository From 8fd43f215c72ecfd175b4bae3ffb0b5e16170e6e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 30 Jun 2017 14:10:37 +0800 Subject: [PATCH 07/26] Revert "Reduce number of layer" (#2086) This reverts commit 789188f13c93e47b8a758c379bd463f7c2792f13. --- Dockerfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 745642d30993f..cc9ec311be592 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,8 +13,8 @@ RUN apk --no-cache add \ s6 \ curl \ openssh \ - tzdata \ - && addgroup \ + tzdata +RUN addgroup \ -S -g 1000 \ git && \ adduser \ @@ -26,9 +26,9 @@ RUN apk --no-cache add \ git && \ echo "git:$(date +%s | sha256sum | base64 | head -c 32)" | chpasswd -ENV USER git \ - GITEA_CUSTOM /data/gitea \ - GODEBUG=netdns=go +ENV USER git +ENV GITEA_CUSTOM /data/gitea +ENV GODEBUG=netdns=go VOLUME ["/data"] From bfd3b7d746c5191174bfc4552c9ea3fe50f5c054 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 09:10:55 +0300 Subject: [PATCH 08/26] Added size of the attachment to the api format Signed-off-by: Petrisor Lacatus --- models/attachment.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/models/attachment.go b/models/attachment.go index cee4c83028a81..a0d652142a9c9 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -59,6 +59,20 @@ func (a *Attachment) IncreaseDownloadCount() error { return nil } +// GetSize gets the size of the attachment in bytes +func (a *Attachment) GetSize() (int64, error) { + f, err := os.Open(a.LocalPath()) + defer f.Close() + if err != nil { + return 0, err + } + info, err := f.Stat() + if err != nil { + return 0, err + } + return info.Size(), nil +} + // AttachmentLocalPath returns where attachment is stored in local file // system based on given UUID. func AttachmentLocalPath(uuid string) string { @@ -72,7 +86,7 @@ func (a *Attachment) LocalPath() string { // APIFormat converts a Attachment to an api.Attachment func (a *Attachment) APIFormat() *api.Attachment { - return &api.Attachment{ + apiAttachment := &api.Attachment{ ID: a.ID, Created: a.Created, Name: a.Name, @@ -80,6 +94,11 @@ func (a *Attachment) APIFormat() *api.Attachment { DownloadURL: setting.AppURL + "/attachments/" + a.UUID, DownloadCount: a.DownloadCount, } + fileSize, err := a.GetSize() + if err == nil { + apiAttachment.Size = fileSize + } + return apiAttachment } // NewAttachment creates a new attachment object. From 270098c056501fdcfb4f4f279ecb8dbfb4e2a6b4 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:17:57 +0300 Subject: [PATCH 09/26] Added two utility methods on attachment, to get an attachment by id and to get all the attachments associated with a given release Signed-off-by: Petrisor Lacatus --- models/attachment.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/models/attachment.go b/models/attachment.go index a0d652142a9c9..2239cd2d14491 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -91,7 +91,7 @@ func (a *Attachment) APIFormat() *api.Attachment { Created: a.Created, Name: a.Name, UUID: a.UUID, - DownloadURL: setting.AppURL + "/attachments/" + a.UUID, + DownloadURL: setting.AppURL + "attachments/" + a.UUID, DownloadCount: a.DownloadCount, } fileSize, err := a.GetSize() @@ -158,6 +158,18 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return getAttachmentByUUID(x, uuid) } +// GetAttachmentByD returns attachment by given ID. +func GetAttachmentByID(id int64) (*Attachment, error) { + attach := &Attachment{ID: id} + has, err := x.Get(attach) + if err != nil { + return nil, err + } else if !has { + return nil, ErrAttachmentNotExist{id, ""} + } + return attach, nil +} + func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments) @@ -174,6 +186,12 @@ func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { return attachments, x.Where("comment_id=?", commentID).Find(&attachments) } +// GetAttachmentsByReleaseID returns all attachments of a release +func GetAttachmentsByReleaseID(releaseId int64) ([]*Attachment, error) { + attachments := make([]*Attachment, 0, 10) + return attachments, x.Where("release_id=?", releaseId).Find(&attachments) +} + // DeleteAttachment deletes the given attachment and optionally the associated file. func DeleteAttachment(a *Attachment, remove bool) error { _, err := DeleteAttachments([]*Attachment{a}, remove) From 7e14752b93c25e17d157c1c7c246763bc4598b77 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:21:47 +0300 Subject: [PATCH 10/26] Implemented the api for getting only the attachments of a release, as well getting a given attachment of a release Signed-off-by: Petrisor Lacatus --- routers/api/v1/api.go | 12 ++++++-- routers/api/v1/repo/release.go | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0e356b1f907ac..8ca0b28b9dd1f 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -420,9 +420,15 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/releases", func() { m.Combo("").Get(repo.ListReleases). Post(bind(api.CreateReleaseOption{}), repo.CreateRelease) - m.Combo("/:id").Get(repo.GetRelease). - Patch(bind(api.EditReleaseOption{}), repo.EditRelease). - Delete(repo.DeleteRelease) + m.Group("/:id", func() { + m.Combo("").Get(repo.GetRelease). + Patch(bind(api.EditReleaseOption{}), repo.EditRelease). + Delete(repo.DeleteRelease) + m.Group("/assets", func() { + m.Combo("").Get(repo.ListReleaseAttachments) + m.Combo("/:assetId").Get(repo.GetReleaseAttachment) + }) + }) }) m.Post("/mirror-sync", repo.MirrorSync) m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig) diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index a367e557173f6..37a59e9f3c625 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -32,6 +32,56 @@ func GetRelease(ctx *context.APIContext) { ctx.JSON(200, release.APIFormat()) } +// ListReleaseAttachments get all the attachments of a release +func ListReleaseAttachments(ctx *context.APIContext) { + id := ctx.ParamsInt64(":id") + release, err := models.GetReleaseByID(id) + if err != nil { + ctx.Error(500, "GetReleaseByID", err) + return + } + if release.RepoID != ctx.Repo.Repository.ID { + ctx.Status(404) + return + } + // load the attachments of this release + attachments, err := models.GetAttachmentsByReleaseID(id) + if err != nil { + ctx.Error(500, "GetAttachmentsByReleaseID", err) + return + } + // build the attachment list + apiAttachments := make([]*api.Attachment, len(attachments)) + for i := range attachments { + apiAttachments[i] = attachments[i].APIFormat() + } + ctx.JSON(200, apiAttachments) +} + +// GetReleaseAttachment get a single attachment of a release +func GetReleaseAttachment(ctx *context.APIContext) { + id := ctx.ParamsInt64(":id") + attachmentId := ctx.ParamsInt64(":assetId") + release, err := models.GetReleaseByID(id) + if err != nil { + ctx.Error(500, "GetReleaseByID", err) + return + } + if release.RepoID != ctx.Repo.Repository.ID { + ctx.Status(404) + return + } + // load the attachments of this release + attachment, err := models.GetAttachmentByID(attachmentId) + // if the attachment was not found, or it was found but is not associated with this release, then throw 404 + if err != nil || id != attachment.ReleaseID { + ctx.Status(404) + return + } + + ctx.JSON(200, attachment.APIFormat()) +} + // ListReleases list a repository's releases func ListReleases(ctx *context.APIContext) { releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, 1, 2147483647) From ba596d135106e2cb81b363b9a15f71748876c2d6 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:27:01 +0300 Subject: [PATCH 11/26] When searching for a release attachments, use the simpler method. Signed-off-by: Petrisor Lacatus --- models/release.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/models/release.go b/models/release.go index 56e920c29b00a..cd88c29513675 100644 --- a/models/release.go +++ b/models/release.go @@ -73,7 +73,11 @@ func (r *Release) loadAttributes(e Engine) error { } // load the attachments of this release if r.Attachments == nil { - GetReleaseAttachments(r) + attachments, err := GetAttachmentsByReleaseID(r.ID) + if err != nil { + return err + } + r.Attachments = attachments } return nil } From b23c0faae71bf25490624d8b9ee53cf00f96232c Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:57:26 +0300 Subject: [PATCH 12/26] Fix linting errors Signed-off-by: Petrisor Lacatus --- models/attachment.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index 2239cd2d14491..d6e87e2696b61 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -158,7 +158,7 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return getAttachmentByUUID(x, uuid) } -// GetAttachmentByD returns attachment by given ID. +// GetAttachmentByID returns attachment by given ID. func GetAttachmentByID(id int64) (*Attachment, error) { attach := &Attachment{ID: id} has, err := x.Get(attach) @@ -187,9 +187,9 @@ func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { } // GetAttachmentsByReleaseID returns all attachments of a release -func GetAttachmentsByReleaseID(releaseId int64) ([]*Attachment, error) { +func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) - return attachments, x.Where("release_id=?", releaseId).Find(&attachments) + return attachments, x.Where("release_id=?", releaseID).Find(&attachments) } // DeleteAttachment deletes the given attachment and optionally the associated file. From 36e1893ffb7ac85bc557f88fdaec0f3853216e35 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 11:00:36 +0300 Subject: [PATCH 13/26] Fix more linting errors Signed-off-by: Petrisor Lacatus --- routers/api/v1/repo/release.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 37a59e9f3c625..2a01bef772208 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -61,7 +61,7 @@ func ListReleaseAttachments(ctx *context.APIContext) { // GetReleaseAttachment get a single attachment of a release func GetReleaseAttachment(ctx *context.APIContext) { id := ctx.ParamsInt64(":id") - attachmentId := ctx.ParamsInt64(":assetId") + attachmentID := ctx.ParamsInt64(":assetId") release, err := models.GetReleaseByID(id) if err != nil { ctx.Error(500, "GetReleaseByID", err) @@ -72,7 +72,7 @@ func GetReleaseAttachment(ctx *context.APIContext) { return } // load the attachments of this release - attachment, err := models.GetAttachmentByID(attachmentId) + attachment, err := models.GetAttachmentByID(attachmentID) // if the attachment was not found, or it was found but is not associated with this release, then throw 404 if err != nil || id != attachment.ReleaseID { ctx.Status(404) From a5ee8e1823cb53006505a2818f3fbe8ead54ddda Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 12:20:03 +0300 Subject: [PATCH 14/26] Changed GetSize for attachment implementation. Added error logging. Signed-off-by: Petrisor Lacatus --- models/attachment.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index d6e87e2696b61..f30025687fcc4 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -16,6 +16,7 @@ import ( "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -61,12 +62,7 @@ func (a *Attachment) IncreaseDownloadCount() error { // GetSize gets the size of the attachment in bytes func (a *Attachment) GetSize() (int64, error) { - f, err := os.Open(a.LocalPath()) - defer f.Close() - if err != nil { - return 0, err - } - info, err := f.Stat() + info, err := os.Stat(a.LocalPath()) if err != nil { return 0, err } @@ -95,6 +91,7 @@ func (a *Attachment) APIFormat() *api.Attachment { DownloadCount: a.DownloadCount, } fileSize, err := a.GetSize() + log.Warn("Error getting the file size for attachment %s. ", a.UUID, err) if err == nil { apiAttachment.Size = fileSize } From b36849dc1f8df55bba9c3ad77993c7d521776842 Mon Sep 17 00:00:00 2001 From: Bwko Date: Fri, 30 Jun 2017 12:29:58 +0200 Subject: [PATCH 15/26] Fix exit status 1 not handled @ getMergeCommit --- models/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/pull.go b/models/pull.go index 94bd7930ff684..1db0a89741432 100644 --- a/models/pull.go +++ b/models/pull.go @@ -495,7 +495,7 @@ func (pr *PullRequest) getMergeCommit() (*git.Commit, error) { if err != nil { // Errors are signaled by a non-zero status that is not 1 - if err.Error() == "exit status 1" { + if strings.Contains(err.Error(), "exit status 1") { return nil, nil } return nil, fmt.Errorf("git merge-base --is-ancestor: %v %v", stderr, err) From 78f201ad890706670781ca539a0c3e7ad275e7c5 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 16:43:23 +0300 Subject: [PATCH 16/26] Implementation of `GET /repos/:owner/:repo/releases/latest` Signed-off-by: Petrisor Lacatus --- models/release.go | 14 +++++++++++--- routers/api/v1/api.go | 1 + routers/api/v1/repo/release.go | 22 +++++++++++++++++++++- routers/repo/release.go | 2 +- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/models/release.go b/models/release.go index cd88c29513675..3c55dd989c818 100644 --- a/models/release.go +++ b/models/release.go @@ -246,15 +246,23 @@ func GetReleaseByID(id int64) (*Release, error) { return rel, nil } -// GetReleasesByRepoID returns a list of releases of repository. -func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err error) { +// GetReleasesByRepoID returns a list of releases of repository. The releases are sorted descending by created date +func GetReleasesByRepoID(repoID int64, page, pageSize int, includeDrafts bool, includePreReleases bool) (rels []*Release, err error) { + var cond = builder.NewCond() if page <= 0 { page = 1 } + cond = cond.And(builder.Eq{"repo_id": repoID}) + if !includeDrafts { + cond = cond.And(builder.Eq{"is_draft": false}) + } + if !includePreReleases { + cond = cond.And(builder.Eq{"is_prerelease": false}) + } err = x. Desc("created_unix"). Limit(pageSize, (page-1)*pageSize). - Find(&rels, Release{RepoID: repoID}) + Where(cond).Find(&rels) return rels, err } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 8ca0b28b9dd1f..c6fd8e31e9ee0 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -420,6 +420,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/releases", func() { m.Combo("").Get(repo.ListReleases). Post(bind(api.CreateReleaseOption{}), repo.CreateRelease) + m.Combo("/latest").Get(repo.GetLatestRelease) m.Group("/:id", func() { m.Combo("").Get(repo.GetRelease). Patch(bind(api.EditReleaseOption{}), repo.EditRelease). diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 2a01bef772208..02a6a59ac46c3 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -84,7 +84,7 @@ func GetReleaseAttachment(ctx *context.APIContext) { // ListReleases list a repository's releases func ListReleases(ctx *context.APIContext) { - releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, 1, 2147483647) + releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, 1, 2147483647, true, true) if err != nil { ctx.Error(500, "GetReleasesByRepoID", err) return @@ -109,6 +109,26 @@ func ListReleases(ctx *context.APIContext) { ctx.JSON(200, rels) } +// GetLatestRelease Gets the latest release in a repository. Draft releases and prereleases are not returned +func GetLatestRelease(ctx *context.APIContext) { + // we set the pageSize to 1 to get back only one release + releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, 1, 1, false, false) + if err != nil { + ctx.Error(500, "GetReleasesByRepoID", err) + return + } + if len(releases) <= 0 { + // no releases found, just return 404 + ctx.Status(404) + return + } + if err := releases[0].LoadAttributes(); err != nil { + ctx.Error(500, "LoadAttributes", err) + return + } + ctx.JSON(200, releases[0].APIFormat()) +} + // CreateRelease create a release func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) { if ctx.Repo.AccessMode < models.AccessModeWrite { diff --git a/routers/repo/release.go b/routers/repo/release.go index 1b2c630a562e8..ef0c9745195ee 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -65,7 +65,7 @@ func Releases(ctx *context.Context) { limit = 10 } - releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit) + releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit, true, true) if err != nil { ctx.Handle(500, "GetReleasesByRepoID", err) return From 31ab468a7fcde1ba17350fe37b2ff9f22f4f5cb0 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 09:10:55 +0300 Subject: [PATCH 17/26] Added size of the attachment to the api format Signed-off-by: Petrisor Lacatus --- models/attachment.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/models/attachment.go b/models/attachment.go index 3b3521bf60364..a0d652142a9c9 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -12,6 +12,7 @@ import ( "path" "time" + api "code.gitea.io/sdk/gitea" "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" @@ -58,6 +59,20 @@ func (a *Attachment) IncreaseDownloadCount() error { return nil } +// GetSize gets the size of the attachment in bytes +func (a *Attachment) GetSize() (int64, error) { + f, err := os.Open(a.LocalPath()) + defer f.Close() + if err != nil { + return 0, err + } + info, err := f.Stat() + if err != nil { + return 0, err + } + return info.Size(), nil +} + // AttachmentLocalPath returns where attachment is stored in local file // system based on given UUID. func AttachmentLocalPath(uuid string) string { @@ -69,6 +84,23 @@ func (a *Attachment) LocalPath() string { return AttachmentLocalPath(a.UUID) } +// APIFormat converts a Attachment to an api.Attachment +func (a *Attachment) APIFormat() *api.Attachment { + apiAttachment := &api.Attachment{ + ID: a.ID, + Created: a.Created, + Name: a.Name, + UUID: a.UUID, + DownloadURL: setting.AppURL + "/attachments/" + a.UUID, + DownloadCount: a.DownloadCount, + } + fileSize, err := a.GetSize() + if err == nil { + apiAttachment.Size = fileSize + } + return apiAttachment +} + // NewAttachment creates a new attachment object. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) { attach := &Attachment{ From 9a74130a293115ccffc5e09a6c71e8f9111852f6 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:17:57 +0300 Subject: [PATCH 18/26] Added two utility methods on attachment, to get an attachment by id and to get all the attachments associated with a given release Signed-off-by: Petrisor Lacatus --- models/attachment.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/models/attachment.go b/models/attachment.go index a0d652142a9c9..2239cd2d14491 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -91,7 +91,7 @@ func (a *Attachment) APIFormat() *api.Attachment { Created: a.Created, Name: a.Name, UUID: a.UUID, - DownloadURL: setting.AppURL + "/attachments/" + a.UUID, + DownloadURL: setting.AppURL + "attachments/" + a.UUID, DownloadCount: a.DownloadCount, } fileSize, err := a.GetSize() @@ -158,6 +158,18 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return getAttachmentByUUID(x, uuid) } +// GetAttachmentByD returns attachment by given ID. +func GetAttachmentByID(id int64) (*Attachment, error) { + attach := &Attachment{ID: id} + has, err := x.Get(attach) + if err != nil { + return nil, err + } else if !has { + return nil, ErrAttachmentNotExist{id, ""} + } + return attach, nil +} + func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments) @@ -174,6 +186,12 @@ func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { return attachments, x.Where("comment_id=?", commentID).Find(&attachments) } +// GetAttachmentsByReleaseID returns all attachments of a release +func GetAttachmentsByReleaseID(releaseId int64) ([]*Attachment, error) { + attachments := make([]*Attachment, 0, 10) + return attachments, x.Where("release_id=?", releaseId).Find(&attachments) +} + // DeleteAttachment deletes the given attachment and optionally the associated file. func DeleteAttachment(a *Attachment, remove bool) error { _, err := DeleteAttachments([]*Attachment{a}, remove) From 377cf381670859e5dcd4c5b546e74b14609634d1 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:21:47 +0300 Subject: [PATCH 19/26] Implemented the api for getting only the attachments of a release, as well getting a given attachment of a release Signed-off-by: Petrisor Lacatus --- routers/api/v1/api.go | 12 ++++++-- routers/api/v1/repo/release.go | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 0e356b1f907ac..8ca0b28b9dd1f 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -420,9 +420,15 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/releases", func() { m.Combo("").Get(repo.ListReleases). Post(bind(api.CreateReleaseOption{}), repo.CreateRelease) - m.Combo("/:id").Get(repo.GetRelease). - Patch(bind(api.EditReleaseOption{}), repo.EditRelease). - Delete(repo.DeleteRelease) + m.Group("/:id", func() { + m.Combo("").Get(repo.GetRelease). + Patch(bind(api.EditReleaseOption{}), repo.EditRelease). + Delete(repo.DeleteRelease) + m.Group("/assets", func() { + m.Combo("").Get(repo.ListReleaseAttachments) + m.Combo("/:assetId").Get(repo.GetReleaseAttachment) + }) + }) }) m.Post("/mirror-sync", repo.MirrorSync) m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig) diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index ed5b8f4f788ba..257a9d34b7b58 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -32,6 +32,56 @@ func GetRelease(ctx *context.APIContext) { ctx.JSON(200, release.APIFormat()) } +// ListReleaseAttachments get all the attachments of a release +func ListReleaseAttachments(ctx *context.APIContext) { + id := ctx.ParamsInt64(":id") + release, err := models.GetReleaseByID(id) + if err != nil { + ctx.Error(500, "GetReleaseByID", err) + return + } + if release.RepoID != ctx.Repo.Repository.ID { + ctx.Status(404) + return + } + // load the attachments of this release + attachments, err := models.GetAttachmentsByReleaseID(id) + if err != nil { + ctx.Error(500, "GetAttachmentsByReleaseID", err) + return + } + // build the attachment list + apiAttachments := make([]*api.Attachment, len(attachments)) + for i := range attachments { + apiAttachments[i] = attachments[i].APIFormat() + } + ctx.JSON(200, apiAttachments) +} + +// GetReleaseAttachment get a single attachment of a release +func GetReleaseAttachment(ctx *context.APIContext) { + id := ctx.ParamsInt64(":id") + attachmentId := ctx.ParamsInt64(":assetId") + release, err := models.GetReleaseByID(id) + if err != nil { + ctx.Error(500, "GetReleaseByID", err) + return + } + if release.RepoID != ctx.Repo.Repository.ID { + ctx.Status(404) + return + } + // load the attachments of this release + attachment, err := models.GetAttachmentByID(attachmentId) + // if the attachment was not found, or it was found but is not associated with this release, then throw 404 + if err != nil || id != attachment.ReleaseID { + ctx.Status(404) + return + } + + ctx.JSON(200, attachment.APIFormat()) +} + // ListReleases list a repository's releases func ListReleases(ctx *context.APIContext) { access, err := models.AccessLevel(ctx.User.ID, ctx.Repo.Repository) From c67b34d2469dce79083fe3bd6741f140dc63490c Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:27:01 +0300 Subject: [PATCH 20/26] When searching for a release attachments, use the simpler method. Signed-off-by: Petrisor Lacatus --- models/release.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/models/release.go b/models/release.go index f12fc06840583..fd1cf573b36d2 100644 --- a/models/release.go +++ b/models/release.go @@ -71,6 +71,14 @@ func (r *Release) loadAttributes(e Engine) error { return err } } + // load the attachments of this release + if r.Attachments == nil { + attachments, err := GetAttachmentsByReleaseID(r.ID) + if err != nil { + return err + } + r.Attachments = attachments + } return nil } @@ -97,6 +105,10 @@ func (r *Release) TarURL() string { // APIFormat convert a Release to api.Release func (r *Release) APIFormat() *api.Release { + apiAttachments := make([]*api.Attachment, len(r.Attachments)) + for i := range r.Attachments { + apiAttachments[i] = r.Attachments[i].APIFormat() + } return &api.Release{ ID: r.ID, TagName: r.TagName, @@ -110,6 +122,7 @@ func (r *Release) APIFormat() *api.Release { CreatedAt: r.Created, PublishedAt: r.Created, Publisher: r.Publisher.APIFormat(), + Attachments: apiAttachments, } } From 3e72f55b85c8b8d6e65b09ca939fdd8bb688132e Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 10:57:26 +0300 Subject: [PATCH 21/26] Fix linting errors Signed-off-by: Petrisor Lacatus --- models/attachment.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index 2239cd2d14491..d6e87e2696b61 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -158,7 +158,7 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return getAttachmentByUUID(x, uuid) } -// GetAttachmentByD returns attachment by given ID. +// GetAttachmentByID returns attachment by given ID. func GetAttachmentByID(id int64) (*Attachment, error) { attach := &Attachment{ID: id} has, err := x.Get(attach) @@ -187,9 +187,9 @@ func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { } // GetAttachmentsByReleaseID returns all attachments of a release -func GetAttachmentsByReleaseID(releaseId int64) ([]*Attachment, error) { +func GetAttachmentsByReleaseID(releaseID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) - return attachments, x.Where("release_id=?", releaseId).Find(&attachments) + return attachments, x.Where("release_id=?", releaseID).Find(&attachments) } // DeleteAttachment deletes the given attachment and optionally the associated file. From ad3ba65173645d73a62552a10040f4b655685eda Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 11:00:36 +0300 Subject: [PATCH 22/26] Fix more linting errors Signed-off-by: Petrisor Lacatus --- routers/api/v1/repo/release.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 257a9d34b7b58..52fd42dd2288b 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -61,7 +61,7 @@ func ListReleaseAttachments(ctx *context.APIContext) { // GetReleaseAttachment get a single attachment of a release func GetReleaseAttachment(ctx *context.APIContext) { id := ctx.ParamsInt64(":id") - attachmentId := ctx.ParamsInt64(":assetId") + attachmentID := ctx.ParamsInt64(":assetId") release, err := models.GetReleaseByID(id) if err != nil { ctx.Error(500, "GetReleaseByID", err) @@ -72,7 +72,7 @@ func GetReleaseAttachment(ctx *context.APIContext) { return } // load the attachments of this release - attachment, err := models.GetAttachmentByID(attachmentId) + attachment, err := models.GetAttachmentByID(attachmentID) // if the attachment was not found, or it was found but is not associated with this release, then throw 404 if err != nil || id != attachment.ReleaseID { ctx.Status(404) From 6f6e21bc62ba63bc7a7f4517da94b0e38b09c537 Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 12:20:03 +0300 Subject: [PATCH 23/26] Changed GetSize for attachment implementation. Added error logging. Signed-off-by: Petrisor Lacatus --- models/attachment.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index d6e87e2696b61..f30025687fcc4 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -16,6 +16,7 @@ import ( "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -61,12 +62,7 @@ func (a *Attachment) IncreaseDownloadCount() error { // GetSize gets the size of the attachment in bytes func (a *Attachment) GetSize() (int64, error) { - f, err := os.Open(a.LocalPath()) - defer f.Close() - if err != nil { - return 0, err - } - info, err := f.Stat() + info, err := os.Stat(a.LocalPath()) if err != nil { return 0, err } @@ -95,6 +91,7 @@ func (a *Attachment) APIFormat() *api.Attachment { DownloadCount: a.DownloadCount, } fileSize, err := a.GetSize() + log.Warn("Error getting the file size for attachment %s. ", a.UUID, err) if err == nil { apiAttachment.Size = fileSize } From 49b4cd703c98eee74df334f13cedaa3e287c1a8a Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 16:43:23 +0300 Subject: [PATCH 24/26] Implementation of `GET /repos/:owner/:repo/releases/latest` Signed-off-by: Petrisor Lacatus --- routers/api/v1/api.go | 1 + routers/api/v1/repo/release.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 8ca0b28b9dd1f..c6fd8e31e9ee0 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -420,6 +420,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/releases", func() { m.Combo("").Get(repo.ListReleases). Post(bind(api.CreateReleaseOption{}), repo.CreateRelease) + m.Combo("/latest").Get(repo.GetLatestRelease) m.Group("/:id", func() { m.Combo("").Get(repo.GetRelease). Patch(bind(api.EditReleaseOption{}), repo.EditRelease). diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 52fd42dd2288b..050e42c17ab6f 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -108,6 +108,26 @@ func ListReleases(ctx *context.APIContext) { ctx.JSON(200, rels) } +// GetLatestRelease Gets the latest release in a repository. Draft releases and prereleases are not returned +func GetLatestRelease(ctx *context.APIContext) { + // we set the pageSize to 1 to get back only one release + releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, 1, 1, false, false) + if err != nil { + ctx.Error(500, "GetReleasesByRepoID", err) + return + } + if len(releases) <= 0 { + // no releases found, just return 404 + ctx.Status(404) + return + } + if err := releases[0].LoadAttributes(); err != nil { + ctx.Error(500, "LoadAttributes", err) + return + } + ctx.JSON(200, releases[0].APIFormat()) +} + // CreateRelease create a release func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) { if ctx.Repo.AccessMode < models.AccessModeWrite { From 159c7f5dd2ea720dc5e97cedcb4c847ee56aed4a Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Fri, 30 Jun 2017 20:32:22 +0300 Subject: [PATCH 25/26] Rebase on top of master Signed-off-by: Petrisor Lacatus --- models/release.go | 10 +++++++--- routers/api/v1/repo/release.go | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/models/release.go b/models/release.go index fd1cf573b36d2..721509a846df2 100644 --- a/models/release.go +++ b/models/release.go @@ -248,8 +248,9 @@ func GetReleaseByID(id int64) (*Release, error) { // FindReleasesOptions describes the conditions to Find releases type FindReleasesOptions struct { - IncludeDrafts bool - TagNames []string + IncludeDrafts bool + IncludePrereleases bool + TagNames []string } func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { @@ -259,13 +260,16 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { if !opts.IncludeDrafts { cond = cond.And(builder.Eq{"is_draft": false}) } + if !opts.IncludeDrafts { + cond = cond.And(builder.Eq{"is_prerelease": false}) + } if len(opts.TagNames) > 0 { cond = cond.And(builder.In("tag_name", opts.TagNames)) } return cond } -// GetReleasesByRepoID returns a list of releases of repository. +// GetReleasesByRepoID returns a list of releases of repository. The results are sorted by created date and id descending func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize int) (rels []*Release, err error) { if page <= 0 { page = 1 diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 050e42c17ab6f..a1aa928291def 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -111,7 +111,10 @@ func ListReleases(ctx *context.APIContext) { // GetLatestRelease Gets the latest release in a repository. Draft releases and prereleases are not returned func GetLatestRelease(ctx *context.APIContext) { // we set the pageSize to 1 to get back only one release - releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, 1, 1, false, false) + releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ + IncludeDrafts: false, + IncludePrereleases: false, + }, 1, 1) if err != nil { ctx.Error(500, "GetReleasesByRepoID", err) return From 856d8ecf7fe2469f3440a6d79fd69576e01ff14c Mon Sep 17 00:00:00 2001 From: Petrisor Lacatus Date: Wed, 5 Jul 2017 10:57:59 +0300 Subject: [PATCH 26/26] Fix incorrect check Signed-off-by: Petrisor Lacatus --- models/release.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/release.go b/models/release.go index 721509a846df2..6d7352ad3c16c 100644 --- a/models/release.go +++ b/models/release.go @@ -260,7 +260,7 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { if !opts.IncludeDrafts { cond = cond.And(builder.Eq{"is_draft": false}) } - if !opts.IncludeDrafts { + if !opts.IncludePrereleases { cond = cond.And(builder.Eq{"is_prerelease": false}) } if len(opts.TagNames) > 0 {