forked from harness/gitness
/
contents.go
59 lines (49 loc) · 1.6 KB
/
contents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package github
import (
"encoding/base64"
"fmt"
)
// These API methods let you retrieve the contents of files within a repository
// as Base64 encoded content.
type ContentResource struct {
client *Client
}
type Content struct {
Type string `json:"type"`
Name string `json:"name"`
Path string `json:"path"`
Encoding string `json:"encoding"`
Content string `json:"content"`
Size int64 `json:"size"`
Sha string `json:"sha"`
}
func (c *Content) DecodeContent() ([]byte, error) {
return base64.StdEncoding.DecodeString(c.Content)
}
// This method returns the contents of a file or directory in a repository.
func (r *ContentResource) Find(owner, repo, path string) (*Content, error) {
content := Content{}
url_path := fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, path)
if err := r.client.do("GET", url_path, nil, &content); err != nil {
return nil, err
}
return &content, nil
}
// This method returns the contents of a file or directory in a repository.
func (r *ContentResource) FindRef(owner, repo, path, ref string) (*Content, error) {
content := Content{}
url_path := fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, path, ref)
if err := r.client.do("GET", url_path, nil, &content); err != nil {
return nil, err
}
return &content, nil
}
// This method returns the preferred README for a repository.
func (r *ContentResource) ReadMe(owner, repo string) (*Content, error) {
content := Content{}
path := fmt.Sprintf("/repos/%s/%s/readme", owner, repo)
if err := r.client.do("GET", path, nil, &content); err != nil {
return nil, err
}
return &content, nil
}