-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(codeqlExecuteScan): added waiting for the SARIF file upload (#4409)
* added waiting for the sarif file uploaded & tests * increased polling time, added timeout for waiting response from server & tests * fixed handling error while waiting sarif uploaded * added params for checking sarif uploaded & refactor * added test logs * fixed logs and test * added returning missed error * changed params descriptions and server response error processing processing * fixed retrying logic * increased polling timeout params & refactored
- Loading branch information
1 parent
8b36ae7
commit 6eb4c2e
Showing
6 changed files
with
244 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package codeql | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type CodeqlSarifUploader interface { | ||
GetSarifStatus() (SarifFileInfo, error) | ||
} | ||
|
||
func NewCodeqlSarifUploaderInstance(url, token string) CodeqlSarifUploaderInstance { | ||
return CodeqlSarifUploaderInstance{ | ||
url: url, | ||
token: token, | ||
} | ||
} | ||
|
||
type CodeqlSarifUploaderInstance struct { | ||
url string | ||
token string | ||
} | ||
|
||
func (codeqlSarifUploader *CodeqlSarifUploaderInstance) GetSarifStatus() (SarifFileInfo, error) { | ||
return getSarifUploadingStatus(codeqlSarifUploader.url, codeqlSarifUploader.token) | ||
} | ||
|
||
type SarifFileInfo struct { | ||
ProcessingStatus string `json:"processing_status"` | ||
Errors []string `json:"errors"` | ||
} | ||
|
||
const internalServerError = "Internal server error" | ||
|
||
func getSarifUploadingStatus(sarifURL, token string) (SarifFileInfo, error) { | ||
client := http.Client{} | ||
req, err := http.NewRequest("GET", sarifURL, nil) | ||
if err != nil { | ||
return SarifFileInfo{}, err | ||
} | ||
req.Header.Add("Authorization", "Bearer "+token) | ||
req.Header.Add("Accept", "application/vnd.github+json") | ||
req.Header.Add("X-GitHub-Api-Version", "2022-11-28") | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return SarifFileInfo{}, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode == http.StatusServiceUnavailable || resp.StatusCode == http.StatusBadGateway || | ||
resp.StatusCode == http.StatusGatewayTimeout { | ||
return SarifFileInfo{ProcessingStatus: internalServerError}, nil | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return SarifFileInfo{}, err | ||
} | ||
|
||
sarifInfo := SarifFileInfo{} | ||
err = json.Unmarshal(body, &sarifInfo) | ||
if err != nil { | ||
return SarifFileInfo{}, err | ||
} | ||
return sarifInfo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters