Skip to content

Commit

Permalink
Adds support to the runner to handle a github-less platform
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jan 21, 2018
1 parent 6ad8fe9 commit eb6e738
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 22 deletions.
31 changes: 31 additions & 0 deletions dangerfile.lite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as fs from "fs"

import { DangerDSLType } from "./source/dsl/DangerDSL"
declare var danger: DangerDSLType
declare function warn(params: string): void

const hasChangelog = danger.git.modified_files.includes("CHANGELOG.md")
if (!hasChangelog) {
warn("Please add a changelog entry for your changes.")
}

import dtsGenerator from "./scripts/danger-dts"
const currentDTS = dtsGenerator()
const savedDTS = fs.readFileSync("source/danger.d.ts").toString()
if (currentDTS !== savedDTS) {
const message = "There are changes to the Danger DSL which are not reflected in the current danger.d.ts."
const idea = "Please run <code>yarn declarations</code> and update this PR."
fail(`${message}<br/><i>${idea}</i>`)
}

// Always ensure we name all CI providers in the README. These
// regularly get forgotten on a PR adding a new one.
const sentence = danger.utils.sentence

import { realProviders } from "./source/ci_source/providers"
const readme = fs.readFileSync("README.md").toString()
const names = realProviders.map(p => new p({}).name)
const missing = names.filter(n => !readme.includes(n))
if (missing.length) {
warn(`These providers are missing from the README: ${sentence(missing)}`)
}
12 changes: 7 additions & 5 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ if (missing.length) {
warn(`These providers are missing from the README: ${sentence(missing)}`)
}

