Skip to content

Commit

Permalink
Add template
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Feb 25, 2018
1 parent 2a8f9c5 commit 310b865
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
3 changes: 0 additions & 3 deletions source/platforms/BitBucketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ export class BitBucketServer implements Platform {
*/
createComment = (comment: string) => this.api.postPRComment(comment)

// In Danger RB we support a danger_id property,
// this should be handled at some point

/**
* Deletes the main Danger comment, used when you have
* fixed all your failures.
Expand Down
3 changes: 0 additions & 3 deletions source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ export class GitHub implements Platform {
*/
createComment = (comment: string) => this.api.postPRComment(comment)

// In Danger RB we support a danger_id property,
// this should be handled at some point

/**
* Deletes the main Danger comment, used when you have
* fixed all your failures.
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/bitbucket_server/BitBucketServerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "../../dsl/BitBucketServerDSL"

import { RepoMetaData } from "../../ci_source/ci_source"
import { dangerSignaturePostfix, dangerIDToString } from "../../runner/templates/githubIssueTemplate"
import { dangerSignaturePostfix, dangerIDToString } from "../../runner/templates/bitbucketServerTemplate"
import { api as fetch } from "../../api/fetch"

// Note that there are parts of this class which don't seem to be
Expand Down
5 changes: 4 additions & 1 deletion source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CISource } from "../ci_source/ci_source"
import { Platform } from "../platforms/platform"
import { DangerResults } from "../dsl/DangerResults"
import { template as githubResultsTemplate } from "./templates/githubIssueTemplate"
import { template as bitbucketServerTemplate } from "./templates/bitbucketServerTemplate"
import exceptionRaisedTemplate from "./templates/exceptionRaisedTemplate"

import * as debug from "debug"
Expand Down Expand Up @@ -195,7 +196,9 @@ export class Executor {
} else if (messageCount > 0) {
console.log("Found only messages, passing those to review.")
}
const comment = githubResultsTemplate(dangerID, results)
const comment = process.env["DANGER_BITBUCKETSERVER_HOST"]
? bitbucketServerTemplate(dangerID, results)
: githubResultsTemplate(dangerID, results)
await this.platform.updateOrCreateComment(dangerID, comment)
}

Expand Down
50 changes: 50 additions & 0 deletions source/runner/templates/bitbucketServerTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { DangerResults } from "../../dsl/DangerResults"
import { Violation } from "../../dsl/Violation"

/**
* Converts a set of violations into a Markdown table
*
* @param {string} name User facing title of table
* @param {string} emoji Emoji name to show next to each item
* @param {Violation[]} violations for table
* @returns {string} HTML
*/
function table(name: string, emoji: string, violations: Violation[]): string {
if (violations.length === 0 || violations.every(violation => !violation.message)) {
return ""
}
return [
`| | ${name}|`,
`|---|---|`,
...violations.map(v => {
return `| ${emoji} | ${v.message.replace(/\r?\n/g, " ").replace(/<\/?code>/g, "`")} |`
}),
].join("\n")
}

export const dangerIDToString = (id: string) => `danger-id-${id};`

/**
* Postfix signature to be attached comment generated / updated by danger.
*/
export const dangerSignaturePostfix = `_Generated by 🚫 [dangerJS](http://github.com/danger/danger-js/)_`

/**
* A template function for creating a GitHub issue comment from Danger Results
* @param {string} dangerID A string that represents a unique build
* @param {DangerResults} results Data to work with
* @returns {string} HTML
*/
export function template(dangerID: string, results: DangerResults): string {
return `
${table("Fails", "🚫", results.fails)}
${table("Warnings", "⚠", results.warnings)}
${table("Messages", "📖", results.messages)}
${results.markdowns.join("\n\n")}
---
${dangerSignaturePostfix}
[](http://${dangerIDToString(dangerID)})
`
}

0 comments on commit 310b865

Please sign in to comment.