-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapiclient.go
103 lines (82 loc) · 2.71 KB
/
apiclient.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 main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
type ApiClient struct {
Client *http.Client
ApiBaseURI string
RepoName string
GitHubAccountName string
}
type CodeReviewRequestResponse struct {
FullRepoName string `json:"full_repo_name"`
CodeReviewID string `json:"code_review_id"`
}
type CodeReviewCommentsResponse struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
WaitInSeconds int `json:"wait_in_seconds"`
CommentsCreated bool `json:"comments_created"`
}
func (apiclient *ApiClient) performRequest(method, url string, headers map[string]string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
if headers != nil {
for key, value := range headers {
req.Header.Add(key, value)
}
}
return apiclient.Client.Do(req)
}
func (apiclient *ApiClient) SubmitCodeReviewRequest(prDetails *PullRequestDetails) (*CodeReviewRequestResponse, error) {
url := fmt.Sprintf("%s/codereview/submit", apiclient.ApiBaseURI)
jsonData, _ := json.Marshal(prDetails)
headers := map[string]string{
"Content-Type": "application/json; charset=UTF-8",
}
resp, err := apiclient.performRequest("POST", url, headers, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var codeReviewRequestResponse CodeReviewRequestResponse
err = json.NewDecoder(resp.Body).Decode(&codeReviewRequestResponse)
if err != nil {
return nil, err
}
return &codeReviewRequestResponse, nil
}
func (apiclient *ApiClient) GetCodeReviewComments(request *CodeReviewRequestResponse) (*CodeReviewCommentsResponse, error) {
url := fmt.Sprintf("%s/codereview/comments?fullreponame=%s&codereviewid=%s", apiclient.ApiBaseURI, request.FullRepoName, request.CodeReviewID)
resp, err := apiclient.performRequest("GET", url, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d, message: %s", resp.StatusCode, string(responseBody))
}
var codeReviewCommentsResponse CodeReviewCommentsResponse
err = json.Unmarshal(responseBody, &codeReviewCommentsResponse)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON: %v", err)
}
return &codeReviewCommentsResponse, nil
}