Skip to content

Commit

Permalink
pass the staged flag by cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
Soyn committed Feb 17, 2020
1 parent e86579c commit dee1ec2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
3 changes: 1 addition & 2 deletions source/commands/danger-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ interface App extends SharedCLI {

program
.usage("[options]")
// TODO: this option
// .option("-s, --staging", "Just use staged changes.")
.description("Runs danger without PR metadata, useful for git hooks.")
.option("-s, --staging", "Just use staged changes.")
.option("-b, --base [branch_name]", "Use a different base branch")
setSharedArgs(program).parse(process.argv)

Expand Down
2 changes: 2 additions & 0 deletions source/dsl/cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface CliArgs {
dangerfile: string
/** So you can have many danger runs in one code review */
id: string
/** Use staged changes */
staged?: boolean
}

// NOTE: if add something new here, you need to change dslGenerator.ts
5 changes: 2 additions & 3 deletions source/platforms/LocalGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class LocalGit implements Platform {
const base = this.options.base || "master"
const head = "HEAD"

this.gitDiff = await localGetDiff(base, head)
this.gitDiff = await localGetDiff(base, head, this.options.staged)
return this.gitDiff
}

Expand All @@ -45,9 +45,8 @@ export class LocalGit implements Platform {
const base = this.options.base || "master"
const head = "HEAD"
const diff = await this.getGitDiff()
const unstagedDiff = await localGetDiff(base, head, false)
const commits: GitCommit[] = await localGetCommits(base, head)
const gitJSON = diffToGitJSONDSL(diff.concat(unstagedDiff), commits)
const gitJSON = diffToGitJSONDSL(diff, commits)

const config: GitJSONToGitDSLConfig = {
repo: process.cwd(),
Expand Down
8 changes: 4 additions & 4 deletions source/platforms/git/localGetDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { debug } from "../../debug"
import { spawn } from "child_process"

const d = debug("localGetDiff")
const stagedDiffArgs = (base: string, head: string) => ["diff", `${base}...${head}`]
const unstagedDiffArgas = (base: string) => ["diff", base]
export const localGetDiff = (base: string, head: string, onlyUsedStaged: boolean = true) =>
const useCommittedDiffArgs = (base: string, head: string) => ["diff", `${base}...${head}`]
const useStagedChanges = (base: string) => ["diff", base]
export const localGetDiff = (base: string, head: string, staged: boolean = false) =>
new Promise<string>(done => {
const args = onlyUsedStaged ? stagedDiffArgs(base, head) : unstagedDiffArgas(base)
const args = staged ? useStagedChanges(base) : useCommittedDiffArgs(base, head)
let stdout = ""

const child = spawn("git", args, { env: process.env })
Expand Down
5 changes: 4 additions & 1 deletion source/runner/_tests/jsonToDSL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ describe("runner/jsonToDSL", () => {

it("should call LocalGit with correct base", async () => {
await jsonToDSL(dsl as DangerDSLJSONType, new FakeCI({}))
expect(localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD")
expect(localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD", undefined)
dsl.settings.cliArgs.staged = true
await jsonToDSL(dsl as DangerDSLJSONType, new FakeCI({}))
expect(localGetDiff).toHaveBeenLastCalledWith("develop", "HEAD", true)
})

it("should expose BitBucketServerAPI if `dsl.bitbucket_server` is passed in", async () => {
Expand Down

0 comments on commit dee1ec2

Please sign in to comment.