-
Notifications
You must be signed in to change notification settings - Fork 53
Add Attachment API #90
Changes from 8 commits
5277236
191c060
771fe76
05408a9
719ad42
51a2a81
1c62562
f13aa38
2e0e53b
d79503e
5b144bb
70dc55f
eeb85eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,101 @@ | |
// license that can be found in the LICENSE file. | ||
|
||
package gitea // import "code.gitea.io/sdk/gitea" | ||
import "time" | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
// Attachment a generic attachment | ||
// swagger:model | ||
type Attachment struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Size int64 `json:"size"` | ||
DownloadCount int64 `json:"download_count"` | ||
Created time.Time `json:"created_at"` | ||
UUID string `json:"uuid"` | ||
DownloadURL string `json:"browser_download_url"` | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Size int64 `json:"size"` | ||
DownloadCount int64 `json:"download_count"` | ||
// swagger:strfmt date-time | ||
Created time.Time `json:"created_at"` | ||
UUID string `json:"uuid"` | ||
DownloadURL string `json:"browser_download_url"` | ||
} | ||
|
||
// ListReleaseAttachments list release's attachments | ||
func (c *Client) ListReleaseAttachments(user, repo string, release int64) ([]*Attachment, error) { | ||
attachments := make([]*Attachment, 0, 10) | ||
err := c.getParsedResponse("GET", | ||
fmt.Sprintf("/repos/%s/%s/releases/%d/attachments", user, repo, release), | ||
nil, nil, &attachments) | ||
return attachments, err | ||
} | ||
|
||
// ListReleaseAttachments list release's attachments | ||
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, error) { | ||
a := new(Attachment) | ||
err := c.getParsedResponse("GET", | ||
fmt.Sprintf("/repos/%s/%s/releases/%d/attachments/%d", user, repo, release, id), | ||
nil, nil, &a) | ||
return a, err | ||
} | ||
|
||
// CreateReleaseAttachment creates an attachment for the given release | ||
func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file *os.File) (*Attachment, error) { | ||
// Read file to upload | ||
fileContents, err := ioutil.ReadAll(file) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that, |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fi, err := file.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
file.Close() | ||
|
||
// Write file to body | ||
body := new(bytes.Buffer) | ||
writer := multipart.NewWriter(body) | ||
part, err := writer.CreateFormFile("attachment", fi.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
part.Write(fileContents) | ||
err = writer.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Send request | ||
attachment := new(Attachment) | ||
err = c.getParsedResponse("POST", | ||
fmt.Sprintf("/repos/%s/%s/releases/%d/attachments", user, repo, release), | ||
http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment) | ||
return attachment, err | ||
} | ||
|
||
// EditReleaseAttachment updates the given attachment with the given options | ||
func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, error) { | ||
body, err := json.Marshal(&form) | ||
if err != nil { | ||
return nil, err | ||
} | ||
attach := new(Attachment) | ||
return attach, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/attachments/%d", user, repo, release, attachment), jsonHeader, bytes.NewReader(body), attach) | ||
} | ||
|
||
// DeleteReleaseAttachment deletes the given attachment including the uploaded file | ||
func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) error { | ||
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/attachments/%d", user, repo, release, id), nil, nil) | ||
return err | ||
} | ||
|
||
// EditAttachmentOptions options for editing attachments | ||
// swagger:model | ||
type EditAttachmentOptions struct { | ||
Name string `json:"name"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,9 @@ type Release struct { | |
// swagger:strfmt date-time | ||
CreatedAt time.Time `json:"created_at"` | ||
// swagger:strfmt date-time | ||
PublishedAt time.Time `json:"published_at"` | ||
Publisher *User `json:"author"` | ||
PublishedAt time.Time `json:"published_at"` | ||
Publisher *User `json:"author"` | ||
Attachments []*Attachment `json:"attachments"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I use here attachments or assets (in JSON)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ref https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository, it should be assets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JonasFranzDEV this is not resolved. |
||
} | ||
|
||
// ListReleases list releases of a repository | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not io.Reader for file content?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is required to get the filename (line 65).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass the filename along? (
fi.Name(), file
)