Skip to content

Commit

Permalink
Merge pull request #761 from danger/logs_ci
Browse files Browse the repository at this point in the history
Improve logs and verifies --process on the CI
  • Loading branch information
orta committed Nov 12, 2018
2 parents b138a3d + 2063cd9 commit 3fa50c8
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Expand Up @@ -29,6 +29,14 @@ matrix:
- yarn build
- node scripts/run-fixtures.js

# Runs both the CI and PR with a custom process to ensure apps like swift/rust work
- node_js: "10.13"
script:
- yarn build
- node distribution/commands/danger-ci.js --process "ruby scripts/danger_runner.rb"
- node distribution/commands/danger-pr.js --process "ruby scripts/danger_runner.rb"
https://github.com/danger/danger-js/pull/760

# Does the real danger run
- node_js: "9"
script:
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Expand Up @@ -12,5 +12,6 @@
"distribution/": true
},
"editor.formatOnSave": true,
"debug.node.autoAttach": "on",
"cSpell.words": ["APIPR", "BITBUCKETSERVER", "Commenter", "JSONDSL", "PRDSL", "bitbucket", "jsome", "repo's"]
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,9 @@

## Master

- Improves debug logging, messaging on CI about status updates - [@orta][]
- Better detection of json results: {} from a sub-process - [@orta][]
- CLI Args are actually passed to subprocess instead of an empty object - [@orta][]
- Fix Netlify integration when repo url includes an access token - [@imorente][]

# 6.1.0
Expand Down
1 change: 1 addition & 0 deletions scripts/danger_runner.rb
Expand Up @@ -5,6 +5,7 @@

# Have a dumb fake response
require "json"
puts "Hello from ruby!"
results = { fails: [], warnings: [], messages: [], markdowns: [] }.to_json

STDOUT.write(results)
2 changes: 1 addition & 1 deletion source/commands/ci/runner.ts
Expand Up @@ -57,7 +57,7 @@ export const runRunner = async (app: SharedCLI, config?: Partial<RunnerConfig>)
}

if (platform) {
const dangerJSONDSL = await jsonDSLGenerator(platform, source)
const dangerJSONDSL = await jsonDSLGenerator(platform, source, app)
const execConfig: ExecutorOptions = {
stdoutOnly: !platform.supportsCommenting() || app.textOnly,
verbose: app.verbose,
Expand Down
2 changes: 1 addition & 1 deletion source/commands/danger-pr.ts
Expand Up @@ -104,7 +104,7 @@ if (program.args.length === 0) {

// Run the first part of a Danger Process and output the JSON to CLI
async function runHalfProcessJSON(platform: Platform, source: CISource) {
const dangerDSL = await jsonDSLGenerator(platform, source)
const dangerDSL = await jsonDSLGenerator(platform, source, program)
const processInput = prepareDangerDSL(dangerDSL)
const output = JSON.parse(processInput)
const dsl = { danger: output }
Expand Down
6 changes: 4 additions & 2 deletions source/commands/danger-process.ts
Expand Up @@ -25,11 +25,13 @@ const d = debug("process")
declare const global: any

let subprocessName: string | undefined
let deprecationWarning = chalk.bold("Semi-deprecated, as ci/pr/local all support --process")

program
.usage("[options] <process_name>")
.description(
"Does a Danger run, but instead of handling the execution of a Dangerfile it will pass the DSL " +
deprecationWarning +
"\n\nDoes a Danger run, but instead of handling the execution of a Dangerfile it will pass the DSL " +
"into another process expecting the process to eventually return results back as JSON. If you don't " +
"provide another process, then it will output to STDOUT."
)
Expand Down Expand Up @@ -70,7 +72,7 @@ getRuntimeCISource(app).then(source => {
}

if (platform) {
jsonDSLGenerator(platform, source).then(dangerJSONDSL => {
jsonDSLGenerator(platform, source, app).then(dangerJSONDSL => {
if (!subprocessName) {
// Just pipe it out to the CLI
const processInput = prepareDangerDSL(dangerJSONDSL)
Expand Down
35 changes: 23 additions & 12 deletions source/commands/utils/runDangerSubprocess.ts
Expand Up @@ -37,11 +37,13 @@ export const runDangerSubprocess = (
const dslJSONString = prepareDangerDSL(dslJSON)
d(`Running subprocess: ${processDisplayName} - ${args}`)
const child = spawn(processName, args, { env: { ...process.env, ...config.additionalEnvVars } })
let allLogs = ""

d(`Started passing in STDIN`)
child.stdin.write(dslJSONString)
child.stdin.end()
d(`Passed in STDIN`)

let allLogs = ""
child.stdout.on("data", async data => {
const stdout = data.toString()
allLogs += stdout
Expand All @@ -50,15 +52,21 @@ export const runDangerSubprocess = (
const maybeJSON = getJSONFromSTDOUT(stdout)
const maybeJSONURL = getJSONURLFromSTDOUT(stdout)

// Remove message sent back to danger-js
const withoutURLs: string = data
.toString()
.replace(maybeJSON, "")
.replace(maybeJSONURL, "")

console.log(withoutURLs)

// Pass it back to the user
if (!results && maybeJSONURL) {
d("Got JSON URL from STDOUT, results are at: \n" + maybeJSONURL)
results = JSON.parse(readFileSync(maybeJSONURL.replace("danger-results:/", ""), "utf8"))
} else if (!results && maybeJSON) {
d("Got JSON results from STDOUT, results: \n" + maybeJSON)
results = JSON.parse(maybeJSON)
} else {
// Pass it back to the user
console.log(data.toString())
}
})

Expand Down Expand Up @@ -98,14 +106,17 @@ const getJSONURLFromSTDOUT = (stdout: string): string | undefined => {

/** Pulls the JSON directly out, this has proven to be less reliable */
const getJSONFromSTDOUT = (stdout: string): string | undefined => {
const trimmed = stdout.trim()
return trimmed.startsWith("{") &&
trimmed.endsWith("}") &&
trimmed.includes("markdowns") &&
trimmed.includes("fails") &&
trimmed.includes("warnings")
? trimmed
: undefined
const lines = stdout.split("\n")
return lines.find(line => {
const trimmed = line.trim()
return (
trimmed.startsWith("{") &&
trimmed.endsWith("}") &&
trimmed.includes("markdowns") &&
trimmed.includes("fails") &&
trimmed.includes("warnings")
)
})
}

export default runDangerSubprocess
6 changes: 6 additions & 0 deletions source/danger-incoming-process-schema.json
Expand Up @@ -486,21 +486,27 @@
"description": "Describes the possible arguments that\ncould be used when calling the CLI",
"properties": {
"base": {
"description": "The base reference used by danger PR (e.g. not master)",
"type": "string"
},
"dangerfile": {
"description": "A custom path for the dangerfile (can also be a remote reference)",
"type": "string"
},
"externalCiProvider": {
"description": "Used by danger-js o allow having a custom CI",
"type": "string"
},
"id": {
"description": "So you can have many danger runs in one code review",
"type": "string"
},
"textOnly": {
"description": "Use SDTOUT instead of posting to the code review side",
"type": "string"
},
"verbose": {
"description": "For debugging",
"type": "string"
}
},
Expand Down
8 changes: 8 additions & 0 deletions source/danger.d.ts
Expand Up @@ -1108,13 +1108,21 @@ interface Violation {
* could be used when calling the CLI
*/
interface CliArgs {
/** The base reference used by danger PR (e.g. not master) */
base: string
/** For debugging */
verbose: string
/** Used by danger-js o allow having a custom CI */
externalCiProvider: string
/** Use SDTOUT instead of posting to the code review side */
textOnly: string
/** A custom path for the dangerfile (can also be a remote reference) */
dangerfile: string
/** So you can have many danger runs in one code review */
id: string
}

// NOTE: if add something new here, you need to change dslGenerator.ts
/** A function with a callback function, which Danger wraps in a Promise */
type CallbackableFn = (callback: (done: any) => void) => void

Expand Down
8 changes: 8 additions & 0 deletions source/dsl/cli-args.ts
Expand Up @@ -3,10 +3,18 @@
* could be used when calling the CLI
*/
export interface CliArgs {
/** The base reference used by danger PR (e.g. not master) */
base: string
/** For debugging */
verbose: string
/** Used by danger-js o allow having a custom CI */
externalCiProvider: string
/** Use SDTOUT instead of posting to the code review side */
textOnly: string
/** A custom path for the dangerfile (can also be a remote reference) */
dangerfile: string
/** So you can have many danger runs in one code review */
id: string
}

// NOTE: if add something new here, you need to change dslGenerator.ts
32 changes: 21 additions & 11 deletions source/platforms/github/GitHubAPI.ts
Expand Up @@ -353,18 +353,28 @@ export class GitHubAPI {
if (passed === "pending") {
state = "pending"
}
const res = await this.post(
`repos/${repo}/statuses/${ref}`,
{},
{
state: state,
context: process.env["PERIL_BOT_USER_ID"] ? "Peril" : "Danger",
target_url: url || "http://danger.systems/js",
description: message,
// Suppress errors, because in OSS
// this failure could be due to access rights.
//
// So only error when it's a real message.
try {
const res = await this.post(
`repos/${repo}/statuses/${ref}`,
{},
{
state: state,
context: process.env["PERIL_BOT_USER_ID"] ? "Peril" : "Danger",
target_url: url || "http://danger.systems/js",
description: message,
},
true
)
return res.ok
} catch (error) {
if (prJSON.base.repo.private) {
console.log("Could not post a commit status.")
}
)

return res.ok
}
}

postCheckRun = async (check: CheckOptions, token: string) => {
Expand Down
8 changes: 4 additions & 4 deletions source/platforms/github/_tests/_github_api.test.ts
Expand Up @@ -162,7 +162,7 @@ describe("API testing", () => {
method: "POST",
body: '{"state":"pending","context":"Danger","target_url":"http://danger.systems/js","description":"message"}',
},
undefined
true
)
})

Expand All @@ -181,7 +181,7 @@ describe("API testing", () => {
method: "POST",
body: '{"state":"failure","context":"Danger","target_url":"http://danger.systems/js","description":"message"}',
},
undefined
true
)
})

Expand All @@ -200,7 +200,7 @@ describe("API testing", () => {
method: "POST",
body: '{"state":"success","context":"Danger","target_url":"http://example.org","description":"message"}',
},
undefined
true
)
})

Expand All @@ -219,7 +219,7 @@ describe("API testing", () => {
method: "POST",
body: '{"state":"success","context":"Danger","target_url":"http://danger.systems/js","description":"message"}',
},
undefined
true
)
})

Expand Down
2 changes: 1 addition & 1 deletion source/runner/_tests/_executor.test.ts
Expand Up @@ -29,7 +29,7 @@ const defaultConfig = {
const fakeCI = new FakeCI({})

const defaultDsl = (platform: any): Promise<DangerDSLType> => {
return jsonDSLGenerator(platform, fakeCI).then(jsonDSL => {
return jsonDSLGenerator(platform, fakeCI, {} as any).then(jsonDSL => {
jsonDSL.github = {
pr: {
number: 1,
Expand Down
17 changes: 15 additions & 2 deletions source/runner/dslGenerator.ts
Expand Up @@ -3,8 +3,13 @@ import { DangerDSLJSONType } from "../dsl/DangerDSL"
import { CliArgs } from "../dsl/cli-args"
import { CISource } from "../ci_source/ci_source"
import { emptyGitJSON } from "../platforms/github/GitHubGit"
import { CommanderStatic } from "commander"

export const jsonDSLGenerator = async (platform: Platform, source: CISource): Promise<DangerDSLJSONType> => {
export const jsonDSLGenerator = async (
platform: Platform,
source: CISource,
program: CommanderStatic
): Promise<DangerDSLJSONType> => {
const useSimpleDSL = platform.getPlatformReviewSimpleRepresentation && source.useEventDSL

const git = useSimpleDSL ? emptyGitJSON() : await platform.getPlatformGitRepresentation()
Expand All @@ -15,6 +20,14 @@ export const jsonDSLGenerator = async (platform: Platform, source: CISource): Pr

const platformDSL = await getDSLFunc!()

const cliArgs: CliArgs = {
base: program.base,
dangerfile: program.dangerfile,
externalCiProvider: program.externalCiProvider,
id: program.id,
textOnly: program.textOnly,
verbose: program.verbose,
}
return {
git,
[platform.name === "BitBucketServer" ? "bitbucket_server" : "github"]: platformDSL,
Expand All @@ -24,7 +37,7 @@ export const jsonDSLGenerator = async (platform: Platform, source: CISource): Pr
additionalHeaders: {},
baseURL: process.env["DANGER_GITHUB_API_BASE_URL"] || undefined,
},
cliArgs: {} as CliArgs,
cliArgs,
},
}
}
9 changes: 7 additions & 2 deletions source/runner/jsonToContext.ts
Expand Up @@ -3,6 +3,7 @@ import { jsonToDSL } from "./jsonToDSL"
import { contextForDanger, DangerContext } from "./Dangerfile"
import { DangerDSLJSON } from "./dangerDSLJSON"
import { CISource } from "../ci_source/ci_source"
import { CommanderStatic } from "commander"

/**
* Reads in the JSON string converts to a dsl object and gets the change context
Expand All @@ -11,8 +12,12 @@ import { CISource } from "../ci_source/ci_source"
* @param program {any} commander
* @returns {Promise<DangerContext>} context for danger
*/
export async function jsonToContext(JSONString: string, program: any, source: CISource): Promise<DangerContext> {
const dslJSON = { danger: new DangerDSLJSON(JSONString, program as CliArgs) }
export async function jsonToContext(
JSONString: string,
program: CommanderStatic,
source: CISource
): Promise<DangerContext> {
const dslJSON = { danger: new DangerDSLJSON(JSONString, (program as any) as CliArgs) }
const dsl = await jsonToDSL(dslJSON.danger, source)
return contextForDanger(dsl)
}
2 changes: 1 addition & 1 deletion source/runner/runners/_tests/vm2.test.ts
Expand Up @@ -48,7 +48,7 @@ runners.forEach(run => {
const source = new FakeCI({})
exec = new Executor(source, platform, run.fn, config)

const dsl = await jsonDSLGenerator(platform, new FakeCI({}))
const dsl = await jsonDSLGenerator(platform, new FakeCI({}), {} as any)
dsl.github = {
pr: {
number: 1,
Expand Down

0 comments on commit 3fa50c8

Please sign in to comment.