Skip to content

Commit

Permalink
Inital work on a danger-id CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Dec 9, 2017
1 parent 686ac34 commit 635f054
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@types/lodash.includes": "^4.3.3",
"@types/node-fetch": "^1.6.6",
"@types/readline-sync": "^1.4.2",
"@types/voca": "^1.3.0",
"babel-cli": "7.0.0-alpha.19",
"babel-core": "7.0.0-alpha.19",
"babel-plugin-syntax-async-functions": "7.0.0-alpha.19",
Expand Down
1 change: 0 additions & 1 deletion source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ declare module "jest-runtime"
declare module "jest-haste-map"
declare module "jest-environment-node"
declare module "jest-config"
declare module "voca"
declare module "jsome"
declare module "jsonpointer"
declare module "parse-link-header"
Expand Down
5 changes: 3 additions & 2 deletions source/commands/danger-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as chalk from "chalk"
import * as program from "commander"

import { getPlatformForEnv } from "../platforms/platform"
import { Executor } from "../runner/Executor"
import { Executor, ExecutorOptions } from "../runner/Executor"
import runDangerSubprocess, { prepareDangerDSL } from "./utils/runDangerSubprocess"
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
import getRuntimeCISource from "./utils/getRuntimeCISource"
Expand Down Expand Up @@ -35,10 +35,11 @@ getRuntimeCISource(app).then(source => {

if (platform) {
jsonDSLGenerator(platform).then(dangerJSONDSL => {
const config = {
const config: ExecutorOptions = {
stdoutOnly: app.textOnly,
verbose: app.verbose,
jsonOnly: false,
dangerID: app.id || "default",
}

const processInput = prepareDangerDSL(dangerJSONDSL)
Expand Down
2 changes: 2 additions & 0 deletions source/commands/utils/sharedDangerfileArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SharedCLI extends program.ICommand {
externalCiProvider: string
textOnly: boolean
dangerfile: string
id: string
}

export default (command: program.ICommand) =>
Expand All @@ -19,3 +20,4 @@ export default (command: program.ICommand) =>
.option("-c, --external-ci-provider [modulePath]", "Specify custom CI provider")
.option("-t, --text-only", "Provide an STDOUT only interface, Danger will not post to your PR")
.option("-d, --dangerfile [filePath]", "Specify a custom dangerfile path")
.option("-i, --id [danger_id]", "Specify a unique Danger ID for the Danger run")
8 changes: 4 additions & 4 deletions source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export class GitHub {
*
* @returns {Promise<boolean>} did it work?
*/
async deleteMainComment(): Promise<boolean> {
const commentIDs = await this.api.getDangerCommentIDs()
async deleteMainComment(dangerID: string): Promise<boolean> {
const commentIDs = await this.api.getDangerCommentIDs(dangerID)
for (let commentID of commentIDs) {
await this.api.deleteCommentWithID(commentID)
}
Expand All @@ -120,8 +120,8 @@ export class GitHub {
* @param {string} newComment string value of comment
* @returns {Promise<boolean>} success of posting comment
*/
async updateOrCreateComment(newComment: string): Promise<boolean> {
const commentIDs = await this.api.getDangerCommentIDs()
async updateOrCreateComment(dangerID: string, newComment: string): Promise<boolean> {
const commentIDs = await this.api.getDangerCommentIDs(dangerID)

if (commentIDs.length) {
// Edit the first comment
Expand Down
7 changes: 5 additions & 2 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as v from "voca"
import { GitHubPRDSL, GitHubUser } from "../../dsl/GitHubDSL"

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

// The Handle the API specific parts of the github
Expand Down Expand Up @@ -75,10 +75,13 @@ export class GitHubAPI {

// The above is the API for Platform

async getDangerCommentIDs(): Promise<number[]> {
async getDangerCommentIDs(dangerID: string): Promise<number[]> {
const userID = await this.getUserID()
const allComments: any[] = await this.getPullRequestComments()
const dangerIDMessage = dangerIDToString(dangerID)

return allComments
.filter(comment => v.includes(comment.body, dangerIDMessage))
.filter(comment => userID || comment.user.id === userID)
.filter(comment => v.includes(comment.body, dangerSignaturePostfix))
.map(comment => comment.id)
Expand Down
6 changes: 3 additions & 3 deletions source/platforms/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export interface Platform {
/** Pulls in the Code Review Diff, and offers a succinct user-API for it */
getPlatformGitRepresentation: () => Promise<GitJSONDSL>
/** Creates a comment on the PR */
createComment: (body: string) => Promise<any>
createComment: (dangerID: string, body: string) => Promise<any>
/** Delete the main Danger comment */
deleteMainComment: () => Promise<boolean>
deleteMainComment: (dangerID: string) => Promise<boolean>
/** Replace the main Danger comment */
updateOrCreateComment: (newComment: string) => Promise<any>
updateOrCreateComment: (dangerID: string, newComment: string) => Promise<any>
/** Sets the current PR's status */
updateStatus: (passed: boolean, message: string) => Promise<boolean>
}
Expand Down
9 changes: 6 additions & 3 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface ExecutorOptions {
jsonOnly: boolean
/** Should Danger post as much info as possible */
verbose: boolean
/** A unique ID to handle multiple Danger runs */
dangerID: string
}

export class Executor {
Expand Down Expand Up @@ -164,6 +166,7 @@ export class Executor {

this.d(results)

const dangerID = this.options.dangerID
const failed = fails.length > 0
const successPosting = await this.platform.updateStatus(!failed, messageForResults(results))
if (this.options.verbose) {
Expand All @@ -173,7 +176,7 @@ export class Executor {

if (failureCount + messageCount === 0) {
console.log("No issues or messages were sent. Removing any existing messages.")
await this.platform.deleteMainComment()
await this.platform.deleteMainComment(dangerID)
} else {
if (fails.length > 0) {
const s = fails.length === 1 ? "" : "s"
Expand All @@ -187,8 +190,8 @@ export class Executor {
} else if (messageCount > 0) {
console.log("Found only messages, passing those to review.")
}
const comment = githubResultsTemplate(results)
await this.platform.updateOrCreateComment(comment)
const comment = githubResultsTemplate(dangerID, results)
await this.platform.updateOrCreateComment(dangerID, comment)
}

// More info, is more info.
Expand Down
15 changes: 10 additions & 5 deletions source/runner/templates/githubIssueTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as v from "voca"

import { DangerResults } from "../../dsl/DangerResults"
import { Violation } from "../../dsl/Violation"
import * as v from "voca"

/**
* Converts a set of violations into a HTML table
Expand Down Expand Up @@ -47,29 +48,33 @@ function getSummary(label: string, violations: Violation[]): string {
)
}

function buildSummaryMessage(results: DangerResults): string {
function buildSummaryMessage(dangerID: string, 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` : ""}`
${markdowns.length > 0 ? `${markdowns.length} markdown notices` : ""}
${dangerIDToString(dangerID)}`
return summary
}

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

/**
* Postfix signature to be attached comment generated / updated by danger.
*/
export const dangerSignaturePostfix = `Generated by :no_entry_sign: <a href="http://github.com/danger/danger-js/">dangerJS</a>`

/**
* 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(results: DangerResults): string {
export function template(dangerID: string, results: DangerResults): string {
return `
<!--
${buildSummaryMessage(results)}
${buildSummaryMessage(dangerID, results)}
-->
${table("Fails", "no_entry_sign", results.fails)}
${table("Warnings", "warning", results.warnings)}
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
version "1.4.2"
resolved "https://registry.yarnpkg.com/@types/readline-sync/-/readline-sync-1.4.2.tgz#516b5f4f250534062b6bb08ae8367a088dcf739f"

"@types/voca@^1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@types/voca/-/voca-1.3.0.tgz#fe4d6701e848fd294f60f0065a92eabf01203188"

abab@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.3.tgz#b81de5f7274ec4e756d797cd834f303642724e5d"
Expand Down

0 comments on commit 635f054

Please sign in to comment.