forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf_suggestion.go
216 lines (180 loc) · 6.18 KB
/
bf_suggestion.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
package service
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/util"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
func getBFSuggestionClient(bbProj evergreen.BuildBaronProject) *bfSuggestionClient {
if bbProj.BFSuggestionServer == "" {
return nil
}
return &bfSuggestionClient{
bbProj.BFSuggestionServer,
bbProj.BFSuggestionUsername,
bbProj.BFSuggestionPassword}
}
const suggestionPath = "/suggestions/{task_id}/{execution}"
const getFeedbackPath = "/feedback/{task_id}/{execution}/user_id/{user_id}"
const sendFeedbackPath = "/feedback/{task_id}/{execution}"
const removeFeedbackPath = "/feedback/{task_id}/{execution}/user_id/{user_id}/feedback_type/{feedback_type}"
type bfSuggestionClient struct {
server string
username string
password string
}
// Send a request to the BF Suggestion server.
func (bfsc *bfSuggestionClient) request(ctx context.Context, req *http.Request) ([]byte, error) {
client := util.GetHTTPClient()
defer util.PutHTTPClient(client)
if bfsc.username != "" {
req.SetBasicAuth(bfsc.username, bfsc.password)
}
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "Failed to read HTTP response body (status: %s)", resp.Status)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("HTTP request returned unexpected status: %s (body: %s)", resp.Status, string(body))
}
return body, nil
}
// Send a GET request to the BF Suggestion server.
func (bfsc *bfSuggestionClient) get(ctx context.Context, url string) ([]byte, error) {
var req *http.Request
var err error
req, err = http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return bfsc.request(ctx, req)
}
// Send a POST request to the BF Suggestion server.
func (bfsc *bfSuggestionClient) post(ctx context.Context, url string, data interface{}) ([]byte, error) {
var body io.Reader = nil
if data != nil {
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, errors.Wrap(err, "Failed to encode the request body into JSON")
}
body = bytes.NewReader(jsonBytes)
}
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
return bfsc.request(ctx, req)
}
// Send a DELETE request to the BF Suggestion server.
func (bfsc *bfSuggestionClient) delete(ctx context.Context, url string) ([]byte, error) {
var req *http.Request
var err error
req, err = http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
return bfsc.request(ctx, req)
}
type bfSuggestion struct {
TestName string `json:"test_name"`
Issues []struct {
Key string `json:"key"`
Summary string `json:"summary"`
Status string `json:"status"`
Resolution string `json:"resolution"`
CreatedDate string `json:"created_date"`
UpdatedDate string `json:"updated_date"`
}
}
type bfSuggestionResponse struct {
Status string `json:"status"`
Suggestions []bfSuggestion `json:"suggestions"`
}
// Retrieve suggestions from the BF Suggestion server.
func (bfsc *bfSuggestionClient) getSuggestions(ctx context.Context, t *task.Task) (bfSuggestionResponse, error) {
data := bfSuggestionResponse{}
url := bfsc.server + suggestionPath
url = strings.Replace(url, "{task_id}", t.Id, -1)
url = strings.Replace(url, "{execution}", strconv.Itoa(t.Execution), -1)
body, err := bfsc.get(ctx, url)
if err != nil {
return data, err
}
if err = json.Unmarshal(body, &data); err != nil {
return data, errors.Wrap(err, "Failed to parse Build Baron suggestions")
}
if data.Status != "ok" {
return data, errors.Errorf("Build Baron suggestions weren't ready: status=%s", data.Status)
}
return data, nil
}
type feedbackItem struct {
UserId string `json:"user_id"`
FeedbackType string `json:"type"`
FeedbackData map[string]interface{} `json:"data"`
}
type feedbackResponse struct {
TaskId string `json:"task_id"`
Execution int `json:"execution"`
FeedbackItems []feedbackItem `json:"items"`
}
// Retrieve user feedback from the BF Suggestion server.
func (bfsc *bfSuggestionClient) getFeedback(ctx context.Context, t *task.Task, userId string) ([]feedbackItem, error) {
data := feedbackResponse{}
items := []feedbackItem{}
url := bfsc.server + getFeedbackPath
url = strings.Replace(url, "{task_id}", t.Id, -1)
url = strings.Replace(url, "{execution}", strconv.Itoa(t.Execution), -1)
url = strings.Replace(url, "{user_id}", userId, -1)
body, err := bfsc.get(ctx, url)
if err != nil {
return items, err
}
if err = json.Unmarshal(body, &data); err != nil {
return items, errors.Wrap(err, "Failed to parse Build Baron feedback")
}
return data.FeedbackItems, nil
}
// Send user feedback to the BF Suggestion server.
func (bfsc *bfSuggestionClient) sendFeedback(ctx context.Context, t *task.Task, userId string,
feedbackType string, feedbackData map[string]interface{}) error {
feedback := feedbackItem{userId, feedbackType, feedbackData}
url := bfsc.server + sendFeedbackPath
url = strings.Replace(url, "{task_id}", t.Id, -1)
url = strings.Replace(url, "{execution}", strconv.Itoa(t.Execution), -1)
_, err := bfsc.post(ctx, url, feedback)
if err != nil {
grip.Error(fmt.Sprintf("Failed to send feedback to BF Suggestion server (url: %s, data: %v): %s", url, feedback, err))
}
return err
}
// Remove user feedback from the BF Suggestion server.
func (bfsc *bfSuggestionClient) removeFeedback(ctx context.Context, t *task.Task, userId string,
feedbackType string) error {
url := bfsc.server + removeFeedbackPath
url = strings.Replace(url, "{task_id}", t.Id, -1)
url = strings.Replace(url, "{execution}", strconv.Itoa(t.Execution), -1)
url = strings.Replace(url, "{user_id}", userId, -1)
url = strings.Replace(url, "{feedback_type}", feedbackType, -1)
_, err := bfsc.delete(ctx, url)
if err != nil {
grip.Error(fmt.Sprintf("Failed to delete user feedback from BF Suggestion server (url: %s): %s", url, err))
}
return err
}