Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --base option for danger local cli #506

Merged
merged 2 commits into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

## Master

* Fix --base options for danger local. [@peterjgrainger][]

* Fix a minor typo in Semaphore CI setup. [@hongrich][]

## 3.1.4
Expand Down
8 changes: 2 additions & 6 deletions source/commands/danger-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import * as debug from "debug"
import * as getSTDIN from "get-stdin"
import chalk from "chalk"

import { contextForDanger } from "../runner/Dangerfile"
import inline from "../runner/runners/inline"
import { dangerfilePath } from "./utils/file-utils"
import { DangerDSLJSONType } from "../dsl/DangerDSL"
import { jsonToDSL } from "../runner/jsonToDSL"
import { jsonToContext } from "../runner/json-to-context"

const d = debug("danger:runner")

Expand Down Expand Up @@ -43,12 +41,10 @@ let runtimeEnv = {} as any
const run = async (jsonString: string) => {
d("Got STDIN for Danger Run")
foundDSL = true
const dslJSON = JSON.parse(jsonString) as { danger: DangerDSLJSONType }
const dsl = await jsonToDSL(dslJSON.danger)
const dangerFile = dangerfilePath(program)

// Set up the runtime env
const context = contextForDanger(dsl)
const context = await jsonToContext(jsonString, program)
runtimeEnv = await inline.createDangerfileRuntimeEnvironment(context)
d(`Evaluating ${dangerFile}`)
await inline.runDangerfileEnvironment(dangerFile, undefined, runtimeEnv)
Expand Down
1 change: 1 addition & 0 deletions source/commands/utils/sharedDangerfileArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export default (command: any) =>
.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")
.option("-b, --base [branch_name]", "Base branch")
2 changes: 1 addition & 1 deletion source/danger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ declare module "danger" {
* pass args/opts from the original CLI call through
* to the process.
*/
cliArgs: any
cliArgs: CliArgs
}
}

Expand Down
3 changes: 2 additions & 1 deletion source/dsl/DangerDSL.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GitDSL, GitJSONDSL } from "../dsl/GitDSL"
import { GitHubDSL } from "../dsl/GitHubDSL"
import { DangerUtilsDSL } from "./DangerUtilsDSL"
import { CliArgs } from "../runner/cli-args"

/**
* The shape of the JSON passed between Danger and a subprocess. It's built
Expand Down Expand Up @@ -71,7 +72,7 @@ export interface DangerDSLJSONType {
* pass args/opts from the original CLI call through
* to the process.
*/
cliArgs: any
cliArgs: CliArgs
}
}

Expand Down
87 changes: 87 additions & 0 deletions source/runner/_tests/json-to-context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { DangerContext } from "../Dangerfile"
import { jsonToContext } from "../json-to-context"

jest.mock("../jsonToDSL.ts")
jest.mock("../Dangerfile")

/**
* Mock the jsonToDSL function
*/
// tslint:disable-next-line
const jsonToDSLMock = require("../jsonToDSL")

/**
* Mock the context for danger function
*/
// tslint:disable-next-line
const bar = require("../Dangerfile")

describe("runner/json-to-context", () => {
let jsonString
let program
let context
beforeEach(async () => {
jsonToDSLMock.jsonToDSL = jest.fn(() => Promise.resolve({ danger: "" }))
bar.contextForDanger = jest.fn(() => Promise.resolve({ danger: "" }))
jsonString = JSON.stringify({
danger: {
settings: {
github: {
baseURL: "",
},
cliArgs: {},
},
},
})

program = {
base: "develop",
}
})

it("should have a function called get context", () => {
expect(jsonToContext).toBeTruthy()
})

it("should return a context", async () => {
context = await jsonToContext(jsonString, program)
expect(context).toBeTruthy()
})

it("should set the base from the input command", async () => {
context = await jsonToContext(jsonString, program)
expect(context.danger).toEqual("")
})

it("should work if no base is set", async () => {
program.base = undefined
await jsonToContext(jsonString, program)
expect(jsonToDSLMock.jsonToDSL).toHaveBeenCalledWith({
settings: {
github: {
baseURL: "",
},
cliArgs: {},
},
})
})

it("should set the base to develop", async () => {
await jsonToContext(jsonString, program)
expect(jsonToDSLMock.jsonToDSL).toHaveBeenCalledWith({
settings: {
github: {
baseURL: "",
},
cliArgs: {
base: "develop",
},
},
})
})

it("should call context for danger with dsl", async () => {
await jsonToContext(jsonString, program)
expect(bar.contextForDanger).toHaveBeenCalledWith({ danger: "" })
})
})
46 changes: 46 additions & 0 deletions source/runner/_tests/jsonToDSL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { jsonToDSL } from "../jsonToDSL"
import { DangerDSLJSONType } from "../../dsl/DangerDSL"
import { GitDSL } from "../../dsl/GitDSL"

