Skip to content

Commit

Permalink
Merge pull request #1451 from szweier/fix-file-status
Browse files Browse the repository at this point in the history
Bug Fix: Properly group files into `created`, `modified`, `deleted`.
  • Loading branch information
orta authored Jun 4, 2024
2 parents bb0f4c2 + 7b7d74b commit d5af2ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
10 changes: 6 additions & 4 deletions source/platforms/git/diffToGitJSONDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { GitJSONDSL } from "../../dsl/GitDSL"
*/

export const diffToGitJSONDSL = (diff: string, commits: GitCommit[]): GitJSONDSL => {
const fileDiffs: any[] = parseDiff(diff)
const fileDiffs: parseDiff.File[] = parseDiff(diff)

const addedDiffs = fileDiffs.filter((diff: any) => diff["new"])
const removedDiffs = fileDiffs.filter((diff: any) => diff["deleted"])
const modifiedDiffs = fileDiffs.filter((diff: any) => !includes(addedDiffs, diff) && !includes(removedDiffs, diff))
const addedDiffs = fileDiffs.filter((diff: parseDiff.File) => diff.new == true) as any[]
const removedDiffs = fileDiffs.filter((diff: parseDiff.File) => diff.deleted == true) as any[]
const modifiedDiffs = fileDiffs.filter(
(diff: any) => !includes(addedDiffs, diff) && !includes(removedDiffs, diff)
) as any[]

return {
// Work around for danger/danger-js#807
Expand Down
10 changes: 10 additions & 0 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const limit = pLimit(25)
export interface GitHubFile {
filename: string
patch: string
status: string
}

// Note that there are parts of this class which don't seem to be
Expand Down Expand Up @@ -373,8 +374,17 @@ export class GitHubAPI {
// This is a hack to get the file patch into a format that parse-diff accepts
// as the GitHub API for listing pull request files is missing file names in the patch.
function prefixedPatch(file: GitHubFile): string {
let fileMode = ""
if (file.status == "added") {
fileMode = "new file mode"
} else if (file.status == "removed") {
fileMode = "deleted file mode"
} else if (file.status == "modified") {
fileMode = "modified file mode"
}
return `
diff --git a/${file.filename} b/${file.filename}
${fileMode} 0
--- a/${file.filename}
+++ b/${file.filename}
${file.patch}
Expand Down
3 changes: 2 additions & 1 deletion source/platforms/github/_tests/_github_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ describe("API testing", () => {
json: jest
.fn()
.mockImplementation(() =>
Promise.resolve<GitHubFile[]>(JSON.parse('[{"filename": "file.txt", "patch": "+ hello"}]'))
Promise.resolve<GitHubFile[]>(JSON.parse('[{"filename": "file.txt", "status": "added", "patch": "+ hello"}]'))
),
})

let diff = await api.getPullRequestDiff()

let expected = `
diff --git a/file.txt b/file.txt
new file mode 0
--- a/file.txt
+++ b/file.txt
+ hello
Expand Down

0 comments on commit d5af2ef

Please sign in to comment.