Skip to content

Instantly share code, notes, and snippets.

@joesturge
Last active November 29, 2024 14:54
Show Gist options
  • Select an option

  • Save joesturge/4bea7a55fe1141bcc3080fea2eed1f9e to your computer and use it in GitHub Desktop.

Select an option

Save joesturge/4bea7a55fe1141bcc3080fea2eed1f9e to your computer and use it in GitHub Desktop.
Backfill the releases of a git repo using the changelog.md in a keepachangelog format
#!/bin/bash
# Configuration
CHANGELOG_PATH="CHANGELOG.md"
REPO="your-repo-name" # Replace with your repository name
# Dry run flag
DRY_RUN=true
# Authenticate to GitHub (only needed if not authenticated with `gh` CLI)
# gh auth login --with-token <<< $GITHUB_TOKEN # Uncomment if needed
# Parse the changelog to extract version, date, and release notes
parse_changelog() {
local changelog_content
changelog_content=$(<"$CHANGELOG_PATH")
while IFS= read -r line; do
if [[ $line =~ ^###\ ([0-9]+\.[0-9]+\.[0-9]+)\ \(([0-9]+-[0-9]+-[0-9]+)\)$ ]]; then
version="${BASH_REMATCH[1]}"
release_date="${BASH_REMATCH[2]}"
release_notes=""
while IFS= read -r note_line; do
if [[ $note_line =~ ^###\ [0-9]+\.[0-9]+\.[0-9]+\ \([0-9]+-[0-9]+-[0-9]+\)$ ]]; then
break
fi
release_notes+="$note_line"$'\n'
done
create_github_release "$version" "$release_date" "$release_notes"
fi
done <<< "$changelog_content"
}
# Create a GitHub release for the given version
create_github_release() {
version=$1
release_date=$2
release_notes=$3
# Prepare the release message
release_title="v$version"
echo "Preparing to create GitHub release for $version..."
if [ "$DRY_RUN" = true ]; then
echo "[Dry-run] Release $version would be created with the following message:"
echo "$release_title"
echo "$release_notes"
else
# Create the release on GitHub using GitHub CLI
gh release create "$release_title" --title "$release_title" --notes "$release_notes"
if [ $? -eq 0 ]; then
echo "Release $version created successfully."
else
echo "Failed to create release $version."
fi
fi
}
# Parse arguments for dry-run option
while [[ "$#" -gt 0 ]]; do
case "$1" in
--dry-run) DRY_RUN=true; shift ;;
--repo) REPO="$2"; shift 2 ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
# Main function
main() {
if [ ! -f "$CHANGELOG_PATH" ]; then
echo "Changelog file not found: $CHANGELOG_PATH"
exit 1
fi
parse_changelog
}
# Execute the main function
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment