Skip to content

Commit 757f8bd

Browse files
cameronbraidcameronbraid
authored andcommitted
feat: added repo ListContents and changed GetContents doc to talk about a single file (#485)
fix #484 Co-authored-by: Cameron Braid <[email protected]> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/485 Reviewed-by: 6543 <[email protected]> Reviewed-by: Lunny Xiao <[email protected]> Co-authored-by: cameronbraid <[email protected]> Co-committed-by: cameronbraid <[email protected]>
1 parent 95ed973 commit 757f8bd

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

gitea/repo_file.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"bytes"
1010
"encoding/json"
1111
"fmt"
12+
"strings"
1213
)
1314

1415
// FileOptions options for all file APIs
@@ -120,14 +121,24 @@ func (c *Client) GetFile(user, repo, ref, tree string) ([]byte, *Response, error
120121
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s/%s", user, repo, ref, tree), nil, nil)
121122
}
122123

123-
// GetContents get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
124+
// GetContents get the metadata and contents of a file in a repository
124125
// ref is optional
125126
func (c *Client) GetContents(owner, repo, ref, filepath string) (*ContentsResponse, *Response, error) {
126127
cr := new(ContentsResponse)
128+
filepath = strings.TrimPrefix(filepath, "/")
127129
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, cr)
128130
return cr, resp, err
129131
}
130132

133+
// ListContents gets a list of entries in a dir
134+
// ref is optional
135+
func (c *Client) ListContents(owner, repo, ref, filepath string) ([]*ContentsResponse, *Response, error) {
136+
cr := make([]*ContentsResponse, 0)
137+
filepath = strings.TrimPrefix(filepath, "/")
138+
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, filepath, ref), jsonHeader, nil, &cr)
139+
return cr, resp, err
140+
}
141+
131142
// CreateFile create a file in a repository
132143
func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions) (*FileResponse, *Response, error) {
133144
var err error

0 commit comments

Comments
 (0)