Blog
`dangerfile.ts` in Twenty, the #1 open-source CRM.

`dangerfile.ts` in Twenty, the #1 open-source CRM.

In this article, we will review dangerfile.ts in Twenty, the #1 open-source CRM. We will look at:

  1. First time contributor alert

  2. schedule in danger.js

  3. danger-plugin-todos

First time contributor alert

You will find the following code in twenty/packages/twenty-utils/dangerfile.ts

// CLA alert if first time contributor
if (
  danger.github &&
  danger.github.pr &&
  (danger.github.pr.author_association === 'FIRST_TIME_CONTRIBUTOR' ||
    danger.github.pr.author_association === 'NONE')
) {
  markdown(
    getMdSection(
      'Welcome!',
      `
Hello there, congrats on your first PR! We're excited to have you contributing to this project.
By submitting your Pull Request, you acknowledge that you agree with the terms of our [Contributor License Agreement](https://github.com/twentyhq/twenty/blob/main/.github/CLA.md).`,
    ),
  );
}

This above code is used to leave a comment in a pr if you make your first pull request to Twenty codebase.

The following image shows how this comment looks like:

Schedule in danger.js

You will find the following code in the same dangerfile.ts

// TODOS / Fixme
schedule(todos());

schedule is imported from danger.

import { danger, markdown, schedule, warn } from 'danger';

todos is imported from danger-plugin-todos.

import todos from 'danger-plugin-todos';

I found the following example for schedule in danger documentation.

// A Dangerfile, in Peril, is evaluated as a script, and so async code does 
// not work out of the box. By using the `schedule` function you can now 
// register a section of code to evaluate across multiple tick cycles.
// `schedule` currently handles two types of arguments, either a promise 
// or a function with a resolve arg.
schedule(asyncFunction: Scheduleable) => void

Schedule is used to execute async code.

danger-plugin-todos

A danger-js plugin to list all todos/fixmes/etc added/changed/removed in a PR.

Intro to Danger

Danger is a tool to automate common code review practices. It can run as part of your CI pipeline and help maintain standards. Check out their github repo at danger/danger-js. You can configure it using a dangerfile, which is a javscript or typescript file in the root of your project. It'll post it's results as a comment in the PRs of your project.

Example

// dangerfile.js
import { schedule } from 'danger'
import todos from 'danger-plugin-todos'

// Using schedule because this is an async task
schedule(todos())


// Optionally provide options
schedule(todos({
    ignore: ['CHANGELOG.md', /test/], // Any files to ignore, can be part of filename or regex pattern to match (default: [])
    keywords: ['TODO', 'FIXME', 'TO-DO'], // Keywords to find (default: ['TODO', 'FIXME'])
    repoUrl: 'https://github.com/rohit-gohri/danger-plugin-todos', // If using github provide the repo url (default: true - tries to pick from package.json -> repository.url)
}))


// For other git providers (that don't follow github style links for files) provide a custom function to turn filepaths into links for the specific commit
schedule(todos({
    repoUrl: (filepath) => `https://custom-git-example.com/rohit-gohri/danger-plugin-todos/tree/${danger.git.commits[0].sha}/${filepath}`,
}))

Results Preview

About me

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Build Shadcn CLI from scratch.

References:

  1. https://github.com/twentyhq/twenty/blob/main/packages/twenty-utils/dangerfile.ts

  2. https://github.com/twentyhq/twenty/pull/12937

  3. https://danger.systems/js/reference

  4. https://www.npmjs.com/package/danger-plugin-todos