Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit c026786

Browse files
jonasfranzlunny
authored andcommitted
Add Attachment API (#90)
* Adding assets struct Signed-off-by: Jonas Franz <[email protected]> * Replacing assets with attachments Signed-off-by: Jonas Franz <[email protected]> * Using pointer to attachment instead of copy Signed-off-by: Jonas Franz <[email protected]> * Adding swagger tags to attachment Renaming Assets to Attachments Signed-off-by: Jonas Franz <[email protected]> * Add client functions for attachments Signed-off-by: Jonas Franz <[email protected]> * Add EditAttachmentForm Add CreateReleaseAttachment function Signed-off-by: Jonas Franz <[email protected]> * Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <[email protected]> * Add missing "Content-Type" to CreateReleaseAttachment Signed-off-by: Jonas Franz <[email protected]> * Using reader for uploading file Signed-off-by: Jonas Franz <[email protected]> * Use io.Copy instead of part.Write Signed-off-by: Jonas Franz <[email protected]> * Add error checking Signed-off-by: Jonas Franz <[email protected]> * Replace Attachments with Assets to make API Github compatible Signed-off-by: Jonas Franz <[email protected]>
1 parent 649bdad commit c026786

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

gitea/attachment.go

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,91 @@
33
// license that can be found in the LICENSE file.
44

55
package gitea // import "code.gitea.io/sdk/gitea"
6-
import "time"
6+
import (
7+
"bytes"
8+
"encoding/json"
9+
"fmt"
10+
"io"
11+
"io/ioutil"
12+
"mime/multipart"
13+
"net/http"
14+
"time"
15+
)
716

817
// Attachment a generic attachment
18+
// swagger:model
919
type Attachment struct {
10-
ID int64 `json:"id"`
11-
Name string `json:"name"`
12-
Size int64 `json:"size"`
13-
DownloadCount int64 `json:"download_count"`
14-
Created time.Time `json:"created_at"`
15-
UUID string `json:"uuid"`
16-
DownloadURL string `json:"browser_download_url"`
20+
ID int64 `json:"id"`
21+
Name string `json:"name"`
22+
Size int64 `json:"size"`
23+
DownloadCount int64 `json:"download_count"`
24+
// swagger:strfmt date-time
25+
Created time.Time `json:"created_at"`
26+
UUID string `json:"uuid"`
27+
DownloadURL string `json:"browser_download_url"`
28+
}
29+
30+
// ListReleaseAttachments list release's attachments
31+
func (c *Client) ListReleaseAttachments(user, repo string, release int64) ([]*Attachment, error) {
32+
attachments := make([]*Attachment, 0, 10)
33+
err := c.getParsedResponse("GET",
34+
fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release),
35+
nil, nil, &attachments)
36+
return attachments, err
37+
}
38+
39+
// ListReleaseAttachments list release's attachments
40+
func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64) (*Attachment, error) {
41+
a := new(Attachment)
42+
err := c.getParsedResponse("GET",
43+
fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id),
44+
nil, nil, &a)
45+
return a, err
46+
}
47+
48+
// CreateReleaseAttachment creates an attachment for the given release
49+
func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file *io.Reader, filename string) (*Attachment, error) {
50+
// Write file to body
51+
body := new(bytes.Buffer)
52+
writer := multipart.NewWriter(body)
53+
part, err := writer.CreateFormFile("attachment", filename)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
if _, err = io.Copy(part, file); err != nil {
59+
return nil, err
60+
}
61+
if err = writer.Close(); err != nil {
62+
return nil, err
63+
}
64+
65+
// Send request
66+
attachment := new(Attachment)
67+
err = c.getParsedResponse("POST",
68+
fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release),
69+
http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment)
70+
return attachment, err
71+
}
72+
73+
// EditReleaseAttachment updates the given attachment with the given options
74+
func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachment int64, form EditAttachmentOptions) (*Attachment, error) {
75+
body, err := json.Marshal(&form)
76+
if err != nil {
77+
return nil, err
78+
}
79+
attach := new(Attachment)
80+
return attach, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, attachment), jsonHeader, bytes.NewReader(body), attach)
81+
}
82+
83+
// DeleteReleaseAttachment deletes the given attachment including the uploaded file
84+
func (c *Client) DeleteReleaseAttachment(user, repo string, release int64, id int64) error {
85+
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil)
86+
return err
87+
}
88+
89+
// EditAttachmentOptions options for editing attachments
90+
// swagger:model
91+
type EditAttachmentOptions struct {
92+
Name string `json:"name"`
1793
}

gitea/release.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type Release struct {
2626
// swagger:strfmt date-time
2727
CreatedAt time.Time `json:"created_at"`
2828
// swagger:strfmt date-time
29-
PublishedAt time.Time `json:"published_at"`
30-
Publisher *User `json:"author"`
29+
PublishedAt time.Time `json:"published_at"`
30+
Publisher *User `json:"author"`
31+
Attachments []*Attachment `json:"assets"`
3132
}
3233

3334
// ListReleases list releases of a repository

0 commit comments

Comments
 (0)