Making a Review Board Extension¶
Development Environment¶
Writing Review Board extensions requires having a Review Board development environment (not a regular production installation). For instructions on how to set this up, see the getting started guide.
After following that guide, you should have a Python Virtual Environment which contains all the Review Board packages, and you should be able to run the Django development server:
$ ./contrib/internal/devserver.py
This will run a local instance of Review Board on http://localhost:8080/
Creating an Extension Package¶
Review Board extensions are distributed as Python packages.
The rbext create command is the quickest way to create the boilerplate package setup for your new extension.
$ rbext create \
--name "Sample Review Board Extension" \
--package-name sample-extension \
--class-name SampleExtension
This will create a new directory named sample-extension that contains the
basic framework for your new extension. Inside that directory are several
files, but the two most important are:
- pyproject.toml:
The python package info. This defines your Python package, its metadata, and the Entry Point, which allows Review Board to discover your extension.
- sample-extension/extension.py:
The main module containing your extension, where the bulk of your work will go (at least initially).
See Extension Files and Package Layout for more details on these.
Note
If you’re using rbext from Review Board 7.1 or newer, the resulting
Python package will use a PEP517-compatible pyproject.toml. On older
versions, it will create a setup.py for the package.
Installing for Development¶
When developing your extension, it’s most convenient to install the package into your Python Virtual Environment in “editable” mode. This allows you to iterate on your package without needing to rebuild and install wheel packages each time.
To do this, inside the sample-extension directory, run:
$ pip3 install -e .
Activating the Extension¶
After installing the extension in development mode, you should be able to open up the admin site in your devserver, select Extensions, and see your new extension in the list. If there are failures, you may see an error message in this list, and any exception backtraces will be visible in the console that you’re running the devserver in.
Click Enable to enable your new extension.
The Review Board devserver will watch your source files as you make changes to your extension, so you can usually just reload pages in the browser without needing to restart the devserver.
Implementing Your Extension¶
At this point, you should have a working development environment and extension package, but it doesn’t actually do anything.
Extension Hooks¶
Extensions connect to Review Board through “hooks.” There are many different types of hooks to accomplish different tasks.
Hooks are used by instantiating them in your extension’s initialize method:
from django.urls import include, path
from reviewboard.extensions.base import Extension
from reviewboard.extensions.hooks import URLHook
class SampleExtension(Extension):
def initialize(self) -> None:
urlpatterns = [
path('sample_extension/', include('sample_extension.urls')),
]
URLHook(self, urlpatterns)
See Extension Hooks for details on the available functionality.
Adding API Resources¶
Extensions can also provide new API resources. This is not done via a hook, but by specifying a list of resource classes inside of your extension class. See Extending the Web API for details.
Next Steps¶
Now that you have the extension framework ready, check out our Full Examples and the Extension Reference.