From 5277236e1d0565a2ca690fb63c563f4c8b835ed8 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Thu, 8 Feb 2018 20:48:19 +0100 Subject: [PATCH 01/12] Adding assets struct Signed-off-by: Jonas Franz --- gitea/release.go | 1 + gitea/release_assets.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 gitea/release_assets.go diff --git a/gitea/release.go b/gitea/release.go index d1ac9a4..0cfa166 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -28,6 +28,7 @@ type Release struct { // swagger:strfmt date-time PublishedAt time.Time `json:"published_at"` Publisher *User `json:"author"` + Assets []Asset `json:"assets"` } // ListReleases list releases of a repository diff --git a/gitea/release_assets.go b/gitea/release_assets.go new file mode 100644 index 0000000..e4266e6 --- /dev/null +++ b/gitea/release_assets.go @@ -0,0 +1,24 @@ +// Copyright 2018 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 "time" + +// Asset represent an attachment of issue/comment/release. +type Asset struct { + ID int64 `json:"id"` + UUID string `json:"uuid"` + URL string `json:"url"` + BrowserDownloadURL string `json:"browser_download_url"` + Name string `json:"name"` + ContentType string `json:"content_type"` + Size int64 `json:"size"` + DownloadCount int64 `json:"download_count"` + // swagger:strfmt date-time `json:"url"` + CreatedAt time.Time `json:"created_at"` + // swagger:strfmt date-time `json:"url"` + UpdatedAt time.Time `json:"updated_at"` + Uploader *User `json:"uploader"` +} From 191c0603f33deaf50a88f7f197bc09380c2c3f43 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Thu, 8 Feb 2018 20:57:07 +0100 Subject: [PATCH 02/12] Replacing assets with attachments Signed-off-by: Jonas Franz --- gitea/release.go | 6 +++--- gitea/release_assets.go | 24 ------------------------ 2 files changed, 3 insertions(+), 27 deletions(-) delete mode 100644 gitea/release_assets.go diff --git a/gitea/release.go b/gitea/release.go index 0cfa166..20054be 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -26,9 +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"` - Assets []Asset `json:"assets"` + PublishedAt time.Time `json:"published_at"` + Publisher *User `json:"author"` + Assets []Attachment `json:"assets"` } // ListReleases list releases of a repository diff --git a/gitea/release_assets.go b/gitea/release_assets.go deleted file mode 100644 index e4266e6..0000000 --- a/gitea/release_assets.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 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 "time" - -// Asset represent an attachment of issue/comment/release. -type Asset struct { - ID int64 `json:"id"` - UUID string `json:"uuid"` - URL string `json:"url"` - BrowserDownloadURL string `json:"browser_download_url"` - Name string `json:"name"` - ContentType string `json:"content_type"` - Size int64 `json:"size"` - DownloadCount int64 `json:"download_count"` - // swagger:strfmt date-time `json:"url"` - CreatedAt time.Time `json:"created_at"` - // swagger:strfmt date-time `json:"url"` - UpdatedAt time.Time `json:"updated_at"` - Uploader *User `json:"uploader"` -} From 771fe76a1dafb553fa9f9feacf9dd67c3da11da6 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Thu, 8 Feb 2018 21:09:05 +0100 Subject: [PATCH 03/12] Using pointer to attachment instead of copy Signed-off-by: Jonas Franz --- gitea/release.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gitea/release.go b/gitea/release.go index 20054be..8c32533 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -26,9 +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"` - Assets []Attachment `json:"assets"` + PublishedAt time.Time `json:"published_at"` + Publisher *User `json:"author"` + Assets []*Attachment `json:"assets"` } // ListReleases list releases of a repository From 05408a9188b961646914e6873b97de1b43a86b6a Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Thu, 8 Feb 2018 22:06:42 +0100 Subject: [PATCH 04/12] Adding swagger tags to attachment Renaming Assets to Attachments Signed-off-by: Jonas Franz --- gitea/attachment.go | 16 +++++++++------- gitea/release.go | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index 10e7a1d..4f2b4a1 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -6,12 +6,14 @@ package gitea // import "code.gitea.io/sdk/gitea" import "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"` } diff --git a/gitea/release.go b/gitea/release.go index 8c32533..e2aea69 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -28,7 +28,7 @@ type Release struct { // swagger:strfmt date-time PublishedAt time.Time `json:"published_at"` Publisher *User `json:"author"` - Assets []*Attachment `json:"assets"` + Attachments []*Attachment `json:"attachments"` } // ListReleases list releases of a repository From 719ad42a282dc680daa91c9b9ac3c12baba0dc03 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Thu, 8 Feb 2018 22:56:41 +0100 Subject: [PATCH 05/12] Add client functions for attachments Signed-off-by: Jonas Franz --- gitea/attachment.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index 4f2b4a1..78bb5d1 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -3,7 +3,10 @@ // license that can be found in the LICENSE file. package gitea // import "code.gitea.io/sdk/gitea" -import "time" +import ( + "fmt" + "time" +) // Attachment a generic attachment // swagger:model @@ -17,3 +20,21 @@ type Attachment struct { 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 +} From 51a2a81aaceb4427decef5b8d99c2ae4e13b7112 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Fri, 9 Feb 2018 11:57:31 +0100 Subject: [PATCH 06/12] Add EditAttachmentForm Add CreateReleaseAttachment function Signed-off-by: Jonas Franz --- gitea/attachment.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/gitea/attachment.go b/gitea/attachment.go index 78bb5d1..d6a496c 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -4,7 +4,11 @@ package gitea // import "code.gitea.io/sdk/gitea" import ( + "bytes" "fmt" + "io/ioutil" + "mime/multipart" + "os" "time" ) @@ -38,3 +42,43 @@ func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64 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) + 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), + nil, body, &attachment) + return attachment, err +} + +// EditAttachmentOptions options for editing attachments +type EditAttachmentOptions struct { + Name string `json:"name"` +} From 1c6256217cf1adc88399753202784a7b35c04015 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Fri, 9 Feb 2018 12:17:55 +0100 Subject: [PATCH 07/12] Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz --- gitea/attachment.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gitea/attachment.go b/gitea/attachment.go index d6a496c..f55c669 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -5,6 +5,7 @@ package gitea // import "code.gitea.io/sdk/gitea" import ( "bytes" + "encoding/json" "fmt" "io/ioutil" "mime/multipart" @@ -78,7 +79,24 @@ func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file 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"` } From f13aa386ae08d7fbb8a587043e6c709464cca197 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Fri, 9 Feb 2018 13:07:32 +0100 Subject: [PATCH 08/12] Add missing "Content-Type" to CreateReleaseAttachment Signed-off-by: Jonas Franz --- gitea/attachment.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index f55c669..0cc83d9 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -9,6 +9,7 @@ import ( "fmt" "io/ioutil" "mime/multipart" + "net/http" "os" "time" ) @@ -75,7 +76,7 @@ func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file attachment := new(Attachment) err = c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/releases/%d/attachments", user, repo, release), - nil, body, &attachment) + http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment) return attachment, err } From 2e0e53b816ec80445ad54335e5db243109dda47b Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 10 Feb 2018 14:53:18 +0100 Subject: [PATCH 09/12] Using reader for uploading file Signed-off-by: Jonas Franz --- gitea/attachment.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index 0cc83d9..0b28dd5 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -7,10 +7,10 @@ import ( "bytes" "encoding/json" "fmt" + "io" "io/ioutil" "mime/multipart" "net/http" - "os" "time" ) @@ -46,23 +46,17 @@ func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64 } // CreateReleaseAttachment creates an attachment for the given release -func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file *os.File) (*Attachment, error) { +func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file *io.Reader, filename string) (*Attachment, error) { // Read file to upload fileContents, err := ioutil.ReadAll(file) 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()) + part, err := writer.CreateFormFile("attachment", filename) if err != nil { return nil, err } From d79503e4bc6bfc3b3828537d76fc3043e7d9d3e3 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Fri, 16 Feb 2018 11:42:06 +0100 Subject: [PATCH 10/12] Use io.Copy instead of part.Write Signed-off-by: Jonas Franz --- gitea/attachment.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index 0b28dd5..5345a8c 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -47,12 +47,6 @@ func (c *Client) GetReleaseAttachment(user, repo string, release int64, id int64 // CreateReleaseAttachment creates an attachment for the given release func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file *io.Reader, filename string) (*Attachment, error) { - // Read file to upload - fileContents, err := ioutil.ReadAll(file) - if err != nil { - return nil, err - } - // Write file to body body := new(bytes.Buffer) writer := multipart.NewWriter(body) @@ -60,7 +54,7 @@ func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file if err != nil { return nil, err } - part.Write(fileContents) + io.Copy(part, file) err = writer.Close() if err != nil { return nil, err From 70dc55f4f76b0f5df01d12349d025f6cafbbdc2f Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Fri, 16 Feb 2018 20:15:53 +0100 Subject: [PATCH 11/12] Add error checking Signed-off-by: Jonas Franz --- gitea/attachment.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index 5345a8c..d81e744 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -54,9 +54,11 @@ func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file if err != nil { return nil, err } - io.Copy(part, file) - err = writer.Close() - if err != nil { + + if _, err = io.Copy(part, file); err != nil { + return nil, err + } + if err = writer.Close(); err != nil { return nil, err } From eeb85eb01b2f661f9e4efe01165d17fc89d76f12 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Tue, 27 Feb 2018 19:04:45 +0100 Subject: [PATCH 12/12] Replace Attachments with Assets to make API Github compatible Signed-off-by: Jonas Franz --- gitea/attachment.go | 10 +++++----- gitea/release.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gitea/attachment.go b/gitea/attachment.go index d81e744..b48bcee 100644 --- a/gitea/attachment.go +++ b/gitea/attachment.go @@ -31,7 +31,7 @@ type Attachment struct { 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), + fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release), nil, nil, &attachments) return attachments, err } @@ -40,7 +40,7 @@ func (c *Client) ListReleaseAttachments(user, repo string, release int64) ([]*At 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), + fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil, &a) return a, err } @@ -65,7 +65,7 @@ func (c *Client) CreateReleaseAttachment(user, repo string, release int64, file // Send request attachment := new(Attachment) err = c.getParsedResponse("POST", - fmt.Sprintf("/repos/%s/%s/releases/%d/attachments", user, repo, release), + fmt.Sprintf("/repos/%s/%s/releases/%d/assets", user, repo, release), http.Header{"Content-Type": {writer.FormDataContentType()}}, body, &attachment) return attachment, err } @@ -77,12 +77,12 @@ func (c *Client) EditReleaseAttachment(user, repo string, release int64, attachm 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) + return attach, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%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) + _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/releases/%d/assets/%d", user, repo, release, id), nil, nil) return err } diff --git a/gitea/release.go b/gitea/release.go index e2aea69..396251d 100644 --- a/gitea/release.go +++ b/gitea/release.go @@ -28,7 +28,7 @@ type Release struct { // swagger:strfmt date-time PublishedAt time.Time `json:"published_at"` Publisher *User `json:"author"` - Attachments []*Attachment `json:"attachments"` + Attachments []*Attachment `json:"assets"` } // ListReleases list releases of a repository