forked from danger/danger-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbucketServerTemplate.ts
90 lines (81 loc) · 2.78 KB
/
bitbucketServerTemplate.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
import { DangerResults } from "../../dsl/DangerResults"
import { Violation } from "../../dsl/Violation"
// This unicode emojis also work for old versions of bitbucket server, were emojis are not supported
const noEntryEmoji = "\u274C"
const warningEmoji = "⚠️"
const messageEmoji = "\u2728"
/**
* Converts a set of violations into a Markdown section
*
* @param {string} name User facing title of section
* @param {string} emoji Emoji name to show
* @param {Violation[]} violations for section
* @returns {string} Markdown
*/
function resultsSection(name: string, emoji: string, violations: Violation[]): string {
if (violations.length === 0 || violations.every(violation => !violation.message)) {
return ""
}
return (
`\n` +
`| ${emoji} | ${name}\n` +
`| --- | --- |\n` +
`\n` +
violations
.map(v => {
return (
"> " +
v.message
.replace(/<\/?code>/g, "`")
.split("\n")
.join("\n> ")
)
})
.join("\n\n") +
`\n`
)
}
export const dangerIDToString = (id: string) => `danger-id-${id};`
export const fileLineToString = (file: string, line: number) => ` File: ${file};
Line: ${line};`
/**
* Postfix signature to be attached comment generated / updated by danger.
*/
export const dangerSignaturePostfix = `
| |
|---:|
| _Generated by ${noEntryEmoji} [dangerJS](http://github.com/danger/danger-js/)_ |
`
/**
* Comment to add when updating the PR status when issues are found
*/
export const messageForResultWithIssues = `${warningEmoji} Danger found some issues. Don't worry, everything is fixable.`
/**
* 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 `
${resultsSection("Fails", noEntryEmoji, results.fails)}
${resultsSection("Warnings", warningEmoji, results.warnings)}
${resultsSection("Messages", messageEmoji, results.messages)}
${results.markdowns.map(v => v.message).join("\n\n")}
${dangerSignaturePostfix}
[](http://${dangerIDToString(dangerID)})
`
}
export function inlineTemplate(dangerID: string, results: DangerResults, file: string, line: number): string {
const printViolation = (emoji: string) => (violation: Violation) => {
return `- ${emoji} ${violation.message}`
}
return `
[//]: # (${dangerIDToString(dangerID)})
[//]: # (${fileLineToString(file, line)})
${results.fails.map(printViolation(noEntryEmoji)).join("\n")}
${results.warnings.map(printViolation(warningEmoji)).join("\n")}
${results.messages.map(printViolation(messageEmoji)).join("\n")}
${results.markdowns.map(v => v.message).join("\n\n")}
`
}