microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/1637-e-ps74-pytest

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

.github/skills/github/gh-code-scanning/scripts/get-code-scanning-alerts.sh

88lines · modecode

1#!/usr/bin/env bash
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4#
5# get-code-scanning-alerts.sh
6# Retrieves and groups GitHub code scanning alerts for a repository.
7#
8# Usage:
9# ./get-code-scanning-alerts.sh -o OWNER -r REPO [-b BRANCH] [-s SEVERITY]
10#
11# Prerequisites:
12# - gh CLI installed and authenticated with security_events scope
13# - jq installed
14
15set -euo pipefail
16
17OWNER=''
18REPO=''
19BRANCH='main'
20SEVERITY=''
21
22usage() {
23 echo "Usage: $0 -o OWNER -r REPO [-b BRANCH] [-s SEVERITY]" >&2
24 echo " -o Repository owner (required)" >&2
25 echo " -r Repository name (required)" >&2
26 echo " -b Branch name (default: main)" >&2
27 echo " -s Filter by security severity (critical, high, medium, low)" >&2
28 exit 1
29}
30
31while getopts ':o:r:b:s:' opt; do
32 case "$opt" in
33 o) OWNER="$OPTARG" ;;
34 r) REPO="$OPTARG" ;;
35 b) BRANCH="$OPTARG" ;;
36 s) SEVERITY="$OPTARG" ;;
37 *) usage ;;
38 esac
39done
40
41if [[ -z "$OWNER" || -z "$REPO" ]]; then
42 echo "Error: -o OWNER and -r REPO are required." >&2
43 usage
44fi
45
46if [[ ! "$OWNER" =~ ^[a-zA-Z0-9._-]+$ ]]; then
47 echo "Error: -o OWNER contains invalid characters." >&2; exit 1
48fi
49if [[ ! "$REPO" =~ ^[a-zA-Z0-9._-]+$ ]]; then
50 echo "Error: -r REPO contains invalid characters." >&2; exit 1
51fi
52if [[ ! "$BRANCH" =~ ^[a-zA-Z0-9._/-]+$ ]]; then
53 echo "Error: -b BRANCH contains invalid characters." >&2; exit 1
54fi
55if [[ -n "$SEVERITY" && ! "$SEVERITY" =~ ^(critical|high|medium|low)$ ]]; then
56 echo "Error: -s SEVERITY must be critical, high, medium, or low." >&2; exit 1
57fi
58
59if ! command -v gh &>/dev/null; then
60 echo "Error: gh CLI not found. Install from https://cli.github.com" >&2
61 exit 1
62fi
63
64if ! command -v jq &>/dev/null; then
65 echo "Error: jq not found. Install from https://jqlang.github.io/jq/" >&2
66 exit 1
67fi
68
69if ! gh auth status &>/dev/null; then
70 echo "Error: gh CLI not authenticated. Run 'gh auth login' and ensure security_events scope." >&2
71 exit 1
72fi
73
74URL="repos/${OWNER}/${REPO}/code-scanning/alerts?state=open&ref=refs/heads/${BRANCH}&per_page=100"
75
76if [[ -n "$SEVERITY" ]]; then
77 URL="${URL}&severity=${SEVERITY}"
78fi
79
80GH_PAGER='' gh api "$URL" --paginate --jq '.[]' | \
81 jq -s 'group_by(.rule.description) | map({
82 RuleDescription: .[0].rule.description,
83 RuleId: .[0].rule.id,
84 Tool: .[0].tool.name,
85 SecuritySeverity: .[0].rule.security_severity_level,
86 Count: length,
87 SamplePaths: ([.[].most_recent_instance.location.path] | unique | sort)
88 }) | sort_by(-.Count)'
89