Last active
March 7, 2024 06:20
-
-
Save knrt10/09a407721ecc1dd73c6db1942735d69a to your computer and use it in GitHub Desktop.
headlamp changelog script
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
#!/usr/bin/env bash | |
# The env variable RANGE specifies the range of commits to be searched for the changelog. | |
# If unset the latest tag until origin/main will be set. | |
# | |
# The env variable GITHUB_TOKEN can specify a GitHub personal access token. | |
# Otherwise, one could run into GitHub rate limits. Go to | |
# https://github.com/settings/tokens to generate a token. | |
set -e | |
if [ -z "${LATEST_TAG}" ]; then | |
echo "Please set the LATEST_TAG environment variable." | |
exit 1 | |
fi | |
RANGE="${LATEST_TAG}..origin/main" | |
if [ ! -z "${GITHUB_TOKEN}" ]; then | |
GITHUB_AUTH="Authorization: Bearer ${GITHUB_TOKEN}" | |
HEADERS=(-H "${GITHUB_AUTH}") | |
fi | |
# Define custom category mappings | |
declare -A custom_categories=( | |
[enhancement]="✨ Enhancements" | |
[bug]="🐞 Bug fixes" | |
[frontend]="Frontend Changes" | |
[backend]="Backend Changes" | |
[documentation]="📝 Documentation" | |
[plugins]="🧱 Plugins" | |
[development]="💻 Development" | |
[chart]="Helm charts" | |
[other]="Other" | |
) | |
# Function to add a pull request to the appropriate category | |
add_to_category() { | |
local label="$1" | |
local title="$2" | |
local pr="$3" | |
local url="$4" | |
echo "- ${title} ([#${pr}](${url}))" >> "category_${label}.md" | |
} | |
# Prioritize labels based on their index in the categories list | |
get_priority_label() { | |
local label_list="$1" | |
local highest_priority="other" | |
for category in "${!custom_categories[@]}"; do | |
if [[ "$label_list" == *"$category"* ]]; then | |
highest_priority="$category" | |
break | |
fi | |
done | |
echo "$highest_priority" | |
} | |
# Loop through the pull requests and categorize them | |
body=$(curl -s "${HEADERS[@]}" "https://api.github.com/repos/headlamp-k8s/headlamp/pulls?state=closed&per_page=100") | |
pull_requests=$(echo "${body}" | jq -r '.[] | select(.merged_at != null and .base.ref == "main") | select(.title | test("deps"; "i") | not) | [.number, .title, .html_url, (.labels[].name | select(. != null)) // ""] | @tsv' | grep -v '^null') | |
# Loop through the categorized pull requests and append them to the respective files | |
while IFS=$'\t' read -r pr title url labels; do | |
if [ -z "$labels" ]; then | |
priority_label=$(get_priority_label "$title") | |
else | |
priority_label=$(get_priority_label "$labels") | |
fi | |
# Check if the title contains "Fix" or "fix" | |
if [[ "$title" =~ [Ff]ix ]]; then | |
priority_label="bug" | |
fi | |
# Check if the title starts with "docs" | |
if [[ "$title" =~ ^docs ]]; then | |
priority_label="documentation" | |
fi | |
add_to_category "$priority_label" "$title" "$pr" "$url" | |
done <<< "$pull_requests" | |
# Add headers for each category and append categorized pull requests to readme.md | |
for category in "${!custom_categories[@]}"; do | |
if [[ -s "category_${category}.md" ]]; then | |
echo "## ${custom_categories[$category]}" >> readme.md | |
cat "category_${category}.md" >> readme.md | |
rm -f "category_${category}.md" | |
fi | |
done | |
# Append pull requests without any category to the "Other" section | |
if [[ -s "category_other.md" ]]; then | |
echo "## ${custom_categories[other]}" >> readme.md | |
cat "category_other.md" >> readme.md | |
rm -f "category_other.md" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment