Skip to content

Commit

Permalink
Platform Implementation for Bitbucket Inline changes
Browse files Browse the repository at this point in the history
  • Loading branch information
codestergit committed Apr 17, 2018
1 parent 46e2a97 commit 5724fb8
Showing 1 changed file with 86 additions and 7 deletions.
93 changes: 86 additions & 7 deletions source/platforms/BitBucketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { BitBucketServerAPI } from "./bitbucket_server/BitBucketServerAPI"
import gitDSLForBitBucketServer from "./bitbucket_server/BitBucketServerGit"
import { Platform, Comment } from "./platform"

import * as debug from "debug"

/** Handles conforming to the Platform Interface for BitBucketServer, API work is handle by BitBucketServerAPI */

export class BitBucketServer implements Platform {
private readonly d = debug("danger:BitBucketServer")
name: string

constructor(public readonly api: BitBucketServerAPI) {
Expand All @@ -30,7 +33,7 @@ export class BitBucketServer implements Platform {
/**
* Gets inline comments for current PR
*/
getInlineComments = async (_: string): Promise<Comment[]> => new Promise<Comment[]>((_resolve, reject) => reject())
getInlineComments = async (dangerID: string): Promise<Comment[]> => this.api.getDangerInlineComments(dangerID)

/**
* Fails the current build, if status setting succeeds
Expand Down Expand Up @@ -91,7 +94,7 @@ export class BitBucketServer implements Platform {
}

supportsInlineComments() {
return false
return true
}

/**
Expand All @@ -108,25 +111,101 @@ export class BitBucketServer implements Platform {
*
* @returns {Promise<any>} JSON response of new comment
*/
createInlineComment = (_git: GitDSL, _comment: string, _path: string, _line: number): Promise<any> =>
new Promise((_resolve, reject) => reject())
createInlineComment = (git: GitDSL, comment: string, path: string, line: number): Promise<any> => {
if (!this.supportsInlineComments) {
return new Promise((_resolve, reject) => reject())
}
return this.findTypeOfLine(git, line, path).then(type => {
return this.api.postInlinePRComment(comment, line, type, path)
})
}

/**
* Finds type of line in given diff. This is needed for Bitbucket Server API
*
* @returns {Promise<string>} A string with type of line
*/
findTypeOfLine = (git: GitDSL, line: number, path: string): Promise<string> => {
console.log("\n\n\n inline ---> Finding position for inline comment." + path + "#" + line)
return git.structuredDiffForFile(path).then(diff => {
return new Promise<string>((resolve, reject) => {
if (diff === undefined) {
this.d("Diff not found for inline comment." + path + "#" + line + ". Diff: " + JSON.stringify(diff))
reject()
}
this.d(
"Diff found for inline comment, now getting a position." +
path +
"#" +
line +
". Diff: " +
JSON.stringify(diff)
)
let change
for (let chunk of diff!.chunks) {
// Search for a change (that is not a deletion) and with given line. We want to look only for destination lines of a change
change = chunk.changes.find((c: any) => c.type != "del" && c.destinationLine == line)
break
}
this.d("Type found for inline comment: " + JSON.stringify(change) + "." + path + "#" + line)
resolve(change.type)
})
})
}

/**
* Updates an inline comment if possible. If platform can't update an inline comment,
* it returns a promise rejection. (e.g. platform doesn't support inline comments or line was out of diff).
*
* @returns {Promise<any>} JSON response of new comment
*/
updateInlineComment = (_comment: string, _commentId: string): Promise<any> =>
new Promise((_resolve, reject) => reject())
updateInlineComment = async (comment: string, commentId: string): Promise<any> => {
if (!this.supportsInlineComments) {
return new Promise((_resolve, reject) => reject())
}
const activities = await this.api.getPullRequestComments()
const updateComment = activities
.filter(activity => activity.commentAnchor)
.map(activity => activity.comment)
.filter(Boolean)
.find(comment => comment!.id.toString() == commentId)

this.d(
"\n\n\n inline ---> Updating inline comment. CommentId: " +
JSON.stringify(commentId) +
"updateComment: " +
JSON.stringify(updateComment)
)

return this.api.updateComment(updateComment!, comment)
}

/**
* Deletes an inline comment, used when you have
* fixed all your failures.
*
* @returns {Promise<boolean>} did it work?
*/
deleteInlineComment = async (_id: string): Promise<boolean> => new Promise<boolean>((_resolve, reject) => reject())
deleteInlineComment = async (id: string): Promise<any> => {
if (!this.supportsInlineComments) {
return new Promise<boolean>((_resolve, reject) => reject())
}
const activities = await this.api.getPullRequestComments()
const deleteComment = activities
.filter(activity => activity.commentAnchor)
.map(activity => activity.comment)
.filter(Boolean)
.find(comment => comment!.id.toString() == id)

this.d(
"\n\n\n inline ---> deleting inline comment. CommentId: " +
JSON.stringify(id) +
"deletecomment: " +
JSON.stringify(deleteComment)
)

return this.api.deleteComment(deleteComment!)
}

/**
* Deletes the main Danger comment, used when you have
Expand Down

0 comments on commit 5724fb8

Please sign in to comment.