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

Poetry upgrade #62

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[run]
branch = True
source = .
source = wtf_bot
omit =
**/test_*
**/ENV/*

[html]
directory = results/coverage
Expand Down
7 changes: 0 additions & 7 deletions .flake8

This file was deleted.

20 changes: 11 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ jobs:
name: build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python $
uses: actions/setup-python@v4
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: '.python-version' # Read python version from a file
- name: Install dependencies
run: |
pip install --require-hashes -r requirements.txt -r dev-requirements.txt
- name: Test with pytest
run: |
make test

- name: Set up poetry
uses: Gr1N/setup-poetry@v9

- name: Runs tests and static analysis
shell: bash
run: make test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ results/
vars.yml

# ide
.idea
.idea

.install.stamp
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.8.12
3.12.6
124 changes: 47 additions & 77 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,92 +1,62 @@
# Makefile for wtf-bot
# Tested with GNU Make 3.8.1
MAKEFLAGS += --warn-undefined-variables
SHELL := /usr/bin/env bash -e
CI_ARG := $(CI)

SHELL := /usr/bin/env bash -e -u -o pipefail
.DEFAULT_GOAL := help
INSTALL_STAMP := .install.stamp
POETRY := $(shell command -v poetry 2> /dev/null)

# cribbed from https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html and https://news.ycombinator.com/item?id=11195539
# cribbed from https://github.com/mozilla-services/telescope/blob/main/Makefile
.PHONY: help
help: ## Prints out documentation for available commands
@awk -F ':|##' \
'/^[^\t].+?:.*?##/ {\
printf "\033[36m%-30s\033[0m %s\n", $$1, $$NF \
}' $(MAKEFILE_LIST)

## Pip / Python

.PHONY: python-install
# python-install recipe all has to run in a single shell because it's running inside a virtualenv
python-install: requirements.txt dev-requirements.txt ## Sets up your python environment for the first time (only need to run once)
pip install virtualenv ;\
virtualenv ENV ;\
source ENV/bin/activate ;\
echo shell ENV activated ;\
pip install --require-hashes -r requirements.txt -r dev-requirements.txt ;\
echo Finished install ;\
echo Please activate the virtualenvironment with: ;\
echo source ENV/bin/activate

# Errors out if VIRTUAL_ENV is not defined and we aren't in a CI environment.
.PHONY: check-env
check-env:
ifndef VIRTUAL_ENV
ifneq ($(CI_ARG), true)
$(error VIRTUAL_ENV is undefined, meaning you aren't running in a virtual environment. Fix by running: 'source ENV/bin/activate')
endif
endif

requirements.txt: requirements.in
pip-compile --allow-unsafe --generate-hashes requirements.in --output-file $@

dev-requirements.txt: dev-requirements.in
pip-compile --allow-unsafe --generate-hashes dev-requirements.in --output-file $@

.PHONY: pip-upgrade
pip-upgrade: ## Upgrade all python dependencies
pip-compile --upgrade --allow-unsafe --generate-hashes requirements.in --output-file requirements.txt
pip-compile --upgrade --allow-unsafe --generate-hashes dev-requirements.in --output-file dev-requirements.txt

SITE_PACKAGES := $(shell pip show pip | grep '^Location' | cut -f2 -d ':')
$(SITE_PACKAGES): requirements.txt dev-requirements.txt check-env
ifeq ($(CI_ARG), true)
@echo "Do nothing; assume python dependencies were installed already"
else
pip-sync requirements.txt dev-requirements.txt
endif

.PHONY: pip-install
pip-install: $(SITE_PACKAGES)

## Test targets
@echo "Please use 'make <target>' where <target> is one of the following commands."
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo "Check the Makefile to know exactly what each target is doing."

install: $(INSTALL_STAMP) ## Install dependencies
$(INSTALL_STAMP): pyproject.toml poetry.lock
@if [[ -z "$(POETRY)" ]]; then echo "Poetry could not be found. See https://python-poetry.org/docs/"; exit 2; fi
@echo "=> Installing python dependencies"
"$(POETRY)" --version
"$(POETRY)" install
touch $(INSTALL_STAMP)

.PHONY: format-check
format-check: $(INSTALL_STAMP) ## runs code formatting
"$(POETRY)" run ruff format --check

.PHONY: format-fix
format-fix: $(INSTALL_STAMP) ## runs code formatting
"$(POETRY)" run ruff format

.PHONY: lint
lint: $(INSTALL_STAMP) ## runs code formatting checks
"$(POETRY)" run ruff check

.PHONY: lint-fix
lint-fix: $(INSTALL_STAMP) ## runs code formatting checks
"$(POETRY)" run ruff check --fix --exit-non-zero-on-fix

## Test
.PHONY: unit-test
unit-test: pip-install ## Run python unit tests
python -m pytest -v --cov --cov-report term --cov-report xml --cov-report html

.PHONY: flake8
flake8: pip-install ## Run Flake8 python static style checking and linting
@echo "flake8 comments:"
flake8 --statistics .
unit-test: $(INSTALL_STAMP) ## Run python unit tests
"$(POETRY)" run pytest -v --cov --cov-report term --cov-report xml --cov-report html

.PHONY: test
test: unit-test flake8 ## Run unit tests, static analysis
test: unit-test format-check lint ## Run unit tests, static analysis
@echo "All tests passed." # This should only be printed if all of the other targets succeed

.PHONY: install-action-lint
install-action-lint: ## Install actionlint
brew install actionlint

.PHONY: actionlint
actionlint: ## Run actionlint
actionlint
.PHONY: check-dependencies
check-dependencies: $(INSTALL_STAMP) ## check dependencies for vulnerabilities
# 22 Aug 2024: 70612 vulnerability found with jinja2 version 3.1.4. At this time, all versions of jinja2 are affected, but vulnerability is being disputed. https://nvd.nist.gov/vuln/detail/CVE-2019-8341
"$(POETRY)" run safety check -r poetry.lock --full-report -i 70612

.PHONY: clean
clean: ## Delete any directories, files or logs that are auto-generated, except python packages
clean: ## Delete any directories, files or logs that are auto-generated
rm -rf results
rm -rf .pytest_cache
rm -f .coverage
find . -type d -name "__pycache__" | xargs rm -rf {};
rm -f .install.stamp .coverage .coverage.*

.PHONY: deepclean
deepclean: clean ## Delete python packages and virtualenv. You must run 'make python-install' after running this.
rm -rf ENV
@echo virtualenvironment was deleted. Type 'deactivate' to deactivate the shims.
deepclean: clean ## Runs cleans and deletes all poetry environments
"$(POETRY)" env remove --all -n
@echo Poetry environments deleted. Type 'exit' to exit the shell.
59 changes: 21 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# wtf-bot

A Flask application that powers the /wtf Slack command. Inspired by a [previous incarnation](https://github.com/paultag/wtf) of similar functionality. Relies on the VA [acronym list](https://github.com/department-of-veterans-affairs/acronyms).
A Flask application that powers the `/wtf` Slack command. Inspired by a [previous incarnation](https://github.com/paultag/wtf) of similar functionality. Relies on the VA [acronym list](https://github.com/department-of-veterans-affairs/acronyms).

## Install to slack

Expand All @@ -23,33 +23,18 @@ $ git clone https://github.com/department-of-veterans-affairs/wtf-bot.git
$ cd /path/to/wtf-bot/
```

### Create and configure virtual environment.

Make sure you have the correct version of python. [Pyenv](https://github.com/pyenv/pyenv) manages python versions.
1. `pyenv install 3.8.12` (use version in `.python-version`)
2. Make sure to configure your shell's environment for pyenv following instructions in step 2
[here](https://github.com/pyenv/pyenv#basic-github-checkout).
3. Once this is done, start a new shell and run `pyenv version` and `python --version` in the terminal from the root directory of the project, both should read `3.8.12`

#### *nix

```
$ make python-install
$ source ENV/bin/activate
```

#### Windows

```
> python3 -m venv ENV
> ENV\Scripts\activate
> pip3 install -r requirements.txt dev-requirements.txt
```
### Set up python and poetry
1. Install python as defined in `.python-version`. You can use [Pyenv](https://github.com/pyenv/pyenv) or [asdf](https://asdf-vm.com/) to manage python versions. Verify with `python --version`
1. [Install poetry](https://python-poetry.org/docs/#installation)
1. Install the python dependencies:
```
make install
```

### Run tests

```
$ make test
make test
```

## Run the server locally
Expand All @@ -58,32 +43,30 @@ $ make test

#### *nix

```
$ export FLASK_APP=/path/to/wtf-bot/wtf.py
$ export FLASK_DEBUG=1
$ export SLACK_TOKENS={comma separated tokens to be defined by you}
$ export DATA_URL=https://raw.githubusercontent.com/department-of-veterans-affairs/acronyms/master/acronyms.csv
```bash
export FLASK_APP=/path/to/wtf-bot/wtf_bot/wtf.py
export FLASK_DEBUG=1
export SLACK_TOKENS={comma separated tokens to be defined by you}
export DATA_URL=https://raw.githubusercontent.com/department-of-veterans-affairs/acronyms/master/acronyms.csv
```

#### Windows

```
> set FLASK_APP=/path/to/wtf-bot/wtf.py
> set FLASK_DEBUG=1
> set SLACK_TOKENS={comma separated tokens to be defined by you}
> set DATA_URL=https://raw.githubusercontent.com/department-of-veterans-affairs/acronyms/master/acronyms.csv
```PowerShell
set FLASK_APP=/path/to/wtf-bot/wtf_bot/wtf.py
set FLASK_DEBUG=1
set SLACK_TOKENS={comma separated tokens to be defined by you}
set DATA_URL=https://raw.githubusercontent.com/department-of-veterans-affairs/acronyms/master/acronyms.csv
```

### Start the local server

```
$ flask run
poetry run flask run
```

## Query the `/slack` endpoint

```
$ curl -X POST http://127.0.0.1:5000/slack -d "text=aaa&token={to be defined by you}"
curl -X POST http://127.0.0.1:5000/slack -d "text=aaa&token={to be defined by you}"
```

## Bugs, feature requests, or contributions
Expand Down
9 changes: 0 additions & 9 deletions dev-requirements.in

This file was deleted.

Loading