|
| 1 | +# Guidelines for contributing |
| 2 | + |
| 3 | +## Table of Contents <!-- omit in toc --> |
| 4 | + |
| 5 | +- [Summary](#summary) |
| 6 | + - [Contributors](#contributors) |
| 7 | + - [Maintainers](#maintainers) |
| 8 | +- [Git](#git) |
| 9 | +- [Python](#python) |
| 10 | + - [Python code style](#python-code-style) |
| 11 | + - [Poetry](#poetry) |
| 12 | +- [Docker](#docker) |
| 13 | + |
| 14 | +## Summary |
| 15 | + |
| 16 | +### Contributors |
| 17 | + |
| 18 | +**PRs welcome!** |
| 19 | + |
| 20 | +- **Consider starting a [discussion](https://docs.github.com/en/discussions) to see if there's interest in what you want to do.** |
| 21 | +- **Submit PRs from feature branches on forks.** |
| 22 | +- **Ensure PRs pass all CI checks.** |
| 23 | +- **Maintain or increase test coverage.** |
| 24 | + |
| 25 | +### Maintainers |
| 26 | + |
| 27 | +- **Make `develop` the default branch.** |
| 28 | +- **Merge PRs into `develop`.** Configure repository settings so that branches are deleted automatically after PRs are merged. |
| 29 | +- **Only merge to `main` if [fast-forwarding](https://www.git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging) from `develop`.** |
| 30 | +- **Enable [branch protection](https://docs.github.com/en/free-pro-team@latest/github/administering-a-repository/about-protected-branches) on `develop` and `main`.** |
| 31 | +- **Set up a release workflow.** Here's an example release workflow, controlled by Git tags: |
| 32 | + - Bump the version number in `pyproject.toml` with `poetry version` and commit the changes to `develop`. |
| 33 | + - Push to `develop` and verify all CI checks pass. |
| 34 | + - Fast-forward merge to `main`, push, and verify all CI checks pass. |
| 35 | + - Create an [annotated and signed Git tag](https://www.git-scm.com/book/en/v2/Git-Basics-Tagging) |
| 36 | + - Follow [SemVer](https://semver.org/) guidelines when choosing a version number. |
| 37 | + - List PRs and commits in the tag message: |
| 38 | + ```sh |
| 39 | + git log --pretty=format:"- %s (%h)" "$(git describe --abbrev=0 --tags)"..HEAD |
| 40 | + ``` |
| 41 | + - Omit the leading `v` (use `1.0.0` instead of `v1.0.0`) |
| 42 | + - Example: `git tag -a -s 1.0.0` |
| 43 | + - Push the tag. GitHub Actions will build and push the Python package and Docker images. |
| 44 | + |
| 45 | +## Git |
| 46 | + |
| 47 | +- _[Why use Git?](https://www.git-scm.com/about)_ Git enables creation of multiple versions of a code repository called branches, with the ability to track and undo changes in detail. |
| 48 | +- Install Git by [downloading](https://www.git-scm.com/downloads) from the website, or with a package manager like [Homebrew](https://brew.sh/). |
| 49 | +- [Configure Git to connect to GitHub with SSH](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh) |
| 50 | +- [Fork](https://docs.github.com/en/free-pro-team@latest/github/getting-started-with-github/fork-a-repo) this repo |
| 51 | +- Create a [branch](https://www.git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell) in your fork. |
| 52 | +- Commit your changes with a [properly-formatted Git commit message](https://chris.beams.io/posts/git-commit/). |
| 53 | +- Create a [pull request (PR)](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests) to incorporate your changes into the upstream project you forked. |
| 54 | + |
| 55 | +## Python |
| 56 | + |
| 57 | +### Python code style |
| 58 | + |
| 59 | +- Python code is formatted with [Black](https://black.readthedocs.io/en/stable/). Configuration for Black is stored in _pyproject.toml_. |
| 60 | +- Python imports are organized automatically with [isort](https://pycqa.github.io/isort/). |
| 61 | + - The isort package organizes imports in three sections: |
| 62 | + 1. Standard library |
| 63 | + 2. Dependencies |
| 64 | + 3. Project |
| 65 | + - Within each of those groups, `import` statements occur first, then `from` statements, in alphabetical order. |
| 66 | + - You can run isort from the command line with `poetry run isort .`. |
| 67 | + - Configuration for isort is stored in _pyproject.toml_. |
| 68 | +- Other web code (JSON, Markdown, YAML) is formatted with [Prettier](https://prettier.io/). |
| 69 | +- Code style is enforced with [pre-commit](https://pre-commit.com/), which runs [Git hooks](https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). |
| 70 | + |
| 71 | + - Configuration is stored in _.pre-commit-config.yaml_. |
| 72 | + - Pre-commit can run locally before each commit (hence "pre-commit"), or on different Git events like `pre-push`. |
| 73 | + - Pre-commit is installed in the Poetry environment. To use: |
| 74 | + |
| 75 | + ```sh |
| 76 | + # after running `poetry install` |
| 77 | + path/to/repo |
| 78 | + ❯ poetry shell |
| 79 | +
|
| 80 | + # install hooks that run before each commit |
| 81 | + path/to/repo |
| 82 | + .venv ❯ pre-commit install |
| 83 | +
|
| 84 | + # and/or install hooks that run before each push |
| 85 | + path/to/repo |
| 86 | + .venv ❯ pre-commit install --hook-type pre-push |
| 87 | + ``` |
| 88 | + |
| 89 | + - Pre-commit is also useful as a CI tool. The GitHub Actions workflows run pre-commit hooks with [GitHub Actions](https://github.com/features/actions). |
| 90 | + |
| 91 | +### Poetry |
| 92 | + |
| 93 | +This project uses [Poetry](https://python-poetry.org/) for dependency management. |
| 94 | + |
| 95 | +#### Highlights |
| 96 | + |
| 97 | +- **Automatic virtual environment management**: Poetry automatically manages the `virtualenv` for the application. |
| 98 | +- **Automatic dependency management**: rather than having to run `pip freeze > requirements.txt`, Poetry automatically manages the dependency file (called _pyproject.toml_), and enables SemVer-level control over dependencies like [npm](https://semver.npmjs.com/). Poetry also manages a lockfile (called _poetry.lock_), which is similar to _package-lock.json_ for npm. Poetry uses this lockfile to automatically track specific versions and hashes for every dependency. |
| 99 | +- **Dependency resolution**: Poetry will automatically resolve any dependency version conflicts. pip did not have dependency resolution [until the end of 2020](https://pip.pypa.io/en/latest/user_guide/#changes-to-the-pip-dependency-resolver-in-20-3-2020). |
| 100 | +- **Dependency separation**: Poetry can maintain separate lists of dependencies for development and production in the _pyproject.toml_. Production installs can skip development dependencies to speed up Docker builds. |
| 101 | +- **Builds**: Poetry has features for easily building the project into a Python package. |
| 102 | + |
| 103 | +#### Installation |
| 104 | + |
| 105 | +The recommended installation method is through the [Poetry custom installer](https://python-poetry.org/docs/#installation), which vendorizes dependencies into an isolated environment, and allows you to update Poetry with `poetry self update`. |
| 106 | + |
| 107 | +You can also install Poetry however you prefer to install your user Python packages (`pipx install poetry`, `pip install --user poetry`, etc). Use the standard update methods with these tools (`pipx upgrade poetry`, `pip install --user --upgrade poetry`, etc). |
| 108 | + |
| 109 | +#### Key commands |
| 110 | + |
| 111 | +```sh |
| 112 | +# Basic usage: https://python-poetry.org/docs/basic-usage/ |
| 113 | +poetry install # create virtual environment and install dependencies |
| 114 | +poetry show --tree # list installed packages |
| 115 | +poetry add PACKAGE@VERSION # add a package to production dependencies, like pip install |
| 116 | +poetry add PACKAGE@VERSION --dev # add a package to development dependencies |
| 117 | +poetry update # update dependencies (not available with standard tools) |
| 118 | +poetry version # list or update version of this package |
| 119 | +poetry shell # activate the virtual environment, like source venv/bin/activate |
| 120 | +poetry run COMMAND # run a command within the virtual environment |
| 121 | +poetry env info # manage environments: https://python-poetry.org/docs/managing-environments/ |
| 122 | +poetry config virtualenvs.in-project true # configure Poetry to install virtualenvs into .venv |
| 123 | +poetry export -f requirements.txt > requirements.txt --dev # export dependencies |
| 124 | +``` |
| 125 | + |
| 126 | +## Docker |
| 127 | + |
| 128 | +- **[Docker](https://www.docker.com/)** is a technology for running lightweight virtual machines called **containers**. |
| 129 | + - An **image** is the executable set of files read by Docker. |
| 130 | + - A **container** is a running image. |
| 131 | + - The **[Dockerfile](https://docs.docker.com/engine/reference/builder/)** tells Docker how to build the container. |
| 132 | +- To [get started with Docker](https://www.docker.com/get-started): |
| 133 | + - Ubuntu Linux: follow the [instructions for Ubuntu Linux](https://docs.docker.com/install/linux/docker-ce/ubuntu/), making sure to follow the [postinstallation steps](https://docs.docker.com/install/linux/linux-postinstall/) to activate the Docker daemon. |
| 134 | + - macOS and Windows: install [Docker Desktop](https://www.docker.com/products/docker-desktop) (available via [Homebrew](https://brew.sh/) with `brew cask install docker`). |
| 135 | +- <details><summary>Expand this details element for more <a href="https://docs.docker.com/engine/reference/commandline/cli/">useful Docker commands</a>.</summary> |
| 136 | + |
| 137 | + ```sh |
| 138 | + # Log in with Docker Hub credentials to pull images |
| 139 | + docker login |
| 140 | + # List images |
| 141 | + docker images |
| 142 | + # List running containers: can also use `docker container ls` |
| 143 | + docker ps |
| 144 | + # View logs for the most recently started container |
| 145 | + docker logs -f $(docker ps -q -n 1) |
| 146 | + # View logs for all running containers |
| 147 | + docker logs -f $(docker ps -aq) |
| 148 | + # Inspect a container (web in this example) and return the IP Address |
| 149 | + docker inspect web | grep IPAddress |
| 150 | + # Stop a container |
| 151 | + docker stop # container hash |
| 152 | + # Stop all running containers |
| 153 | + docker stop $(docker ps -aq) |
| 154 | + # Remove a downloaded image |
| 155 | + docker image rm # image hash or name |
| 156 | + # Remove a container |
| 157 | + docker container rm # container hash |
| 158 | + # Prune images |
| 159 | + docker image prune |
| 160 | + # Prune stopped containers (completely wipes them and resets their state) |
| 161 | + docker container prune |
| 162 | + # Prune everything |
| 163 | + docker system prune |
| 164 | + # Open a shell in the most recently started container (like SSH) |
| 165 | + docker exec -it $(docker ps -q -n 1) /bin/bash |
| 166 | + # Or, connect as root: |
| 167 | + docker exec -u 0 -it $(docker ps -q -n 1) /bin/bash |
| 168 | + # Copy file to/from container: |
| 169 | + docker cp [container_name]:/path/to/file destination.file |
| 170 | + ``` |
| 171 | + |
| 172 | + </summary> |
0 commit comments