Setting up Continuous Quality with GitHub and Circle for Python

Python, GitHub, Travis and DeepSource

Pre-requisites

TIP

To integrate DeepSource with GitHub, please follow the quickstart guide. Source code for this guide resides is available on GitHub.

The following steps walk through how to add python and test-coverage analyzers to DeepSource, how to setup test coverage with Circle CI and optionally how to enable branch protection rules with GitHub.

Configure python and test-coverage analyzers in .deepsource.toml

To enable analysis on the repository, an analysis configuration should be defined in .deepsource.toml file in the repository's root. Let's configure for Python with test coverage.

# Version of the DeepSource TOML. Should be 1.
version = 1

# Glob pattern of the test files
test_patterns = [
  "tests/*"
]

# List of analyzers

# Python analyzer
[[analyzers]]
name = "python"
enabled = true
runtime_version = "3.x.x"

  [analyzers.meta]
  max_line_length = 90

# Test coverage analyzer
[[analyzers]]
name = "test-coverage"
enabled = true

Now that the configuration is written, create a file .deepsource.toml with the above contents in the repository's root.

GitHub file browser GitHub / Repository root

TIP

Refer to specific analyzer's configuration to see other available options.

Activate DeepSource analysis on the repository

  • To activate continuous analysis, go to DeepSource dashboard and click Activate new repo.

  • Since we've already added the configuration to the repository, select the repository and click I've already added the config, skip this step on right bottom and click Activate analysis on the repository overview page.

Configure Circle CI to run tests and report test coverage

Let's configure Travis CI to run the tests:

version: 2

jobs:
  build:
    docker:
      - image: circleci/python:3.7

    steps:
      - checkout

      - run:
          name: Install dependencies
          command: |
            sudo pip install -r requirements.txt

      - run:
          name: Run tests
          command: |
            coverage run test_hello.py

      - run:
          name: Report results to DeepSource
          command: |
            # Generate coverage report in xml format
            coverage xml

            # Install deepsource CLI
            curl https://deepsource.io/cli | sh

            # Report coverage artifact to 'test-coverage' analyzer
            ./bin/deepsource report --analyzer test-coverage --key python --value-file ./coverage.xml

TIP

Refer to Circle CI documentation to customize the build.

  • Create a file .circleci/config.yml with the above contents in the repository's root.

Activate Circle builds

  • Go to Settings page of the repository dashboard in DeepSource and copy the DSN from the Reporting section.

Repository settings page of DeepSource DeepSource / Repository Settings

  • On Circle CI, go to Settings > Environment Variables and add DEEPSOURCE_DSN environment variable with value as the DSN copied above.

Circle CI settings page Circle CI / Dashboard

WARNING

Do not add the DEEPSOURCE_DSN variable as part of .circleci/config.yml as the value contains sensitive information.

  • Trigger the build on Circle CI.

Setup GitHub status checks (Optional)

Required status checks ensure that all required checks are passing before collaborators can make changes to a branch.

  • Go to repository Settings page on GitHub and click Branches tab. Under Branch protection rules section, add a new branch by clicking Add rule (or) updating existing branch protection configuration by clicking Edit against the listed branch.

Repository settings page of GitHub GitHub / Repository Settings

  • Enable DeepSource:Python, DeepSource: Test Coverage, ci/circleci: Build Error and ci/circleci: build

Branch protection section of repository settings page of GitHub GitHub / Repository Settings / Branch protection

WARNING

If you enable required status checks, Circle CI and DeepSource checks will be strictly enforced -- pull requests can be merged only after all the tests pass and all issues of major or critical severity are fixed.

🎉 Circle CI and DeepSource now runs tests and quality analysis continuously on every code change.

DeepSource repository dashboard DeepSource / Repository Dashboard