Skip to content

Commit

Permalink
Adds changelog + some light docs
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Mar 26, 2018
1 parent d2b2315 commit 484671d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 171 deletions.
38 changes: 34 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,40 @@

## Master

* []
## 3.3.0

## 3.2.0
* Adds support for inline comments when using GitHub.

This is one of those "massive under the hood" changes, that has a tiny user DSL surface. From this point onwards
`fail`, `warn`, `message` and `markdown` all take an extra two optional params: `file?: string` and `line?: number`.

Adding `file` and `line` to the call of any exported communication function will trigger one of two things:

* Danger will create a new comment inline inside your PR with your warning/message/fail/markdown
* Danger will append a in the main Danger comment with your warning/message/fail/markdown

=======
Inline messages are edited/created/deleted with each subsequent run of `danger ci` in the same way the main
comment does. This is really useful for: linters, test runners uotput and basically anything that relies on
the contents of a file itself.

If you're using `danger process` to communicate with an external process, you can return JSON like:

```json
{
"markdowns": [
{
"file": "package.swift",
"line": 3,
"message": "Needs more base"
}
]
// [...]
}
```

-- [@sunshinejr][]

## 3.2.0

* Add BitBucket Server support.

Expand Down Expand Up @@ -60,7 +89,7 @@

You can see more in the docs for [Danger + BitBucket Server](http://danger.systems/js/usage/bitbucket_server.html).

* [@azz][]
-- [@azz][]

* Don't check for same user ID on comment when running as a GitHub App. [@tibdex][]

Expand Down Expand Up @@ -963,4 +992,5 @@ Not usable for others, only stubs of classes etc. - [@orta][]
[@peterjgrainger]: https://github.com/peterjgrainger
[@azz]: https://github.com/azz
[@mifi]: https://github.com/ionutmiftode
[@sunshinejr]: https://github.com/sunshinejr
[ref]: http://danger.systems/js/reference.html
159 changes: 1 addition & 158 deletions source/danger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,7 @@ declare module "danger" {
}
/**
* The representation of what running a Dangerfile generates.
*
* In the future I'd like this to be cross process, so please
* do not add functions, only data to this interface.
* This needs to be passed between processes, so data only please.
*/
interface DangerResults {
/**
Expand Down Expand Up @@ -488,15 +486,12 @@ declare module "danger" {
interface DangerInlineResults {
/**
* Path to the file
*
*/
file: string

/**
* Line in the file
*
*/

line: number

/**
Expand All @@ -519,158 +514,6 @@ declare module "danger" {
*/
markdowns: string[]
}

export const emptyDangerResults = {
fails: [],
warnings: [],
messages: [],
markdowns: [],
}

export function inlineResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => isInline(m)),
warnings: results.warnings.filter(m => isInline(m)),
messages: results.messages.filter(m => isInline(m)),
markdowns: results.markdowns.filter(m => isInline(m)),
}
}

export function regularResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => !isInline(m)),
warnings: results.warnings.filter(m => !isInline(m)),
messages: results.messages.filter(m => !isInline(m)),
markdowns: results.markdowns.filter(m => !isInline(m)),
}
}

export function mergeResults(results1: DangerResults, results2: DangerResults): DangerResults {
return {
fails: results1.fails.concat(results2.fails),
warnings: results1.warnings.concat(results2.warnings),
messages: results1.messages.concat(results2.messages),
markdowns: results1.markdowns.concat(results2.markdowns),
}
}

export function sortInlineResults(inlineResults: DangerInlineResults[]): DangerInlineResults[] {
// First sort messages in every inline result
const sortedInlineResults = inlineResults.map(i => {
return {
file: i.file,
line: i.line,
fails: i.fails.sort(),
warnings: i.warnings.sort(),
messages: i.messages.sort(),
markdowns: i.markdowns.sort(),
}
})

// Then sort a whole array of inline results based on file/line
return sortedInlineResults.sort((a, b) => {
if (a.file < b.file) {
return -1
} else if (a.file > b.file) {
return 1
} else if (a.line < b.line) {
return -1
} else if (a.line > b.line) {
return 1
} else {
// both file & line are the same
return 0
}
})
}

