-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachments.go
72 lines (65 loc) · 2.1 KB
/
attachments.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
package redmine
import (
"io"
"net/http"
"net/url"
"strconv"
redmine "github.com/nixys/nxs-go-redmine/v5"
)
// UploadRequest is a request to upload a file
// you can use either only Path to specify the file from the filesystem
// OR Stream to specify the file from a stream. In this case, Path is used as a filename
type UploadRequest struct {
Path string
Stream io.Reader
}
// DeleteAttachment deletes an attachment by its ID
func (r *Redmine) DeleteAttachment(attachmentID int64) error {
log := r.cfg.Log.With().Int64("attachment_id", attachmentID).Logger()
if !r.Enabled() {
log.Debug().Msg("redmine is disabled, ignoring DeleteAttachment() call")
return nil
}
if attachmentID == 0 {
return nil
}
r.wg.Add(1)
defer r.wg.Done()
err := Retry(&log, func() (redmine.StatusCode, error) {
return r.cfg.api.Del(nil, nil, url.URL{Path: "/attachments/" + strconv.FormatInt(attachmentID, 10) + ".json"}, http.StatusNoContent)
})
if err != nil {
log.Error().Err(err).Msg("failed to delete attachment")
return err
}
return nil
}
// uploadAttachments uploads attachments to Redmine, if any
// why it's not exported? Because Redmine REST API doesn't have a method to upload an attachment to an issue,
// attachment has to be uploaded first, and then attached to an issue. That can be done using NewIssue() or UpdateIssue() methods
func (r *Redmine) uploadAttachments(files ...*UploadRequest) *[]redmine.AttachmentUploadObject {
var uploads *[]redmine.AttachmentUploadObject
for _, req := range files {
if req == nil {
continue
}
upload, err := RetryResult(r.cfg.Log, func() (redmine.AttachmentUploadObject, redmine.StatusCode, error) {
if req.Stream == nil {
return r.cfg.api.AttachmentUpload(req.Path)
}
if streamCloser, ok := req.Stream.(io.Closer); ok {
defer streamCloser.Close()
}
return r.cfg.api.AttachmentUploadStream(req.Stream, req.Path)
})
if err != nil {
r.cfg.Log.Error().Err(err).Msg("failed to upload attachment")
continue
}
if uploads == nil {
uploads = &[]redmine.AttachmentUploadObject{}
}
*uploads = append(*uploads, upload)
}
return uploads
}