Merge branch 'pre-commit' into develop

This commit is contained in:
Mike Salvatore 2021-04-07 11:07:27 -04:00
commit 958fa50d83
5 changed files with 79 additions and 2 deletions

30
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,30 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.8.0
hooks:
- id: isort
name: isort (python)
- id: isort
name: isort (cython)
types: [cython]
- id: isort
name: isort (pyi)
types: [pyi]
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-json
- id: check-merge-conflict
- id: detect-private-key
- id: end-of-file-fixer
- id: trailing-whitespace

View File

@ -24,7 +24,7 @@ install:
# Python # Python
- pip freeze - pip freeze
- pip install -r monkey/monkey_island/requirements.txt # for unit tests - pip install -r monkey/monkey_island/requirements.txt # for unit tests
- pip install black flake8 pytest pytest-cov isort # for next stages - pip install black==20.8b1 flake8==3.9.0 pytest pytest-cov isort==5.8.0 # for next stages
- pip install coverage # for code coverage - pip install coverage # for code coverage
- pip install -r monkey/infection_monkey/requirements.txt # for unit tests - pip install -r monkey/infection_monkey/requirements.txt # for unit tests
- pip install pipdeptree - pip install pipdeptree

View File

@ -63,3 +63,22 @@ After the `deploy_linux.sh` script completes, you can start the monkey island.
cd infection_monkey/monkey cd infection_monkey/monkey
./monkey_island/linux/run.sh ./monkey_island/linux/run.sh
``` ```
## Pre-commit hooks
Both the Linux and Windows deployment scrips will install and configure
[pre-commit](https://pre-commit.com/). Pre-commit is a multi-language package
manager for pre-commit hooks. It will run a set of checks when you attempt to
commit. If your commit does not pass all checks, it will be reformatted and/or
you'll be given a list of errors and warnings that need to be fixed before you
can commit.
Our CI system runs the same checks when pull requests are submitted. This
system may report that the build has failed if the pre-commit hooks have not
been run or all issues have not been resolved.
### Manually installing pre-commit
To install and configure pre-commit manually, run `pip install --user
pre-commit`. Next, go to the top level directory of this repository and run
`pre-commit install` Now, pre-commit will automatically run whenever you `git commit`.

View File

@ -23,6 +23,13 @@ log_message() {
echo -e "DEPLOYMENT SCRIPT: $1" echo -e "DEPLOYMENT SCRIPT: $1"
} }
configure_precommit() {
$1 -m pip install --user pre-commit
pushd "$2"
$HOME/.local/bin/pre-commit install
popd
}
if is_root; then if is_root; then
log_message "Please don't run this script as root" log_message "Please don't run this script as root"
exit 1 exit 1
@ -225,5 +232,7 @@ fi
sudo chmod +x "${INFECTION_MONKEY_DIR}/build_linux.sh" sudo chmod +x "${INFECTION_MONKEY_DIR}/build_linux.sh"
configure_precommit ${python_cmd} ${monkey_home}
log_message "Deployment script finished." log_message "Deployment script finished."
exit 0 exit 0

View File

@ -9,6 +9,23 @@ param(
[Bool] [Bool]
$agents = $true $agents = $true
) )
function Configure-precommit([String] $git_repo_dir)
{
Write-Output "Installing pre-commit and setting up pre-commit hook"
Push-Location $git_repo_dir
python -m pip install pre-commit
if ($LastExitCode) {
exit
}
pre-commit install
if ($LastExitCode) {
exit
}
Pop-Location
Write-Output "Pre-commit successfully installed"
}
function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName, [String] $branch = "develop") function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName, [String] $branch = "develop")
{ {
Write-Output "Downloading to $monkey_home" Write-Output "Downloading to $monkey_home"
@ -119,6 +136,8 @@ function Deploy-Windows([String] $monkey_home = (Get-Item -Path ".\").FullName,
$scoutsuiteRequirements = Join-Path -Path $monkey_home -ChildPath $SCOUTSUITE_DIR | Join-Path -ChildPath "\requirements.txt" $scoutsuiteRequirements = Join-Path -Path $monkey_home -ChildPath $SCOUTSUITE_DIR | Join-Path -ChildPath "\requirements.txt"
& python -m pip install --user -r $scoutsuiteRequirements & python -m pip install --user -r $scoutsuiteRequirements
Configure-precommit($monkey_home)
$user_python_dir = cmd.exe /c 'py -m site --user-site' $user_python_dir = cmd.exe /c 'py -m site --user-site'
$user_python_dir = Join-Path (Split-Path $user_python_dir) -ChildPath "\Scripts" $user_python_dir = Join-Path (Split-Path $user_python_dir) -ChildPath "\Scripts"
if (!($ENV:Path | Select-String -SimpleMatch $user_python_dir)) if (!($ENV:Path | Select-String -SimpleMatch $user_python_dir))