-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathissues.go
99 lines (81 loc) · 2.37 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
package youtrack
import (
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
)
type ProjectID struct {
ID string `json:"id"`
}
type Issue struct {
Summary string `json:"summary"`
Description string `json:"description"`
Project ProjectID `json:"project"`
}
type IDResult struct {
ID string `json:"id"`
}
type IssueResult struct {
IDResult
NumberInProject int `json:"numberInProject"`
}
// IssueURL returns a user facing (rather than REST API) URL to the issue.
// Note that because this issue uses the short project name in the URL, the link
// could be broken if the project short name changes.
func IssueURL(baseURL *url.URL, shortProjectName string, issueNumberInProject int) *url.URL {
path := fmt.Sprintf("../issue/%s-%d", shortProjectName, issueNumberInProject)
return baseURL.ResolveReference(&url.URL{Path: path})
}
func (api *Api) IssueURL(shortProjectName string, issueNumberInProject int) *url.URL {
return IssueURL(api.BaseURL, shortProjectName, issueNumberInProject)
}
// CreateIssue returns the issue ID on success.
func (api *Api) CreateIssue(ctx context.Context, project, summary, description string) (*IssueResult, error) {
issue := &Issue{
Summary: summary,
Description: description,
Project: ProjectID{
ID: project,
},
}
result := new(IssueResult)
u := &url.URL{
Path: "issues",
RawQuery: "fields=id,numberInProject",
}
err := api.Post(ctx, u, issue, result)
if err != nil {
return nil, err
}
return result, nil
}
type IssueAttachment struct {
Name string `json:"name"`
Base64Content string `json:"base64Content"`
}
// CreateIssueAttachment attached a file to the given issue ID. On success, the attachment ID is returned.
func (api *Api) CreateIssueAttachment(ctx context.Context, issueID string, attachment io.Reader, name, mediaType string) (string, error) {
data, err := ioutil.ReadAll(attachment)
if err != nil {
log.Print("Failed to read attachment", err)
return "", err
}
issueAttachment := &IssueAttachment{
Name: name,
Base64Content: "data:" + mediaType + ";base64," + base64.StdEncoding.EncodeToString(data),
}
u := &url.URL{
Path: fmt.Sprintf("issues/%s/attachments", issueID),
}
result := &IDResult{}
err = api.Post(ctx, u, issueAttachment, result)
if err != nil {
log.Print("Failed to post attachment.", err)
return "", err
}
return result.ID, nil
}