Skip to content

Commit

Permalink
feat(template): provides message summary on top of comment
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jan 17, 2017
1 parent 69e2bbc commit af56421
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// Add your own contribution below

* Add summary comment for danger message - kwonoj

### 0.9.0

* Adds support for `git.commits` and `github.commits` - orta
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"lodash.find": "^4.6.0",
"lodash.includes": "^4.3.0",
"node-fetch": "^1.6.3",
"parse-diff": "^0.4.0"
"parse-diff": "^0.4.0",
"voca": "^1.2.0"
}
}
1 change: 1 addition & 0 deletions source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ declare module "lodash.includes";
declare module "lodash.find";
declare module "jest-runtime";
declare module "jest-environment-node";
declare module "voca";

declare module "*/package.json";
7 changes: 7 additions & 0 deletions source/runner/_tests/fixtures/ExampleDangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ export const failsResults: DangerResults = {
messages: [],
markdowns: []
}

export const summaryResults: DangerResults = {
fails: [{ message: "Failing message Failing message" }],
warnings: [{ message: "Warning message Warning message" }],
messages: [{ message: "message" }],
markdowns: ["markdown"],
}
16 changes: 15 additions & 1 deletion source/runner/templates/_tests/github-issue-templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { emptyResults, warnResults, failsResults } from "../../_tests/fixtures/ExampleDangerResults"
import { emptyResults, warnResults, failsResults, summaryResults } from "../../_tests/fixtures/ExampleDangerResults"
import { template as githubResultsTemplate } from "../../templates/github-issue-template"

describe("generating messages", () => {
Expand All @@ -25,4 +25,18 @@ describe("generating messages", () => {
const issues = githubResultsTemplate(warnResults)
expect(issues).not.toMatch(/(\r?\n){2}[ \t]+</)
})

it("Should include summary on top of message", () => {
const issues = githubResultsTemplate(summaryResults)
const expected =
`
<!--
1 failure: Failing message F...
1 warning: Warning message W...
1 messages
1 markdown notices
-->`

expect(issues).toContain(expected)
})
})
19 changes: 19 additions & 0 deletions source/runner/templates/github-issue-template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DangerResults } from "../../dsl/DangerResults"
import { Violation } from "../../platforms/messaging/violation"
import * as v from "voca"

/**
* Converts a set of violations into a HTML table
Expand Down Expand Up @@ -28,13 +29,31 @@ function table(name: string, emoji: string, violations: Array<Violation>): strin
`
}

function getSummary(label: string, violations: Array<Violation>): string {
return violations.map(x => v.truncate(x.message, 20))
.reduce((acc, value, idx) => `${acc} ${value}${idx === violations.length - 1 ? "" : ","}`, `${violations.length} ${label}: `)
}

function buildSummaryMessage(results: DangerResults): string {
const {fails, warnings, messages, markdowns } = results
const summary =
` ${getSummary("failure", fails)}
${getSummary("warning", warnings)}
${messages.length > 0 ? `${messages.length} messages` : ""}
${markdowns.length > 0 ? `${markdowns.length} markdown notices` : ""}`
return summary
}

/**
* A template function for creating a GitHub issue comment from Danger Results
* @param {DangerResults} results Data to work with
* @returns {string} HTML
*/
export function template(results: DangerResults): string {
return `
<!--
${buildSummaryMessage(results)}
-->
${table("Fails", "no_entry_sign", results.fails)}
${table("Warnings", "warning", results.warnings)}
${table("Messages", "book", results.messages)}
Expand Down

0 comments on commit af56421

Please sign in to comment.