From 757f8bdb90333c0861b043d32bf561282207c981 Mon Sep 17 00:00:00 2001 From: cameronbraid Date: Mon, 15 Feb 2021 14:51:38 +0800 Subject: [PATCH] feat: added repo ListContents and changed GetContents doc to talk about a single file (#485) fix #484 Co-authored-by: Cameron Braid Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/485 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Lunny Xiao Co-authored-by: cameronbraid Co-committed-by: cameronbraid --- gitea/repo_file.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/gitea/repo_file.go b/gitea/repo_file.go index e6605c1..87f5d06 100644 --- a/gitea/repo_file.go +++ b/gitea/repo_file.go @@ -9,6 +9,7 @@ import ( "bytes" "encoding/json" "fmt" + "strings" ) // FileOptions options for all file APIs @@ -120,14 +121,24 @@ func (c *Client) GetFile(user, repo, ref, tree string) ([]byte, *Response, error return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s/%s", user, repo, ref, tree), nil, nil) } -// GetContents get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir +// GetContents get the metadata and contents of a file in a repository // ref is optional func (c *Client) GetContents(owner, repo, ref, filepath string) (*ContentsResponse, *Response, error) { cr := new(ContentsResponse) + filepath = strings.TrimPrefix(filepath, "/") resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, cr) return cr, resp, err } +// ListContents gets a list of entries in a dir +// ref is optional +func (c *Client) ListContents(owner, repo, ref, filepath string) ([]*ContentsResponse, *Response, error) { + cr := make([]*ContentsResponse, 0) + filepath = strings.TrimPrefix(filepath, "/") + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, &cr) + return cr, resp, err +} + // CreateFile create a file in a repository func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions) (*FileResponse, *Response, error) { var err error