Skip to content

Commit

Permalink
Hook up the JSONDSL -> DSL process
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Oct 16, 2017
1 parent fcbe5b3 commit 6b2cbf9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
4 changes: 2 additions & 2 deletions source/commands/danger-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
import getRuntimeCISource from "./utils/getRuntimeCISource"

import inlineRunner from "../runner/runners/inline"
import { dsLGenerator } from "../runner/dslGenerator"
import { jsonDSLGenerator } from "../runner/dslGenerator"

// Given the nature of this command, it can be tricky to test, so I use a command like this:
//
Expand Down Expand Up @@ -65,7 +65,7 @@ async function run() {
verbose: app.verbose,
}

const dangerDSL = await dsLGenerator(platform)
const dangerDSL = await jsonDSLGenerator(platform)
const processInput = prepareDangerDSL(dangerDSL)

if (!subprocessName) {
Expand Down
2 changes: 1 addition & 1 deletion source/dsl/DangerDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface DangerDSLType {
export class DangerDSL {
public readonly github: GitHubDSL

constructor(platformDSL: any, public readonly git: GitDSL, public readonly utils: DangerUtilsDSL) {
constructor(platformDSL: any, public readonly git: GitJSONDSL, public readonly utils: DangerUtilsDSL) {
// As GitLab etc support is added this will need to be changed
this.github = platformDSL
}
Expand Down
22 changes: 16 additions & 6 deletions source/platforms/github/GitHubGit.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { GitDSL, JSONPatchOperation, GitJSONDSL } from "../../dsl/GitDSL"
import { GitHubCommit, GitHubJSONDSL, GitHubDSL } from "../../dsl/GitHubDSL"
import { GitHubCommit, GitHubDSL } from "../../dsl/GitHubDSL"
import { GitCommit } from "../../dsl/Commit"

import { GitHubAPI } from "../github/GitHubAPI"

import * as GitHub from "github"

import * as os from "os"

import * as parseDiff from "parse-diff"
Expand Down Expand Up @@ -58,7 +56,7 @@ export default async function gitDSLForGitHub(api: GitHubAPI): Promise<GitJSONDS
}
}

export const gitJSONToGitDSL = (github: GitHubDSL, api: GitHub, json: GitJSONDSL): GitDSL => {
export const gitJSONToGitDSL = (github: GitHubDSL, json: GitJSONDSL): GitDSL => {
/**
* Takes a filename, and pulls from the PR the two versions of a file
* where we then pass that off to the rfc6902 JSON patch generator.
Expand Down Expand Up @@ -164,8 +162,16 @@ export const gitJSONToGitDSL = (github: GitHubDSL, api: GitHub, json: GitJSONDSL
* @param filename File path for the diff
*/
const diffForFile = async (filename: string) => {
// const diff = await api.getPullRequestDiff()
const diff = await api.pullRequests.get()
// TODO: Remove GitHubAPI by switching entirely to node-github
// See https://github.com/octokit/node-github/issues/602

const ghAPI = new GitHubAPI(
{ repoSlug: github.pr.head.repo.full_name, pullRequestID: String(github.pr.number) },
process.env["DANGER_GITHUB_API_TOKEN"]
)

const diff = await ghAPI.getPullRequestDiff()

const fileDiffs: any[] = parseDiff(diff)
const structuredDiff = fileDiffs.find((diff: any) => diff.from === filename || diff.to === filename)
if (!structuredDiff) {
Expand All @@ -178,12 +184,16 @@ export const gitJSONToGitDSL = (github: GitHubDSL, api: GitHub, json: GitJSONDSL

return {
before: await github.utils.fileContents(filename, github.pr.base.repo.full_name, github.pr.base.sha),

after: await github.utils.fileContents(filename, github.pr.head.repo.full_name, github.pr.head.sha),

diff: allLines.map(getContent).join(os.EOL),

added: allLines
.filter(byType("add"))
.map(getContent)
.join(os.EOL),

removed: allLines
.filter(byType("del"))
.map(getContent)
Expand Down
19 changes: 18 additions & 1 deletion source/platforms/github/GitHubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,24 @@ const utils = (pr: GitHubPRDSL, api: GitHub): GitHubUtilsDSL => {

return {
fileLinks,
fileContents: api.fileContents,
fileContents: async (path: string, repoSlug?: string, ref?: string): Promise<string> => {
// Use the current state of PR if no repo/ref is passed
if (!repoSlug || !ref) {
repoSlug = pr.head.repo.full_name
ref = pr.head.ref
}

// api.getFileContents(path, repoSlug, ref)
const data = await api.repos.getContent({
ref,
path,
repo: repoSlug.split("\\")[1],
owner: repoSlug.split("\\")[0],
})

const buffer = new Buffer(data.content, "base64")
return buffer.toString()
},
}
}

Expand Down
4 changes: 3 additions & 1 deletion source/platforms/github/_tests/_github_git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FakeCI } from "../../../ci_source/providers/Fake"
import { readFileSync } from "fs"
import { resolve, join as pathJoin } from "path"
import { EOL } from "os"
import { gitJSONToGitDSL } from "../GitHubGit"

const fixtures = resolve(__dirname, "..", "..", "_tests", "fixtures")

Expand Down Expand Up @@ -42,7 +43,8 @@ describe("the dangerfile gitDSL", async () => {
})

it("sets the modified/created/deleted", async () => {
const gitDSL = await github.getPlatformGitRepresentation()
const gitJSONDSL = await github.getPlatformGitRepresentation()
const gitDSL = gitJSONToGitDSL(gitJSONDSL)

expect(gitDSL.modified_files).toEqual([
"CHANGELOG.md",
Expand Down
4 changes: 2 additions & 2 deletions source/platforms/platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Env, CISource } from "../ci_source/ci_source"
import { GitDSL } from "../dsl/GitDSL"
import { GitJSONDSL } from "../dsl/GitDSL"
import { GitHub } from "./GitHub"
import { GitHubAPI } from "./github/GitHubAPI"

Expand Down Expand Up @@ -34,7 +34,7 @@ export interface Platform {
/** Pulls in the platform specific metadata for inspection */
getPlatformDSLRepresentation: () => Promise<any>
/** Pulls in the Code Review Diff, and offers a succinct user-API for it */
getPlatformGitRepresentation: () => Promise<GitDSL>
getPlatformGitRepresentation: () => Promise<GitJSONDSL>
/** Creates a comment on the PR */
createComment: (body: string) => Promise<any>
/** Delete the main Danger comment */
Expand Down
7 changes: 5 additions & 2 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as debug from "debug"
import * as chalk from "chalk"
import { sentence, href } from "./DangerUtils"
import { DangerRunner } from "./runners/runner"
import { jsonToDSL } from "./jsonToDSL"
import { jsonDSLGenerator } from "./dslGenerator"

// This is still badly named, maybe it really should just be runner?

Expand Down Expand Up @@ -45,8 +47,9 @@ export class Executor {
* @returns {DangerfileRuntimeEnv} A runtime environment to run Danger in
*/
async setupDanger(): Promise<DangerContext> {
const dsl = await this.dslForDanger()
const context = contextForDanger(dsl)
const dsl = await jsonDSLGenerator(this.platform)
const realDSL = await jsonToDSL(dsl)
const context = contextForDanger(realDSL)
return await this.runner.createDangerfileRuntimeEnvironment(context)
}

Expand Down
2 changes: 1 addition & 1 deletion source/runner/dslGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Platform } from "../platforms/platform"
import { DangerDSLJSONType } from "../dsl/DangerDSL"

export const dsLGenerator = async (platform: Platform): Promise<DangerDSLJSONType> => {
export const jsonDSLGenerator = async (platform: Platform): Promise<DangerDSLJSONType> => {
const git = await platform.getPlatformGitRepresentation()
const platformDSL = await platform.getPlatformDSLRepresentation()

Expand Down
2 changes: 1 addition & 1 deletion source/runner/jsonToDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sentence, href } from "./DangerUtils"
export const jsonToDSL = async (dsl: DangerDSLJSONType): Promise<DangerDSLType> => {
const api = githubAPIForDSL(dsl)
const github = githubJSONToGitHubDSL(dsl.github, api)
const git = gitJSONToGitDSL(github, api, dsl.git)
const git = gitJSONToGitDSL(github, dsl.git)
return {
git,
github: github,
Expand Down

0 comments on commit 6b2cbf9

Please sign in to comment.