-
Notifications
You must be signed in to change notification settings - Fork 6
/
git.go
55 lines (50 loc) · 1.71 KB
/
git.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
package main
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
)
// GithubAPIBase github api uri
var GithubAPIBase = "https://api.github.com"
func createGithubIssue(env Environment, issueRequest *IssueRequest, channel string) IssueResponse {
request, err := getCreateIssueRequest(env, issueRequest, channel)
var issueResponse IssueResponse
fatal(err)
if err != nil {
setIssueResponseError(&issueResponse, errors.New("Failed to perform getInstallationIDRequest"))
}
body, err2 := processRequest(request)
fatal(err2)
if err2 != nil {
setIssueResponseError(&issueResponse, errors.New("Failed to perform processRequest(request)"))
}
err3 := json.NewDecoder(bytes.NewReader(body)).Decode(&issueResponse)
fatal(err3)
if err3 != nil {
setIssueResponseError(&issueResponse, errors.New("Failed to perform json.NewDecoder(response.Body)"))
}
return issueResponse
}
func getCreateIssueRequest(env Environment, issueRequest *IssueRequest, channel string) (*http.Request, error) {
token, err := getInstallationToken(environment)
fatal(err)
if err != nil {
return nil, errors.New("Failed to get installation token")
}
issueRequest.Labels = append(issueRequest.Labels, "From-"+channel)
postBody, _ := json.Marshal(issueRequest)
log.Printf("Create request for new issue using installation token %v and request body %v", token, string(postBody))
requestBody := bytes.NewBuffer(postBody)
request, err3 := http.NewRequest("POST", GithubIssueURI, requestBody)
fatal(err3)
if err3 != nil {
return nil, errors.New("Failed to perform http.NewRequest")
}
getJwtHeaders(request, token)
return request, nil
}
func setIssueResponseError(issueResponse *IssueResponse, err error) {
issueResponse.Message = err.Error()
}