-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
GitLab.ts
196 lines (163 loc) · 6.15 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
import GitLabAPI from "./gitlab/GitLabAPI"
import { Platform, Comment } from "./platform"
import { GitDSL, GitJSONDSL } from "../dsl/GitDSL"
import { GitCommit } from "../dsl/Commit"
import { GitLabDSL, GitLabJSONDSL, GitLabNote } from "../dsl/GitLabDSL"
import { debug } from "../debug"
import { dangerIDToString } from "../runner/templates/githubIssueTemplate"
const d = debug("GitLab")
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 as string,
date: (commit.authored_date as Date).toString(),
},
committer: {
name: commit.committer_name as string,
email: commit.committer_email as string,
date: (commit.committed_date as Date).toString(),
},
message: commit.message,
parents: commit.parent_ids as string[],
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,
// XXX: we should re-use the logic in getDangerNotes, need to check what inline comment template we're using if any
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 })
const notes: GitLabNote[] = await this.getDangerNotes(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<any> => {
d("createComment", { comment })
return this.api.createMergeRequestNote(comment)
}
createInlineComment = async (git: GitDSL, comment: string, path: string, line: number): Promise<string> => {
d("createInlineComment", { git, comment, path, line })
const mr = await this.api.getMergeRequestInfo()
return this.api.createMergeRequestDiscussion(comment, {
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)
}
deleteMainComment = async (dangerID: string): Promise<boolean> => {
const notes = await this.getDangerNotes(dangerID)
for (let note of notes) {
d("deleteMainComment", { id: note.id })
await this.api.deleteMergeRequestNote(note.id)
}
return notes.length > 0
}
getDangerNotes = async (dangerID: string): Promise<GitLabNote[]> => {
const { id: dangerUserId } = await this.api.getUser()
const notes: GitLabNote[] = await this.api.getMergeRequestNotes()
return notes.filter(
({ author: { id }, body, system, type }: GitLabNote) =>
!system && // system notes are generated when the user interacts with the UI e.g. changing a PR title
type == null && // we only want "normal" comments on the main body of the MR;
id === dangerUserId &&
body!.includes(dangerIDToString(dangerID)) // danger-id-(dangerID) is included in a hidden comment in the githubIssueTemplate
)
}
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,
},
api: api.apiInstance,
})