1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 )
13
14
15 type Tree struct {
16 SHA *string `json:"sha,omitempty"`
17 Entries []*TreeEntry `json:"tree,omitempty"`
18
19
20
21
22
23 Truncated *bool `json:"truncated,omitempty"`
24 }
25
26 func (t Tree) String() string {
27 return Stringify(t)
28 }
29
30
31
32
33 type TreeEntry struct {
34 SHA *string `json:"sha,omitempty"`
35 Path *string `json:"path,omitempty"`
36 Mode *string `json:"mode,omitempty"`
37 Type *string `json:"type,omitempty"`
38 Size *int `json:"size,omitempty"`
39 Content *string `json:"content,omitempty"`
40 URL *string `json:"url,omitempty"`
41 }
42
43 func (t TreeEntry) String() string {
44 return Stringify(t)
45 }
46
47
48
49
50
51 type treeEntryWithFileDelete struct {
52 SHA *string `json:"sha"`
53 Path *string `json:"path,omitempty"`
54 Mode *string `json:"mode,omitempty"`
55 Type *string `json:"type,omitempty"`
56 Size *int `json:"size,omitempty"`
57 Content *string `json:"content,omitempty"`
58 URL *string `json:"url,omitempty"`
59 }
60
61 func (t *TreeEntry) MarshalJSON() ([]byte, error) {
62 if t.SHA == nil && t.Content == nil {
63 return json.Marshal(struct {
64 SHA *string `json:"sha"`
65 Path *string `json:"path,omitempty"`
66 Mode *string `json:"mode,omitempty"`
67 Type *string `json:"type,omitempty"`
68 }{
69 nil,
70 t.Path,
71 t.Mode,
72 t.Type,
73 })
74 }
75 return json.Marshal(struct {
76 SHA *string `json:"sha,omitempty"`
77 Path *string `json:"path,omitempty"`
78 Mode *string `json:"mode,omitempty"`
79 Type *string `json:"type,omitempty"`
80 Size *int `json:"size,omitempty"`
81 Content *string `json:"content,omitempty"`
82 URL *string `json:"url,omitempty"`
83 }{
84 SHA: t.SHA,
85 Path: t.Path,
86 Mode: t.Mode,
87 Type: t.Type,
88 Size: t.Size,
89 Content: t.Content,
90 URL: t.URL,
91 })
92 }
93
94
95
96
97 func (s *GitService) GetTree(ctx context.Context, owner string, repo string, sha string, recursive bool) (*Tree, *Response, error) {
98 u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha)
99 if recursive {
100 u += "?recursive=1"
101 }
102
103 req, err := s.client.NewRequest("GET", u, nil)
104 if err != nil {
105 return nil, nil, err
106 }
107
108 t := new(Tree)
109 resp, err := s.client.Do(ctx, req, t)
110 if err != nil {
111 return nil, resp, err
112 }
113
114 return t, resp, nil
115 }
116
117
118 type createTree struct {
119 BaseTree string `json:"base_tree,omitempty"`
120 Entries []interface{} `json:"tree"`
121 }
122
123
124
125
126
127
128 func (s *GitService) CreateTree(ctx context.Context, owner string, repo string, baseTree string, entries []*TreeEntry) (*Tree, *Response, error) {
129 u := fmt.Sprintf("repos/%v/%v/git/trees", owner, repo)
130
131 newEntries := make([]interface{}, 0, len(entries))
132 for _, entry := range entries {
133 if entry.Content == nil && entry.SHA == nil {
134 newEntries = append(newEntries, treeEntryWithFileDelete{
135 Path: entry.Path,
136 Mode: entry.Mode,
137 Type: entry.Type,
138 Size: entry.Size,
139 URL: entry.URL,
140 })
141 continue
142 }
143 newEntries = append(newEntries, entry)
144 }
145
146 body := &createTree{
147 BaseTree: baseTree,
148 Entries: newEntries,
149 }
150 req, err := s.client.NewRequest("POST", u, body)
151 if err != nil {
152 return nil, nil, err
153 }
154
155 t := new(Tree)
156 resp, err := s.client.Do(ctx, req, t)
157 if err != nil {
158 return nil, resp, err
159 }
160
161 return t, resp, nil
162 }
163
View as plain text