Skip to content

Commit

Permalink
Merged by Peril
Browse files Browse the repository at this point in the history
Custom runtime naming in danger js sub-processes
  • Loading branch information
peril-staging[bot] committed Dec 16, 2018
2 parents f648c8a + c83dc56 commit 258cde3
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 37 deletions.
23 changes: 22 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,30 @@

<!-- Your comment below this -->

- Allow sub-processes to pass their own name an href so that it doesn't say made by Danger JS [@orta][]

This is done by extending the `DangerResults` object passed back to Danger JS, by adding a meta section to the JSON:

```json
{
"markdowns":[],
"fails:" [],
"warnings:[],
"messages":[],
"meta": {
"runtimeHref": "https://mysite.com",
"runtimeName": "My Danger Runner"
}
}
```

`"meta"` is optional, and will fall back to the DangerJS one.

- Removed a dependency (voca) now that we're using TypeScript and have access to .includes [@orta][]

# 6.1.12

- Fix issue with detecting Babel if `babel-core` is installed [@sajjadzamani][]
- Fix issue with detecting Babel if `babel-core` is installed - [@sajjadzamani][]

# 6.2.0

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@
"require-from-string": "^2.0.2",
"rfc6902": "^3.0.1",
"supports-hyperlinks": "^1.0.1",
"vm2": "^3.6.3",
"voca": "^1.4.0"
"vm2": "^3.6.3"
},
"optionalDependencies": {},
"husky": {
Expand Down
14 changes: 14 additions & 0 deletions source/danger-outgoing-process-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
},
"type": "array"
},
"meta": {
"description": "Meta information about the runtime evaluation",
"properties": {
"runtimeHref": {
"description": "e.g. \"https://danger.systems/js\"",
"type": "string"
},
"runtimeName": {
"description": "E.g. \"dangerJS\", or \"Danger Swift\"",
"type": "string"
}
},
"type": "object"
},
"warnings": {
"description": "Messages for info",
"items": {
Expand Down
12 changes: 9 additions & 3 deletions source/danger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ interface BitBucketServerChangesValueMove {
}
}

type BitBucketServerChangesValue =
| BitBucketServerChangesValueAddCopyModifyDelete
| BitBucketServerChangesValueMove
type BitBucketServerChangesValue = BitBucketServerChangesValueAddCopyModifyDelete | BitBucketServerChangesValueMove

/** A platform agnostic reference to a Git commit */
interface GitCommit {
Expand Down Expand Up @@ -506,6 +504,14 @@ interface DangerResults {
* Markdown messages to attach at the bottom of the comment
*/
markdowns: Violation[]

/** Meta information about the runtime evaluation */
meta?: {
/** E.g. "dangerJS", or "Danger Swift" */
runtimeName: string
/** e.g. "https://danger.systems/js" */
runtimeHref: string
}
}

interface DangerRuntimeContainer extends DangerResults {
Expand Down
8 changes: 8 additions & 0 deletions source/dsl/DangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export interface DangerResults {
* Markdown messages to attach at the bottom of the comment
*/
markdowns: Violation[]

/** Meta information about the runtime evaluation */
meta?: {
/** E.g. "dangerJS", or "Danger Swift" */
runtimeName: string
/** e.g. "https://danger.systems/js" */
runtimeHref: string
}
}

export interface DangerRuntimeContainer extends DangerResults {
Expand Down
7 changes: 3 additions & 4 deletions source/platforms/bitbucket_server/BitBucketServerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { debug } from "../../debug"
import * as node_fetch from "node-fetch"
import { Agent } from "http"
import HttpsProxyAgent from "https-proxy-agent"
import v from "voca"

import {
BitBucketServerPRDSL,
Expand All @@ -18,7 +17,7 @@ import {
import { Comment } from "../platform"

import { Env } from "../../ci_source/ci_source"
import { dangerSignature, dangerIDToString } from "../../runner/templates/bitbucketServerTemplate"
import { 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 Expand Up @@ -207,9 +206,9 @@ export class BitBucketServerAPI {
const comments = activities.map(activity => activity.comment).filter(Boolean) as BitBucketServerPRComment[]

return comments
.filter(comment => v.includes(comment!.text, dangerIDMessage))
.filter(comment => comment!.text.includes(dangerIDMessage))
.filter(comment => username || comment!.author.name === username)
.filter(comment => v.includes(comment!.text, dangerSignature))
.filter(comment => comment!.text.includes("Generated by"))
}

getDangerInlineComments = async (dangerID: string): Promise<Comment[]> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BitBucketServerAPI } from "../BitBucketServerAPI"
import { dangerSignaturePostfix } from "../../../runner/templates/bitbucketServerTemplate"
import { DangerResults } from "../../../dsl/DangerResults"

describe("API testing - BitBucket Server", () => {
let api: BitBucketServerAPI
Expand Down Expand Up @@ -169,7 +170,7 @@ describe("API testing - BitBucket Server", () => {
values: [
{
comment: {
text: `FAIL! danger-id-1; ${dangerSignaturePostfix(commitID)}`,
text: `FAIL! danger-id-1; ${dangerSignaturePostfix({} as DangerResults, commitID)}`,
author: {
name: "username",
},
Expand Down Expand Up @@ -197,7 +198,7 @@ describe("API testing - BitBucket Server", () => {
)
expect(result).toEqual([
{
text: `FAIL! danger-id-1; ${dangerSignaturePostfix(commitID)}`,
text: `FAIL! danger-id-1; ${dangerSignaturePostfix({} as DangerResults, commitID)}`,
author: {
name: "username",
},
Expand Down
7 changes: 3 additions & 4 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import GitHubNodeAPI from "@octokit/rest"
import { debug } from "../../debug"
import * as node_fetch from "node-fetch"
import parse from "parse-link-header"
import v from "voca"
import pLimit from "p-limit"

import { GitHubPRDSL, GitHubUser } from "../../dsl/GitHubDSL"

import { dangerSignature, dangerIDToString } from "../../runner/templates/githubIssueTemplate"
import { dangerIDToString } from "../../runner/templates/githubIssueTemplate"
import { api as fetch } from "../../api/fetch"
import { Comment } from "../platform"
import { RepoMetaData } from "../../dsl/BitBucketServerDSL"
Expand Down Expand Up @@ -91,9 +90,9 @@ export class GitHubAPI {
this.d(`User ID: ${userID}`)
this.d(`Looking at ${allComments.length} comments for ${dangerIDMessage}`)
return allComments
.filter(comment => v.includes(comment.body, dangerIDMessage)) // does it contain the right danger ID?
.filter(comment => comment.body.includes(dangerIDMessage)) // does it contain the right danger ID?
.filter(comment => comment.user.id === userID) // Does it have the right user ID?
.filter(comment => v.includes(comment.body, dangerSignature)) // Does it look like a danger message?
.filter(comment => comment.body.include("Generated by")) // Does it look like a danger message?
.map(comment => comment.id) // only return IDs
}

Expand Down
11 changes: 11 additions & 0 deletions source/runner/_tests/fixtures/ExampleDangerResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,14 @@ export const asyncResults: DangerResults = {
messages: [],
markdowns: [],
}

export const resultsWithCustomMeta: DangerResults = {
messages: [],
warnings: [{ message: "Test message", file: "File.swift", line: 10 }],
fails: [],
markdowns: [],
meta: {
runtimeName: "DangerMajick",
runtimeHref: "https://danger.magic",
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ markdown
| |
|---:|
| _Generated by ❌ [dangerJS](http://github.com/danger/danger-js/) against e70f3d6468f61a4bef68c9e6eaba9166b096e23c_ |
| _Generated by ❌ [dangerJS](https://danger.systems/js) against e70f3d6468f61a4bef68c9e6eaba9166b096e23c_ |
[](http://danger-id-blankID;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ exports[`generating inline messages Shows correct messages for inline/regular vi
<p align=\\"right\\">
Generated by :no_entry_sign: <a href=\\"http://github.com/danger/danger-js/\\">dangerJS</a> against e70f3d6468f61a4bef68c9e6eaba9166b096e23c
Generated by :no_entry_sign: <a href=\\"https://danger.systems/js\\">dangerJS</a> against e70f3d6468f61a4bef68c9e6eaba9166b096e23c
</p>
"
`;
Expand Down Expand Up @@ -92,7 +92,7 @@ exports[`generating messages avoids adding space inside the <td> for proper vert
<p align=\\"right\\">
Generated by :no_entry_sign: <a href=\\"http://github.com/danger/danger-js/\\">dangerJS</a> against e70f3d6468f61a4bef68c9e6eaba9166b096e23c
Generated by :no_entry_sign: <a href=\\"https://danger.systems/js\\">dangerJS</a> against e70f3d6468f61a4bef68c9e6eaba9166b096e23c
</p>
"
`;
Expand Down Expand Up @@ -171,7 +171,7 @@ List of things:
* three
<p align=\\"right\\">
Generated by :no_entry_sign: <a href=\\"http://github.com/danger/danger-js/\\">dangerJS</a> against e70f3d6468f61a4bef68c9e6eaba9166b096e23c
Generated by :no_entry_sign: <a href=\\"https://danger.systems/js\\">dangerJS</a> against e70f3d6468f61a4bef68c9e6eaba9166b096e23c
</p>
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
markdownResults,
} from "../../_tests/fixtures/ExampleDangerResults"
import { dangerSignaturePostfix, template, inlineTemplate } from "../bitbucketServerTemplate"
import { DangerResults } from "../../../dsl/DangerResults"

const noEntryEmoji = "\u274C"
const warningEmoji = "⚠️"
Expand Down Expand Up @@ -47,7 +48,7 @@ describe("generating messages for BitBucket server", () => {
})

it("shows a postfix message indicating the current commit ID at the time of comment", () => {
expect(template("blankID", commitID, emptyResults)).toContain(dangerSignaturePostfix(commitID))
expect(template("blankID", commitID, emptyResults)).toContain(dangerSignaturePostfix({} as DangerResults, commitID))
})
})

Expand Down
9 changes: 8 additions & 1 deletion source/runner/templates/_tests/_githubIssueTemplates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
markdownResults,
inlineRegularResults,
inlineRegularResultsForTheSameLine,
resultsWithCustomMeta,
} from "../../_tests/fixtures/ExampleDangerResults"
import {
dangerSignaturePostfix,
Expand Down Expand Up @@ -87,7 +88,13 @@ describe("generating messages", () => {

it("shows a postfix message indicating the current commit ID at the time of comment", () => {
const issues = githubResultsTemplate("example-id", commitID, emptyResults)
expect(issues).toContain(dangerSignaturePostfix(commitID))
expect(issues).toContain(dangerSignaturePostfix({} as any, commitID))
})

it("handles custom names/hrefs for a platform from results", () => {
const issues = githubResultsTemplate("example-id", commitID, resultsWithCustomMeta)
expect(issues).toContain(resultsWithCustomMeta.meta!.runtimeHref)
expect(issues).toContain(resultsWithCustomMeta.meta!.runtimeName)
})
})

Expand Down
11 changes: 7 additions & 4 deletions source/runner/templates/bitbucketServerTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ export const dangerIDToString = (id: string) => `danger-id-${id};`
export const fileLineToString = (file: string, line: number) => ` File: ${file};
Line: ${line};`

export const dangerSignature = `Generated by ${noEntryEmoji} [dangerJS](http://github.com/danger/danger-js/)`
export const dangerSignature = (results: DangerResults) => {
let meta = results.meta || { runtimeName: "dangerJS", runtimeHref: "https://danger.systems/js" }
return `Generated by ${noEntryEmoji} [${meta.runtimeName}](${meta.runtimeHref})`
}

/**
* Postfix signature to be attached comment generated / updated by danger.
*/
export const dangerSignaturePostfix = (commitID: string) => `
export const dangerSignaturePostfix = (results: DangerResults, commitID: string) => `
| |
|---:|
| _${dangerSignature} against ${commitID}_ |
| _${dangerSignature(results)} against ${commitID}_ |
`
/**
* Comment to add when updating the PR status when issues are found
Expand All @@ -72,7 +75,7 @@ ${resultsSection("Messages", messageEmoji, results.messages)}
${results.markdowns.map(v => v.message).join("\n\n")}
${dangerSignaturePostfix(commitID)}
${dangerSignaturePostfix(results, commitID)}
[](http://${dangerIDToString(dangerID)})
`
Expand Down
26 changes: 20 additions & 6 deletions source/runner/templates/githubIssueTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import v from "voca"

import { DangerResults } from "../../dsl/DangerResults"
import { Violation, isInline } from "../../dsl/Violation"

Expand Down Expand Up @@ -52,9 +50,20 @@ function noViolationsOrAllOfThemEmpty(violations: Violation[]) {
return violations.length === 0 || violations.every(violation => !violation.message)
}

const truncate = (msg: string, count: number) => {
if (!msg) {
return msg
}
if (msg.length < count) {
return msg
} else {
return msg.substr(0, count - 3) + "..."
}
}

function getSummary(label: string, violations: Violation[]): string {
return violations
.map(x => v.truncate(x.message, 20))
.map(x => truncate(x.message, 20))
.reduce(
(acc, value, idx) => `${acc} ${value}${idx === violations.length - 1 ? "" : ","}`,
`${violations.length} ${label}: `
Expand All @@ -75,12 +84,17 @@ export const dangerIDToString = (id: string) => `DangerID: danger-id-${id};`
export const fileLineToString = (file: string, line: number) => ` File: ${file};
Line: ${line};`

export const dangerSignature = `Generated by :no_entry_sign: <a href="http://github.com/danger/danger-js/">dangerJS</a>`
export const dangerSignature = (results: DangerResults) => {
let meta = results.meta || { runtimeName: "dangerJS", runtimeHref: "https://danger.systems/js" }

return `Generated by :no_entry_sign: <a href="${meta.runtimeHref}">${meta.runtimeName}</a>`
}

/**
* Postfix signature to be attached comment generated / updated by danger.
*/
export const dangerSignaturePostfix = (commitID: string) => `${dangerSignature} against ${commitID}`
export const dangerSignaturePostfix = (results: DangerResults, commitID: string) =>
`${dangerSignature(results)} against ${commitID}`

/**
* Comment to add when updating the PR status when issues are found
Expand All @@ -104,7 +118,7 @@ ${table("Warnings", "warning", results.warnings)}
${table("Messages", "book", results.messages)}
${results.markdowns.map(v => v.message).join("\n\n")}
<p align="right">
${dangerSignaturePostfix(commitID)}
${dangerSignaturePostfix(results, commitID)}
</p>
`
}
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9555,11 +9555,6 @@ vm2@^3.6.3:
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.6.3.tgz#6dd426bb67a387d03055c5d276720f3f23203b72"
integrity sha512-9sGC9T+R/afjDSVyG15ATUPzm5ZHzvIJvwkVmQ+4H2Cy55uDp0dXneXV4gXC7RMd2crWcL/awfdHjCsNSm+ufg==

voca@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/voca/-/voca-1.4.0.tgz#e15ac58b38290b72acc0c330366b6cc7984924d7"
integrity sha512-8Xz4H3vhYRGbFupLtl6dHwMx0ojUcjt0HYkqZ9oBCfipd/5mD7Md58m2/dq7uPuZU/0T3Gb1m66KS9jn+I+14Q==

walkdir@^0.0.12:
version "0.0.12"
resolved "https://registry.yarnpkg.com/walkdir/-/walkdir-0.0.12.tgz#2f24f1ade64aab1e458591d4442c8868356e9281"
Expand Down

0 comments on commit 258cde3

Please sign in to comment.