From e401d1da7cecf725df5c68e79d8e3fc78f29a938 Mon Sep 17 00:00:00 2001 From: James Treanor Date: Mon, 8 Jul 2019 15:00:10 +0100 Subject: [PATCH 1/2] Use createPRDSL instead of createPRJSONDSL in runPRRun --- .../events/_tests/_github_runner-prs.test.ts | 2 +- .../_pr-create-fixture.test.ts.snap | 33 ------------------- .../_tests/_pr-create-fixture.test.ts | 2 ++ .../PerilRunnerPRBootStrapExample.json | 27 --------------- api/source/github/events/handlers/pr.ts | 4 +-- 5 files changed, 5 insertions(+), 63 deletions(-) diff --git a/api/source/github/events/_tests/_github_runner-prs.test.ts b/api/source/github/events/_tests/_github_runner-prs.test.ts index 1eab7614..1f454aff 100644 --- a/api/source/github/events/_tests/_github_runner-prs.test.ts +++ b/api/source/github/events/_tests/_github_runner-prs.test.ts @@ -25,7 +25,7 @@ import { runPRRun } from "../handlers/pr" const apiFixtures = resolve(__dirname, "fixtures") const fixture = (file: string) => JSON.parse(readFileSync(resolve(apiFixtures, file), "utf8")) -jest.mock("../createPRDSL", () => ({ createPRJSONDSL: () => Promise.resolve({}) })) +jest.mock("../createPRDSL", () => ({ createPRDSL: () => Promise.resolve({}) })) it("runs a Dangerfile for a PR with a local", async () => { mockContents.mockImplementationOnce(() => Promise.resolve("fail('dangerfile')")) diff --git a/api/source/github/events/handlers/_tests/__snapshots__/_pr-create-fixture.test.ts.snap b/api/source/github/events/handlers/_tests/__snapshots__/_pr-create-fixture.test.ts.snap index 85df5c3d..311f1cb1 100644 --- a/api/source/github/events/handlers/_tests/__snapshots__/_pr-create-fixture.test.ts.snap +++ b/api/source/github/events/handlers/_tests/__snapshots__/_pr-create-fixture.test.ts.snap @@ -12,39 +12,6 @@ Object { ], "payload": Object { "dsl": Object { - "git": Object { - "JSONDiffForFile": [Function], - "JSONPatchForFile": [Function], - "commits": Array [ - Object { - "author": Object { - "date": "1", - "email": "1", - "name": "1", - }, - "committer": Object { - "date": "1", - "email": "1", - "name": "1", - }, - "message": "456", - "sha": "123", - "tree": Object { - "sha": "123", - "url": "123", - }, - "url": "123", - }, - ], - "created_files": Array [], - "deleted_files": Array [], - "diffForFile": [Function], - "fileMatch": [Function], - "linesOfCode": [Function], - "modified_files": Array [], - "structuredDiffForFile": [Function], - }, - "github": Object {}, "settings": Object { "cliArgs": Object {}, "github": Object { diff --git a/api/source/github/events/handlers/_tests/_pr-create-fixture.test.ts b/api/source/github/events/handlers/_tests/_pr-create-fixture.test.ts index 9d854c0a..44da2a52 100644 --- a/api/source/github/events/handlers/_tests/_pr-create-fixture.test.ts +++ b/api/source/github/events/handlers/_tests/_pr-create-fixture.test.ts @@ -34,6 +34,8 @@ jest.mock("../../../../danger/peril_platform", () => ({ getPerilPlatformForDSL: () => mockPlatform, })) +jest.mock("../../createPRDSL", () => ({ createPRDSL: () => Promise.resolve({}) })) + const apiFixtures = resolve(__dirname, "../../_tests/fixtures") const fixture = (file: string) => JSON.parse(readFileSync(resolve(apiFixtures, file), "utf8")) diff --git a/api/source/github/events/handlers/_tests/fixtures/PerilRunnerPRBootStrapExample.json b/api/source/github/events/handlers/_tests/fixtures/PerilRunnerPRBootStrapExample.json index 9950e123..4a478b91 100644 --- a/api/source/github/events/handlers/_tests/fixtures/PerilRunnerPRBootStrapExample.json +++ b/api/source/github/events/handlers/_tests/fixtures/PerilRunnerPRBootStrapExample.json @@ -5,33 +5,6 @@ }, "payload": { "dsl": { - "git": { - "modified_files": [], - "created_files": [], - "deleted_files": [], - "commits": [ - { - "sha": "123", - "author": { - "name": "1", - "email": "1", - "date": "1" - }, - "committer": { - "name": "1", - "email": "1", - "date": "1" - }, - "message": "456", - "tree": { - "sha": "123", - "url": "123" - }, - "url": "123" - } - ] - }, - "github": {}, "settings": { "github": { "accessToken": "12345", diff --git a/api/source/github/events/handlers/pr.ts b/api/source/github/events/handlers/pr.ts index b99601c4..75197b43 100644 --- a/api/source/github/events/handlers/pr.ts +++ b/api/source/github/events/handlers/pr.ts @@ -2,7 +2,7 @@ import { DangerResults } from "danger/distribution/dsl/DangerResults" import { DangerRun, RunType } from "../../../danger/danger_run" import { runDangerForInstallation } from "../../../danger/danger_runner" import { canUserWriteToRepo, getGitHubFileContents } from "../../lib/github_helpers" -import { createPRJSONDSL } from "../createPRDSL" +import { createPRDSL } from "../createPRDSL" import { GitHubRunSettings } from "../github_runner" import { Pull_request } from "../types/pull_request_updated.types" import { githubAPIForCommentable } from "../utils/commenting" @@ -57,7 +57,7 @@ export const runPRRun = async ( settings: settings.installationSettings, } - const dangerDSL = await createPRJSONDSL(githubAPI) + const dangerDSL = await createPRDSL(githubAPI) const results = await runDangerForInstallation( eventName, contents, From cf51f3b01a4385cfec0d144458a8160dcc4a2eb2 Mon Sep 17 00:00:00 2001 From: James Treanor Date: Mon, 8 Jul 2019 17:36:36 +0100 Subject: [PATCH 2/2] Fix github.api authentication by passing access token to jsonToDSL --- api/source/github/events/createPRDSL.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/source/github/events/createPRDSL.ts b/api/source/github/events/createPRDSL.ts index 03a2cb7f..1739a6fc 100644 --- a/api/source/github/events/createPRDSL.ts +++ b/api/source/github/events/createPRDSL.ts @@ -14,6 +14,11 @@ import { source } from "../../danger/peril_ci_source" */ export const createPRDSL = async (githubAPI: GitHubAPI) => { const jsonDSL = await createPRJSONDSL(githubAPI) + // Danger JS expects the Github access token to be set on + // settings.github.accessToken, so put it there if we have one + if (githubAPI.token !== undefined) { + jsonDSL.settings.github.accessToken = githubAPI.token + } return await jsonToDSL(jsonDSL, source) }