Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload sarif report file to gh registry #111

Merged
merged 15 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

.DS_Store
.idea/
60 changes: 60 additions & 0 deletions bin/upload_sarif_to_github
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -euo pipefail

sarif_file=$1
owner=$2
repo=$3

if [ -z "$sarif_file" ] || [ -z "$owner" ] || [ -z "$repo" ]; then
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
echo "Not enough arguments provided. Usage: ./upload_sarif_to_gh.sh <path to .sarif report> <gh owner> <gh repo>"
exit 1
fi

if [ ! -r "$sarif_file" ]; then
echo "Error: The specified sarif file '$sarif_file' is not readable."
exit 1
fi
wzieba marked this conversation as resolved.
Show resolved Hide resolved

# Check that GITHUB_TOKEN is set
if [ -z "$GITHUB_TOKEN" ]; then
AliSoftware marked this conversation as resolved.
Show resolved Hide resolved
echo "Error: GITHUB_TOKEN is not defined."
exit 1
fi

sarif_base64_temp_file=$(mktemp)

gzip -c "$sarif_file" | base64 > "$sarif_base64_temp_file"

if [[ -n $BUILDKITE_PULL_REQUEST ]]; then
json=$(jq -n \
--arg commit_sha "$BUILDKITE_COMMIT" \
--arg pr_number "$BUILDKITE_PULL_REQUEST" \
--rawfile sarif "$sarif_base64_temp_file" \
'{
"commit_sha": $commit_sha,
"ref": ("refs/pull/"+$pr_number+"/head"),
"sarif": $sarif
}')
elif [[ "$BUILDKITE_BRANCH" == "$BUILDKITE_PIPELINE_DEFAULT_BRANCH" ]]; then
json=$(jq -n \
--arg commit_sha "$BUILDKITE_COMMIT" \
--arg branch "$BUILDKITE_BRANCH" \
--rawfile sarif "$sarif_base64_temp_file" \
'{
"commit_sha": $commit_sha,
"ref": ("refs/heads/$branch"),
"sarif": $sarif
}')
fi

sarif_json_temp_file=$(mktemp)
echo "$json" > "$sarif_json_temp_file"

curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--data-binary "@$sarif_json_temp_file" \
https://api.github.com/repos/"$owner"/"$repo"/code-scanning/sarifs