-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment.go
213 lines (181 loc) · 6.13 KB
/
attachment.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package backlog
import (
"encoding/json"
"errors"
"io"
"path"
"strconv"
)
func validateAttachmentID(attachmentID int) error {
if attachmentID < 1 {
return newValidationError("attachmentID must not be less than 1")
}
return nil
}
// SpaceAttachmentService hs methods for attachment.
type SpaceAttachmentService struct {
method *method
}
// Upload uploads a any file to the space.
//
// File's path and name are must not empty.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/post-attachment-file
func (s *SpaceAttachmentService) Upload(fileName string, r io.Reader) (*Attachment, error) {
resp, err := s.method.Upload("space/attachment", fileName, r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
v := Attachment{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, err
}
return &v, nil
}
func listAttachments(get clientGet, spath string) ([]*Attachment, error) {
resp, err := get(spath, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
v := []*Attachment{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, err
}
return v, nil
}
func removeAttachment(delete clientDelete, spath string) (*Attachment, error) {
resp, err := delete(spath, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
v := Attachment{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, err
}
return &v, nil
}
// WikiAttachmentService hs methods for attachment file of wiki.
type WikiAttachmentService struct {
method *method
}
// Attach attachs files uploaded to space to the wiki.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/attach-file-to-wiki
func (s *WikiAttachmentService) Attach(wikiID int, attachmentIDs []int) ([]*Attachment, error) {
if err := validateWikiID(wikiID); err != nil {
return nil, err
}
if len(attachmentIDs) == 0 {
return nil, errors.New("attachmentIDs must not be empty")
}
form := NewFormParams()
for _, id := range attachmentIDs {
if err := validateAttachmentID(id); err != nil {
return nil, err
}
form.Add("attachmentId[]", strconv.Itoa(id))
}
spath := path.Join("wikis/", strconv.Itoa(wikiID), "/attachments")
resp, err := s.method.Post(spath, form)
if err != nil {
return nil, err
}
defer resp.Body.Close()
v := []*Attachment{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, err
}
return v, nil
}
// List returns a list of all attachments in the wiki.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-list-of-wiki-attachments
func (s *WikiAttachmentService) List(wikiID int) ([]*Attachment, error) {
if err := validateWikiID(wikiID); err != nil {
return nil, err
}
spath := path.Join("wikis", strconv.Itoa(wikiID), "attachments")
return listAttachments(s.method.Get, spath)
}
// Remove removes a file attached to the wiki.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/remove-wiki-attachment
func (s *WikiAttachmentService) Remove(wikiID, attachmentID int) (*Attachment, error) {
if err := validateWikiID(wikiID); err != nil {
return nil, err
}
if err := validateAttachmentID(attachmentID); err != nil {
return nil, err
}
spath := path.Join("wikis", strconv.Itoa(wikiID), "attachments", strconv.Itoa(attachmentID))
return removeAttachment(s.method.Delete, spath)
}
// IssueAttachmentService hs methods for attachment file of issue.
type IssueAttachmentService struct {
method *method
}
// List returns a list of all attachments in the issue.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-list-of-issue-attachments
func (s *IssueAttachmentService) List(issueIDOrKey string) ([]*Attachment, error) {
if err := validateIssueIDOrKey(issueIDOrKey); err != nil {
return nil, err
}
spath := path.Join("issues", issueIDOrKey, "attachments")
return listAttachments(s.method.Get, spath)
}
// Remove removes a file attached to the issue.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/delete-issue-attachment
func (s *IssueAttachmentService) Remove(issueIDOrKey string, attachmentID int) (*Attachment, error) {
if err := validateIssueIDOrKey(issueIDOrKey); err != nil {
return nil, err
}
if err := validateAttachmentID(attachmentID); err != nil {
return nil, err
}
spath := path.Join("issues", issueIDOrKey, "attachments", strconv.Itoa(attachmentID))
return removeAttachment(s.method.Delete, spath)
}
// PullRequestAttachmentService hs methods for attachment file of pull request.
type PullRequestAttachmentService struct {
method *method
}
// List returns a list of all attachments in the pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-list-of-pull-request-attachment
func (s *PullRequestAttachmentService) List(projectIDOrKey string, repositoryIDOrName string, prNumber int) ([]*Attachment, error) {
if err := validateProjectIDOrKey(projectIDOrKey); err != nil {
return nil, err
}
if err := validateRepositoryIDOrName(repositoryIDOrName); err != nil {
return nil, err
}
if err := validatePRNumber(prNumber); err != nil {
return nil, err
}
spath := path.Join("projects", projectIDOrKey, "git", "repositories", repositoryIDOrName, "pullRequests", strconv.Itoa(prNumber), "attachments")
return listAttachments(s.method.Get, spath)
}
// Remove removes a file attached to the pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/delete-pull-request-attachments
func (s *PullRequestAttachmentService) Remove(projectIDOrKey string, repositoryIDOrName string, prNumber int, attachmentID int) (*Attachment, error) {
if err := validateProjectIDOrKey(projectIDOrKey); err != nil {
return nil, err
}
if err := validateRepositoryIDOrName(repositoryIDOrName); err != nil {
return nil, err
}
if err := validatePRNumber(prNumber); err != nil {
return nil, err
}
if err := validateAttachmentID(attachmentID); err != nil {
return nil, err
}
spath := path.Join("projects", projectIDOrKey, "git", "repositories", repositoryIDOrName, "pullRequests", strconv.Itoa(prNumber), "attachments", strconv.Itoa(attachmentID))
return removeAttachment(s.method.Delete, spath)
}