-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
BitBucketCloud.ts
191 lines (162 loc) · 5.37 KB
/
BitBucketCloud.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
import { GitJSONDSL, GitDSL } from "../dsl/GitDSL"
import { BitBucketCloudPRDSL, BitBucketCloudJSONDSL, BitBucketCloudDSL } from "../dsl/BitBucketCloudDSL"
import { BitBucketCloudAPI } from "./bitbucket_cloud/BitBucketCloudAPI"
import gitDSLForBitBucketCloud from "./bitbucket_cloud/BitBucketCloudGit"
import { Platform, Comment } from "./platform"
import { debug } from "../debug"
export class BitBucketCloud implements Platform {
private readonly d = debug("BitBucketCloud")
name: string
constructor(public readonly api: BitBucketCloudAPI) {
this.name = "BitBucketCloud"
}
/**
* Get the Code Review description metadata
*
* @returns {Promise<any>} JSON representation
*/
getReviewInfo = (): Promise<BitBucketCloudPRDSL> => this.api.getPullRequestInfo()
/**
* Get the Code Review diff representation
*
* @returns {Promise<GitDSL>} the git DSL
*/
getPlatformGitRepresentation = (): Promise<GitJSONDSL> => gitDSLForBitBucketCloud(this.api)
/**
* Returns the `bitbucket_cloud` object on the Danger DSL
*
* @returns {Promise<BitBucketCloudJSONDSL>} JSON response of the DSL
*/
getPlatformReviewDSLRepresentation = async (): Promise<BitBucketCloudJSONDSL> => {
let pr: BitBucketCloudPRDSL
try {
pr = await this.getReviewInfo()
} catch {
process.exitCode = 1
throw `
Could not find pull request information,
perhaps Danger does not have permission to access the repo.
`
}
const commits = await this.api.getPullRequestCommits()
const comments = await this.api.getPullRequestComments()
const activities = await this.api.getPullRequestActivities()
return {
metadata: this.api.repoMetadata,
pr,
commits,
comments,
activities,
}
}
supportsCommenting() {
return true
}
supportsInlineComments() {
return true
}
/**
* Returns the response for the new comment
*
* @param {string} comment you want to post
* @returns {Promise<any>} JSON response of new comment
*/
createComment = (comment: string) => this.api.postPRComment(comment)
/**
* Deletes the main Danger comment, used when you have
* fixed all your failures.
*
* @returns {Promise<boolean>} did it work?
*/
deleteMainComment = async (dangerID: string): Promise<boolean> => {
const comments = await this.api.getDangerMainComments(dangerID)
for (let comment of comments) {
await this.api.deleteComment(comment.id.toString())
}
return comments.length > 0
}
/**
* Either updates an existing comment, or makes a new one
*
* @param {string} dangerID the UUID for the run
* @param {string} newComment string value of comment
* @returns {Promise<string>} the URL for the comment
*/
async updateOrCreateComment(dangerID: string, newComment: string): Promise<string | undefined> {
const comments = await this.api.getDangerMainComments(dangerID)
let issue = null
if (comments.length) {
// Edit the first comment
issue = await this.api.updateComment(comments[0].id.toString(), newComment)
// Delete any dupes
for (let comment of comments) {
if (comment !== comments[0]) {
await this.api.deleteComment(comment.id.toString())
}
}
} else {
issue = await this.createComment(newComment)
}
if (issue && issue.links && issue.links.html && issue.links.html.href) {
return issue.links.html.href
}
return undefined
}
getInlineComments = async (dangerID: string): Promise<Comment[]> => this.api.getDangerInlineComments(dangerID)
createInlineComment = async (git: GitDSL, comment: string, path: string, line: number): Promise<any> => {
this.d("Trying to get inline comment:" + comment + " atPath: " + path + " line: " + line)
this.d("GIT: " + git)
return this.api.postInlinePRComment(comment, line, path)
}
updateInlineComment = async (comment: string, commentId: string): Promise<any> => {
this.d("Trying to update inline comment:" + commentId + " message:" + comment)
return this.api.updateComment(commentId, comment)
}
deleteInlineComment = async (commentId: string): Promise<boolean> => {
this.d("Trying to delete inline comment: " + commentId)
await this.api.deleteComment(commentId)
return true
}
updateStatus = async (
passed: boolean | "pending",
message: string,
url?: string,
dangerID?: string,
ciCommitHash?: string
): Promise<boolean> => {
const pr = await this.api.getPullRequestInfo()
const commitId = ciCommitHash || pr.source.commit.hash
let state: "SUCCESSFUL" | "INPROGRESS" | "FAILED" = passed ? "SUCCESSFUL" : "FAILED"
if (passed === "pending") {
state = "INPROGRESS" as const
}
let name = "Danger"
let key = "danger.systems"
if (process.env["PERIL_INTEGRATION_ID"]) {
name = "Peril"
} else if (dangerID) {
name = dangerID
key = dangerID
}
try {
await this.api.postBuildStatus(commitId, {
state,
key: key,
name: name,
url: url || "http://danger.systems/js",
description: message,
})
return true
} catch (error) {
return false
}
}
getFileContents = this.api.getFileContents
}
export const bitbucketCloudJSONToBitBucketCloudDSL = (
bitbucket: BitBucketCloudJSONDSL,
api: BitBucketCloudAPI
): BitBucketCloudDSL => ({
...bitbucket,
api,
})