danger.github.utils.fileContents("scripts/run-fixtures.js").then(fixtures => {
if (fixtures.includes("const writeResults = true")) {
fail("Fixtures test script is still in write mode, edit `scripts/run-fixtures.js`.")
}
})
if (danger.github) {
danger.github.utils.fileContents("scripts/run-fixtures.js").then(fixtures => {
if (fixtures.includes("const writeResults = true")) {
fail("Fixtures test script is still in write mode, edit `scripts/run-fixtures.js`.")
}
})
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"danger-pr": "distribution/commands/danger-pr.js",
"danger-process": "distribution/commands/danger-process.js",
"danger-ci": "distribution/commands/danger-ci.js",
"danger-runner": "distribution/commands/danger-runner.js",
"danger-init": "distribution/commands/danger-init.js"
"danger-init": "distribution/commands/danger-init.js",
"danger-local": "distribution/commands/danger-local.js"
},
"jest": {
"transform": {
Expand All @@ -36,12 +36,12 @@
},
"scripts": {
"precommit": "lint-staged",
"prepush": "yarn run build",
"prepush": "yarn build; yarn danger:pre",
"test": "jest",
"test:watch": "jest --watch",
"lint": "tslint \"source/**/*.ts\"",
"lint:fix": "tslint \"source/**/*.ts\" --fix",
"prepublishOnly": "yarn run build && yarn declarations && yarn build:flow-types",
"prepublishOnly": "yarn build && yarn declarations && yarn build:flow-types",
"build": "shx rm -rf ./distribution && tsc -p tsconfig.production.json && madge ./distribution --circular",
"build:fast": "tsc -p tsconfig.production.json",
"build:flow-types":
Expand All @@ -53,7 +53,8 @@
"mkdir docs/docs_generate; cp source/danger.d.ts docs/docs_generate; cp node_modules/@octokit/rest/index.d.ts docs/docs_generate/github.d.ts",
"docs":
"yarn run docs:cp_defs; yarn typedoc --ignoreCompilerErrors --mode modules --json docs/js_ref_dsl_docs.json --includeDeclarations source",
"dts-lint": "yarn run declarations && yarn dtslint types"
"dts-lint": "yarn run declarations && yarn dtslint types",
"danger:prepush": "node distribution/commands/danger.js local --dangerfile dangerfile.lite.ts"
},
"repository": {
"type": "git",
Expand Down
7 changes: 4 additions & 3 deletions source/commands/ci/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { CISource } from "../../ci_source/ci_source"
const d = debug("danger:process_runner")

export interface RunnerConfig {
source: CISource
platform: Platform
source?: CISource
platform?: Platform
additionalArgs?: string[]
}

export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => {
Expand All @@ -43,7 +44,7 @@ export const runRunner = async (app: SharedCLI, config?: RunnerConfig) => {
const dangerJSONDSL = await jsonDSLGenerator(platform)

const config: ExecutorOptions = {
stdoutOnly: app.textOnly,
stdoutOnly: !platform.supportsCommenting() || app.textOnly,
verbose: app.verbose,
jsonOnly: false,
dangerID: app.id || "default",
Expand Down
2 changes: 1 addition & 1 deletion source/commands/danger-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ const app = (program as any) as App
const base = app.base || "master"
const localPlatform = new LocalGit({ base, staged: app.staging })
const fakeSource = new FakeCI(process.env)
runRunner(app, { source: fakeSource, platform: localPlatform })
runRunner(app, { source: fakeSource, platform: localPlatform, additionalArgs: ["--local"] })
4 changes: 4 additions & 0 deletions source/commands/utils/_tests/dangerRunToRunnerCLI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ describe("it can handle the command", () => {
expect(dangerRunToRunnerCLI(["danger", "ci"])).toEqual("danger runner".split(" "))
})

it("`danger local`", () => {
expect(dangerRunToRunnerCLI(["danger", "local"])).toEqual("danger runner".split(" "))
})

it("`danger ci --dangerfile myDangerfile.ts`", () => {
expect(dangerRunToRunnerCLI(["danger", "ci", "--dangerfile", "myDangerfile.ts"])).toEqual(
"danger runner --dangerfile myDangerfile.ts".split(" ")
Expand Down
12 changes: 10 additions & 2 deletions source/commands/utils/dangerRunToRunnerCLI.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const usesProcessSeparationCommands = ["ci", "pr", "local"]

const dangerRunToRunnerCLI = (argv: string[]) => {
let newCommand = []
newCommand.push(argv[0])

// e.g. node --inspect distribution/commands/danger-run-ci.js --dangerfile myDangerfile.ts
// or node distribution/commands/danger-pr.js --dangerfile myDangerfile.ts

if (argv.length === 1) {
return ["danger", "runner"]
} else if (argv[0].includes("node")) {
const newJSFile = argv[1].replace("-ci", "-runner").replace("-pr", "-runner")
// convert
let newJSFile = argv[1]
usesProcessSeparationCommands.forEach(name => {
newJSFile = newJSFile.replace("-" + name, "-runner")
})

newCommand.push(newJSFile)
for (let index = 2; index < argv.length; index++) {
newCommand.push(argv[index])
Expand All @@ -16,7 +24,7 @@ const dangerRunToRunnerCLI = (argv: string[]) => {
// e.g. danger ci --dangerfile
// if you do `danger run` start looking at args later
newCommand.push("runner")
let index = argv[1] === "ci" || argv[1] === "pr" ? 2 : 1
let index = usesProcessSeparationCommands.includes(argv[1]) ? 2 : 1
for (; index < argv.length; index++) {
newCommand.push(argv[index])
}
Expand Down
2 changes: 2 additions & 0 deletions source/commands/utils/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export function dangerfilePath(program: any): string {
if (program.dangerfile) {
return program.dangerfile
}

if (existsSync("dangerfile.ts")) {
return "dangerfile.ts"
}

if (existsSync("dangerfile.js")) {
return "dangerfile.js"
}
Expand Down
4 changes: 4 additions & 0 deletions source/platforms/FakePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class FakePlatform implements Platform {
}
}

supportsCommenting() {
return true
}

async updateOrCreateComment(_newComment: string): Promise<boolean> {
return true
}
Expand Down
4 changes: 4 additions & 0 deletions source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export class GitHub {
}
}

supportsCommenting() {
return true
}

/**
* Returns the response for the new comment
*
Expand Down
6 changes: 5 additions & 1 deletion source/platforms/LocalGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class LocalGit implements Platform {

const config: GitJSONToGitDSLConfig = {
repo: process.cwd(),
baseSHA: this.options.base,
baseSHA: this.options.base || "master",
headSHA: "HEAD",
getFileContents: localGetFileAtSHA,
getFullDiff: localGetDiff,
Expand All @@ -38,6 +38,10 @@ export class LocalGit implements Platform {
return gitJSONToGitDSL(gitJSON, config)
}

supportsCommenting() {
return false
}

async updateOrCreateComment(_newComment: string): Promise<boolean> {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion source/platforms/git/localGetDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { exec } from "child_process"
const d = debug("danger:localGetDiff")

export const localGetDiff = (base: string, head: string) =>
new Promise(done => {
new Promise<string>(done => {
const call = `git diff --no-index ${base} ${head}`
d(call)

Expand Down
2 changes: 1 addition & 1 deletion source/platforms/git/localGetFileAtSHA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { exec } from "child_process"
const d = debug("danger:localGetFileAtSHA")

export const localGetFileAtSHA = (path: string, _repo: string | undefined, sha: string) =>
new Promise(done => {
new Promise<string>(done => {
const call = `git show ${sha}:"${path}"`
d(call)

Expand Down
2 changes: 2 additions & 0 deletions source/platforms/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface Platform {
getPlatformDSLRepresentation: () => Promise<any>
/** Pulls in the Code Review Diff, and offers a succinct user-API for it */
getPlatformGitRepresentation: () => Promise<GitJSONDSL>
/** Can it update comments? */
supportsCommenting: () => boolean
/** Creates a comment on the PR */
createComment: (dangerID: string, body: string) => Promise<any>
/** Delete the main Danger comment */
Expand Down
18 changes: 15 additions & 3 deletions source/runner/jsonToDSL.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import * as GitHubNodeAPI from "@octokit/rest"

import { DangerDSLJSONType, DangerDSLType } from "../dsl/DangerDSL"
import { gitHubGitDSL as gitJSONToGitDSL } from "../platforms/github/GitHubGit"
import { gitHubGitDSL as githubJSONToGitDSL } from "../platforms/github/GitHubGit"
import { githubJSONToGitHubDSL } from "../platforms/GitHub"
import { sentence, href } from "./DangerUtils"
import { LocalGit } from "../platforms/LocalGit"
import { GitDSL } from "../dsl/GitDSL"

export const jsonToDSL = async (dsl: DangerDSLJSONType): Promise<DangerDSLType> => {
const api = githubAPIForDSL(dsl)
const github = githubJSONToGitHubDSL(dsl.github, api)
const git = gitJSONToGitDSL(github, dsl.git)
const platformExists = [dsl.github].some(p => !!p)
const github = dsl.github && githubJSONToGitHubDSL(dsl.github, api)
// const gitlab = dsl.gitlab && githubJSONToGitLabDSL(dsl.gitlab, api)

let git: GitDSL
if (!platformExists) {
const localPlatform = new LocalGit({ base: "master" })
git = await localPlatform.getPlatformGitRepresentation()
} else {
git = githubJSONToGitDSL(github, dsl.git)
}

return {
git,
github: github,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"exclude": [
"dangerfile.ts",
"dangerfile.lite.ts",
"scripts",
"node_modules",
"source/**/fixtures/*",
Expand Down

0 comments on commit eb6e738

Please sign in to comment.