Last active
April 29, 2023 04:30
-
-
Save navicore/1470153740193748c2b2db57914dd07d to your computer and use it in GitHub Desktop.
bash scritp to walk a repo looking for prometheus alerts and give the git info for each alert in a csv report
This file contains hidden or 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
| #!/bin/bash | |
| # Set the file pattern | |
| file_pattern="telemetry*.yaml" | |
| # Set the Git repository path | |
| repo_path="." | |
| # Initialize the report with header | |
| report="alert_name,first_added,first_added_user,last_changed,last_changed_user,file_path\n" | |
| # Find files matching the pattern and process each of them | |
| while IFS= read -r file_path; do | |
| # Extract line numbers of the alerts | |
| alert_lines=$(grep -n "^\s*- alert" "$file_path" | cut -d : -f 1) | |
| # Iterate over the alert lines | |
| for line in $alert_lines; do | |
| # Find the commit that first added the line | |
| first_commit=$(git -C $repo_path log --reverse -S "^\s*- alert" -p -L ${line},${line}:"$file_path" | grep '^commit' | head -1 | awk '{print $2}') | |
| # Find the date and user of the first commit | |
| first_commit_info=$(git -C $repo_path show -s --format='%ci,%an' $first_commit) | |
| first_commit_date=$(echo $first_commit_info | cut -d ',' -f 1) | |
| first_commit_user=$(echo $first_commit_info | cut -d ',' -f 2-) | |
| # Find the most recent commit that changed the line | |
| last_commit=$(git -C $repo_path log -S "^\s*- alert" -p -L ${line},${line}:"$file_path" | grep '^commit' | head -1 | awk '{print $2}') | |
| # Find the date and user of the most recent commit | |
| last_commit_info=$(git -C $repo_path show -s --format='%ci,%an' $last_commit) | |
| last_commit_date=$(echo $last_commit_info | cut -d ',' -f 1) | |
| last_commit_user=$(echo $last_commit_info | cut -d ',' -f 2-) | |
| # Find the alert name | |
| alert_name=$(sed -n "${line}p" "$file_path" | awk -F': ' '{print $2}') | |
| # Add the alert, the dates, the users, and the file path to the report | |
| report+="${alert_name},${first_commit_date},${first_commit_user},${last_commit_date},${last_commit_user},${file_path}\n" | |
| done | |
| done < <(find "$repo_path" -name "$file_pattern") | |
| # Print the report | |
| echo -e "$report" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment