Skip to content

Using Danger in Pocket

CMasterson edited this page May 17, 2023 · 2 revisions

Danger is a static analysis framework that allows developers to add checks to their PRs.

Danger is used in Pocket iOS to run SwiftLint, check code coverage and ensure certain testing environment variables are not switched on by default.

Adding Checks

You can add additional rules by modifying our Dangerfile, which you can find here. Firefox also uses Danger, here.

The Danger-Swift Dangerfile uses Swift syntax.

To add a new check you should add a new function to perform that check. To actually run the check you should add the call near the top of the file, following the existing calls.

import Foundation

let danger = Danger()

// Calls to checking functions
someCheck()

// Checking functions
func someCheck() {
    // Swift code the perform a check
}

You can make use of Swift print() statements while debugging. Your messages will appear in Terminal as you run Danger-swift.
To echo a message in your PR pipeline use message(“Hello World”).

Asserts

If something is worth highlighting for fixing but does not fail the build, you can warn(“This needs fixing”)
If you encounter a failing state or error you can use fail(“The reason for failing”)

The Danger Object

You can assess the details of the current PR by accessing the Danger object.
By default a Dangerfile will count the files created and modified in a PR.

import Foundation

let danger = Danger()

changedFiles()

func changedFiles() {
	message("Edited \(danger.git.modifiedFiles.count) files")
	message("Created \(danger.git.createdFiles.count) files")
}

modifiedFiles and createdFiles are standard arrays of filenames and are available for all the usual operations.
let modifiedSwiftFiles = danger.git.modifiedFiles.filter { $0.contains(".swift") }

Working with Files & Changes

//Talk about Hunks

Danger also allows you to read an entire file’s contents.
danger.utils.readFile(modifiedSwiftFiles.first()!) From here you can do whatever style of pattern matching you wish.

Testing Your Changes

To test your changes locally you need to install Danger-Swift. You can check the version the CI will be using here. (3.15.0 at the time of writing).

Installation

To install Danger-swift locally you can use Homebrew: brew install danger/tap/danger-swift Caveat: You have no control over the version installed, so this is a fast-but-brittle solution
You can confirm the version installed with brew info danger-swift

Running Locally

When you have your changes ready you can test them locally. This is done through Terminal. danger-swift local -b develop

Running Against a PR

If you create a PR for your changes you can run: danger-swift pr https://github.com/Pocket/pocket-ios/pull/[PR-NUMBER] Danger will run your local Dangerfile. You can make changes locally, save them and without committing or pushing run danger-swift pr to run your local revision. Be advised that there is some required interaction with GitHub, so without setting a GitHub token you get approximately 4 runs/hour before GitHub will throttle your requests.

GitHub Throttling

If you need more runs there is an easy fix; Generate a GitHub token(Classic): https://github.com/settings/tokens Only give the token public_repo access. In Terminal run export DANGER_GITHUB_API_TOKEN='exampletoken' Until your token expires, future Danger runs should not be throttled (Up to 5000/hour).

Finishing up

When you are happy with your changes you can commit and push as normal and our CI should pick them up.

References

Danger

API