Skip to content

Commit a7355a3

Browse files
authored
Merge pull request #1197 from danger/fb/fix-moved-json-crash
Fix: [git] JSONDiffForFile when passed the path of a JSON File that was moved, crashes
2 parents d896baf + 3411074 commit a7355a3

File tree

6 files changed

+47
-211
lines changed

6 files changed

+47
-211
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ source/danger-outgoing-process-schema.json
22
source/danger-incoming-process-schema.json
33
source/platforms/_tests/fixtures/bbs-dsl-input.json
44
source/platforms/_tests/fixtures/bbc-dsl-input.json
5+
CHANGELOG.md

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<!-- Your comment below this -->
1717

1818
- Fix: [Github] Multiple Inline Comments on the same file/line should all be posted [#1176](https://github.com/danger/danger-js/pull/1176) [@Rouby]
19+
- Fix: [git] JSONDiffForFile when passed the path of a JSON File that was moved, crashes [#1193](https://github.com/danger/danger-js/pull/1193) [@fbartho]
1920

2021
<!-- Your comment above this -->
2122

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"@types/http-proxy-agent": "^2.0.1",
101101
"@types/jest": "^24.0.11",
102102
"@types/json5": "^0.0.30",
103+
"@types/jsonpointer": "^4.0.0",
103104
"@types/jsonwebtoken": "^7.2.8",
104105
"@types/lodash.includes": "^4.3.4",
105106
"@types/lodash.mapvalues": "^4.6.6",

source/ambient.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ declare module "jest-runtime"
77
declare module "jest-haste-map"
88
declare module "jest-environment-node"
99
declare module "jest-config"
10-
declare module "jsonpointer"
1110
declare module "parse-link-header"
1211
declare module "pinpoint"
1312
declare module "prettyjson"

source/platforms/git/gitJSONToGitDSL.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
6565
* Takes a filename, and pulls from the PR the two versions of a file
6666
* where we then pass that off to the rfc6902 JSON patch generator.
6767
*
68+
* If you provide the current filename:
69+
* - If a file was renamed then you'll see { before: null }
70+
*
6871
* @param filename The path of the file
6972
*/
7073
const JSONPatchForFile = async (filename: string) => {
@@ -110,16 +113,18 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
110113
// Thanks to @wtgtybhertgeghgtwtg for getting this started in #175
111114
// The idea is to loop through all the JSON patches, then pull out the before and after from those changes.
112115

113-
const { diff, before, after } = patchObject
114-
return diff.reduce((accumulator, { path }) => {
116+
const { diff: outerDiff, before, after } = patchObject
117+
return outerDiff.reduce((accumulator, { path }) => {
115118
// We don't want to show the last root object, as these tend to just go directly
116119
// to a single value in the patch. This is fine, but not useful when showing a before/after
117120
const pathSteps = path.split("/")
118121
const backAStepPath = pathSteps.length <= 2 ? path : pathSteps.slice(0, pathSteps.length - 1).join("/")
119122

120123
const diff: any = {
121-
after: jsonpointer.get(after, backAStepPath) || null,
122-
before: jsonpointer.get(before, backAStepPath) || null,
124+
// If a file is moved/renamed, the file will be in "danger.git.modified_files"
125+
// JSONPatchForFile will return null for the old file, so we need to check for that
126+
before: (before && jsonpointer.get(before, backAStepPath)) || null,
127+
after: (after && jsonpointer.get(after, backAStepPath)) || null,
123128
}
124129

125130
const emptyValueOfCounterpart = (other: any) => {

0 commit comments

Comments
 (0)