Skip to content

Commit 89fabea

Browse files
authored
Merge pull request #1366 from snowe2010/feature/fix-remote-dangerfile-parsing
Allow parsing remote files as typescript
2 parents 4321bc7 + 0a308d2 commit 89fabea

File tree

8 files changed

+34
-16
lines changed

8 files changed

+34
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<!-- Your comment below this -->
1818

19+
- Fix remote dangerfiles always parsing as JavaScript. TypeScript files should now work properly. - [@snowe2010]
1920
- Add support for BitBucket Cloud Repository Access Token - [@thawankeane]
2021

2122
<!-- Your comment above this -->
@@ -2029,6 +2030,7 @@ Not usable for others, only stubs of classes etc. - [@orta]
20292030
[@sgtcoolguy]: https://github.com/sgtcoolguy
20302031
[@sharkysharks]: https://github.com/sharkysharks
20312032
[@shyim]: https://github.com/shyim
2033+
[@snowe2010]: https://github.com/snowe2010
20322034
[@sogame]: https://github.com/sogame
20332035
[@soyn]: https://github.com/Soyn
20342036
[@stefanbuck]: https://github.com/stefanbuck

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"
@@ -95,8 +94,7 @@ if (program.args.length === 0) {
9594
const isJSON = app.js || app.json
9695
const note = isJSON ? console.error : console.log
9796
note(`Starting Danger PR on ${pr.repo}#${pr.pullRequestNumber}`)
98-
99-
if (customProcess || isJSON || validateDangerfileExists(dangerfilePath(program))) {
97+
if (customProcess || isJSON || dangerfilePath(program)) {
10098
if (!customProcess) {
10199
d(`executing dangerfile at ${dangerfilePath(program)}`)
102100
}

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)