-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
213 lines (178 loc) · 6.48 KB
/
main.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/google/go-github/v48/github"
"github.com/sethvargo/go-githubactions"
"golang.org/x/oauth2"
)
// To be set by the build workflow
var APIEndpoint string
type PullRequestFileChanges struct {
File string `json:"file"`
Status string `json:"status"`
Patch string `json:"patch"`
}
type PullRequestDetails struct {
GitHubAccountName string `json:"github_account_name"`
RepositoryName string `json:"repository_name"`
PullNumber int `json:"pull_number"`
FileChanges []PullRequestFileChanges `json:"file_changes"`
PullRequestAuthor string `json:"pull_request_author"`
}
const (
OperationStatusDispatched = "Dispatched"
OperationStatusSucceeded = "Succeeded"
OperationStatusError = "Error"
)
func getGitHubClient() (*github.Client, context.Context, error) {
pat := os.Getenv("INPUT_PAT")
if len(pat) == 0 {
return nil, nil, errors.New("a GitHub token must be passed as 'PAT' variable to the action")
}
ctx := context.Background()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: pat},
)
httpClient := oauth2.NewClient(ctx, tokenSource)
return github.NewClient(httpClient), ctx, nil
}
func getDebugMode() bool {
isDebugMode := false
debugModeStr, exists := os.LookupEnv("INPUT_DEBUGMODE")
if exists {
debugMode, err := strconv.ParseBool(debugModeStr)
if err == nil {
isDebugMode = debugMode
}
}
return isDebugMode
}
func printDebugMessageIfRequired(isDebugMode bool, format string, args ...any) {
if isDebugMode {
githubactions.Infof(format, args)
}
}
func getPullRequestDetailsFromEnvironment(isDebugMode bool) (*PullRequestDetails, error) {
gitHubRepository, exists := os.LookupEnv("GITHUB_REPOSITORY")
if !exists {
return nil, errors.New("could not read GITHUB_REPOSITORY environment variable")
}
gitHubRepositoryParts := strings.Split(gitHubRepository, "/")
githubAccountName := gitHubRepositoryParts[0]
repositoryName := gitHubRepositoryParts[1]
gitHubEvent, exists := os.LookupEnv("GITHUB_EVENT_NAME")
if !exists {
return nil, errors.New("could not read GITHUB_EVENT_NAME environment variable")
}
if !strings.EqualFold(gitHubEvent, "pull_request") {
return nil, errors.New("github event is not pull request")
}
gitHubRef, exists := os.LookupEnv("GITHUB_REF")
if !exists {
return nil, errors.New("could not read GITHUB_REF environment variable")
}
gitHubRefParts := strings.Split(gitHubRef, "/")
if len(gitHubRefParts) != 4 {
return nil, errors.New("environment variable GITHUB_REF is not in expected format")
}
pullNumber, err := strconv.Atoi(gitHubRefParts[2])
if err != nil {
return nil, fmt.Errorf("error converting pull request number %s to int. error:%v", gitHubRefParts[2], err)
}
client, ctx, err := getGitHubClient()
if err != nil {
return nil, err
}
pr, _, err := client.PullRequests.Get(ctx, githubAccountName, repositoryName, pullNumber)
if err != nil {
return nil, fmt.Errorf("error getting pull request: %v", err)
}
// Get GitHub user who created this pull request
pullRequestAuthor := pr.GetUser().GetLogin()
printDebugMessageIfRequired(isDebugMode, "owner:%s repo=%s pullNumber=%d author=%s", githubAccountName, repositoryName, pullNumber, pullRequestAuthor)
comparison, _, err := client.Repositories.CompareCommits(ctx, githubAccountName, repositoryName, pr.GetBase().GetSHA(), pr.GetHead().GetSHA(), &github.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error comparing commits: %v", err)
}
prDetails := PullRequestDetails{
GitHubAccountName: githubAccountName,
RepositoryName: repositoryName,
PullNumber: pullNumber,
FileChanges: []PullRequestFileChanges{},
PullRequestAuthor: pullRequestAuthor,
}
// Print file changes
for _, file := range comparison.Files {
printDebugMessageIfRequired(isDebugMode, "File: %s, Status: %s Diff:\n%s\n", file.GetFilename(), file.GetStatus(), file.GetPatch())
prDetails.FileChanges = append(prDetails.FileChanges, PullRequestFileChanges{
File: file.GetFilename(),
Status: file.GetStatus(),
Patch: file.GetPatch(),
})
}
return &prDetails, nil
}
func submitPRDetailsAndGetCodeFeedback(prDetails *PullRequestDetails, isDebugMode bool) (bool, error) {
responseReceived := false
apiClient := ApiClient{
Client: &http.Client{},
ApiBaseURI: APIEndpoint + "/v1/app/",
}
response, err := apiClient.SubmitCodeReviewRequest(prDetails)
if err != nil {
return responseReceived, fmt.Errorf("error submitting code review request: %v", err)
}
responseBytes, _ := json.Marshal(response)
printDebugMessageIfRequired(isDebugMode, "SubmitCodeReviewResponse:%s", string(responseBytes))
time.Sleep(20 * time.Second)
var reviewComments *CodeReviewCommentsResponse
for i := 0; i < 20 && !responseReceived; i++ {
reviewComments, err = apiClient.GetCodeReviewComments(response)
if err != nil {
return responseReceived, fmt.Errorf("error retrieving code review comments: %v", err)
}
if reviewComments.Status == OperationStatusDispatched {
githubactions.Infof("%d attempt to retrieve response: sleeping for 30 seconds", i)
time.Sleep(30 * time.Second)
} else {
responseReceived = true
if reviewComments.Status == OperationStatusError {
message := fmt.Sprintf("Error while using StepSecurity AI Code Reviewer. \nError details:%s", reviewComments.Error)
githubactions.Errorf(message)
}
break
}
}
return responseReceived, nil
}
func main() {
isDebugMode := getDebugMode()
envVariables := strings.Join(os.Environ(), ",")
printDebugMessageIfRequired(isDebugMode, "Environment Variables:%s", envVariables)
prDetails, err := getPullRequestDetailsFromEnvironment(isDebugMode)
if err != nil {
githubactions.Errorf("could not retrieve pull request details. Error:%v", err)
return
}
if strings.EqualFold(prDetails.PullRequestAuthor, "dependabot[bot]") || strings.EqualFold(prDetails.PullRequestAuthor, "renovate[bot]") {
githubactions.Infof("Skipping as the PR is created by a dependency update bot (%s)", prDetails.PullRequestAuthor)
return
}
responseReceived, err := submitPRDetailsAndGetCodeFeedback(prDetails, isDebugMode)
if err != nil {
githubactions.Errorf("error while processing pull request changes with StepSecurity APIs. Error details:%v", err)
return
}
if !responseReceived {
message := "StepSecurity AI Code Reviewer request timed out after 10 minutes"
githubactions.Fatalf(message)
}
}