Skip to content

Commit 51bdc14

Browse files
committed
Allow parsing remote files as typescript
Currently, the transpiler.ts makes an assumption that the filename will always end with .ts, but this is incorrect in the case of remote dangerfiles that have refs attached, for example abc/123/dangerfile.ts?feature/branch Due to this, typescript parsing will always fail for remote files. This commit adds a new parameter to the transpiler function, which parses the filename differently if the file is a remote one. This change also had to cascade up the stack to the runDangerfileEnvironment function, where we now accept a set of tuples, where the first is the filename, and the second is if it is remote or not. The only place the remote flag is being set currently is in the GitHub runner, where we download the remote dangerfile. Finally, the pr subcommand failed to use the remote feature correctly, since the file would never exist when the command is first run. The check for the file has been removed, simply leaving behind the same check that is everywhere else.
1 parent f8dc6b5 commit 51bdc14

7 files changed

Lines changed: 32 additions & 16 deletions

File tree

source/commands/danger-pr.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import prettyjson from "prettyjson"
77
import { FakeCI } from "../ci_source/providers/Fake"
88
import { pullRequestParser } from "../platforms/pullRequestParser"
99
import { dangerfilePath } from "./utils/fileUtils"
10-
import validateDangerfileExists from "./utils/validateDangerfileExists"
1110
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
1211
import { jsonDSLGenerator } from "../runner/dslGenerator"
1312
import { prepareDangerDSL } from "./utils/runDangerSubprocess"
@@ -94,8 +93,7 @@ if (program.args.length === 0) {
9493
const isJSON = app.js || app.json
9594
const note = isJSON ? console.error : console.log
9695
note(`Starting Danger PR on ${pr.repo}#${pr.pullRequestNumber}`)
97-
98-
if (customProcess || isJSON || validateDangerfileExists(dangerfilePath(program))) {
96+
if (customProcess || isJSON || dangerfilePath(program)) {
9997
if (!customProcess) {
10098
d(`executing dangerfile at ${dangerfilePath(program)}`)
10199
}

source/commands/danger-runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const run = (config: SharedCLI) => async (jsonString: string) => {
6262
if (platform.executeRuntimeEnvironment) {
6363
await platform.executeRuntimeEnvironment(inline.runDangerfileEnvironment, dangerFile, runtimeEnv)
6464
} else {
65-
await inline.runDangerfileEnvironment([dangerFile], [undefined], runtimeEnv)
65+
await inline.runDangerfileEnvironment([[dangerFile, false]], [undefined], runtimeEnv)
6666
}
6767
}
6868

source/platforms/GitHub.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ const executeRuntimeEnvironment = async (
121121
// dangerfile comes from the web, and do all the prefixing etc
122122
let path: string
123123
let content: string
124+
let remote: boolean = false
124125
if (existsSync(dangerfilePath)) {
125126
path = dangerfilePath
126127
content = readFileSync(dangerfilePath, "utf8")
@@ -142,8 +143,9 @@ const executeRuntimeEnvironment = async (
142143
// Chop off the danger import
143144
const newDangerfile = cleanDangerfile(dangerfileContent)
144145

146+
remote = true
145147
// Cool, transpile it into something we can run
146-
content = transpiler(newDangerfile, dangerfilePath)
148+
content = transpiler(newDangerfile, dangerfilePath, remote)
147149
}
148150

149151
// If there's an event.json - we should always pass it inside the default export
@@ -155,7 +157,7 @@ const executeRuntimeEnvironment = async (
155157
}
156158

157159
// Actually start up[ the runtime evaluation
158-
await start([path], [content], environment, defaultExport)
160+
await start([[path, remote]], [content], environment, defaultExport)
159161

160162
// Undo the runtime
161163
restoreOriginalModuleLoader()

source/runner/Executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class Executor {
9999
// If an eval of the Dangerfile fails, we should generate a
100100
// message that can go back to the CI
101101
try {
102-
results = await this.runner.runDangerfileEnvironment([file], [undefined], runtime)
102+
results = await this.runner.runDangerfileEnvironment([[file, false]], [undefined], runtime)
103103
} catch (error) {
104104
results = this.resultsForError(error as Error)
105105
}

source/runner/runners/inline.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const runAllScheduledTasks = async (results: DangerRuntimeContainer) => {
5555
* @returns {DangerResults} the results of the run
5656
*/
5757
export const runDangerfileEnvironment = async (
58-
filenames: string[],
58+
filenames: [string, boolean][],
5959
originalContents: (string | undefined)[] | undefined,
6060
environment: DangerContext,
6161
injectedObjectToExport?: any,
@@ -84,11 +84,16 @@ export const runDangerfileEnvironment = async (
8484
// Loop through all files and their potential contents, they edit
8585
// results inside the env, so no need to keep track ourselves
8686

87-
for (const filename of filenames) {
88-
const index = filenames.indexOf(filename)
89-
const originalContent = (originalContents && originalContents[index]) || fs.readFileSync(filename, "utf8")
87+
for (let index = 0; index < filenames.length; index++) {
88+
const [filename, remote] = filenames[index]
89+
let fn: string = filename
90+
if (remote) {
91+
d(`File ${filename} is a remote dangerfile`)
92+
fn = filename.split("@")[0]
93+
}
94+
const originalContent = (originalContents && originalContents[index]) || fs.readFileSync(fn, "utf8")
9095
let content = cleanDangerfile(originalContent)
91-
let compiled = compile(content, filename)
96+
let compiled = compile(content, filename, remote)
9297

9398
try {
9499
// Move all the DSL attributes into the global scope

source/runner/runners/runner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ export interface DangerRunner {
66
* Executes a Dangerfile at a specific path, with a context.
77
* The values inside a Danger context are applied as globals to the Dangerfiles runtime.
88
*
9-
* @param {string[]} filenames a set of file paths for the dangerfile
9+
* @param {[string, boolean][]} filenames a set of tuples of file paths for the dangerfile, with a boolean indicating
10+
* if it is a remote path
1011
* @param {string[] | undefined[]} originalContents optional, the JS pre-compiled
1112
* @param {DangerContext} environment the results of createDangerfileRuntimeEnvironment
1213
* @param {any | undefined} injectedObjectToExport an optional object for passing into default exports
1314
* @returns {DangerResults} the results of the run
1415
*/
1516
runDangerfileEnvironment: (
16-
filenames: string[],
17+
filenames: [string, boolean][],
1718
originalContents: string[] | undefined[] | undefined,
1819
environment: any,
1920
injectedObjectToExport?: any

source/runner/runners/utils/transpiler.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,33 @@ export const babelify = (content: string, filename: string, extraPlugins: string
183183
return result.code
184184
}
185185

186-
export default (code: string, filename: string) => {
186+
export default (code: string, filename: string, remoteFile: boolean = false) => {
187187
if (!hasChecked) {
188188
checkForNodeModules()
189189
}
190190

191-
const filetype = path.extname(filename)
191+
let filetype: string
192+
if (remoteFile) {
193+
d(`Parsing the file from the remote reference ${filename}`)
194+
let [file, _] = filename.split("@")
195+
filetype = path.extname(file)
196+
} else {
197+
filetype = path.extname(filename)
198+
}
192199
const isModule = filename.includes("node_modules")
193200
if (isModule) {
194201
return code
195202
}
196203

197204
let result = code
198205
if (hasNativeTypeScript && filetype.startsWith(".ts")) {
206+
d("compiling with typescript")
199207
result = typescriptify(code, path.dirname(filename))
200208
} else if (hasBabel && hasBabelTypeScript && filetype.startsWith(".ts")) {
209+
d("compiling as typescript with babel")
201210
result = babelify(code, filename, [`${babelPackagePrefix}plugin-transform-typescript`])
202211
} else if (hasBabel && filetype.startsWith(".js")) {
212+
d("babelifying as javascript")
203213
result = babelify(code, filename, hasFlow ? [`${babelPackagePrefix}plugin-transform-flow-strip-types`] : [])
204214
}
205215

0 commit comments

Comments
 (0)