#!/bin/bash ################################################################################## # This script extracts the changelog for a given release version from the project # changelog file. It is intended to be used in a release pipeline to generate a # summary of the changes for the current release. The version number can be # specified with the -v/--version flag. # # Usage: ./scripts/extract_changelog.sh -v # # Example: ./scripts/extract_changelog.sh -v 0.1.0 CHANGELOG.md # # Dependencies: grep, cut, sed, awk ################################################################################## # Get flags VALID_ARGS=$(getopt -o v --long version -- "$@") if [[ $? -ne 0 ]] then exit 1; fi # Parse flags eval set -- "$VALID_ARGS" while [ : ]; do case "$1" in -v | --version) version="$3"; changelog="$4" shift ;; --) shift; break;; esac done # Check if changelog is set or use default if [ -z "$changelog" ] then changelog="CHANGELOG.md" fi # Check if changelog exists if [ ! -f "$changelog" ] then echo "Changelog file not found: $changelog" exit 1 fi # Check if version is set if [ -z "$version" ] then echo "Please specify a version, using the -v flag." exit 1 fi start_line=$(grep -n "## \[$version\]" $changelog | cut -d: -f1) # Check if version exists in changelog if [ -z "$start_line" ] then echo "Version $version not found in changelog" exit 1 fi end_lines=$(grep -n "^## \[" $changelog | cut -d: -f1) # Find first line containing a version after the specified version while read -r end_line; do if (( end_line > start_line )); then break fi done <<< "$end_lines" # If no version is found, set end_line to the end of the file if [ -z "$end_line" ] then end_line=$(wc -l < $changelog) fi # Subtract 2 from end_line to remove empty line and header for next section end_line=$(($end_line-2)) # Return text between start_line and end_line TEXT=$(awk "NR>=$start_line && NR<=$end_line" $changelog) echo -e "$TEXT"