Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: [git] JSONDiffForFile when passed the path of a JSON File that was moved, crashes #1197

Merged
merged 2 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ source/danger-outgoing-process-schema.json
source/danger-incoming-process-schema.json
source/platforms/_tests/fixtures/bbs-dsl-input.json
source/platforms/_tests/fixtures/bbc-dsl-input.json
CHANGELOG.md
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<!-- Your comment below this -->

- Fix: [Github] Multiple Inline Comments on the same file/line should all be posted [#1176](https://github.com/danger/danger-js/pull/1176) [@Rouby]
- 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]

<!-- Your comment above this -->

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"@types/http-proxy-agent": "^2.0.1",
"@types/jest": "^24.0.11",
"@types/json5": "^0.0.30",
"@types/jsonpointer": "^4.0.0",
"@types/jsonwebtoken": "^7.2.8",
"@types/lodash.includes": "^4.3.4",
"@types/lodash.mapvalues": "^4.6.6",
Expand Down
1 change: 0 additions & 1 deletion source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ declare module "jest-runtime"
declare module "jest-haste-map"
declare module "jest-environment-node"
declare module "jest-config"
declare module "jsonpointer"
declare module "parse-link-header"
declare module "pinpoint"
declare module "prettyjson"
Expand Down
13 changes: 9 additions & 4 deletions source/platforms/git/gitJSONToGitDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
* Takes a filename, and pulls from the PR the two versions of a file
* where we then pass that off to the rfc6902 JSON patch generator.
*
* If you provide the current filename:
* - If a file was renamed then you'll see { before: null }
*
* @param filename The path of the file
*/
const JSONPatchForFile = async (filename: string) => {
Expand Down Expand Up @@ -110,16 +113,18 @@ export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLC
// Thanks to @wtgtybhertgeghgtwtg for getting this started in #175
// The idea is to loop through all the JSON patches, then pull out the before and after from those changes.

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

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

const emptyValueOfCounterpart = (other: any) => {
Expand Down
Loading