Skip to content

Repository files navigation

PyPIPython versionsCI

python

Write your Dangerfiles in Python.

Requirements

⚠️danger-python is currently work in progress. Breaking changes may occur.

Running danger-python requires:

  • Python 3.13+ (developed and tested under Python 3.14)
  • danger-js 9.2 (tested under danger-js 9.2.10)

Installation

In order to test the script please run the following commands:

# install danger-js
npm install -g danger
# install danger-python
pip install danger-python
# run danger-python
danger-python pr https://github.com/microsoft/TypeScript/pull/34806

dangerfile.py

You can choose between shorter untyped version of dangerfile.py:

title=danger.github.pr.titlemarkdown(title)

Or more verbose typed version:

fromdanger_pythonimportDanger, markdowndanger=Danger()
title=danger.github.pr.titlemarkdown(title)

Using as GitHub Action

  1. Create a dangerfile.py in the root directory of your repository.
  2. Add a following workflow:
name: Dangeron: [pull_request]jobs:
build:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v4
- uses: danger/python@masterenv:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Using as a CI step

If you already run your own pipeline, add danger-python as a step after your test suite instead of using the prebuilt action above. This works on any CI provider — the example below uses GitHub Actions:

name: CIon: [pull_request]jobs:
build:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5with:
python-version: "3.14"
- uses: actions/setup-node@v4with:
node-version: "20"# ... install dependencies and run your test suite here ...
- run: npm install -g danger
- run: pip install danger-python
- run: danger-python ci -venv:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Development

To develop the danger-python, clone the repository and run the following commands:

# install danger-js
npm install -g danger
# install uv (https://docs.astral.sh/uv/getting-started/installation/)# create the virtual environment and install all dependencies from uv.lock
uv sync
# run the full local gate: format, lint, type-check, tests (enforces 100% coverage)
uv run ruff format --check
uv run ruff check
uv run pyrefly check
uv run pytest

To regenerate the input JSONSchema, put the latest version in scripts/input_schema.json and run the following commands:

cd scripts
uv run python generate_scheme.py

This should update the danger_python/models.py file.

⚠️ Please, be careful with the generation script since the master branch contains the hand edits to the schema. They are all covered by the test suite, though.

Building plugins

To build a plugin, add a danger-python as a dependency and subclass the DangerPlugin class:

fromdanger_python.pluginsimportDangerPluginclassExamplePlugin(DangerPlugin):
defhello_world(self):
count=len(self.danger.git.modified_files)
self.message(f"ExamplePlugin says hello to {count} modified files")

Importing the package in a dangerfile.py will import all the instance methods to the module-level globals:

importexample_plugin# calls hello_world method from the plugin classexample_plugin.hello_world()

About

⚠️ Stop saying "you forgot to …" in code review

Resources

Stars

114 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages