1+ #! /bin/bash
2+
3+ # -----------------------------------------------------------------------------
4+ # Script Name: get-list-of-resolved-secret-scanning-alerts.sh
5+ # Description: This script retrieves and lists all resolved secret scanning
6+ # alerts for a specified GitHub repository. It uses the GitHub API
7+ # to fetch the alerts and displays them in a tabular format.
8+ #
9+ # Usage:
10+ # ./get-list-of-resolved-secret-scanning-alerts.sh -o <organization> -r <repository> [-t <token>]
11+ #
12+ # Parameters:
13+ # -o <organization> GitHub organization name (required)
14+ # -r <repository> GitHub repository name (required)
15+ # -t <token> GitHub personal access token (optional, will use GITHUB_TOKEN
16+ # environment variable if not provided)
17+ # -h Display help message
18+ #
19+ # Requirements:
20+ # - curl: Command-line tool for making HTTP requests
21+ # - jq: Command-line JSON processor
22+ #
23+ # Notes:
24+ # - The script supports pagination to handle repositories with a large number
25+ # of resolved alerts.
26+ # - The GitHub token must have the necessary permissions to access secret
27+ # scanning alerts for the specified repository.
28+ # -----------------------------------------------------------------------------
29+
30+ # Function to display usage information
31+ function display_usage {
32+ echo " Usage: $0 -o <organization> -r <repository> [-t <token>]"
33+ echo " -o <organization> GitHub organization name"
34+ echo " -r <repository> GitHub repository name"
35+ echo " -t <token> GitHub personal access token (optional, will use GITHUB_TOKEN env var if not provided)"
36+ echo " -h Display this help message"
37+ exit 1
38+ }
39+
40+ # Parse command line arguments
41+ while getopts " o:r:t:h" opt; do
42+ case ${opt} in
43+ o ) org_name=$OPTARG ;; # GitHub organization name
44+ r ) repo_name=$OPTARG ;; # GitHub repository name
45+ t ) github_token=$OPTARG ;; # GitHub personal access token
46+ h ) display_usage ;; # Display help message
47+ \? ) display_usage ;; # Handle invalid options
48+ esac
49+ done
50+
51+ # Check if required parameters are provided
52+ if [ -z " $org_name " ] || [ -z " $repo_name " ]; then
53+ echo " Error: Organization name and repository name are required."
54+ display_usage
55+ fi
56+
57+ # If token not provided as argument, try to use GITHUB_TOKEN environment variable
58+ if [ -z " $github_token " ]; then
59+ github_token=$GITHUB_TOKEN
60+ if [ -z " $github_token " ]; then
61+ echo " Error: GitHub token not provided. Either provide it with -t option or set the GITHUB_TOKEN environment variable."
62+ exit 1
63+ fi
64+ fi
65+
66+ # Set API URL for secret scanning alerts with state=resolved
67+ api_url=" https://api.github.com/repos/$org_name /$repo_name /secret-scanning/alerts?state=resolved&per_page=100"
68+ page=1
69+ total_alerts=0
70+
71+ # Display header for the output table
72+ echo " Retrieving resolved secret scanning alerts for $org_name /$repo_name ..."
73+ echo " --------------------------------------------------------------------"
74+ echo " | Alert ID | Created At | Resolved At | Secret Type | Resolution |"
75+ echo " --------------------------------------------------------------------"
76+
77+ # Loop through paginated results
78+ while true ; do
79+ # Make API request
80+ response=$( curl -s -H " Authorization: token $github_token " \
81+ -H " Accept: application/vnd.github.v3+json" \
82+ -H " X-GitHub-Api-Version: 2022-11-28" \
83+ " $api_url &page=$page " )
84+
85+ # Check if response contains error
86+ if echo " $response " | grep -q " message" ; then
87+ error_message=$( echo " $response " | grep -o ' "message":"[^"]*' | cut -d' "' -f4)
88+ echo " Error: $error_message "
89+ exit 1
90+ fi
91+
92+ # Check if response is empty array
93+ if [ " $response " = " []" ]; then
94+ break
95+ fi
96+
97+ # Count the number of alerts in this page and add to total
98+ page_alerts=$( echo " $response " | jq ' . | length' )
99+ total_alerts=$(( total_alerts + page_alerts))
100+
101+ # Process and display alerts
102+ echo " $response " | jq -r ' .[] | [.number, .created_at, .resolved_at, .secret_type, .resolution] | @tsv' |
103+ while read -r alert_id created_at resolved_at secret_type resolution; do
104+ # Format dates for better readability
105+ created_date=$( date -d " $created_at " " +%Y-%m-%d %H:%M" 2> /dev/null || echo " $created_at " )
106+ resolved_date=$( date -d " $resolved_at " " +%Y-%m-%d %H:%M" 2> /dev/null || echo " $resolved_at " )
107+
108+ printf " | %-8s | %-19s | %-19s | %-20s | %-10s |\n" \
109+ " $alert_id " " $created_date " " $resolved_date " " $secret_type " " $resolution "
110+ done
111+
112+ # Check if there are more pages
113+ link_header=$( curl -s -I -H " Authorization: token $github_token " \
114+ -H " Accept: application/vnd.github.v3+json" \
115+ -H " X-GitHub-Api-Version: 2022-11-28" \
116+ " $api_url &page=$page " | grep -i " link:" )
117+
118+ if ! echo " $link_header " | grep -q ' rel="next"' ; then
119+ break
120+ fi
121+
122+ (( page++ ))
123+ done
124+
125+ # Display footer and total count
126+ echo " --------------------------------------------------------------------"
127+ echo " Total resolved secret scanning alerts found: $total_alerts "
128+ echo " "
129+ echo " Note: This script requires 'curl' and 'jq' to be installed."
0 commit comments