-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
GitDSL.ts
195 lines (173 loc) · 6.07 KB
/
GitDSL.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
// Please don't have includes in here that aren't inside the DSL folder, or the d.ts/flow defs break
import { GitCommit } from "./Commit"
import { Chainsmoker } from "../commands/utils/chainsmoker"
import { File } from "parse-diff"
/** All Text diff values will be this shape */
export interface TextDiff {
/** The value before the PR's applied changes */
before: string
/** The value after the PR's applied changes */
after: string
/** A string containing the full set of changes */
diff: string
/** A string containing just the added lines */
added: string
/** A string containing just the removed lines */
removed: string
}
/** Git diff sliced into chunks */
export interface StructuredDiff {
/** Git diff chunks */
chunks: File["chunks"]
/** The file path pre-change */
fromPath: string | undefined
}
/** The results of running a JSON patch */
export interface JSONPatch {
/** The JSON in a file at the PR merge base */
before: any
/** The JSON in a file from the PR submitter */
after: any
/** The set of operations to go from one JSON to another JSON */
diff: JSONPatchOperation[]
}
/** An individual operation inside an rfc6902 JSON Patch */
export interface JSONPatchOperation {
/** An operation type */
op: string
/** The JSON keypath which the operation applies on */
path: string
/** The changes for applied */
value: string
}
/** All JSON diff values will be this shape */
export interface JSONDiffValue {
/** The value before the PR's applied changes */
before: any
/** The value after the PR's applied changes */
after: any
/** If both before & after are arrays, then you optionally get what is added. Empty if no additional objects. */
added?: any[]
/** If both before & after are arrays, then you optionally get what is removed. Empty if no removed objects. */
removed?: any[]
}
/** A map of string keys to JSONDiffValue */
export interface JSONDiff {
[name: string]: JSONDiffValue
}
// This is `danger.git`
/**
*
* The Git Related Metadata which is available inside the Danger DSL JSON
*
* @namespace JSONDSL
*/
export interface GitJSONDSL {
/**
* Filepaths with changes relative to the git root
*/
readonly modified_files: string[]
/**
* Newly created filepaths relative to the git root
*/
readonly created_files: string[]
/**
* Removed filepaths relative to the git root
*/
readonly deleted_files: string[]
/** The Git commit metadata */
readonly commits: GitCommit[]
}
/** The shape of the Chainsmoker response */
type GitMatchResult = {
/** Did any file paths match from the git modified list? */
modified: any
/** Did any file paths match from the git created list? */
created: any
/** Did any file paths match from the combination of the git modified and created list? */
edited: any
/** Did any file paths match from the git deleted list? */
deleted: any
}
/** The git specific metadata for a PR */
export interface GitDSL extends GitJSONDSL {
/**
* The git commit Danger is comparing from.
*/
base: string
/**
* The git commit Danger is comparing to.
*/
head: string
/**
* A Chainsmoker object to help match paths as an elegant DSL. It
* lets you write a globbed string and then get booleans on whether
* there are matches within a certain part of the git DSL.
*
* Use this to create an object which has booleans set on 4 keys
* `modified`, `created`, `edited` (created + modified) and `deleted`.
*
* @example
* const packageJSON = danger.git.fileMatch("package.json")
* const lockfile = danger.git.fileMatch("yarn.lock")
*
* if (packageJSON.modified && !lockfile.modified) {
* warn("You might have forgotten to run `yarn`.")
* }
*
* @example
* const needsSchemaChange = danger.git.fileMatch("src/app/analytics/*.ts")
* const schema = danger.git.fileMatch("src/app/analytics/schema.ts")
*
* if (needsSchemaChange.edited && !schema.modified) {
* fail("Changes to the analytics files need to edit update the schema.")
* }
*/
fileMatch: Chainsmoker<GitMatchResult>
/**
* Offers the diff for a specific file
*
* @param {string} filename the path to the json file
*/
diffForFile(filename: string): Promise<TextDiff | null>
/**
* Offers the structured diff for a specific file
*
* @param {string} filename the path to the json file
*/
structuredDiffForFile(filename: string): Promise<StructuredDiff | null>
/**
* Provides a JSON patch (rfc6902) between the two versions of a JSON file,
* returns null if you don't have any changes for the file in the diff.
*
* Note that if you are looking to just see changes like: before, after, added or removed - you
* should use `JSONDiffForFile` instead, as this can be a bit unwieldy for a Dangerfile.
*
* @param {string} filename the path to the json file
*/
JSONPatchForFile(filename: string): Promise<JSONPatch | null>
/**
* Provides a simplified JSON diff between the two versions of a JSON file. This will always
* be an object whose keys represent what has changed inside a JSON file.
*
* Any changed values will be represented with the same path, but with a different object instead.
* This object will always show a `before` and `after` for the changes. If both values are arrays or
* objects the `before` and `after`, then there will also be `added` and `removed` inside the object.
*
* In the case of two objects, the `added` and `removed` will be an array of keys rather than the values.
*
* This object is represented as `JSONDiffValue` but I don't know how to make TypeScript force
* declare that kind of type structure.
*
* This should make it really easy to do work when specific keypaths have changed inside a JSON file.
*
* @param {string} filename the path to the json file
*/
JSONDiffForFile(filename: string): Promise<JSONDiff>
/**
* Offers the overall lines of code added/removed in the diff
*
* @param {string} pattern an option glob pattern to filer files that will considered for lines of code.
*/
linesOfCode(pattern?: string): Promise<number | null>
}