Skip to content

Commit e352b2a

Browse files
authored
Merge pull request #16 from step-security/int
main <- int
2 parents c6cc08e + 9d34588 commit e352b2a

4 files changed

Lines changed: 12 additions & 259 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ jobs:
4444
runs-on: ubuntu-latest
4545
permissions:
4646
contents: read
47-
pull-requests: write
48-
id-token: write
47+
pull-requests: read
4948
steps:
5049
- name: Harden Runner
5150
uses: step-security/harden-runner@128a63446a954579617e875aaab7d2978154e969 # v2.4.0

apiclient.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@ func (apiclient *ApiClient) performRequest(method, url string, headers map[strin
3333
return nil, err
3434
}
3535

36-
for k, v := range headers {
37-
req.Header.Add(k, v)
36+
if headers != nil {
37+
for key, value := range headers {
38+
req.Header.Add(key, value)
39+
}
3840
}
3941

4042
return apiclient.Client.Do(req)
4143
}
4244

43-
func (apiclient *ApiClient) SubmitCodeReviewRequest(oidcToken string, prDetails *PullRequestDetails) (*CodeReviewRequestResponse, error) {
45+
func (apiclient *ApiClient) SubmitCodeReviewRequest(prDetails *PullRequestDetails) (*CodeReviewRequestResponse, error) {
4446
url := fmt.Sprintf("%s/codereview/submit", apiclient.ApiBaseURI)
4547
jsonData, _ := json.Marshal(prDetails)
4648

4749
headers := map[string]string{
48-
"Content-Type": "application/json; charset=UTF-8",
49-
"Authorization": fmt.Sprintf("Bearer %s", oidcToken),
50+
"Content-Type": "application/json; charset=UTF-8",
5051
}
5152

5253
resp, err := apiclient.performRequest("POST", url, headers, bytes.NewBuffer(jsonData))
@@ -69,14 +70,10 @@ func (apiclient *ApiClient) SubmitCodeReviewRequest(oidcToken string, prDetails
6970
return &codeReviewRequestResponse, nil
7071
}
7172

72-
func (apiclient *ApiClient) GetCodeReviewComments(oidcToken string, request *CodeReviewRequestResponse) (*CodeReviewCommentsResponse, error) {
73+
func (apiclient *ApiClient) GetCodeReviewComments(request *CodeReviewRequestResponse) (*CodeReviewCommentsResponse, error) {
7374
url := fmt.Sprintf("%s/codereview/comments?fullreponame=%s&codereviewid=%s", apiclient.ApiBaseURI, request.FullRepoName, request.CodeReviewID)
7475

75-
headers := map[string]string{
76-
"Authorization": fmt.Sprintf("Bearer %s", oidcToken),
77-
}
78-
79-
resp, err := apiclient.performRequest("GET", url, headers, nil)
76+
resp, err := apiclient.performRequest("GET", url, nil, nil)
8077
if err != nil {
8178
return nil, err
8279
}

main.go

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"io/ioutil"
98
"net/http"
109
"os"
1110
"strconv"
@@ -40,15 +39,6 @@ const (
4039
OperationStatusError = "Error"
4140
)
4241

43-
func getTokenRemainingValidity(timestamp interface{}) float64 {
44-
if validity, ok := timestamp.(float64); ok {
45-
tm := time.Unix(int64(validity), 0)
46-
remainder := time.Until(tm)
47-
return remainder.Seconds()
48-
}
49-
return 0
50-
}
51-
5242
func getGitHubClient() (*github.Client, context.Context, error) {
5343
pat := os.Getenv("INPUT_PAT")
5444
if len(pat) == 0 {
@@ -159,22 +149,12 @@ func getPullRequestDetailsFromEnvironment(isDebugMode bool) (*PullRequestDetails
159149

160150
func submitPRDetailsAndGetCodeFeedback(prDetails *PullRequestDetails, isDebugMode bool) (bool, error) {
161151
responseReceived := false
162-
audience := APIEndpoint
163-
oidcClient, err := DefaultOIDCClient(audience)
164-
if err != nil {
165-
return responseReceived, fmt.Errorf("error generating OIDC auth token. error:%v", err)
166-
}
167-
168-
actionsJWT, exp, err := getActionsJWTAndExp(oidcClient, isDebugMode)
169-
if err != nil {
170-
return responseReceived, fmt.Errorf("error generating OIDC auth token. error:%v", err)
171-
}
172152

173153
apiClient := ApiClient{
174154
Client: &http.Client{},
175155
ApiBaseURI: APIEndpoint + "/v1/app/",
176156
}
177-
response, err := apiClient.SubmitCodeReviewRequest(actionsJWT.Value, prDetails)
157+
response, err := apiClient.SubmitCodeReviewRequest(prDetails)
178158
if err != nil {
179159
return responseReceived, fmt.Errorf("error submitting code review request: %v", err)
180160
}
@@ -185,15 +165,7 @@ func submitPRDetailsAndGetCodeFeedback(prDetails *PullRequestDetails, isDebugMod
185165
var reviewComments *CodeReviewCommentsResponse
186166

187167
for i := 0; i < 20 && !responseReceived; i++ {
188-
remainder := getTokenRemainingValidity(exp)
189-
if remainder < 60 {
190-
githubactions.Infof("Renewing OIDC token as it's only valid for %f", remainder)
191-
actionsJWT, exp, err = getActionsJWTAndExp(oidcClient, isDebugMode)
192-
if err != nil {
193-
return responseReceived, fmt.Errorf("error renewing OIDC token. Error: %v", err)
194-
}
195-
}
196-
reviewComments, err = apiClient.GetCodeReviewComments(actionsJWT.Value, response)
168+
reviewComments, err = apiClient.GetCodeReviewComments(response)
197169
if err != nil {
198170
return responseReceived, fmt.Errorf("error retrieving code review comments: %v", err)
199171
}
@@ -204,30 +176,7 @@ func submitPRDetailsAndGetCodeFeedback(prDetails *PullRequestDetails, isDebugMod
204176
responseReceived = true
205177
if reviewComments.Status == OperationStatusError {
206178
message := fmt.Sprintf("Error while using StepSecurity AI Code Reviewer. \nError details:%s", reviewComments.Error)
207-
client, ctx, err := getGitHubClient()
208-
if err != nil {
209-
return responseReceived, fmt.Errorf("error getting github client:%v", err)
210-
}
211-
comment := "COMMENT"
212-
_, commentResponse, err := client.PullRequests.CreateReview(
213-
ctx,
214-
prDetails.GitHubAccountName,
215-
prDetails.RepositoryName,
216-
prDetails.PullNumber,
217-
&github.PullRequestReviewRequest{
218-
Body: &message,
219-
Event: &comment,
220-
})
221-
if err != nil {
222-
errorMessage := fmt.Sprintf("Error writing comment on pull request: %v\n", err)
223-
responseBody, err := ioutil.ReadAll(commentResponse.Body)
224-
if err == nil {
225-
errorMessage += fmt.Sprintf(" response body:%s", responseBody)
226-
} else {
227-
errorMessage += fmt.Sprintf(" could not retrieve response body for error details. error:%v", err)
228-
}
229-
return responseReceived, errors.New(errorMessage)
230-
}
179+
githubactions.Errorf(message)
231180
}
232181
break
233182
}
@@ -259,22 +208,6 @@ func main() {
259208

260209
if !responseReceived {
261210
message := "StepSecurity AI Code Reviewer request timed out after 10 minutes"
262-
comment := "COMMENT"
263-
client, ctx, err := getGitHubClient()
264-
if err != nil {
265-
githubactions.Errorf("error getting github client:%v", err)
266-
return
267-
}
268-
client.PullRequests.CreateReview(
269-
ctx,
270-
prDetails.GitHubAccountName,
271-
prDetails.RepositoryName,
272-
prDetails.PullNumber,
273-
&github.PullRequestReviewRequest{
274-
Body: &message,
275-
Event: &comment,
276-
})
277-
278211
githubactions.Fatalf(message)
279212
}
280213
}

oidc.go

Lines changed: 0 additions & 176 deletions
This file was deleted.

0 commit comments

Comments
 (0)