-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
GitLab.ts
295 lines (255 loc) · 10 KB
/
GitLab.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import GitLabAPI from "./gitlab/GitLabAPI"
import { Comment, Platform } from "./platform"
import { GitDSL, GitJSONDSL } from "../dsl/GitDSL"
import { GitCommit } from "../dsl/Commit"
import { GitLabDiscussion, GitLabDSL, GitLabJSONDSL, GitLabNote } from "../dsl/GitLabDSL"
import { debug } from "../debug"
import { dangerIDToString } from "../runner/templates/githubIssueTemplate"
const d = debug("GitLab")
/**
* Determines whether Danger should use threads for the "main" Danger comment.
*/
const useThreads = () =>
process.env.DANGER_GITLAB_USE_THREADS === "1" || process.env.DANGER_GITLAB_USE_THREADS === "true"
class GitLab implements Platform {
public readonly name: string
constructor(public readonly api: GitLabAPI) {
this.name = "GitLab"
}
getReviewInfo = async (): Promise<any> => {
return this.api.getMergeRequestInfo()
}
// returns the `danger.gitlab` object
getPlatformReviewDSLRepresentation = async (): Promise<GitLabJSONDSL> => {
const mr = await this.getReviewInfo()
const commits = await this.api.getMergeRequestCommits()
const approvals = await this.api.getMergeRequestApprovals()
return {
metadata: this.api.repoMetadata,
mr,
commits,
approvals,
}
}
getPlatformGitRepresentation = async (): Promise<GitJSONDSL> => {
const changes = await this.api.getMergeRequestChanges()
const commits = await this.api.getMergeRequestCommits()
const mappedCommits: GitCommit[] = commits.map((commit) => {
return {
sha: commit.id,
author: {
name: commit.author_name,
email: commit.author_email,
date: commit.authored_date,
},
committer: {
name: commit.committer_name,
email: commit.committer_email,
date: commit.committed_date,
},
message: commit.message,
parents: commit.parent_ids,
url: `${this.api.projectURL}/commit/${commit.id}`,
tree: null,
}
})
// XXX: does "renamed_file"/move count is "delete/create", or "modified"?
const modified_files: string[] = changes
.filter((change) => !change.new_file && !change.deleted_file)
.map((change) => change.new_path)
const created_files: string[] = changes.filter((change) => change.new_file).map((change) => change.new_path)
const deleted_files: string[] = changes.filter((change) => change.deleted_file).map((change) => change.new_path)
return {
modified_files,
created_files,
deleted_files,
commits: mappedCommits,
}
}
getInlineComments = async (dangerID: string): Promise<Comment[]> => {
const dangerUserID = (await this.api.getUser()).id
return (await this.api.getMergeRequestInlineNotes()).map((note) => {
return {
id: `${note.id}`,
body: note.body,
ownedByDanger: note.author.id === dangerUserID && note.body.includes(dangerID),
}
})
}
supportsCommenting() {
return true
}
supportsInlineComments() {
return true
}
updateOrCreateComment = async (dangerID: string, newComment: string): Promise<string> => {
d("updateOrCreateComment", { dangerID, newComment })
if (useThreads()) {
const discussions = await this.getDangerDiscussions(dangerID)
const firstDiscussion = discussions.shift()
const existingNote = firstDiscussion?.notes[0]
// Delete all notes from all other danger discussions (discussions cannot be deleted as a whole):
await this.deleteNotes(this.reduceNotesFromDiscussions(discussions)) //delete the rest
let newOrUpdatedNote: GitLabNote
if (existingNote) {
// update the existing comment
newOrUpdatedNote = await this.api.updateMergeRequestNote(existingNote.id, newComment)
} else {
// create a new comment
newOrUpdatedNote = await this.createComment(newComment)
}
// create URL from note
// "https://gitlab.com/group/project/merge_requests/154#note_132143425"
return `${this.api.mergeRequestURL}#note_${newOrUpdatedNote.id}`
} else {
const notes: GitLabNote[] = await this.getMainDangerNotes(dangerID)
let note: GitLabNote
if (notes.length) {
// update the first
note = await this.api.updateMergeRequestNote(notes[0].id, newComment)
// delete the rest
for (let deleteme of notes) {
if (deleteme === notes[0]) {
continue
}
await this.api.deleteMergeRequestNote(deleteme.id)
}
} else {
// create a new note
note = await this.api.createMergeRequestNote(newComment)
}
// create URL from note
// "https://gitlab.com/group/project/merge_requests/154#note_132143425"
return `${this.api.mergeRequestURL}#note_${note.id}`
}
}
createComment = async (comment: string): Promise<GitLabNote> => {
d("createComment", { comment })
if (useThreads()) {
return (await this.api.createMergeRequestDiscussion(comment)).notes[0]
}
return this.api.createMergeRequestNote(comment)
}
createInlineComment = async (git: GitDSL, comment: string, path: string, line: number): Promise<GitLabDiscussion> => {
d("createInlineComment", { git, comment, path, line })
const mr = await this.api.getMergeRequestInfo()
return this.api.createMergeRequestDiscussion(comment, {
position: {
position_type: "text",
base_sha: mr.diff_refs.base_sha,
start_sha: mr.diff_refs.start_sha,
head_sha: mr.diff_refs.head_sha,
old_path: path,
old_line: null,
new_path: path,
new_line: line,
},
})
}
updateInlineComment = async (comment: string, id: string): Promise<GitLabNote> => {
d("updateInlineComment", { comment, id })
const nid = parseInt(id) // fingers crossed
return await this.api.updateMergeRequestNote(nid, comment)
}
deleteInlineComment = async (id: string): Promise<boolean> => {
d("deleteInlineComment", { id })
const nid = parseInt(id) // fingers crossed
return await this.api.deleteMergeRequestNote(nid)
}
/**
* Attempts to delete the "main" Danger comment. If the "main" Danger
* comment has any comments on it then that comment will not be deleted.
*/
deleteMainComment = async (dangerID: string): Promise<boolean> => {
if (useThreads()) {
// There is no API to delete entire discussion. They can only be deleted fully by deleting every note:
const discussions = await this.getDangerDiscussions(dangerID)
return await this.deleteNotes(this.reduceNotesFromDiscussions(discussions))
} else {
const notes = await this.getMainDangerNotes(dangerID)
for (let note of notes) {
d("deleteMainComment", { id: note.id })
await this.api.deleteMergeRequestNote(note.id)
}
return notes.length > 0
}
}
deleteNotes = async (notes: GitLabNote[]): Promise<boolean> => {
for (const note of notes) {
d("deleteNotes", { id: note.id })
await this.api.deleteMergeRequestNote(note.id)
}
return notes.length > 0
}
/**
* Only fetches the discussions where danger owns the top note
*/
getDangerDiscussions = async (dangerID: string): Promise<GitLabDiscussion[]> => {
const noteFilter = await this.getDangerDiscussionNoteFilter(dangerID)
const discussions: GitLabDiscussion[] = await this.api.getMergeRequestDiscussions()
return discussions.filter(({ notes }) => notes.length && noteFilter(notes[0]))
}
reduceNotesFromDiscussions = (discussions: GitLabDiscussion[]): GitLabNote[] => {
return discussions.reduce<GitLabNote[]>((acc, { notes }) => [...acc, ...notes], [])
}
/**
* Attempts to find the "main" Danger note and should return at most
* one item. If the "main" Danger note has any comments on it then that
* note will not be returned.
*/
getMainDangerNotes = async (dangerID: string): Promise<GitLabNote[]> => {
const noteFilter = await this.getDangerMainNoteFilter(dangerID)
const notes: GitLabNote[] = await this.api.getMergeRequestNotes()
return notes.filter(noteFilter)
}
/**
* Filters a note to determine if it was created by Danger.
*/
getDangerDiscussionNoteFilter = async (dangerID: string): Promise<(note: GitLabNote) => boolean> => {
const { id: dangerUserId } = await this.api.getUser()
return ({ author: { id }, body, system }: GitLabNote): boolean => {
return (
!system && // system notes are generated when the user interacts with the UI e.g. changing a PR title
id === dangerUserId &&
body.includes(dangerIDToString(dangerID))
)
}
}
/**
* Filters a note to the "main" Danger note. If that note has any
* comments on it then it will not be found.
*/
getDangerMainNoteFilter = async (dangerID: string): Promise<(note: GitLabNote) => boolean> => {
const { id: dangerUserId } = await this.api.getUser()
return ({ author: { id }, body, system, type }: GitLabNote): boolean => {
return (
!system && // system notes are generated when the user interacts with the UI e.g. changing a PR title
id === dangerUserId &&
// This check for the type being `null` seems to be the best option
// we have to determine whether this note is the "main" Danger note.
// This assumption does not hold if there are any comments on the
// "main" Danger note and in that case a new "main" Danger note will
// be created instead of updating the existing note. This behavior is better
// than the current alternative which is the bug described here:
// https://github.com/danger/danger-js/issues/1351
type == null &&
body.includes(dangerIDToString(dangerID))
)
}
}
updateStatus = async (): Promise<boolean> => {
d("updateStatus", {})
return true
}
getFileContents = this.api.getFileContents
}
export default GitLab
export const gitlabJSONToGitLabDSL = (gl: GitLabDSL, api: GitLabAPI): GitLabDSL => ({
...gl,
utils: {
fileContents: api.getFileContents,
addLabels: api.addLabels,
removeLabels: api.removeLabels,
},
api: api.apiInstance,
})