-
Notifications
You must be signed in to change notification settings - Fork 1
/
zube.go
225 lines (189 loc) · 6.14 KB
/
zube.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
214
215
216
217
218
219
220
221
222
223
224
225
package infrastructure
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
"github.com/shintaro-uchiyama/slack-suite/functions/slack_event/domain"
"github.com/dgrijalva/jwt-go"
)
var _ domain.ZubeInterface = (*Zube)(nil)
type Zube struct {
accessToken string
clientID string
}
type ZubeTokenResponse struct {
AccessToken string `json:"access_token"`
}
func NewZube(zubePrivateKey []byte) (*Zube, error) {
signKey, err := jwt.ParseRSAPrivateKeyFromPEM(zubePrivateKey)
if err != nil {
return nil, fmt.Errorf("load signKey error: %w", err)
}
clientID := os.Getenv("CLIENT_ID")
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(1 * time.Hour).Unix(),
Issuer: clientID,
})
refreshToken, err := token.SignedString(signKey)
if err != nil {
return nil, fmt.Errorf("get token string error: %w", err)
}
httpClient := &http.Client{}
httpReq, err := http.NewRequest("POST", "https://zube.io/api/users/tokens", nil)
if err != nil {
return nil, fmt.Errorf("zube token http request error: %w", err)
}
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", refreshToken))
httpReq.Header.Add("X-Client-ID", clientID)
httpReq.Header.Add("Accept", "application/json")
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("access token erquest error: %w", err)
}
bodyByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read access token response error: %w", err)
}
var response ZubeTokenResponse
if err := json.Unmarshal(bodyByte, &response); err != nil {
return nil, fmt.Errorf("unmarshal access token response error: %w", err)
}
return &Zube{
accessToken: response.AccessToken,
clientID: os.Getenv("CLIENT_ID"),
}, nil
}
type CreateCardRequest struct {
ProjectId int `json:"project_id"`
Title string `json:"title"`
Body string `json:"body"`
LabelIds []int `json:"label_ids"`
CategoryName string `json:"category_name"`
EpicId int `json:"epic_id"`
GithubIssue int `json:"github_issue"`
AssigneeIds []int `json:"assignee_ids"`
Points int `json:"points"`
Priority int `json:"priority"`
SprintId int `json:"sprint_id"`
WorkspaceId int `json:"workspace_id"`
}
type CreateCardResponse struct {
ID int `json:"id"`
}
func (z Zube) Create(task domain.Task) (int, error) {
requestByte, err := json.Marshal(CreateCardRequest{
ProjectId: task.Project().ID(),
WorkspaceId: task.Project().WorkspaceID(),
Title: task.Title(),
Body: task.Body(),
LabelIds: task.Labels(),
})
if err != nil {
return 0, fmt.Errorf("createCardRequest error: %w", err)
}
httpReq, err := http.NewRequest("POST", "https://zube.io/api/cards", bytes.NewReader(requestByte))
if err != nil {
return 0, fmt.Errorf("zube create http request error: %w", err)
}
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", z.accessToken))
httpReq.Header.Add("X-Client-ID", z.clientID)
httpReq.Header.Add("Content-Type", "application/json")
httpClient := &http.Client{}
resp, _ := httpClient.Do(httpReq)
bodyByte, _ := ioutil.ReadAll(resp.Body)
var response CreateCardResponse
if err := json.Unmarshal(bodyByte, &response); err != nil {
return 0, fmt.Errorf("unmarshal zube response error: %w", err)
}
return response.ID, nil
}
func (z Zube) Delete(cardID int) error {
httpClient := &http.Client{}
httpReq, err := http.NewRequest(
"PUT",
fmt.Sprintf("https://zube.io/api/cards/%s/archive", strconv.Itoa(cardID)),
nil,
)
if err != nil {
return fmt.Errorf("zube archive http request error: %w", err)
}
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", z.accessToken))
httpReq.Header.Add("X-Client-ID", z.clientID)
httpReq.Header.Add("Content-Type", "application/json")
_, err = httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("zube archive http request error: %w", err)
}
return nil
}
type Workspace struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Project struct {
ID int `json:"id"`
Name string `json:"name"`
Workspaces []Workspace `json:"workspaces"`
}
type ResponseBody struct {
Data []Project `json:"data"`
}
func (z Zube) GetProjects() ([]Project, error) {
httpClient := &http.Client{}
httpReq, err := http.NewRequest("GET", "https://zube.io/api/projects", nil)
if err != nil {
return nil, fmt.Errorf("zube get projects http request error: %w", err)
}
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", z.accessToken))
httpReq.Header.Add("X-Client-ID", z.clientID)
httpReq.Header.Add("Content-Type", "application/json")
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("convert zube project id from string to int error: %w", err)
}
bodyByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read zube get projects http body error: %w", err)
}
var response ResponseBody
if err := json.Unmarshal(bodyByte, &response); err != nil {
return nil, fmt.Errorf("unmarshal zube response error: %w", err)
}
return response.Data, nil
}
type Label struct {
ID int `json:"id"`
Name string `json:"name"`
}
type LabelsResponse struct {
Data []Label `json:"data"`
}
func (z Zube) GetLabels(zubeProjectID string) (interface{}, error) {
httpClient := &http.Client{}
httpReq, err := http.NewRequest("GET", fmt.Sprintf("https://zube.io/api/projects/%s/labels", zubeProjectID), nil)
if err != nil {
return nil, fmt.Errorf("zube get labels http request error: %w", err)
}
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", z.accessToken))
httpReq.Header.Add("X-Client-ID", z.clientID)
httpReq.Header.Add("Content-Type", "application/json")
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("http request error: %w", err)
}
bodyByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read zube labels http body error: %w", err)
}
var response LabelsResponse
if err := json.Unmarshal(bodyByte, &response); err != nil {
return nil, fmt.Errorf("unmarshal zube response error: %w", err)
}
return response, nil
}