From bf068577a738c80ccf33d4b62e6237414d7158bc Mon Sep 17 00:00:00 2001 From: Benny Wong Date: Thu, 7 Sep 2017 22:55:28 -0400 Subject: [PATCH] GitDSL: Include created and removed files for `JSONDiffForFile` * Related to: danger/danger-js#368 --- changelog.md | 2 +- source/platforms/github/GitHubGit.ts | 30 ++++++++---- .../github/_tests/_github_git.test.ts | 48 +++++++++++++++++++ 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/changelog.md b/changelog.md index b87c73683..5352ed708 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,7 @@ ### Master -- Updates `diffForFile` and `JSONPatchForFile` to include created and removed files - #368 - bdotdub +- Updates `diffForFile`, `JSONPatchForFile`, and `JSONDiffForFile` to include created and removed files - #368 - bdotdub ### 2.0.0-alpha.14 diff --git a/source/platforms/github/GitHubGit.ts b/source/platforms/github/GitHubGit.ts index fdf4bf274..420a823ec 100644 --- a/source/platforms/github/GitHubGit.ts +++ b/source/platforms/github/GitHubGit.ts @@ -106,25 +106,37 @@ export default async function gitDSLForGitHub(api: GitHubAPI): Promise { const backAStepPath = pathSteps.length <= 2 ? path : pathSteps.slice(0, pathSteps.length - 1).join("/") const diff: any = { - after: jsonpointer.get(after, backAStepPath), - before: jsonpointer.get(before, backAStepPath), + after: jsonpointer.get(after, backAStepPath) || null, + before: jsonpointer.get(before, backAStepPath) || null, } + const counterpart = (other) => { + if (Array.isArray(other)) { + return [] + } else if (isobject(diff.after)) { + return {} + } + + return null + } + + const beforeValue = diff.before || counterpart(diff.after) + const afterValue = diff.after || counterpart(diff.before) + // If they both are arrays, add some extra metadata about what was // added or removed. This makes it really easy to act on specific // changes to JSON DSLs - if (Array.isArray(diff.after) && Array.isArray(diff.before)) { - const arrayBefore = diff.before as any[] - const arrayAfter = diff.after as any[] + if (Array.isArray(afterValue) && Array.isArray(beforeValue)) { + const arrayBefore = beforeValue as any[] + const arrayAfter = afterValue as any[] diff.added = arrayAfter.filter(o => !includes(arrayBefore, o)) diff.removed = arrayBefore.filter(o => !includes(arrayAfter, o)) - // Do the same, but for keys inside an object if they both are objects. - } else if (isobject(diff.after) && isobject(diff.before)) { - const beforeKeys = keys(diff.before) as string[] - const afterKeys = keys(diff.after) as string[] + } else if (isobject(afterValue) && isobject(beforeValue)) { + const beforeKeys = keys(beforeValue) as string[] + const afterKeys = keys(afterValue) as string[] diff.added = afterKeys.filter(o => !includes(beforeKeys, o)) diff.removed = beforeKeys.filter(o => !includes(afterKeys, o)) } diff --git a/source/platforms/github/_tests/_github_git.test.ts b/source/platforms/github/_tests/_github_git.test.ts index e9e1e3961..21ad4a734 100644 --- a/source/platforms/github/_tests/_github_git.test.ts +++ b/source/platforms/github/_tests/_github_git.test.ts @@ -235,6 +235,54 @@ describe("the dangerfile gitDSL", async () => { expect(empty).toEqual({}) }) + it("handles showing a patch for a created file", async () => { + github.api.fileContents = async (path, repo, ref) => { + const after = { + a: "o, world", + b: 3, + c: ["one", "two", "three", "four"], + d: ["one", "two"], + e: ["five", "one", "three"], + } + + const obj = ref === masterSHA ? {} : after + return JSON.stringify(obj) + } + const gitDSL = await github.getPlatformGitRepresentation() + const empty = await gitDSL.JSONDiffForFile("data/schema.json") + expect(empty).toEqual({ + a: { after: "o, world", before: null }, + b: { after: 3, before: null }, + c: { added: ["one", "two", "three", "four"], after: ["one", "two", "three", "four"], before: null, removed: [] }, + d: { added: ["one", "two"], after: ["one", "two"], before: null, removed: [] }, + e: { added: ["five", "one", "three"], after: ["five", "one", "three"], before: null, removed: [] }, + }) + }) + + it("handles showing a patch for a deleted file", async () => { + github.api.fileContents = async (path, repo, ref) => { + const before = { + a: "o, world", + b: 3, + c: ["one", "two", "three", "four"], + d: ["one", "two"], + e: ["five", "one", "three"], + } + + const obj = ref === masterSHA ? before : {} + return JSON.stringify(obj) + } + const gitDSL = await github.getPlatformGitRepresentation() + const empty = await gitDSL.JSONDiffForFile("data/schema.json") + expect(empty).toEqual({ + a: { after: null, before: "o, world" }, + b: { after: null, before: 3 }, + c: { added: [], after: null, before: ["one", "two", "three", "four"], removed: ["one", "two", "three", "four"] }, + d: { added: [], after: null, before: ["one", "two",], removed: ["one", "two"] }, + e: { added: [], after: null, before: ["five", "one", "three"], removed: ["five", "one", "three"] }, + }) + }) + it("handles showing a patch for two different diff files", async () => { github.api.fileContents = async (path, repo, ref) => { const before = {