-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathissues.go
103 lines (87 loc) · 2.79 KB
/
issues.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
package taigo
import (
"errors"
"fmt"
"strconv"
"github.com/google/go-querystring/query"
)
// IssueService is a handle to actions related to Issues
//
// https://taigaio.github.io/taiga-doc/dist/api.html#issues
type IssueService struct {
client *Client
defaultProjectID int
Endpoint string
}
// List => https://taigaio.github.io/taiga-doc/dist/api.html#issues-list
func (s *IssueService) List(queryParams *IssueQueryParams) ([]Issue, error) {
url := s.client.MakeURL(s.Endpoint)
switch {
case queryParams != nil:
paramValues, _ := query.Values(queryParams)
url = fmt.Sprintf("%s?%s", url, paramValues.Encode())
case s.defaultProjectID != 0:
url = url + projectIDQueryParam(s.defaultProjectID)
}
// execute requests
var issues IssueDetailLIST
_, err := s.client.Request.Get(url, &issues)
if err != nil {
return nil, err
}
return issues.AsIssues()
}
// CreateAttachment creates a new Issue attachment => https://taigaio.github.io/taiga-doc/dist/api.html#issues-create-attachment
func (s *IssueService) CreateAttachment(attachment *Attachment, issue *Issue) (*Attachment, error) {
url := s.client.MakeURL(s.Endpoint, "attachments")
return newfileUploadRequest(s.client, url, attachment, issue)
}
// Get -> https://taigaio.github.io/taiga-doc/dist/api.html#issues-get
//
// Available Meta: *IssueDetailGET
func (s *IssueService) Get(issueID int) (*Issue, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(issueID))
var issue IssueDetailGET
_, err := s.client.Request.Get(url, &issue)
if err != nil {
return nil, err
}
return issue.AsIssue()
}
// Edit sends a PATCH request to edit a Issue -> https://taigaio.github.io/taiga-doc/dist/api.html#issues-edit
// Available Meta: IssueDetail
func (s *IssueService) Edit(issue *Issue) (*Issue, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(issue.ID))
var responseIssue IssueDetail
if issue.ID == 0 {
return nil, errors.New("passed Issue does not have an ID yet. Does it exist?")
}
// Taiga OCC
remoteIssue, err := s.Get(issue.ID)
if err != nil {
return nil, err
}
issue.Version = remoteIssue.Version
_, err = s.client.Request.Patch(url, &issue, &responseIssue)
if err != nil {
return nil, err
}
return responseIssue.AsIssue()
}
// Create creates a new Issue | https://taigaio.github.io/taiga-doc/dist/api.html#issues-create
//
// Available Meta: *IssueDetail
func (s *IssueService) Create(issue *Issue) (*Issue, error) {
url := s.client.MakeURL(s.Endpoint)
var issueDetail IssueDetail
// Check for required fields
// project, subject
if isEmpty(issue.Project) || isEmpty(issue.Subject) {
return nil, errors.New("a mandatory field is missing. See API documentataion")
}
_, err := s.client.Request.Post(url, &issue, &issueDetail)
if err != nil {
return nil, err
}
return issueDetail.AsIssue()
}