-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
179 lines (154 loc) · 4.97 KB
/
handler.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
package main
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/google/go-github/v56/github"
"github.com/shurcooL/githubv4"
)
type handler struct {
ghClient *github.Client
ghGraphQLClient *githubv4.Client
httpClient *http.Client
}
func newHandler(ghClient *github.Client, ghGraphQLClient *githubv4.Client) *handler {
return &handler{
ghClient: ghClient,
ghGraphQLClient: ghGraphQLClient,
httpClient: &http.Client{Timeout: 10 * time.Second},
}
}
func (h *handler) handleRunTask(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Failed to load request: %v", err)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
key := os.Getenv("TFC_RUN_TASK_HMAC_KEY")
mac := hmac.New(sha512.New, []byte(key))
mac.Write(body)
expectedSig := hex.EncodeToString(mac.Sum(nil))
requestedSig := r.Header.Get("X-TFC-Task-Signature")
if expectedSig != requestedSig {
log.Printf("Invalid x-tfc-task-signature value: %s. Please check your HMAC Key", requestedSig)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
if r.Method != http.MethodPost {
log.Printf("This method is not alloed: %s. Expected: %s.", r.Method, http.MethodPost)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
req := &TFERunTasksRequest{}
if err := json.Unmarshal(body, req); err != nil {
log.Printf("Failed to unmarshal request: %v", err)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
if req.AccessToken == "test-token" {
log.Printf("Succeeded initializing run tasks")
return
}
ctx := context.Background()
if req.VCSPullRequestURL == "" {
log.Printf("Skip this run because this might not be the event based on PR: %s", req.RunID)
msg := "Skipped pushing the plan result to VCS"
if err := h.sendCallback(ctx, req.TaskResultCallbackURL, req.AccessToken, msg); err != nil {
log.Printf("Failed to send callback to TFC: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
return
}
plan, err := parsePlan(ctx, h.httpClient, req.PlanJSONAPIURL, req.AccessToken)
if err != nil {
log.Printf("Failed to get the plan: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
url, err := newGitURL(req.VCSPullRequestURL)
if err != nil {
log.Printf("Unable to parse VCS pull request URL: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
var (
owner = url.Owner()
repo = url.Repository()
prNumber = url.PullRequest()
)
latestComment, err := findLatestComment(ctx, h.ghGraphQLClient, owner, repo, prNumber)
if err != nil && !errors.Is(err, errNotFound) {
log.Printf("Unable to query the previous comment to minimize: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
comment, err := makeIssueComment(plan, req.RunAppURL, req.VCSCommitURL)
if err != nil {
log.Printf("Failed to get the plan: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
if err := createIssueComment(ctx, h.ghClient, owner, repo, prNumber, comment); err != nil {
log.Printf("Failed to create an issue comment: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
if latestComment != nil && bool(!latestComment.IsMinimized) {
if err := minimizeComment(ctx, h.ghGraphQLClient, latestComment.ID, "OUTDATED"); err != nil {
log.Printf("Failed to minimize comment: %v", err)
return
}
}
msg := "Succeeded pushing the plan result to VCS"
if err := h.sendCallback(ctx, req.TaskResultCallbackURL, req.AccessToken, msg); err != nil {
log.Printf("Failed to send callback to TFC: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
}
func (h *handler) sendCallback(ctx context.Context, url, token, message string) error {
data := &TFERunTasksResponse{
Data: &TFERunTasksResponseData{
Type: "task-results",
Attributes: &TFERunTasksResponseAttributes{
Status: "passed",
Message: message,
},
},
}
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(data); err != nil {
log.Printf("Failed to marshal a response data")
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, buf)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/vnd.api+json")
resp, err := h.httpClient.Do(req)
if err != nil {
log.Printf("Unable to send data to webhook url: %v", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Printf("Unexpected status was returned: %d", resp.StatusCode)
return fmt.Errorf("Unexpected status was returned: %d", resp.StatusCode)
}
_, err = io.ReadAll(resp.Body)
return err
}