Skip to content

Added attachments to the releases API #2084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
23f0728
Don't ignore gravatar error
ethantkoenig Jun 29, 2017
05e3726
Use default avatar on error
ethantkoenig Jun 29, 2017
2b410e4
lint
ethantkoenig Jun 29, 2017
49f5ddc
Added attachments to the releases API
stefan-lacatus Jun 29, 2017
935b40f
Merge pull request #2083 from ethantkoenig/check_err/org_avatar
andreynering Jun 30, 2017
789188f
Reduce number of layer (#2078)
Jun 30, 2017
b187b79
Revert changes done to vendor package gitea-sdk
stefan-lacatus Jun 30, 2017
8fd43f2
Revert "Reduce number of layer" (#2086)
lunny Jun 30, 2017
bfd3b7d
Added size of the attachment to the api format
stefan-lacatus Jun 30, 2017
270098c
Added two utility methods on attachment, to get an attachment by id a…
stefan-lacatus Jun 30, 2017
7e14752
Implemented the api for getting only the attachments of a release,
stefan-lacatus Jun 30, 2017
ba596d1
When searching for a release attachments, use the simpler method.
stefan-lacatus Jun 30, 2017
b23c0fa
Fix linting errors
stefan-lacatus Jun 30, 2017
36e1893
Fix more linting errors
stefan-lacatus Jun 30, 2017
a5ee8e1
Changed GetSize for attachment implementation.
stefan-lacatus Jun 30, 2017
b36849d
Fix exit status 1 not handled @ getMergeCommit
Bwko Jun 30, 2017
12cb6cd
Merge pull request #2087 from Bwko/fix_error_exit
lafriks Jun 30, 2017
78f201a
Implementation of `GET /repos/:owner/:repo/releases/latest`
stefan-lacatus Jun 30, 2017
31ab468
Added size of the attachment to the api format
stefan-lacatus Jun 30, 2017
9a74130
Added two utility methods on attachment, to get an attachment by id a…
stefan-lacatus Jun 30, 2017
377cf38
Implemented the api for getting only the attachments of a release,
stefan-lacatus Jun 30, 2017
c67b34d
When searching for a release attachments, use the simpler method.
stefan-lacatus Jun 30, 2017
3e72f55
Fix linting errors
stefan-lacatus Jun 30, 2017
ad3ba65
Fix more linting errors
stefan-lacatus Jun 30, 2017
6f6e21b
Changed GetSize for attachment implementation.
stefan-lacatus Jun 30, 2017
49b4cd7
Implementation of `GET /repos/:owner/:repo/releases/latest`
stefan-lacatus Jun 30, 2017
159c7f5
Rebase on top of master
stefan-lacatus Jun 30, 2017
6822f6f
Merge remote-tracking branch 'origin/master'
stefan-lacatus Jun 30, 2017
856d8ec
Fix incorrect check
stefan-lacatus Jul 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path"
"time"

api "code.gitea.io/sdk/gitea"
"github.com/go-xorm/xorm"
gouuid "github.com/satori/go.uuid"

Expand Down Expand Up @@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use

info, err := os.Stat(a.LocalPath());
if err != nil {
    return 0, err
}
return info.Size(), nil

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 {
Expand All @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is error please log it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks

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{
Expand Down Expand Up @@ -126,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)
Expand All @@ -142,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming releaseId -> releaseID

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)
Expand Down
13 changes: 13 additions & 0 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -110,6 +122,7 @@ func (r *Release) APIFormat() *api.Release {
CreatedAt: r.Created,
PublishedAt: r.Created,
Publisher: r.Publisher.APIFormat(),
Attachments: apiAttachments,
}
}

Expand Down
12 changes: 9 additions & 3 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions routers/api/v1/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down