Then use the run(...rules) function from endanger in your dangerfile:
// dangerfile.tsimport { run } from "endanger"import myFirstRule from "./danger/myFirstRule"import mySecondRule from "./danger/mySecondRule"import myThirdRule from "./danger/myThirdRule"run( myFirstRule(), mySecondRule(), myThirdRule({ someOption: "foo", }), myThirdRule({ someOption: "bar", }),)
Now let's write your first endanger rule.
import { Rule } from "endanger"export default function myFirstRule() { return new Rule({ match: { // "Glob" patterns of files you want to look at in this rule. files: ["scary-directory/**"], }, // A map of strings for different warnings/failures/etc so you don't have to // clutter your rule code. messages: { // Pro-tip: The indentation will automatically be stripped away :P myFirstWarning: ` Hey you added a new file to "scary-directory/"! `, }, // And here goes your code for the rule... async run({ files, context }) { // You can explore the state of the files you matched with your glob patterns. for (let file of files.created) {
This rule warns you whenever you create a new file in the scary-directory/. But endanger makes it
easy to write lots of other types of rules.
import { Rule } from "endanger"export default function mySecondRule() { return new Rule({ match: { files: ["api/routes/*.py"], }, messages: { foundNewRouteWithoutRateLimit: `...`, foundRemovedRateLimit: `...`, foundAddedRateLimit: `...`, }, // And here goes your code for the rule... async run({ files, context }) { // files.modifiedOrCreated will give you a list of all files created or modified for (let file of files.modifiedOrCreated) { // file.created will tell you if the current file was created in this diff if (file.created) { // file.contains() will tell you if the file contains a string or regex
You can have rules that fire on things other than files, you could also match commits like so:
import { Rule } from "endanger"let TICKET_REGEX = /\b(JIRA-\d+)\b/export default function mySecondRule() { return new Rule({ match: { commit: [TICKET_REGEX], }, messages: { jiraLink: ` [View linked ticket {ticket} on JIRA](https://jira.intranet.corp/{ticket}) `, }, async run({ commits, context }) { for (let commit of commits) { let match = commit.message.match(TICKET_REGEX) if (match) { context.message("jiraLink"
Important! You can only access files or commits in your rule if you have a match filter
defined for them. And you can only access files or commits which match your defined filter.
API
run
This should be in your Dangerfile, pass Rule's run them.
import { run } from "endanger"import rule1 from "./danger/rule1"import rule2 from "./danger/rule2"import rule3 from "./danger/rule3"run( rule1, rule2, rule3,)
Rule
import { Rule } from "endanger"export default function myRule() { return new Rule({ match: { files: ["path/to/**", "{glob,patterns}"], commits: ["messages that contain this string", /or match this regex/], }, messages: { myFirstWarning: `...`, mySecondWarning: `...`, }, async run({ files, commits, context }) { // ... }, })}
Note: It's recommended you wrap your rules with a function so you could add options to them
later. For example, you could run the same rule twice on different directories provided as
options.
This represents some readable data whether it be a File, FileState, or
Diff.
// Read the contents of this file/diff/etc.await bytes.contents() // "line1/nline2"// Does this file/diff/etc contain a string or match a regex?await bytes.contains("string") // true/falseawait bytes.contains(/regex/) // true/false
// Get the file path (relative to repo root)file.path // "path/to/file.ext"// Get the file's namefile.name // "file.ext"// Get the file dirname (relative to repo root)file.dirname // "path/to"// Get the file basenamefile.basename // "file"// Get the file extensionfile.extension // ".ext"// Does the file path match a set of glob patterns?file.matches("path/to/**", "{glob,patterns}") // true/false// Parse the file as JSONawait file.json() // { ... }// Parse the file as YAMLawait file.yaml() // { ... }// Read this file line by lineawait file.
// Has this diff line's content been addedd?diffLine.added // true | false// Has this diff line's content been removed?diffLine.removed // true | false// Has this diff line's content been changed (added or removed)?diffLine.changed // true | false// Is this diff line's content unchanged?diffLine.unchanged // true | false// What is the line number before the change?diffLine.lineNumberBefore // number | null// What is the line number after the change?diffLine.lineNumberAfter // number | null
Diff
// Only the added linesawait diff.added() // [DiffLine, DiffLine]// Only the removed linesawait diff.removed() // [DiffLine, DiffLine]// All of the changed linesawait diff.changed() // [DiffLine, DiffLine, DiffLine, DiffLine]// All of the changed lines with several lines of surrounding contextawait diff.unified() // [DiffLine, DiffLine, DiffLine, DiffLine, DiffLine, ...]// Returns a JSONDiff of the file (assuming the file is JSON)await diff.jsonDiff() // JSONDiff { ... }// Returns a JSONPatch of the file (assuming the file is JSON)await diff.jsonPatch() // JSONPatch { ... }// Get stats on the diff (number of changed/added/removed/etc lines)await diff.stats() // { changed: 5, added: 3, removed: 2, before: 2, after: 3 }
// Has the file been created?file.created // true/false// Has the file been deleted?file.deleted // true/false// Has the file been modified? (This doesn't include created files)file.modifiedOnly // true/false// Has the file been modified or created?file.modifiedOrCreated // true/false// Has the file been touched (created, modified, or deleted)?file.touched // true/false// Has the file been moved from another location?await file.moved() // true/false// Get the state of the file before all the changes made.file.before() // File | null// Get information about the diff of the filefile.diff() // Diff
// Get all of the created files.files.created // [File, File, ...]// Get all of the deleted files.files.deleted // [File, File, ...]// Get all of the modified (not including created) files.files.modifiedOnly // [File, File, ...]// Get all of the modified and created files.files.modifiedOrCreated // [File, File, ...]// Get all of the touched (created, modified, or deleted) files.files.touched // [File, File, ...]// Get all of the untouched files.files.untouched // [File, File, ...]// Get all files regardless of if they have been touched or not.files.all // [File, File, ...]// Get a specific file. (throws if it doesn't exist)files.get("path/to/file.ext") // File// Filter files by a set of glob patternsfiles.matches("path/to/**",