Skip to content

Commit

Permalink
Add support for looking at a specific file's diff
Browse files Browse the repository at this point in the history
  • Loading branch information
danger-systems committed Dec 12, 2016
1 parent 4e91872 commit 3877eed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

* The env vars `DANGER_TEST_REPO` and `DANGER_TEST_PR` will allow you initialize the FakeCI with a repo of your choice. See README.md for more info
* Improved error messaging around not including a `DANGER_GITHUB_API_TOKEN` in the ENV - nsfmc / orta
* Adds support for getting the diff for a specific file from git: e.g.
```js
// Politely ask for their name on the entry too
const changelogDiff = danger.git.diffForFile("changelog.md")
const contributorName = danger.github.pr.user.login
if (changelogDiff && changelogDiff.indexOf(contributorName) === -1) {
warn("Please add your GitHub name to the changelog entry, so we can attribute you.")
}
```

### 0.6.3

Expand Down
8 changes: 7 additions & 1 deletion source/platforms/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import parseDiff from "parse-diff"
import fetch from "node-fetch"
import "babel-polyfill"

import os from "os"

// This pattern of re-typing specific strings has worked well for Artsy in Swift
// so, I'm willing to give it a shot here.

Expand Down Expand Up @@ -59,7 +61,11 @@ export class GitHub {
diffForFile: (name: string) => {
const diff = fileDiffs.find((diff) => diff.from === name || diff.to === name)
if (!diff) { return null }
return diff.chunks.reduce((chunk) => chunk.content.reduce((content) => content.join("")))

const changes = diff.chunks.map((c) => { return c.changes })
.reduce((a, b) => a.concat(b), [])
const lines = changes.map((c) => { return c.content })
return lines.join(os.EOL)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion source/platforms/_tests/GitHub.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
/*eslint-disable */

import { GitHub } from "../GitHub"
import Fake from "../../ci_source/Fake"
Expand Down Expand Up @@ -80,9 +81,10 @@ describe("with fixtured data", () => {
expect(gitDSL.deleted_files).toEqual(["lib/components/artist/related_artists/index.js", "lib/components/artist/related_artists/related_artist.js", "lib/components/gene/about_gene.js"])
})

// sorry windows users - I guess this test will fail for you
it("shows the diff for a specific file", async () => {
const gitDSL:GitDSL = await github.getReviewDiff()
expect(gitDSL.diffForFile("CHANGELOG.md")).toEqual("sads")
expect(gitDSL.diffForFile("CHANGELOG.md")).toEqual(` - [dev] Updates Flow to 0.32 - orta\n - [dev] Updates React to 0.34 - orta\n - [dev] Turns on \"keychain sharing\" to fix a keychain bug in sim - orta\n+- GeneVC now shows about information, and trending artists - orta\n \n ### 1.1.0-beta.2\n `)
})
})
})

0 comments on commit 3877eed

Please sign in to comment.