danger-js icon indicating copy to clipboard operation
danger-js copied to clipboard

Feature: provider-agnostic API to fetch file contents of a different ref

Open edno opened this issue 4 years ago • 6 comments

Describe the bug danger.github is undefined.

Cannot read property 'pr' of undefined
TypeError: Cannot read property 'pr' of undefined
    at dangerfile.js:5:62

To Reproduce

  • git clone https://github.com/edno/danger-github-undefined.git
  • cd danger-github-undefined
  • yarn install (Yarn 2)
  • git checkout new-branch
  • yarn danger local -b main

Expected behavior This should display

message(`:tada: The PR added ${additions} and removed ${deletions} lines.`)

Environment

software version
danger.js ^10.6.2
node v12.18.3
yarn 2.4.0
Operating System MacOS 11.2.1

edno avatar Feb 13 '21 19:02 edno

Those two DSL attributes rely on the GitHub API (on a GitHub repo), I'd be open to PRs which de-couple that on danger local 👍🏻

orta avatar Feb 14 '21 07:02 orta

Those two DSL attributes rely on the GitHub API (on a GitHub repo), I'd be open to PRs which de-couple that on danger local 👍🏻

Fair point! But in that case, how do you fetch a file content by ref while using local?

edno avatar Feb 14 '21 07:02 edno

I don't think there's an API for that today, I have generally only looked at the files which changed and not necessarily the contents - I think there could be a danger.local API which feels like github which wraps calls to git to provide those sorts of functions?

orta avatar Feb 14 '21 07:02 orta

git cat-file would be a way to fetch file content locally.

edno avatar Feb 14 '21 08:02 edno

Yep, good thinking, I'd accept a PR adding an API something like:

import {danger} from "danger"

 // could maybe be always "HEAD", but could also be a branch reference, hard to say what it should be with --staging though
danger.local.head
 
// master, or whatever someone changed it to
danger.local.base 

// New to .git - when in `local` use wrapped git call, otherwise use the available APIs 
// Defaults to the head version
danger.git.getFileContents("file.md")

// So, you can get the other SHA refs
danger.git.getFileContents("file.md", danger.local.base)

orta avatar Feb 14 '21 11:02 orta

Sure. I will look at it this week.

edno avatar Feb 14 '21 19:02 edno