-
Notifications
You must be signed in to change notification settings - Fork 9
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
Pre-commit hooks #589
Pre-commit hooks #589
Conversation
WalkthroughThis pull request updates the contributing guidelines by adding instructions for installing and using pre-commit hooks. It introduces a new script for automating hook installation and makes several comment modifications across source and test files by appending the marker Changes
Sequence Diagram(s)sequenceDiagram
participant D as Developer
participant S as install_hooks.sh
participant PC as pre-commit
participant BH as Homebrew
D->>S: Run install_hooks.sh
S->>PC: Check if pre-commit is installed
alt pre-commit not installed
S->>BH: Install pre-commit
BH-->>S: pre-commit installed
end
S->>PC: Execute "pre-commit install"
PC-->>S: Hooks successfully installed
S->>D: Display success message
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (3)
📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (6)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (1)
README.md (1)
136-136
: Enhance pre-commit documentation.Consider adding more detailed information about the pre-commit workflow:
We use `mypy` for static type checking, and `isort`, `black` and `autoflake` for linting, and `pre-commit` to minimise unwanted pushes to the public repositories. These all run as steps in CI, but `pre-commit` also needs to be installed locally using the provided `install_hooks.sh` script. + +### Git Hooks + +The repository includes several Git hooks to ensure code quality: + +1. **Pre-commit**: Runs linting and type checking before each commit +2. **Post-merge**: Automatically updates pre-commit hooks when configuration changes +3. **Prepare-commit-msg**: Verifies pre-commit hooks are installed + +To set up the hooks: + +```bash +# Install pre-commit and configure hooks +./install_hooks.sh + +# Verify installation +pre-commit run --all-files +```
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
.github/workflows/python_ci.yaml
is excluded by!**/*.yaml
.pre-commit-config.yaml
is excluded by!**/*.yaml
📒 Files selected for processing (4)
.githooks/post-merge
(1 hunks).githooks/prepare-commit-msg
(1 hunks)README.md
(1 hunks)install_hooks.sh
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
- GitHub Check: pytest - Python 3.12.x - Integration with Local Chain
- GitHub Check: pytest - Python 3.12.x - Unit Tests
- GitHub Check: pytest - Python 3.11.x - Integration with Local Chain
- GitHub Check: pytest - Python 3.11.x - Unit Tests
- GitHub Check: pytest - Python 3.10.x - Integration with Local Chain
- GitHub Check: pytest - Python 3.10.x - Unit Tests
- GitHub Check: mypy
echo "pre-commit is not installed. Installing now..." | ||
brew install pre-commit | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add cross-platform support for pre-commit installation.
The script assumes brew
is available, which is macOS specific. Consider adding support for other package managers:
if ! command -v pre-commit &> /dev/null
then
echo "pre-commit is not installed. Installing now..."
- brew install pre-commit
+ if command -v brew &> /dev/null; then
+ brew install pre-commit
+ elif command -v apt-get &> /dev/null; then
+ sudo apt-get install -y pre-commit
+ elif command -v pip &> /dev/null; then
+ pip install pre-commit
+ else
+ echo "Error: Could not find a package manager to install pre-commit"
+ exit 1
+ fi
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.
echo "pre-commit is not installed. Installing now..." | |
brew install pre-commit | |
fi | |
echo "pre-commit is not installed. Installing now..." | |
if command -v brew &> /dev/null; then | |
brew install pre-commit | |
elif command -v apt-get &> /dev/null; then | |
sudo apt-get install -y pre-commit | |
elif command -v pip &> /dev/null; then | |
pip install pre-commit | |
else | |
echo "Error: Could not find a package manager to install pre-commit" | |
exit 1 | |
fi | |
fi |
install_hooks.sh
Outdated
# Create a symlink for the hooks | ||
ln -sfn .githooks .git/hooks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add validation for symlink creation.
The symlink creation lacks error handling and directory existence checks.
# Create a symlink for the hooks
+if [ ! -d ".githooks" ]; then
+ echo "Error: .githooks directory not found"
+ exit 1
+fi
+
ln -sfn .githooks .git/hooks
+if [ $? -ne 0 ]; then
+ echo "Error: Failed to create symlink"
+ exit 1
+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.
# Create a symlink for the hooks | |
ln -sfn .githooks .git/hooks | |
# Create a symlink for the hooks | |
if [ ! -d ".githooks" ]; then | |
echo "Error: .githooks directory not found" | |
exit 1 | |
fi | |
ln -sfn .githooks .git/hooks | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to create symlink" | |
exit 1 | |
fi |
.githooks/prepare-commit-msg
Outdated
if [ ! -f .git/hooks/pre-commit ]; then | ||
echo "Error: Pre-commit hooks are not installed." | ||
echo "Please run 'pre-commit install' before committing." | ||
exit 1 | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use absolute paths and check executable permissions.
The hook should use absolute paths and verify executable permissions.
-if [ ! -f .git/hooks/pre-commit ]; then
+HOOK_PATH="$(git rev-parse --git-dir)/hooks/pre-commit"
+if [ ! -f "$HOOK_PATH" ]; then
echo "Error: Pre-commit hooks are not installed."
echo "Please run 'pre-commit install' before committing."
exit 1
+elif [ ! -x "$HOOK_PATH" ]; then
+ echo "Error: Pre-commit hook exists but is not executable."
+ echo "Please run 'chmod +x $HOOK_PATH' to fix."
+ exit 1
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.
if [ ! -f .git/hooks/pre-commit ]; then | |
echo "Error: Pre-commit hooks are not installed." | |
echo "Please run 'pre-commit install' before committing." | |
exit 1 | |
fi | |
HOOK_PATH="$(git rev-parse --git-dir)/hooks/pre-commit" | |
if [ ! -f "$HOOK_PATH" ]; then | |
echo "Error: Pre-commit hooks are not installed." | |
echo "Please run 'pre-commit install' before committing." | |
exit 1 | |
elif [ ! -x "$HOOK_PATH" ]; then | |
echo "Error: Pre-commit hook exists but is not executable." | |
echo "Please run 'chmod +x $HOOK_PATH' to fix." | |
exit 1 | |
fi |
.githooks/post-merge
Outdated
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q ".pre-commit-config.yaml"; then | ||
echo "Pre-commit config changed, updating hooks..." | ||
pre-commit autoupdate | ||
pre-commit install | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling and verbose output.
The hook should handle errors from pre-commit commands and provide clear feedback.
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q ".pre-commit-config.yaml"; then
echo "Pre-commit config changed, updating hooks..."
- pre-commit autoupdate
- pre-commit install
+ if ! pre-commit autoupdate; then
+ echo "Error: Failed to update pre-commit hooks"
+ exit 1
+ fi
+ echo "Hooks updated successfully"
+
+ if ! pre-commit install; then
+ echo "Error: Failed to install pre-commit hooks"
+ exit 1
+ fi
+ echo "Hooks installed successfully"
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.
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q ".pre-commit-config.yaml"; then | |
echo "Pre-commit config changed, updating hooks..." | |
pre-commit autoupdate | |
pre-commit install | |
fi | |
if git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep -q ".pre-commit-config.yaml"; then | |
echo "Pre-commit config changed, updating hooks..." | |
if ! pre-commit autoupdate; then | |
echo "Error: Failed to update pre-commit hooks" | |
exit 1 | |
fi | |
echo "Hooks updated successfully" | |
if ! pre-commit install; then | |
echo "Error: Failed to install pre-commit hooks" | |
exit 1 | |
fi | |
echo "Hooks installed successfully" | |
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (4)
scripts/detect_web3_private_keys.py (3)
34-35
: Log validation failures for debugging.Consider logging the specific exception when key validation fails to help debug false positives.
- except Exception: + except Exception as e: + if os.getenv("DEBUG"): + print(f"Key validation failed: {e}", file=sys.stderr) return False
44-54
: Enhance error reporting and add batch processing capabilities.The current implementation could be improved in several ways:
- Add line content to error messages for context
- Support batch reporting instead of immediate printing
- Add progress indication for large files
Would you like me to provide an enhanced implementation that addresses these points?
59-75
: Add support for directory scanning and gitignore patterns.The script could be more useful if it could:
- Recursively scan directories
- Respect .gitignore patterns
- Support exclusion patterns
Would you like me to provide an implementation that includes these features?
tests_integration_with_local_chain/safe/test_constants.py (1)
1-1
: LGTM! Consider enhancing the comment.The addition of the
web3-private-key-ok
comment is good as it explicitly marks this as an approved test key. To provide even more clarity for future developers, consider expanding the comment to indicate this is Anvil's default private key for local testing.-ANVIL_PKEY1 = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" # web3-private-key-ok +ANVIL_PKEY1 = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" # web3-private-key-ok: Anvil's default private key for local testing🧰 Tools
🪛 Gitleaks (8.21.2)
1-1: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (3)
.pre-commit-config.yaml
is excluded by!**/*.yaml
poetry.lock
is excluded by!**/*.lock
,!**/*.lock
pyproject.toml
is excluded by!**/*.toml
📒 Files selected for processing (11)
install_hooks.sh
(1 hunks)prediction_market_agent_tooling/gtypes.py
(1 hunks)prediction_market_agent_tooling/markets/omen/data_models.py
(1 hunks)prediction_market_agent_tooling/markets/omen/omen.py
(1 hunks)prediction_market_agent_tooling/tools/hexbytes_custom.py
(1 hunks)scripts/detect_web3_private_keys.py
(1 hunks)scripts/store_prediction.py
(1 hunks)tests/markets/omen/test_omen_subgraph_handler.py
(9 hunks)tests/tools/test_web3_utils.py
(1 hunks)tests_integration_with_local_chain/markets/omen/test_omen.py
(1 hunks)tests_integration_with_local_chain/safe/test_constants.py
(1 hunks)
✅ Files skipped from review due to trivial changes (5)
- scripts/store_prediction.py
- tests/markets/omen/test_omen_subgraph_handler.py
- tests_integration_with_local_chain/markets/omen/test_omen.py
- prediction_market_agent_tooling/markets/omen/omen.py
- prediction_market_agent_tooling/markets/omen/data_models.py
🚧 Files skipped from review as they are similar to previous changes (1)
- install_hooks.sh
🧰 Additional context used
🪛 Gitleaks (8.21.2)
tests/tools/test_web3_utils.py
16-16: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
tests_integration_with_local_chain/safe/test_constants.py
1-1: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: pytest - Python 3.12.x - Integration with Local Chain
- GitHub Check: pytest - Python 3.12.x - Unit Tests
- GitHub Check: pytest - Python 3.11.x - Integration with Local Chain
- GitHub Check: pytest - Python 3.11.x - Integration Tests
- GitHub Check: pytest - Python 3.11.x - Unit Tests
- GitHub Check: pytest - Python 3.10.x - Integration with Local Chain
- GitHub Check: pytest - Python 3.10.x - Integration Tests
- GitHub Check: pytest - Python 3.10.x - Unit Tests
- GitHub Check: mypy
🔇 Additional comments (3)
tests/tools/test_web3_utils.py (1)
16-16
: LGTM! Test uses appropriate annotation.The added comment correctly indicates that this is an intentional test private key.
🧰 Tools
🪛 Gitleaks (8.21.2)
16-16: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
prediction_market_agent_tooling/tools/hexbytes_custom.py (1)
26-26
: LGTM! Appropriate annotation for schema example.The example is correctly annotated as a web3 private key example used for schema validation.
prediction_market_agent_tooling/gtypes.py (1)
85-85
: LGTM! Clear example with appropriate annotation.The example effectively demonstrates the padding behavior and is correctly annotated.
scripts/detect_web3_private_keys.py
Outdated
from eth_utils import decode_hex | ||
|
||
# Regular expression to match Ethereum private keys | ||
KEY_PATTERN = re.compile(r"\b(0x)?[a-fA-F0-9]{64}\b") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Strengthen the private key regex pattern.
The current pattern might miss some edge cases. Consider using a more precise pattern that enforces length constraints.
-KEY_PATTERN = re.compile(r"\b(0x)?[a-fA-F0-9]{64}\b")
+KEY_PATTERN = re.compile(r"(?:^|[^a-fA-F0-9])(0x)?[a-fA-F0-9]{64}(?:$|[^a-fA-F0-9])")
📝 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.
KEY_PATTERN = re.compile(r"\b(0x)?[a-fA-F0-9]{64}\b") | |
KEY_PATTERN = re.compile(r"(?:^|[^a-fA-F0-9])(0x)?[a-fA-F0-9]{64}(?:$|[^a-fA-F0-9])") |
pip install pre-commit | ||
- name: Run Pre-Commit | ||
run: | | ||
pre-commit run --all-files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pre-commit needs to be installed locally (see install_hooks.sh
), but as a last resort, also run it in CI, so at least we won't miss it by accident if the key gets leaked.
.pre-commit-config.yaml
Outdated
hooks: | ||
- id: detect-web3-private-key | ||
name: Detect Web3 Private Key | ||
entry: python scripts/detect_web3_private_keys.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After review, I'll try to get this into https://github.com/pre-commit/pre-commit-hooks, if they don't accept, I'll create a separate repo for it, so we can share it across our repos
@@ -82,5 +82,5 @@ def secretstr_to_v1_secretstr(s: SecretStr | None) -> SecretStrV1 | None: | |||
|
|||
|
|||
def int_to_hexbytes(v: int) -> HexBytes: | |||
# Example: 1 -> HexBytes("0x0000000000000000000000000000000000000000000000000000000000000001"). | |||
# Example: 1 -> HexBytes("0x0000000000000000000000000000000000000000000000000000000000000001"). # web3-private-key-ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like private keys don't have any strong format. I had to add this # web3-private-key-ok
so we can tell the script that these aren't real private keys.
@gabrielfior please check out scripts/detect_web3_private_keys.py, if you know a better way of how to detect private keys, I'll be happy 😄
No description provided.