export function sortResults(results: DangerResults): DangerResults {
const sortByFile = (a: Violation, b: Violation): number => {
if (a.file === undefined) {
return -1
}
if (b.file === undefined) {
return 1
}

if (a.file == b.file) {
if (a.line == undefined) {
return -1
}
if (b.line == undefined) {
return 1
}

if (a.line < b.line) {
return -1
} else if (a.line > b.line) {
return 1
} else {
return 0
}
}

if (a.file < b.file) {
return -1
} else {
return 1
}
}

return {
fails: results.fails.sort(sortByFile),
warnings: results.warnings.sort(sortByFile),
messages: results.messages.sort(sortByFile),
markdowns: results.markdowns.sort(sortByFile),
}
}

export function resultsIntoInlineResults(results: DangerResults): DangerInlineResults[] {
// Here we iterate through all keys ("fails", "warnings", "messages", "markdowns") and for each violation
// in given kind we produce new DangerInlineResult or append a violation to existing result. This is all
// happening in a `violationsIntoInlineResults` function that mutates an out-of-scope variable `dangerInlineResults`.

const dangerInlineResults: DangerInlineResults[] = []

const violationsIntoInlineResults = (kind: string) => {
for (let violation of results[kind]) {
if (violation.file && violation.line) {
const findInlineResult = dangerInlineResults.find(r => r.file == violation.file && r.line == violation.line)
if (findInlineResult) {
findInlineResult[kind].push(violation.message)
} else {
const inlineResult = {
file: violation.file,
line: violation.line,
fails: [],
warnings: [],
messages: [],
markdowns: [],
}
inlineResult[kind].push(violation.message)
dangerInlineResults.push(inlineResult)
}
}
}
}
Object.keys(results).forEach(violationsIntoInlineResults)

return dangerInlineResults
}

export function inlineResultsIntoResults(inlineResults: DangerInlineResults): DangerResults {
const messageToViolation = (message: string): Violation => {
return { message: message, file: inlineResults.file, line: inlineResults.line }
}

return {
fails: inlineResults.fails.map(messageToViolation),
warnings: inlineResults.warnings.map(messageToViolation),
messages: inlineResults.messages.map(messageToViolation),
markdowns: inlineResults.markdowns.map(messageToViolation),
}
}

/**
* The Danger Utils DSL contains utility functions
* that are specific to universal Danger use-cases.
Expand Down
15 changes: 7 additions & 8 deletions source/dsl/DangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { Violation, isInline } from "../dsl/Violation"

/**
* The representation of what running a Dangerfile generates.
*
* In the future I'd like this to be cross process, so please
* do not add functions, only data to this interface.
* This needs to be passed between processes, so data only please.
*/
export interface DangerResults {
/**
Expand Down Expand Up @@ -38,17 +36,12 @@ export interface DangerRuntimeContainer extends DangerResults {
export interface DangerInlineResults {
/**
* Path to the file
*
* @type {string}
*/
file: string

/**
* Line in the file
*
* @type {string}
*/

line: number

/**
Expand All @@ -72,13 +65,16 @@ export interface DangerInlineResults {
markdowns: string[]
}

/// End of Danger DSL definition

export const emptyDangerResults = {
fails: [],
warnings: [],
messages: [],
markdowns: [],
}

/** Returns only the inline violations from Danger results */
export function inlineResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => isInline(m)),
Expand All @@ -88,6 +84,7 @@ export function inlineResults(results: DangerResults): DangerResults {
}
}

/** Returns only the main-comment commentsviolations from Danger results */
export function regularResults(results: DangerResults): DangerResults {
return {
fails: results.fails.filter(m => !isInline(m)),
Expand All @@ -97,6 +94,7 @@ export function regularResults(results: DangerResults): DangerResults {
}
}

/** Concat all the violations into a new results */
export function mergeResults(results1: DangerResults, results2: DangerResults): DangerResults {
return {
fails: results1.fails.concat(results2.fails),
Expand All @@ -106,6 +104,7 @@ export function mergeResults(results1: DangerResults, results2: DangerResults):
}
}

/** Sorts all of the results according to their files and lines */
export function sortInlineResults(inlineResults: DangerInlineResults[]): DangerInlineResults[] {
// First sort messages in every inline result
const sortedInlineResults = inlineResults.map(i => {
Expand Down
2 changes: 1 addition & 1 deletion source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export class Executor {
* Handle showing results inside a code review platform
*
* @param {DangerResults} results a JSON representation of the end-state for a Danger run
* @param {GitDSL} git a reference to a git implementation so that inline comments find diffs to work with
*/
// TODO: Instead of danger, pass gitDSL
async handleResultsPostingToPlatform(results: DangerResults, git: GitDSL) {
// Delete the message if there's nothing to say
const { fails, warnings, messages, markdowns } = results
Expand Down

0 comments on commit 484671d

Please sign in to comment.