-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathgitJSONToGitDSL.ts
263 lines (220 loc) · 9.88 KB
/
gitJSONToGitDSL.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import * as os from "os"
import parseDiff from "parse-diff"
import includes from "lodash.includes"
import isobject from "lodash.isobject"
import keys from "lodash.keys"
import memoize from "lodash.memoize"
import * as jsonDiff from "fast-json-patch"
import jsonpointer from "jsonpointer"
import JSON5 from "json5"
import micromatch from "micromatch"
import { GitDSL, JSONPatchOperation, GitJSONDSL, StructuredDiff } from "../../dsl/GitDSL"
import chainsmoker from "../../commands/utils/chainsmoker"
/*
* As Danger JS bootstraps from JSON like all `danger process` commands
* this file takes the original JSON, and wraps it into a complete DSL with
* useful functions etc.
*/
export interface GitJSONToGitDSLConfig {
/** This is used in getFileContents to figure out your repo */
repo?: string
/** Whether to diff only files from the staging area */
staging?: boolean
/** The sha things are going into */
baseSHA: string
/** The sha which we're merging */
headSHA: string
/** A promise which will return the string content of a file at a sha */
getFileContents: (path: string, repo: string | undefined, sha: string) => Promise<string>
/** A promise which will return the diff string content for a file between shas */
getFullDiff?: (base: string, head: string, staging?: boolean) => Promise<string>
getStructuredDiffForFile?: (base: string, head: string, filename: string) => Promise<GitStructuredDiff>
}
export type GitStructuredDiff = Pick<parseDiff.File, "chunks" | "from" | "to">[]
export type Changes = parseDiff.Change[]
export const gitJSONToGitDSL = (gitJSONRep: GitJSONDSL, config: GitJSONToGitDSLConfig): GitDSL => {
const getFullDiff: ((base: string, head: string) => Promise<string>) | null = config.getStructuredDiffForFile
? null
: memoize(
(base: string, head: string) => {
return config.getFullDiff!(base, head, config.staging)
},
(base: string, head: string) => `${base}...${head}`
)
/**
* 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) => {
// We already have access to the diff, so see if the file is in there
// if it's not return an empty diff
if (!gitJSONRep.modified_files.includes(filename)) {
return null
}
// Grab the two files contents.
const baseFile = await config.getFileContents(filename, config.repo, config.baseSHA)
const headFile = await config.getFileContents(filename, config.repo, config.headSHA)
// Parse JSON. `fileContents` returns empty string for files that are
// missing in one of the refs, ie. when the file is created or deleted.
const baseJSON = baseFile === "" ? {} : JSON5.parse(baseFile)
const headJSON = headFile === "" ? {} : JSON5.parse(headFile)
// Tiny bit of hand-waving here around the types. JSONPatchOperation is
// a simpler version of all operations inside the rfc6902 d.ts. Users
// of danger wont care that much, so I'm smudging the classes slightly
// to be ones we can add to the hosted docs.
return {
before: baseFile === "" ? null : baseJSON,
after: headFile === "" ? null : headJSON,
diff: jsonDiff.compare(baseJSON, headJSON) as JSONPatchOperation[],
}
}
/**
* Takes a path, generates a JSON patch for it, then parses that into something
* that's much easier to use inside a "DSL"" like the Dangerfile.
*
* @param filename path of the file
*/
const JSONDiffForFile = async (filename: string) => {
const patchObject = await JSONPatchForFile(filename)
if (!patchObject) {
return {}
}
// 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: 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 = {
// 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) => {
if (Array.isArray(other)) {
return []
} else if (isobject(diff.after)) {
return {}
}
return null
}
const beforeValue = diff.before || emptyValueOfCounterpart(diff.after)
const afterValue = diff.after || emptyValueOfCounterpart(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(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(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))
}
jsonpointer.set(accumulator, backAStepPath, diff)
return accumulator
}, Object.create(null))
}
const linesOfCode = async (pattern?: string) => {
const isPatternMatch = (path: string) => pattern === undefined || micromatch.isMatch(path, pattern)
const [createdFilesDiffs, modifiedFilesDiffs, deletedFilesDiffs] = await Promise.all([
Promise.all(gitJSONRep.created_files.filter(isPatternMatch).map((path) => diffForFile(path))),
Promise.all(gitJSONRep.modified_files.filter(isPatternMatch).map((path) => diffForFile(path))),
Promise.all(gitJSONRep.deleted_files.filter(isPatternMatch).map((path) => diffForFile(path))),
])
let additions = createdFilesDiffs
.map((diff) => (!diff ? 0 : diff.added === "" ? 0 : diff.added.split("\n").length))
.reduce((mem, value) => mem + value, 0)
let deletions = deletedFilesDiffs
.map((diff) => (!diff ? 0 : diff.removed === "" ? 0 : diff.removed.split("\n").length))
.reduce((mem, value) => mem + value, 0)
const modifiedLines = modifiedFilesDiffs.map((diff) => [
!diff ? 0 : diff.added === "" ? 0 : diff.added.split("\n").length,
!diff ? 0 : diff.removed === "" ? 0 : diff.removed.split("\n").length,
])
additions = modifiedLines.reduce((mem, value) => mem + value[0], additions)
deletions = modifiedLines.reduce((mem, value) => mem + value[1], deletions)
return additions + deletions
}
const byType =
(t: string) =>
({ type }: { type: string }) =>
type === t
const getContent = ({ content }: { content: string }) => content
/**
* Gets the git-style diff for a single file.
*
* @param filename File path for the diff
*/
const structuredDiffForFile = async (filename: string): Promise<StructuredDiff | null> => {
let fileDiffs: GitStructuredDiff
if (config.getStructuredDiffForFile) {
fileDiffs = await config.getStructuredDiffForFile(config.baseSHA, config.headSHA, filename)
} else {
const diff = await getFullDiff!(config.baseSHA, config.headSHA)
fileDiffs = parseDiff(diff)
}
const structuredDiff = fileDiffs.find((diff) => diff.from === filename || diff.to === filename)
if (structuredDiff !== undefined && structuredDiff.chunks !== undefined) {
return {
chunks: structuredDiff.chunks,
// '/dev/null' will be in the 'from' path if the file is new.
fromPath: structuredDiff.from === "/dev/null" ? undefined : structuredDiff.from,
}
} else {
return null
}
}
/**
* Gets the git-style diff for a single file.
*
* @param filename File path for the diff
*/
const diffForFile = async (filename: string) => {
const structuredDiff = await structuredDiffForFile(filename)
if (!structuredDiff) {
return null
}
const allLines = structuredDiff.chunks
.map((c: { changes: Changes }) => c.changes)
.reduce((a: Changes, b: Changes) => a.concat(b), [])
return {
before: await config.getFileContents(filename, config.repo, config.baseSHA),
after: await config.getFileContents(filename, config.repo, config.headSHA),
diff: allLines.map(getContent).join(os.EOL),
added: allLines.filter(byType("add")).map(getContent).join(os.EOL),
removed: allLines.filter(byType("del")).map(getContent).join(os.EOL),
}
}
return {
base: config.baseSHA,
head: config.headSHA,
fileMatch: chainsmoker({
modified: gitJSONRep.modified_files,
created: gitJSONRep.created_files,
deleted: gitJSONRep.deleted_files,
edited: gitJSONRep.modified_files.concat(gitJSONRep.created_files),
}),
modified_files: gitJSONRep.modified_files,
created_files: gitJSONRep.created_files,
deleted_files: gitJSONRep.deleted_files,
commits: gitJSONRep.commits,
diffForFile,
structuredDiffForFile,
JSONPatchForFile,
JSONDiffForFile,
linesOfCode,
}
}