This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
forked from danger/peril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
danger_runner.ts
136 lines (111 loc) · 4.22 KB
/
danger_runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Platform } from "danger/distribution/platforms/platform"
import winston from "../logger"
import { GitHubInstallation, GitHubInstallationSettings } from "../db"
import { RootObject as PR } from "../github/events/types/pull_request_opened.types"
import { getCISourceForEnv } from "danger/distribution/ci_source/get_ci_source"
import { DangerResults } from "danger/distribution/dsl/DangerResults"
import { GitHub } from "danger/distribution/platforms/GitHub"
import { GitHubAPI } from "danger/distribution/platforms/github/GitHubAPI"
import { runDangerfileEnvironment } from "danger/distribution/runner/DangerfileRunner"
import { Executor, ExecutorOptions } from "danger/distribution/runner/Executor"
import * as os from "os"
import * as path from "path"
import * as write from "write-file-promise"
import { dsl } from "./danger_run"
import perilPlatform from "./peril_platform"
/** Logs */
const log = (message: string) => {
winston.info(`[runner] - ${message}`)
}
// What does the Peril object look like inside the runtime
// TODO: Expose this usefully somehow
export interface PerilDSL {
// A list of accepted ENV vars into the peril runtime, configurable in settings.
env: any | false
}
/**
* The single function to run danger against an installation
*/
export async function runDangerForInstallation(
contents: string,
filepath: string,
api: GitHubAPI | null,
type: dsl,
installationSettings: GitHubInstallationSettings,
dangerDSL?: any
) {
// We need this for things like repo slugs, PR IDs etc
// https://github.com/danger/danger-js/blob/master/source/ci_source/ci_source.js
log(`Running Danger`)
const gh = api ? new GitHub(api) : null
const platform = perilPlatform(type, gh, dangerDSL)
const exec = await executorForInstallation(platform)
const randomName = Math.random().toString(36)
const localDangerfilePath = path.resolve("./" + "danger-" + randomName + path.extname(filepath))
const peril = perilObjectForInstallation(installationSettings, process.env)
return await runDangerAgainstFile(localDangerfilePath, contents, exec, peril)
}
export const perilObjectForInstallation = (settings: GitHubInstallationSettings, environment: any): PerilDSL => ({
env: settings.env_vars && Object.assign({}, ...settings.env_vars.map(k => ({ [k]: environment[k] }))),
})
/**
* Sets up the custom peril environment and runs danger against a local file
*/
export async function runDangerAgainstFile(filepath: string, contents: string, exec: Executor, peril: PerilDSL) {
const runtimeEnv = await exec.setupDanger()
// This can expand with time
if (runtimeEnv.sandbox) {
runtimeEnv.sandbox.peril = peril
}
// runtimeEnv.rquire.root = dangerfile_runtime_env
let results: DangerResults
try {
results = await runDangerfileEnvironment(filepath, contents, runtimeEnv)
} catch (error) {
results = resultsForCaughtError(filepath, contents, error)
}
return results
}
/** Returns Markdown results to post if an exception is raised during the danger run */
const resultsForCaughtError = (file: string, contents: string, error: Error): DangerResults => {
const failure = `Danger failed to run \`${file}\`.`
const errorMD = `## Error ${error.name}
\`\`\`
${error.message}
${error.stack}
\`\`\`
### Dangerfile
\`\`\`ts
${contents}
\`\`\`
`
return { fails: [{ message: failure }], warnings: [], markdowns: [errorMD], messages: [] }
}
/**
* Let's Danger handle the results, as well as providing a hook for Peril to do work
*/
export async function handleDangerResults(results: DangerResults, exec: Executor) {
return await exec.handleResults(results)
}
/**
* Generates a Danger Executor based on the installation's context
*/
export function executorForInstallation(platform: Platform) {
// We need this for things like repo slugs, PR IDs etc
// https://github.com/danger/danger-js/blob/master/source/ci_source/ci_source.js
const source = {
env: process.env,
isCI: true,
isPR: true,
name: "Peril",
pullRequestID: "not used",
repoSlug: "not used",
supportedPlatforms: [],
}
const execConfig = {
stdoutOnly: false,
verbose: !!process.env.LOG_FETCH_REQUESTS,
}
// Source can be removed in the next release of Danger
return new Executor(source, platform, execConfig)
}