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

Added attachments to the releases API #2084

wants to merge 29 commits into from

Conversation

stefan-lacatus
Copy link

@stefan-lacatus stefan-lacatus commented Jun 29, 2017

Very simple initial fix for #711.
It's also my first interaction with GO so i'm sorry for stupid mistakes.

Includes changes to the endpoints:

  • Get releases GET /repos/:owner/:repo/releases[/:id] - now includes assets
  • List assets: GET /repos/:owner/:repo/releases/:id/assets
  • Get single asset: GET /repos/:owner/:repo/releases/assets/:id

If the API for github must be followed, where attachments are "assets", with a lot of additional metadata then this PR can be skipped, but for my limited usecase this is all I need.

Depends on go-gitea/go-sdk#63

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

The error returned by GetReleaseAttachments(r) is unchecked.

Copy link
Member

@lafriks lafriks left a comment

Choose a reason for hiding this comment

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

I think we should follow GitHub api as close as possible so it would be better to name features same as GitHub so in this case as Assets.

Also do not change anything under vendor directory directly. If changes need to be done in sdk first you need to submit PR in https://github.com/go-gitea/go-sdk

Aslo this would need test

@lafriks lafriks added this to the 1.x.x milestone Jun 29, 2017
@lafriks lafriks added modifies/api This PR adds API routes or modifies them type/enhancement An improvement of existing functionality type/testing and removed type/testing labels Jun 29, 2017
andreynering and others added 2 commits June 29, 2017 21:30
Somes layer are created and aren't usefull, so I compress this :)
@stefan-lacatus
Copy link
Author

Understood. I'll separate this into 2 PRs for the appropriate repos.
I'll also rename the json keys to match the github ones as much as possible.

Thanks for the tips

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 30, 2017
stefan-lacatus and others added 6 commits June 30, 2017 09:08
…nd to get all the attachments associated with a given release

Signed-off-by: Petrisor Lacatus <[email protected]>
as well getting a given attachment of a release

Signed-off-by: Petrisor Lacatus <[email protected]>
@stefan-lacatus
Copy link
Author

Fixed the issue mentioned by @ethantkoenig

  • Get releases GET /repos/:owner/:repo/releases[/:id] - now includes assets
  • List assets: GET /repos/:owner/:repo/releases/:id/assets
  • Get single asset: GET /repos/:owner/:repo/releases/assets/:id

@tboerger : can you give me some quick tips about adding tests? I'm a bit in the dark there.

Signed-off-by: Petrisor Lacatus <[email protected]>
Signed-off-by: Petrisor Lacatus <[email protected]>
@@ -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

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

@@ -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

@lafriks
Copy link
Member

lafriks commented Jun 30, 2017

Please rebase on master

…nd to get all the attachments associated with a given release

Signed-off-by: Petrisor Lacatus <[email protected]>
as well getting a given attachment of a release

Signed-off-by: Petrisor Lacatus <[email protected]>
Signed-off-by: Petrisor Lacatus <[email protected]>
Signed-off-by: Petrisor Lacatus <[email protected]>
Added error logging.

Signed-off-by: Petrisor Lacatus <[email protected]>
Signed-off-by: Petrisor Lacatus <[email protected]>
# Conflicts:
#	models/release.go
#	routers/api/v1/repo/release.go
#	routers/repo/release.go
@@ -246,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 {
Copy link
Member

Choose a reason for hiding this comment

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

There should be check for IncludePrereleases

Signed-off-by: Petrisor Lacatus <[email protected]>
Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

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

Error Checking and a question

DownloadCount: a.DownloadCount,
}
fileSize, err := a.GetSize()
log.Warn("Error getting the file size for attachment %s. ", a.UUID, err)
Copy link
Member

Choose a reason for hiding this comment

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

This should if in a if err != nil check.

I'd do this:

fileSize, err := a.GetSize()
if err != nil {
  log.Warn(...)
}
// Effectively doing `Size = 0` on errors.
apiAttachment.Size = fileSize

return apiAttachment

ctx.Error(500, "LoadAttributes", err)
return
}
ctx.JSON(200, releases[0].APIFormat())
Copy link
Member

Choose a reason for hiding this comment

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

e we sure that [0] is the latest one? (I don't see anything about sorting in the PR 🙂 )

@stefan-lacatus
Copy link
Author

I'll revisit this next week, as i've also identified some bugs internally.

@lunny
Copy link
Member

lunny commented Nov 21, 2017

any update?

@stefan-lacatus
Copy link
Author

I'll redo on top of the master

@lunny lunny removed this from the 1.x.x milestone Dec 3, 2017
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants