| 1 |
.. _contributing: |
| 2 |
|
| 3 |
Contributing |
| 4 |
============ |
| 5 |
|
| 6 |
Datasette is an open source project. We welcome contributions! |
| 7 |
|
| 8 |
This document describes how to contribute to Datasette core. You can also contribute to the wider Datasette ecosystem by creating new :ref:`plugins`. |
| 9 |
|
| 10 |
General guidelines |
| 11 |
------------------ |
| 12 |
|
| 13 |
* **main should always be releasable**. Incomplete features should live in branches. This ensures that any small bug fixes can be quickly released. |
| 14 |
* **The ideal commit** should bundle together the implementation, unit tests and associated documentation updates. The commit message should link to an associated issue. |
| 15 |
* **New plugin hooks** should only be shipped if accompanied by a separate release of a non-demo plugin that uses them. |
| 16 |
|
| 17 |
.. _devenvironment: |
| 18 |
|
| 19 |
Setting up a development environment |
| 20 |
------------------------------------ |
| 21 |
|
| 22 |
If you have Python 3.7 or higher installed on your computer (on OS X the quickest way to do this `is using homebrew <https://docs.python-guide.org/starting/install3/osx/>`__) you can install an editable copy of Datasette using the following steps. |
| 23 |
|
| 24 |
If you want to use GitHub to publish your changes, first `create a fork of datasette <https://github.com/simonw/datasette/fork>`__ under your own GitHub account. |
| 25 |
|
| 26 |
Now clone that repository somewhere on your computer:: |
| 27 |
|
| 28 |
git clone [email protected]:YOURNAME/datasette |
| 29 |
|
| 30 |
If you want to get started without creating your own fork, you can do this instead:: |
| 31 |
|
| 32 |
git clone [email protected]:simonw/datasette |
| 33 |
|
| 34 |
The next step is to create a virtual environment for your project and use it to install Datasette's dependencies:: |
| 35 |
|
| 36 |
cd datasette |
| 37 |
# Create a virtual environment in ./venv |
| 38 |
python3 -m venv ./venv |
| 39 |
# Now activate the virtual environment, so pip can install into it |
| 40 |
source venv/bin/activate |
| 41 |
# Install Datasette and its testing dependencies |
| 42 |
python3 -m pip install -e '.[test]' |
| 43 |
|
| 44 |
That last line does most of the work: ``pip install -e`` means "install this package in a way that allows me to edit the source code in place". The ``.[test]`` option means "use the setup.py in this directory and install the optional testing dependencies as well". |
| 45 |
|
| 46 |
.. _contributing_running_tests: |
| 47 |
|
| 48 |
Running the tests |
| 49 |
----------------- |
| 50 |
|
| 51 |
Once you have done this, you can run the Datasette unit tests from inside your ``datasette/`` directory using `pytest <https://docs.pytest.org/>`__ like so:: |
| 52 |
|
| 53 |
pytest |
| 54 |
|
| 55 |
You can run the tests faster using multiple CPU cores with `pytest-xdist <https://pypi.org/project/pytest-xdist/>`__ like this:: |
| 56 |
|
| 57 |
pytest -n auto -m "not serial" |
| 58 |
|
| 59 |
``-n auto`` detects the number of available cores automatically. The ``-m "not serial"`` skips tests that don't work well in a parallel test environment. You can run those tests separately like so:: |
| 60 |
|
| 61 |
pytest -m "serial" |
| 62 |
|
| 63 |
.. _contributing_using_fixtures: |
| 64 |
|
| 65 |
Using fixtures |
| 66 |
-------------- |
| 67 |
|
| 68 |
To run Datasette itself, type ``datasette``. |
| 69 |
|
| 70 |
You're going to need at least one SQLite database. A quick way to get started is to use the fixtures database that Datasette uses for its own tests. |
| 71 |
|
| 72 |
You can create a copy of that database by running this command:: |
| 73 |
|
| 74 |
python tests/fixtures.py fixtures.db |
| 75 |
|
| 76 |
Now you can run Datasette against the new fixtures database like so:: |
| 77 |
|
| 78 |
datasette fixtures.db |
| 79 |
|
| 80 |
This will start a server at ``http://127.0.0.1:8001/``. |
| 81 |
|
| 82 |
Any changes you make in the ``datasette/templates`` or ``datasette/static`` folder will be picked up immediately (though you may need to do a force-refresh in your browser to see changes to CSS or JavaScript). |
| 83 |
|
| 84 |
If you want to change Datasette's Python code you can use the ``--reload`` option to cause Datasette to automatically reload any time the underlying code changes:: |
| 85 |
|
| 86 |
datasette --reload fixtures.db |
| 87 |
|
| 88 |
You can also use the ``fixtures.py`` script to recreate the testing version of ``metadata.json`` used by the unit tests. To do that:: |
| 89 |
|
| 90 |
python tests/fixtures.py fixtures.db fixtures-metadata.json |
| 91 |
|
| 92 |
Or to output the plugins used by the tests, run this:: |
| 93 |
|
| 94 |
python tests/fixtures.py fixtures.db fixtures-metadata.json fixtures-plugins |
| 95 |
Test tables written to fixtures.db |
| 96 |
- metadata written to fixtures-metadata.json |
| 97 |
Wrote plugin: fixtures-plugins/register_output_renderer.py |
| 98 |
Wrote plugin: fixtures-plugins/view_name.py |
| 99 |
Wrote plugin: fixtures-plugins/my_plugin.py |
| 100 |
Wrote plugin: fixtures-plugins/messages_output_renderer.py |
| 101 |
Wrote plugin: fixtures-plugins/my_plugin_2.py |
| 102 |
|
| 103 |
Then run Datasette like this:: |
| 104 |
|
| 105 |
datasette fixtures.db -m fixtures-metadata.json --plugins-dir=fixtures-plugins/ |
| 106 |
|
| 107 |
.. _contributing_debugging: |
| 108 |
|
| 109 |
Debugging |
| 110 |
--------- |
| 111 |
|
| 112 |
Any errors that occur while Datasette is running while display a stack trace on the console. |
| 113 |
|
| 114 |
You can tell Datasette to open an interactive ``pdb`` debugger session if an error occurs using the ``--pdb`` option:: |
| 115 |
|
| 116 |
datasette --pdb fixtures.db |
| 117 |
|
| 118 |
.. _contributing_formatting: |
| 119 |
|
| 120 |
Code formatting |
| 121 |
--------------- |
| 122 |
|
| 123 |
Datasette uses opinionated code formatters: `Black <https://github.com/psf/black>`__ for Python and `Prettier <https://prettier.io/>`__ for JavaScript. |
| 124 |
|
| 125 |
These formatters are enforced by Datasette's continuous integration: if a commit includes Python or JavaScript code that does not match the style enforced by those tools, the tests will fail. |
| 126 |
|
| 127 |
When developing locally, you can verify and correct the formatting of your code using these tools. |
| 128 |
|
| 129 |
.. _contributing_formatting_black: |
| 130 |
|
| 131 |
Running Black |
| 132 |
~~~~~~~~~~~~~ |
| 133 |
|
| 134 |
Black will be installed when you run ``pip install -e '.[test]'``. To test that your code complies with Black, run the following in your root ``datasette`` repository checkout:: |
| 135 |
|
| 136 |
$ black . --check |
| 137 |
All done! ✨ 🍰 ✨ |
| 138 |
95 files would be left unchanged. |
| 139 |
|
| 140 |
If any of your code does not conform to Black you can run this to automatically fix those problems:: |
| 141 |
|
| 142 |
$ black . |
| 143 |
reformatted ../datasette/setup.py |
| 144 |
All done! ✨ 🍰 ✨ |
| 145 |
1 file reformatted, 94 files left unchanged. |
| 146 |
|
| 147 |
.. _contributing_formatting_blacken_docs: |
| 148 |
|
| 149 |
blacken-docs |
| 150 |
~~~~~~~~~~~~ |
| 151 |
|
| 152 |
The `blacken-docs <https://pypi.org/project/blacken-docs/>`__ command applies Black formatting rules to code examples in the documentation. Run it like this:: |
| 153 |
|
| 154 |
blacken-docs -l 60 docs/*.rst |
| 155 |
|
| 156 |
.. _contributing_formatting_prettier: |
| 157 |
|
| 158 |
Prettier |
| 159 |
~~~~~~~~ |
| 160 |
|
| 161 |
To install Prettier, `install Node.js <https://nodejs.org/en/download/package-manager/>`__ and then run the following in the root of your ``datasette`` repository checkout:: |
| 162 |
|
| 163 |
$ npm install |
| 164 |
|
| 165 |
This will install Prettier in a ``node_modules`` directory. You can then check that your code matches the coding style like so:: |
| 166 |
|
| 167 |
$ npm run prettier -- --check |
| 168 |
> prettier |
| 169 |
> prettier 'datasette/static/*[!.min].js' "--check" |
| 170 |
|
| 171 |
Checking formatting... |
| 172 |
[warn] datasette/static/plugins.js |
| 173 |
[warn] Code style issues found in the above file(s). Forgot to run Prettier? |
| 174 |
|
| 175 |
You can fix any problems by running:: |
| 176 |
|
| 177 |
$ npm run fix |
| 178 |
|
| 179 |
.. _contributing_documentation: |
| 180 |
|
| 181 |
Editing and building the documentation |
| 182 |
-------------------------------------- |
| 183 |
|
| 184 |
Datasette's documentation lives in the ``docs/`` directory and is deployed automatically using `Read The Docs <https://readthedocs.org/>`__. |
| 185 |
|
| 186 |
The documentation is written using reStructuredText. You may find this article on `The subset of reStructuredText worth committing to memory <https://simonwillison.net/2018/Aug/25/restructuredtext/>`__ useful. |
| 187 |
|
| 188 |
You can build it locally by installing ``sphinx`` and ``sphinx_rtd_theme`` in your Datasette development environment and then running ``make html`` directly in the ``docs/`` directory:: |
| 189 |
|
| 190 |
# You may first need to activate your virtual environment: |
| 191 |
source venv/bin/activate |
| 192 |
|
| 193 |
# Install the dependencies needed to build the docs |
| 194 |
pip install -e .[docs] |
| 195 |
|
| 196 |
# Now build the docs |
| 197 |
cd docs/ |
| 198 |
make html |
| 199 |
|
| 200 |
This will create the HTML version of the documentation in ``docs/_build/html``. You can open it in your browser like so:: |
| 201 |
|
| 202 |
open _build/html/index.html |
| 203 |
|
| 204 |
Any time you make changes to a ``.rst`` file you can re-run ``make html`` to update the built documents, then refresh them in your browser. |
| 205 |
|
| 206 |
For added productivity, you can use use `sphinx-autobuild <https://pypi.org/project/sphinx-autobuild/>`__ to run Sphinx in auto-build mode. This will run a local webserver serving the docs that automatically rebuilds them and refreshes the page any time you hit save in your editor. |
| 207 |
|
| 208 |
``sphinx-autobuild`` will have been installed when you ran ``pip install -e .[docs]``. In your ``docs/`` directory you can start the server by running the following:: |
| 209 |
|
| 210 |
make livehtml |
| 211 |
|
| 212 |
Now browse to ``http://localhost:8000/`` to view the documentation. Any edits you make should be instantly reflected in your browser. |
| 213 |
|
| 214 |
.. _contributing_documentation_cog: |
| 215 |
|
| 216 |
Running Cog |
| 217 |
~~~~~~~~~~~ |
| 218 |
|
| 219 |
Some pages of documentation (in particular the :ref:`cli_reference`) are automatically updated using `Cog <https://github.com/nedbat/cog>`__. |
| 220 |
|
| 221 |
To update these pages, run the following command:: |
| 222 |
|
| 223 |
cog -r docs/*.rst |
| 224 |
|
| 225 |
.. _contributing_continuous_deployment: |
| 226 |
|
| 227 |
Continuously deployed demo instances |
| 228 |
------------------------------------ |
| 229 |
|
| 230 |
The demo instance at `latest.datasette.io <https://latest.datasette.io/>`__ is re-deployed automatically to Google Cloud Run for every push to ``main`` that passes the test suite. This is implemented by the GitHub Actions workflow at `.github/workflows/deploy-latest.yml <https://github.com/simonw/datasette/blob/main/.github/workflows/deploy-latest.yml>`__. |
| 231 |
|
| 232 |
Specific branches can also be set to automatically deploy by adding them to the ``on: push: branches`` block at the top of the workflow YAML file. Branches configured in this way will be deployed to a new Cloud Run service whether or not their tests pass. |
| 233 |
|
| 234 |
The Cloud Run URL for a branch demo can be found in the GitHub Actions logs. |
| 235 |
|
| 236 |
.. _contributing_release: |
| 237 |
|
| 238 |
Release process |
| 239 |
--------------- |
| 240 |
|
| 241 |
Datasette releases are performed using tags. When a new release is published on GitHub, a `GitHub Action workflow <https://github.com/simonw/datasette/blob/main/.github/workflows/deploy-latest.yml>`__ will perform the following: |
| 242 |
|
| 243 |
* Run the unit tests against all supported Python versions. If the tests pass... |
| 244 |
* Build a Docker image of the release and push a tag to https://hub.docker.com/r/datasetteproject/datasette |
| 245 |
* Re-point the "latest" tag on Docker Hub to the new image |
| 246 |
* Build a wheel bundle of the underlying Python source code |
| 247 |
* Push that new wheel up to PyPI: https://pypi.org/project/datasette/ |
| 248 |
|
| 249 |
To deploy new releases you will need to have push access to the main Datasette GitHub repository. |
| 250 |
|
| 251 |
Datasette follows `Semantic Versioning <https://semver.org/>`__:: |
| 252 |
|
| 253 |
major.minor.patch |
| 254 |
|
| 255 |
We increment ``major`` for backwards-incompatible releases. Datasette is currently pre-1.0 so the major version is always ``0``. |
| 256 |
|
| 257 |
We increment ``minor`` for new features. |
| 258 |
|
| 259 |
We increment ``patch`` for bugfix releass. |
| 260 |
|
| 261 |
:ref:`contributing_alpha_beta` may have an additional ``a0`` or ``b0`` prefix - the integer component will be incremented with each subsequent alpha or beta. |
| 262 |
|
| 263 |
To release a new version, first create a commit that updates the version number in ``datasette/version.py`` and the :ref:`the changelog <changelog>` with highlights of the new version. An example `commit can be seen here <https://github.com/simonw/datasette/commit/0e1e89c6ba3d0fbdb0823272952cf356f3016def>`__:: |
| 264 |
|
| 265 |
# Update changelog |
| 266 |
git commit -m " Release 0.51a1 |
| 267 |
|
| 268 |
Refs #1056, #1039, #998, #1045, #1033, #1036, #1034, #976, #1057, #1058, #1053, #1064, #1066" -a |
| 269 |
git push |
| 270 |
|
| 271 |
Referencing the issues that are part of the release in the commit message ensures the name of the release shows up on those issue pages, e.g. `here <https://github.com/simonw/datasette/issues/581#ref-commit-d56f402>`__. |
| 272 |
|
| 273 |
You can generate the list of issue references for a specific release by copying and pasting text from the release notes or GitHub changes-since-last-release view into this `Extract issue numbers from pasted text <https://observablehq.com/@simonw/extract-issue-numbers-from-pasted-text>`__ tool. |
| 274 |
|
| 275 |
To create the tag for the release, create `a new release <https://github.com/simonw/datasette/releases/new>`__ on GitHub matching the new version number. You can convert the release notes to Markdown by copying and pasting the rendered HTML into this `Paste to Markdown tool <https://euangoddard.github.io/clipboard2markdown/>`__. |
| 276 |
|
| 277 |
Publishing a non-prerelease also runs ``.github/workflows/stable-docs.yml``, which resets the ``stable`` branch to the release tag. Read the Docs then rebuilds the documentation at ``/en/stable/``. This workflow must be present in the release tag, including releases from maintenance branches such as ``0.65.x``. Verify that the workflow and the subsequent Read the Docs build both succeed. |
| 278 |
|
| 279 |
Between releases, include ``!stable-docs`` in a commit message on ``main`` or ``0.65.x`` to copy the documentation files changed by that commit to ``stable``. This copies whole files, so those pages must describe the stable release. Changes made only on ``stable`` are replaced by the next release; keep documentation and build configuration fixes in the release source branch too. |
| 280 |
|
| 281 |
Finally, post a news item about the release on `datasette.io <https://datasette.io/>`__ by editing the `news.yaml <https://github.com/simonw/datasette.io/blob/main/news.yaml>`__ file in that site's repository. |
| 282 |
|
| 283 |
.. _contributing_alpha_beta: |
| 284 |
|
| 285 |
Alpha and beta releases |
| 286 |
----------------------- |
| 287 |
|
| 288 |
Alpha and beta releases are published to preview upcoming features that may not yet be stable - in particular to preview new plugin hooks. |
| 289 |
|
| 290 |
You are welcome to try these out, but please be aware that details may change before the final release. |
| 291 |
|
| 292 |
Please join `discussions on the issue tracker <https://github.com/simonw/datasette/issues>`__ to share your thoughts and experiences with on alpha and beta features that you try out. |
| 293 |
|
| 294 |
.. _contributing_bug_fix_branch: |
| 295 |
|
| 296 |
Releasing bug fixes from a branch |
| 297 |
--------------------------------- |
| 298 |
|
| 299 |
If it's necessary to publish a bug fix release without shipping new features that have landed on ``main`` a release branch can be used. |
| 300 |
|
| 301 |
Create it from the relevant last tagged release like so:: |
| 302 |
|
| 303 |
git branch 0.52.x 0.52.4 |
| 304 |
git checkout 0.52.x |
| 305 |
|
| 306 |
Next cherry-pick the commits containing the bug fixes:: |
| 307 |
|
| 308 |
git cherry-pick COMMIT |
| 309 |
|
| 310 |
Write the release notes in the branch, and update the version number in ``version.py``. Then push the branch:: |
| 311 |
|
| 312 |
git push -u origin 0.52.x |
| 313 |
|
| 314 |
Once the tests have completed, publish the release from that branch target using the GitHub `Draft a new release <https://github.com/simonw/datasette/releases/new>`__ form. |
| 315 |
|
| 316 |
Finally, cherry-pick the commit with the release notes and version number bump across to ``main``:: |
| 317 |
|
| 318 |
git checkout main |
| 319 |
git cherry-pick COMMIT |
| 320 |
git push |
| 321 |
|
| 322 |
.. _contributing_upgrading_codemirror: |
| 323 |
|
| 324 |
Upgrading CodeMirror |
| 325 |
-------------------- |
| 326 |
|
| 327 |
Datasette bundles `CodeMirror <https://codemirror.net/>`__ for the SQL editing interface, e.g. on `this page <https://latest.datasette.io/fixtures>`__. Here are the steps for upgrading to a new version of CodeMirror: |
| 328 |
|
| 329 |
* Download and extract latest CodeMirror zip file from https://codemirror.net/codemirror.zip |
| 330 |
* Rename ``lib/codemirror.js`` to ``codemirror-5.57.0.js`` (using latest version number) |
| 331 |
* Rename ``lib/codemirror.css`` to ``codemirror-5.57.0.css`` |
| 332 |
* Rename ``mode/sql/sql.js`` to ``codemirror-5.57.0-sql.js`` |
| 333 |
* Edit both JavaScript files to make the top license comment a ``/* */`` block instead of multiple ``//`` lines |
| 334 |
* Minify the JavaScript files like this:: |
| 335 |
|
| 336 |
npx uglify-js codemirror-5.57.0.js -o codemirror-5.57.0.min.js --comments '/LICENSE/' |
| 337 |
npx uglify-js codemirror-5.57.0-sql.js -o codemirror-5.57.0-sql.min.js --comments '/LICENSE/' |
| 338 |
|
| 339 |
* Check that the LICENSE comment did indeed survive minification |
| 340 |
* Minify the CSS file like this:: |
| 341 |
|
| 342 |
npx clean-css-cli codemirror-5.57.0.css -o codemirror-5.57.0.min.css |
| 343 |
|
| 344 |
* Edit the ``_codemirror.html`` template to reference the new files |
| 345 |
* ``git rm`` the old files, ``git add`` the new files |