Skip to content

Commit

Permalink
GitDSL: Include created and removed files for JSONDiffForFile
Browse files Browse the repository at this point in the history
* Related to: #368
  • Loading branch information
bdotdub committed Sep 8, 2017
1 parent 95bac1b commit bf06857
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 21 additions & 9 deletions source/platforms/github/GitHubGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,37 @@ export default async function gitDSLForGitHub(api: GitHubAPI): Promise<GitDSL> {
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))
}
Expand Down
48 changes: 48 additions & 0 deletions source/platforms/github/_tests/_github_git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit bf06857

Please sign in to comment.