Skip to content

Commit 9bbe7d6

Browse files
dbastnorhh
andauthored
Add pre-commit hook definition + test (#1877)
* Add pre-commit hook + test * Update README.md * Update README.md * update README.md Co-authored-by: Nikhil Parasaram <[email protected]> --------- Co-authored-by: Nikhil Parasaram <[email protected]> Co-authored-by: Nikhil Parasaram <[email protected]>
1 parent e64be9d commit 9bbe7d6

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: pre-commit
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- develop
8+
tags:
9+
- '*'
10+
pull_request:
11+
paths:
12+
- .github/workflows/pre-commit-hooks-test.yml
13+
- .pre-commit-hooks.yaml
14+
- tests/pre-commit-hooks/*
15+
- requirements.txt
16+
- setup.py
17+
18+
jobs:
19+
hooks-test:
20+
runs-on: ubuntu-latest
21+
name: test hooks
22+
steps:
23+
- uses: actions/checkout@v4
24+
- uses: actions/setup-python@v5
25+
with:
26+
python-version: '3.12'
27+
- name: Install pre-commit
28+
run: |
29+
python -m pip install pre-commit
30+
- name: Test hooks
31+
run: |
32+
./tests/pre-commit-hooks/test.sh

.pre-commit-hooks.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- id: mythril
2+
name: Mythril
3+
description: Analyze EVM bytecode with Mythril
4+
entry: myth
5+
args:
6+
- analyze
7+
language: python
8+
types: ["solidity"]

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ Install from Pypi (Python 3.7-3.10):
3131
$ pip3 install mythril
3232
```
3333

34+
Use it via pre-commit hook (replace `$GIT_TAG` with real tag):
35+
36+
```YAML
37+
- repo: https://github.com/Consensys/mythril
38+
rev: $GIT_TAG
39+
hooks:
40+
- id: mythril
41+
```
42+
43+
Additionally, set `args: [disassemble]` or `args: [read-storage]` to use a different command than `analyze`.
44+
3445
See the [docs](https://mythril-classic.readthedocs.io/en/master/installation.html) for more detailed instructions.
3546

3647
## Usage

tests/pre-commit-hooks/Counter.sol

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.26;
3+
4+
contract Counter {
5+
uint256 public number;
6+
7+
function setNumber(uint256 newNumber) public {
8+
number = newNumber;
9+
}
10+
11+
function increment() public {
12+
number++;
13+
}
14+
}

tests/pre-commit-hooks/test.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errtrace -o nounset -o pipefail -o errexit
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6+
7+
# Create temp working directory for mock repo
8+
MOCK_REPO=$(mktemp -d)
9+
if [[ ! "$MOCK_REPO" || ! -d "$MOCK_REPO" ]]; then
10+
echo "Could not create temp dir"
11+
exit 1
12+
fi
13+
function cleanup {
14+
echo "Deleting temp working directory $MOCK_REPO"
15+
rm -rf "$MOCK_REPO"
16+
}
17+
trap cleanup EXIT
18+
19+
# Filling the mock repo
20+
pushd "$MOCK_REPO" >/dev/null || exit 1
21+
git init --initial-branch=master
22+
git config user.email "[email protected]"
23+
git config user.name "pre-commit test"
24+
cp "$SCRIPT_DIR/Counter.sol" .
25+
git add .
26+
git commit -m "Initial commit"
27+
28+
# Run pre-commit inside the mock repo while referencing the mythril directory,
29+
# where the .pre-commit-hooks.yaml is located.
30+
pre-commit try-repo "$SCRIPT_DIR/../.." mythril --verbose --color=always --all-files

0 commit comments

Comments
 (0)