Skip to content

Commit

Permalink
Merge pull request #215 from alex3165/improvement/git-diff
Browse files Browse the repository at this point in the history
Add filter per type to gitdiff
  • Loading branch information
orta committed Apr 12, 2017
2 parents 9baa8a5 + d7688f0 commit 53ad560
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/danger.d.ts
Expand Up @@ -165,7 +165,7 @@ export interface GitDSL {
readonly deleted_files: Array<string>

/** Offers the diff for a specific file */
diffForFile(filename: string): string | null,
diffForFile(filename: string, diffTypes?: string[]): string | null,

/**
* Provides a JSON patch (rfc6902) between the two versions of a JSON file,
Expand Down
2 changes: 1 addition & 1 deletion source/dsl/GitDSL.ts
Expand Up @@ -60,7 +60,7 @@ export interface GitDSL {
readonly deleted_files: Array<string>

/** Offers the diff for a specific file */
diffForFile(filename: string): string | null,
diffForFile(filename: string, diffTypes?: string[]): string | null,

/**
* Provides a JSON patch (rfc6902) between the two versions of a JSON file,
Expand Down
12 changes: 9 additions & 3 deletions source/platforms/github/GitHubGit.ts
Expand Up @@ -139,12 +139,18 @@ export default async function gitDSLForGitHub(api: GitHubAPI): Promise<GitDSL> {
*
* @param name File path for the diff
*/
const diffForFile = (name: string) => {
const diffForFile = (name: string, diffTypes?: string[]) => {
const diff = find(fileDiffs, (diff: any) => diff.from === name || diff.to === name)
if (!diff) { return null }

const changes = diff.chunks.map((c: any) => c.changes)
.reduce((a: any, b: any) => a.concat(b), [])
let changes = diff.chunks.map((c: any) => c.changes)
.reduce((a: any, b: any) => a.concat(b), [])

// Filter the diffs by type
if (diffTypes && diffTypes.length > 0) {
changes = changes.filter((c: any) => diffTypes.indexOf(c.type) !== -1)
}

const lines = changes.map((c: any) => c.content)
return lines.join(os.EOL)
}
Expand Down
7 changes: 7 additions & 0 deletions source/platforms/github/_tests/_github_git.test.ts
Expand Up @@ -48,6 +48,13 @@ describe("the dangerfile gitDSL", async () => {
expect(gitDSL.diffForFile("CHANGELOG.md")).toEqual(expected)
})

it("should show only diff of specified type", async () => {
const expected = "+- GeneVC now shows about information, and trending artists - orta"
const gitDSL = await github.getPlatformGitRepresentation()

expect(gitDSL.diffForFile("CHANGELOG.md", ["add"])).toEqual(expected)
})

it("sets up commit data correctly", async () => {
const exampleCommit: GitCommit = {
"author": {
Expand Down

0 comments on commit 53ad560

Please sign in to comment.