Skip to content

feat(keep-a-changelog): Add $REQUEST_BODY_FILE for curl request and cleanup #12

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

Merged
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 @@ -107,3 +107,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Renamed "Changelog AI" hooks to "PartCAD: Update CHANGELOG.md"
- Renamed "Git Commit AI" hooks to "PartCAD: Prepare Commit Message"
- Improved cleanup mechanism for temporary files

## [1.8.1] - 2024-12-12

### Added

- Added $REQUEST_BODY_FILE for curl request and cleanup.
- Secure permissions on temporary files - `chmod 600`.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
![GitHub Release](https://img.shields.io/github/v/release/partcad/pre-commit)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)
[![Keep a Changelog v1.1.0 badge][changelog-badge]][changelog] [![Apache 2.0 License Badge][license-badge]][license]

# `pre-commit` - Git Hooks backed by AI

Fed up with crafting commit messages by hand?
Expand Down Expand Up @@ -217,3 +221,8 @@ Join `#pre-commit` channel on
[Apache License 2.0]: https://github.com/partcad/pre-commit/blob/main/.github/LICENSE
[`mrgoonie/cmai.git`]: https://github.com/mrgoonie/cmai
[MIT]: https://github.com/mrgoonie/cmai/tree/3398ab52778b999fa170a734411be6d69c4f1697?tab=readme-ov-file#license
[changelog]: ./CHANGELOG.md
[changelog-badge]: https://img.shields.io/badge/changelog-Keep%20a%20Changelog%20v1.1.0-%23E05735
[license]: ./.github/LICENSE
[version-badge]: https://img.shields.io/badge/version-1.8.1-blue.svg
[license-badge]: https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg
24 changes: 21 additions & 3 deletions keep-a-changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ debug_log "System prompt saved to $SYSTEM_PROMPT_FILE"
# Ensure cleanup on script exit
cleanup() {
local exit_code=$?
rm -f "$PROMPT_FILE" "$SYSTEM_PROMPT_FILE" 2>/dev/null
rm -f "$PROMPT_FILE" "$SYSTEM_PROMPT_FILE" "$REQUEST_BODY_FILE" 2>/dev/null
exit $exit_code
}
trap cleanup EXIT INT TERM
Expand All @@ -227,13 +227,31 @@ REQUEST_BODY=$(jq -n \
)
debug_log "Request body prepared with model: $MODEL" "$REQUEST_BODY"

REQUEST_BODY_FILE=$(mktemp)
if [ ! -f "$REQUEST_BODY_FILE" ]; then
echo "ERROR: Failed to create temporary file"
exit 1
fi
chmod 600 "$REQUEST_BODY_FILE" || {
echo "ERROR: Failed to set secure permissions on temporary file"
rm -f "$REQUEST_BODY_FILE"
exit 1
}
echo "$REQUEST_BODY" > "$REQUEST_BODY_FILE"
debug_log "Request body saved to $REQUEST_BODY_FILE"

# Make the API request
debug_log "Making API request to OpenRouter"
RESPONSE=$(curl -s -X POST "https://openrouter.ai/api/v1/chat/completions" \
if ! RESPONSE=$(curl -s -X POST "https://openrouter.ai/api/v1/chat/completions" \
-H "Authorization: Bearer ${OPENROUTER_API_KEY}" \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY")
-d @"$REQUEST_BODY_FILE"); then
echo "ERROR: API request failed with exit code $?"
exit 1
fi
debug_log "API response received" "$RESPONSE"
debug_log "Cleaning up temporary files"
rm -v "$REQUEST_BODY_FILE" 2>/dev/null || true

# Check for errors
if [[ "$RESPONSE" == *'"error"'* ]]; then
Expand Down
16 changes: 14 additions & 2 deletions prepare-commit-msg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,27 @@ debug_log "Cleaning up temporary files"
rm -v "$PROMPT_FILE" "$SYSTEM_PROMPT_FILE"

REQUEST_BODY_FILE=$(mktemp)
if [ ! -f "$REQUEST_BODY_FILE" ]; then
echo "ERROR: Failed to create temporary file"
exit 1
fi
chmod 600 "$REQUEST_BODY_FILE" || {
echo "ERROR: Failed to set secure permissions on temporary file"
rm -f "$REQUEST_BODY_FILE"
exit 1
}
echo "$REQUEST_BODY" > "$REQUEST_BODY_FILE"
debug_log "Request body saved to $REQUEST_BODY_FILE"

# Make the API request
debug_log "Making API request to OpenRouter"
RESPONSE=$(curl -s -X POST "https://openrouter.ai/api/v1/chat/completions" \
if ! RESPONSE=$(curl -s -X POST "https://openrouter.ai/api/v1/chat/completions" \
-H "Authorization: Bearer ${OPENROUTER_API_KEY}" \
-H "Content-Type: application/json" \
-d @"$REQUEST_BODY_FILE")
-d @"$REQUEST_BODY_FILE"); then
echo "ERROR: API request failed with exit code $?"
exit 1
fi
debug_log "API response received" "$RESPONSE"
debug_log "Cleaning up temporary files"
rm -v "$REQUEST_BODY_FILE"
Expand Down