'Get Current Git branch name in Danger Js script

I am writing a custom danger js check in danger js. I want to avoid running this check when the branch is a hotfix/* branch

I want to know how to get the current branch name in the javascript scope in which the dangerfile.js file runs.

I've read the danger.systems documentation but nothing is mentioned about the branch name.



Solution 1:[1]

I think this will give you the branch name

import { danger } from 'danger';
console.log('### branch: ',  danger.github.pr.head.ref)

This is from GitHubMergeRef (from node_modules/danger/distribution/danger.d.ts) which has this comment:

  /**
   * The reference point for the merge, e.g. "master"
   */
  ref: string

If you do want to find out more info, just do a console.log('### pr: ', danger.github.pr) and search for your branch name in the results. (when doing a: npx danger pr https://github.com/org-name/repo-name/pull/2282)

Solution 2:[2]

There seems to be a documentation gap, see https://github.com/danger/danger-js/issues/1078 - maybe you would like to add another comment there.

I found this archived danger-plugin-dangerfile-builder - it mentionds some shorthands:

targetBranch
Branch name where the pull request will be merged into (shorthand for danger.github.pr.base.ref).

sourceBranch
Branch name where the pull request is being send from (shorthand for danger.github.pr.head.ref).

I am not sure if this helps, maybe you can scan the code yourself.

Try danger.github.pr.base.ref and/or danger.github.pr.head.ref.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2