From 09e50a90472eb822764c1fb0291b573a1797c05c Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 13:16:50 -0400 Subject: [PATCH 01/10] Adds wrapper for blob content --- gitea/git_blob.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index abdd95a..067df05 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -4,11 +4,35 @@ package gitea +import ( + "bytes" + "code.gitea.io/gitea/modules/git" + "encoding/json" +) + // GitBlobResponse represents a git blob -type GitBlobResponse struct { - Content string `json:"content"` +type BlobResponse struct { + Content *BlobContentResponse `json:"content"` Encoding string `json:"encoding"` URL string `json:"url"` SHA string `json:"sha"` Size int64 `json:"size"` } + +type BlobContentResponse struct { + Blob *git.Blob +} + +func (bc *BlobContentResponse) MarshalJSON() ([]byte, error) { + reader, err := bc.Blob.DataAsync() + if err != nil { + return nil, err + } + defer reader.Close() + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(reader) + if err != nil { + return nil, err + } + return json.Marshal(buf.Bytes()) +} From 7d4988345b22f7d9b2b3e88b0602591b51cda8fa Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 13:19:09 -0400 Subject: [PATCH 02/10] Fixes imports --- gitea/git_blob.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 067df05..57aa576 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -6,8 +6,9 @@ package gitea import ( "bytes" - "code.gitea.io/gitea/modules/git" "encoding/json" + + "code.gitea.io/gitea/modules/git" ) // GitBlobResponse represents a git blob From 1da9749e195c715be20a48ccc889fd64bdb4ac4d Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 15:08:34 -0400 Subject: [PATCH 03/10] Improved wrapping of Blob --- gitea/git_blob.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 57aa576..5c57974 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -21,7 +21,7 @@ type BlobResponse struct { } type BlobContentResponse struct { - Blob *git.Blob + *git.Blob } func (bc *BlobContentResponse) MarshalJSON() ([]byte, error) { From 3a27cefbc5376de1deeafeb705ec890f59a98ee4 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 15:09:30 -0400 Subject: [PATCH 04/10] Updates comment --- gitea/git_blob.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 5c57974..851f24d 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -11,7 +11,7 @@ import ( "code.gitea.io/gitea/modules/git" ) -// GitBlobResponse represents a git blob +// BlobResponse represents a git blob type BlobResponse struct { Content *BlobContentResponse `json:"content"` Encoding string `json:"encoding"` From e69bb3d723883daaba07e7a5c270baea0542d622 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 15:10:36 -0400 Subject: [PATCH 05/10] Updates blob --- gitea/git_blob.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 851f24d..57bea50 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -25,7 +25,7 @@ type BlobContentResponse struct { } func (bc *BlobContentResponse) MarshalJSON() ([]byte, error) { - reader, err := bc.Blob.DataAsync() + reader, err := bc.DataAsync() if err != nil { return nil, err } From 408452a606cae79b171816cc861178cd1eecc29a Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 15:35:18 -0400 Subject: [PATCH 06/10] Adds comment --- gitea/git_blob.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 57bea50..20d7480 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -20,6 +20,7 @@ type BlobResponse struct { Size int64 `json:"size"` } +// BlobContentReponse is a wrapper for a git.Blob to be serializable type BlobContentResponse struct { *git.Blob } From 70946ed78b76e6ea48e52f99508782508aaa079c Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Mon, 1 Apr 2019 15:46:48 -0400 Subject: [PATCH 07/10] Adds comment --- gitea/git_blob.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 20d7480..f64e326 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -20,7 +20,7 @@ type BlobResponse struct { Size int64 `json:"size"` } -// BlobContentReponse is a wrapper for a git.Blob to be serializable +// BlobContentResponse is a wrapper for a git.Blob to be serializable type BlobContentResponse struct { *git.Blob } From 817f1b0f806638608adc96d851347eb5fdfa6842 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Tue, 2 Apr 2019 05:56:40 -0400 Subject: [PATCH 08/10] Makes the UploadFileOptions a bit more clear --- gitea/repo_file.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gitea/repo_file.go b/gitea/repo_file.go index 1e9e45e..5739b36 100644 --- a/gitea/repo_file.go +++ b/gitea/repo_file.go @@ -30,19 +30,20 @@ type CreateFileOptions struct { Content string `json:"content"` } -// DeleteFileOptions options for deleting files (used for other File structs below) -type DeleteFileOptions struct { - FileOptions - SHA string `json:"sha" binding:"Required"` -} - // UpdateFileOptions options for updating files type UpdateFileOptions struct { - DeleteFileOptions + FileOptions + SHA string `json:"sha" binding:"Required"` Content string `json:"content"` FromPath string `json:"from_path" binding:"MaxSize(500)"` } +// DeleteFileOptions options for deleting files (used for other File structs below) +type DeleteFileOptions struct { + FileOptions + SHA string `json:"sha" binding:"Required"` +} + // FileLinksResponse contains the links for a repo's file type FileLinksResponse struct { Self string `json:"url"` From 70146888ac94faed03825b087244678c1fbc0c9a Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Tue, 2 Apr 2019 05:57:16 -0400 Subject: [PATCH 09/10] Format --- gitea/git_blob.go | 8 ++++---- gitea/repo_file.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index f64e326..1ad0cde 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -14,10 +14,10 @@ import ( // BlobResponse represents a git blob type BlobResponse struct { Content *BlobContentResponse `json:"content"` - Encoding string `json:"encoding"` - URL string `json:"url"` - SHA string `json:"sha"` - Size int64 `json:"size"` + Encoding string `json:"encoding"` + URL string `json:"url"` + SHA string `json:"sha"` + Size int64 `json:"size"` } // BlobContentResponse is a wrapper for a git.Blob to be serializable diff --git a/gitea/repo_file.go b/gitea/repo_file.go index 5739b36..13c4237 100644 --- a/gitea/repo_file.go +++ b/gitea/repo_file.go @@ -33,7 +33,7 @@ type CreateFileOptions struct { // UpdateFileOptions options for updating files type UpdateFileOptions struct { FileOptions - SHA string `json:"sha" binding:"Required"` + SHA string `json:"sha" binding:"Required"` Content string `json:"content"` FromPath string `json:"from_path" binding:"MaxSize(500)"` } From 031b2852a9b3f4a8a2cfbc0c0f9a88687056da6b Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Tue, 2 Apr 2019 05:59:28 -0400 Subject: [PATCH 10/10] Addsc omment --- gitea/git_blob.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gitea/git_blob.go b/gitea/git_blob.go index 1ad0cde..1c1e4d4 100644 --- a/gitea/git_blob.go +++ b/gitea/git_blob.go @@ -25,6 +25,7 @@ type BlobContentResponse struct { *git.Blob } +// MarshalJSON Marshals the BlobContentResponse by reading the blob so it can be encoded to base64 func (bc *BlobContentResponse) MarshalJSON() ([]byte, error) { reader, err := bc.DataAsync() if err != nil {