Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(changelog): improve changelog generation and error handling #18

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Added transforms for improved results.
- Modified `keep-a-changelog.sh` and `prepare-commit-msg.sh` for better results.

## [1.8.3] - 2024-12-12

- Added filtering for `.stl` and `.step` files in `git diff` to avoid 5xx errors in API requests.
- Improved error handling and logging (potential debugging improvements).
- Added `--fail` option to curl requests.
- Added examples of API costs.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ amount of information shared.
- Costs approximately $0.00001 per commit
- Large diffs may incur higher costs

![Costs](./images/costs.png)

Comment on lines +149 to +150
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Time to make this image as accessible as a properly pressurized airlock

Dr. Brand would appreciate if we added an alt text to this image. It's not just about looking pretty – it's about making sure everyone can access the information, even in low visibility conditions.

-![Costs](./images/costs.png)
+![API usage costs breakdown showing approximately $0.00001 per commit](./images/costs.png)

This change has a 95% chance of improving accessibility, with a 5% margin of sass.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
![Costs](./images/costs.png)
![API usage costs breakdown showing approximately $0.00001 per commit](./images/costs.png)

## Uninstallation

- [`pre-commit uninstall`](https://pre-commit.com/#pre-commit-uninstall)
Expand Down
Binary file added images/costs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 17 additions & 8 deletions keep-a-changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ EOF
esac
done

# # Enable debug mode if DEBUG is set to true
# if [ "$DEBUG" = true ]; then
# set -x
# fi
Comment on lines +135 to +138
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Detected commented-out code, initiating removal protocol

These commented-out debug lines are like having a backup parachute that's still in its packaging - not very useful. Either implement them or remove them entirely.

-# # Enable debug mode if DEBUG is set to true
-# if [ "$DEBUG" = true ]; then
-#   set -x
-# fi

Don't worry, we already have a proper debug_log function. Like Dr. Brand said, "We must think not as individuals but as a species." Let's keep our codebase clean for future generations.


# Debug function
debug_log() {
if [ "$DEBUG" = true ]; then
Expand Down Expand Up @@ -160,19 +165,18 @@ if [ -n "$(git diff --cached --name-only -- "${CHANGELOG_FILENAME}")" ]; then
fi

# Get git changes
CHANGES=$(git diff --cached --ignore-all-space)
CHANGES=$(git diff --cached --ignore-all-space -- ':!*.stl' ':!*.step')
CURRENT_CHANGELOG=""
if [ -f "${CHANGELOG_FILENAME}" ]; then
CURRENT_CHANGELOG=$(cat "${CHANGELOG_FILENAME}")
fi

# # Maximum size of changes to send to API (in bytes)
# MAX_CHANGES_SIZE=10000
#
# if [ "${#CHANGES}" -gt "$MAX_CHANGES_SIZE" ]; then
# CHANGES=$(echo "$CHANGES" | head -c "$MAX_CHANGES_SIZE")
# CHANGES+=$'\n... (truncated due to size)'
# fi
# Maximum size of changes to send to API (in bytes)
MAX_CHANGES_SIZE=10000
if [ "${#CHANGES}" -gt "$MAX_CHANGES_SIZE" ]; then
CHANGES=$(echo "$CHANGES" | head -c "$MAX_CHANGES_SIZE")
CHANGES+=$'\n... (truncated due to size)'
fi
Comment on lines +174 to +179
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Size limitation detected, but warning system offline

Like that time I had to tell Cooper about the 90% honesty setting, we should probably tell users when we're truncating their data.

 # Maximum size of changes to send to API (in bytes)
 MAX_CHANGES_SIZE=10000
 if [ "${#CHANGES}" -gt "$MAX_CHANGES_SIZE" ]; then
+    echo "WARNING: Changes exceed maximum size of $MAX_CHANGES_SIZE bytes. Truncating..."
     CHANGES=$(echo "$CHANGES" | head -c "$MAX_CHANGES_SIZE")
     CHANGES+=$'\n... (truncated due to size)'
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Maximum size of changes to send to API (in bytes)
MAX_CHANGES_SIZE=10000
if [ "${#CHANGES}" -gt "$MAX_CHANGES_SIZE" ]; then
CHANGES=$(echo "$CHANGES" | head -c "$MAX_CHANGES_SIZE")
CHANGES+=$'\n... (truncated due to size)'
fi
# Maximum size of changes to send to API (in bytes)
MAX_CHANGES_SIZE=10000
if [ "${#CHANGES}" -gt "$MAX_CHANGES_SIZE" ]; then
echo "WARNING: Changes exceed maximum size of $MAX_CHANGES_SIZE bytes. Truncating..."
CHANGES=$(echo "$CHANGES" | head -c "$MAX_CHANGES_SIZE")
CHANGES+=$'\n... (truncated due to size)'
fi


# shellcheck disable=SC2059
USER_PROMPT=$(printf "$USER_PROMPT" "$CHANGES")
Expand Down Expand Up @@ -244,6 +248,7 @@ debug_log "Request body saved to $REQUEST_BODY_FILE"
# Make the API request
debug_log "Making API request to OpenRouter"
if ! RESPONSE=$(curl -s -X POST "https://openrouter.ai/api/v1/chat/completions" \
--fail \
-H "Authorization: Bearer ${OPENROUTER_API_KEY}" \
-H "Content-Type: application/json" \
-d @"$REQUEST_BODY_FILE"); then
Expand All @@ -254,6 +259,10 @@ debug_log "API response received" "$RESPONSE"
debug_log "Cleaning up temporary files"
rm -v "$REQUEST_BODY_FILE" 2>/dev/null || true

# TODO: Check if the response is valid JSON, if not print the response and exit
# echo "API response:"
# echo $RESPONSE
# echo "API response end"
Comment on lines +262 to +265
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

TODO detected: JSON validation missing, probability of failure: 23.4%

This TODO is like having an unfinished equation before a slingshot maneuver - risky business.

Would you like me to generate an implementation for JSON validation? Here's a preview:

-# TODO: Check if the response is valid JSON, if not print the response and exit
-# echo "API response:"
-# echo $RESPONSE
-# echo "API response end"
+# Validate JSON response
+if ! echo "$RESPONSE" | jq . >/dev/null 2>&1; then
+    echo "ERROR: Invalid JSON response received:"
+    echo "$RESPONSE"
+    exit 1
+fi

Just say the word, and I'll help implement this faster than you can say "quantum data".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# TODO: Check if the response is valid JSON, if not print the response and exit
# echo "API response:"
# echo $RESPONSE
# echo "API response end"
# Validate JSON response
if ! echo "$RESPONSE" | jq . >/dev/null 2>&1; then
echo "ERROR: Invalid JSON response received:"
echo "$RESPONSE"
exit 1
fi

# Check for errors
if [[ "$RESPONSE" == *'"error"'* ]]; then
error_message=$(echo "$RESPONSE" | jq -r '.error.message // .error // "Unknown error"')
Expand Down
4 changes: 2 additions & 2 deletions prepare-commit-msg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ fi

# Get git changes
if [ "$OPEN_SOURCE" = true ]; then
CHANGES=$(git diff --cached --ignore-all-space)
CHANGES=$(git diff --cached --ignore-all-space -- ':!*.stl' ':!*.step')
else
CHANGES=$(git diff --cached --ignore-all-space --name-status)
CHANGES=$(git diff --cached --ignore-all-space --name-status -- ':!*.stl' ':!*.step')
fi


Expand Down