/**
* Mock everything that calls externaly
*/
jest.mock("../../platforms/github/GitHubGit")
jest.mock("../../platforms/GitHub")
jest.mock("../../platforms/git/localGetDiff")
jest.mock("../../platforms/git/localGetCommits")
jest.mock("../../platforms/git/diffToGitJSONDSL")
jest.mock("../../platforms/git/gitJSONToGitDSL")
jest.mock("@octokit/rest")

// tslint:disable-next-line
const foo = require("../../platforms/git/localGetDiff")
foo.localGetDiff = jest.fn(() => Promise.resolve({}))

describe("runner/jsonToDSL", () => {
let dsl
beforeEach(() => {
dsl = {
settings: {
github: {},
cliArgs: {
base: "develop",
},
},
}
})

it("should have a function named jsonToDSL", () => {
expect(jsonToDSL).toBeTruthy()
})

it("should return config", async () => {
const outputDsl = await jsonToDSL(dsl as DangerDSLJSONType)
expect(outputDsl.github).toBeUndefined()
})

it("should call LocalGit with correct base", async () => {
const outputDsl = await jsonToDSL(dsl as DangerDSLJSONType)
expect(foo.localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD")
})
})
12 changes: 12 additions & 0 deletions source/runner/cli-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Describes the possible arguments that
* could be used when calling the CLI
*/
export interface CliArgs {
base: string
verbose: string
externalCiProvider: string
textOnly: string
dangerfile: string
id: string
}
35 changes: 35 additions & 0 deletions source/runner/danger-dsl-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { DangerDSLJSONType } from "../dsl/DangerDSL"
import { GitJSONDSL } from "../dsl/GitDSL"
import { GitHubDSL } from "../dsl/GitHubDSL"
import { CliArgs } from "./cli-args"

/**
* Using the input JSON create an DangerDSL
*
* @see DangerDSLJSONType for more detailed definition
*/
export class DangerDSLJSON implements DangerDSLJSONType {
git: GitJSONDSL
github: GitHubDSL
settings: {
github: {
accessToken: string
baseURL: string | undefined
additionalHeaders: any
}
cliArgs: CliArgs
}
/**
* Parse the JSON and assign danger to this object
*
* Also add the arguments sent to the CLI
*
* @param JSONString DSL in JSON format
* @param cliArgs arguments used running danger command
*/
constructor(JSONString: string, cliArgs: CliArgs) {
const parsedString = JSON.parse(JSONString)
Object.assign(this, parsedString.danger)
this.settings.cliArgs = cliArgs
}
}
3 changes: 2 additions & 1 deletion source/runner/dslGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Platform } from "../platforms/platform"
import { DangerDSLJSONType } from "../dsl/DangerDSL"
import { CliArgs } from "./cli-args"

export const jsonDSLGenerator = async (platform: Platform): Promise<DangerDSLJSONType> => {
const git = await platform.getPlatformGitRepresentation()
Expand All @@ -14,7 +15,7 @@ export const jsonDSLGenerator = async (platform: Platform): Promise<DangerDSLJSO
additionalHeaders: {},
baseURL: process.env["DANGER_GITHUB_API_BASE_URL"] || undefined,
},
cliArgs: {},
cliArgs: {} as CliArgs,
},
}
}
17 changes: 17 additions & 0 deletions source/runner/json-to-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CliArgs } from "./cli-args"
import { jsonToDSL } from "./jsonToDSL"
import { contextForDanger, DangerContext } from "./Dangerfile"
import { DangerDSLJSON } from "./danger-dsl-json"

/**
* Reads in the JSON string converts to a dsl object and gets the change context
* to be used for Danger.
* @param JSONString {string} from stdin
* @param program {any} commander
* @returns {Promise<DangerContext>} context for danger
*/
export async function jsonToContext(JSONString: string, program: any): Promise<DangerContext> {
const dslJSON = { danger: new DangerDSLJSON(JSONString, program as CliArgs) }
const dsl = await jsonToDSL(dslJSON.danger)
return contextForDanger(dsl)
}
2 changes: 1 addition & 1 deletion source/runner/jsonToDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const jsonToDSL = async (dsl: DangerDSLJSONType): Promise<DangerDSLType>

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