Skip to content

Commit

Permalink
Refactor chunks into StructuredDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinejr committed Mar 18, 2018
1 parent 1c38bb1 commit d2b2315
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
17 changes: 13 additions & 4 deletions source/danger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,6 @@ declare module "danger" {
}
/** All Text diff values will be this shape */
interface TextDiff {
/** Git diff chunks */
// Not sure if this is the right place for this one, but few methods needs both chunks & text diffs and I didn't want to parse two times the same file
// We could rename the interface, but couldn't find a good one
chunks: any[]
/** The value before the PR's applied changes */
before: string
/** The value after the PR's applied changes */
Expand All @@ -716,6 +712,12 @@ declare module "danger" {
removed: string
}

/** Git diff sliced into chunks */
interface StructuredDiff {
/** Git diff chunks */
chunks: any[]
}

/** The results of running a JSON patch */
interface JSONPatch {
/** The JSON in a file at the PR merge base */
Expand Down Expand Up @@ -791,6 +793,13 @@ declare module "danger" {
*/
diffForFile(filename: string): Promise<TextDiff | null>

/**
* Offers the structured diff for a specific file
*
* @param {string} filename the path to the json file
*/
structuredDiffForFile(filename: string): Promise<StructuredDiff | null>

/**
* Provides a JSON patch (rfc6902) between the two versions of a JSON file,
* returns null if you don't have any changes for the file in the diff.
Expand Down
17 changes: 13 additions & 4 deletions source/dsl/GitDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { GitCommit } from "./Commit"

/** All Text diff values will be this shape */
export interface TextDiff {
/** Git diff chunks */
// Not sure if this is the right place for this one, but few methods needs both chunks & text diffs and I didn't want to parse two times the same file
// We could rename the interface, but couldn't find a good one
chunks: any[]
/** The value before the PR's applied changes */
before: string
/** The value after the PR's applied changes */
Expand All @@ -18,6 +14,12 @@ export interface TextDiff {
removed: string
}

/** Git diff sliced into chunks */
export interface StructuredDiff {
/** Git diff chunks */
chunks: any[]
}

/** The results of running a JSON patch */
export interface JSONPatch {
/** The JSON in a file at the PR merge base */
Expand Down Expand Up @@ -96,6 +98,13 @@ export interface GitDSL extends GitJSONDSL {
*/
diffForFile(filename: string): Promise<TextDiff | null>

/**
* Offers the structured diff for a specific file
*
* @param {string} filename the path to the json file
*/
structuredDiffForFile(filename: string): Promise<StructuredDiff | null>

/**
* Provides a JSON patch (rfc6902) between the two versions of a JSON file,
* returns null if you don't have any changes for the file in the diff.
Expand Down
3 changes: 2 additions & 1 deletion source/platforms/FakePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export class FakePlatform implements Platform {
modified_files: [],
created_files: [],
deleted_files: [],
diffForFile: async () => ({ chunks: [], before: "", after: "", diff: "", added: "", removed: "" }),
diffForFile: async () => ({ before: "", after: "", diff: "", added: "", removed: "" }),
structuredDiffForFile: async () => ({ chunks: [] }),
JSONDiffForFile: async () => ({} as any),
JSONPatchForFile: async () => ({} as any),
commits: [
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class GitHub implements Platform {
* @returns {Promise<number>} A number with given position
*/
findPositionForInlineComment = (git: GitDSL, line: number, path: string): Promise<number> => {
return git.diffForFile(path).then(diff => {
return git.structuredDiffForFile(path).then(diff => {
return new Promise<number>((resolve, reject) => {
if (diff === undefined) {
reject()
Expand Down
20 changes: 17 additions & 3 deletions source/platforms/git/gitJSONToGitDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as keys from "lodash.keys"
import * as jsonDiff from "rfc6902"
import * as jsonpointer from "jsonpointer"

import { GitDSL, JSONPatchOperation, GitJSONDSL } from "../../dsl/GitDSL"
import { GitDSL, JSONPatchOperation, GitJSONDSL, StructuredDiff } from "../../dsl/GitDSL"

/*
* As Danger JS bootstraps from JSON like all `danger process` commands
Expand Down Expand Up @@ -151,7 +151,7 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
*
* @param filename File path for the diff
*/
const diffForFile = async (filename: string) => {
const structuredDiffForFile = async (filename: string): Promise<StructuredDiff | null> => {
let fileDiffs: GitStructuredDiff

if (config.getFullStructuredDiff) {
Expand All @@ -161,6 +161,20 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
fileDiffs = parseDiff(diff)
}
const structuredDiff = fileDiffs.find(diff => diff.from === filename || diff.to === filename)
if (structuredDiff !== undefined) {
return { chunks: structuredDiff.chunks }
} else {
return null
}
}

/**
* Gets the git-style diff for a single file.
*
* @param filename File path for the diff
*/
const diffForFile = async (filename: string) => {
const structuredDiff = await structuredDiffForFile(filename)

if (!structuredDiff) {
return null
Expand All @@ -171,7 +185,6 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
.reduce((a: Changes, b: Changes) => a.concat(b), [])

return {
chunks: structuredDiff.chunks,
before: await config.getFileContents(filename, config.repo, config.baseSHA),
after: await config.getFileContents(filename, config.repo, config.headSHA),

Expand All @@ -195,6 +208,7 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
deleted_files: gitJSONRep.deleted_files,
commits: gitJSONRep.commits,
diffForFile,
structuredDiffForFile,
JSONPatchForFile,
JSONDiffForFile,
}
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/github/_tests/_github_git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe("the dangerfile gitDSL", async () => {
})

it("show diff chunks for a specific file", async () => {
const { chunks } = await gitDSL.diffForFile("tsconfig.json")
const { chunks } = await gitDSL.structuredDiffForFile("tsconfig.json")

expect(chunks).toMatchSnapshot()
})
Expand Down

0 comments on commit d2b2315

Please sign in to comment.