Skip to content

Commit f9c165f

Browse files
authored
Merge pull request #1436 from danger/init-improv
Improve the init CLI
2 parents 9d9bd6e + 8733b5d commit f9c165f

File tree

6 files changed

+102
-25
lines changed

6 files changed

+102
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<!-- Your comment below this -->
1818
<!-- Your comment above this -->
1919

20+
21+
## 12.1.0
22+
23+
It's been 7 years since I looked at `danger init` and err, the world of CI has changed quite a bit since then. So, Danger JS's
24+
`init` command now knows that GitHub Actions exists and will correctly offer some advice on how to set up a Dangerfile for it. - [@orta]
25+
2026
## 12.0.0
2127

2228
Bumping to 12.x because we've raised the minimum to node version from 14 to 18. This is due to some of our dependencies

source/commands/danger-init.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import program from "commander"
66
import * as fs from "fs"
77

88
import { generateDefaultDangerfile } from "./init/default-dangerfile"
9-
import { travis, circle, unsure } from "./init/add-to-ci"
9+
import { travis, circle, unsure, githubActions } from "./init/add-to-ci"
1010
import { generateInitialState, createUI } from "./init/state-setup"
1111
import { InitUI, InitState, highlight } from "./init/interfaces"
1212

@@ -42,9 +42,15 @@ const go = async (app: App) => {
4242
state.isAnOSSRepo = isOSS
4343

4444
await setupDangerfile(ui, state)
45-
await setupGitHubAccount(ui, state)
46-
await setupGHAccessToken(ui, state)
47-
await addToCI(ui, state)
45+
46+
if (state.isAnOSSRepo) {
47+
await setupGitHubAccount(ui, state)
48+
await setupGHAccessToken(ui, state)
49+
await addToCI(ui, state)
50+
} else {
51+
// We can use the private github workflow for private repos
52+
await githubActions(ui, state)
53+
}
4854
await wrapItUp(ui, state)
4955
await thanks(ui, state)
5056
}
@@ -65,9 +71,9 @@ const showTodoState = async (ui: InitUI) => {
6571
await ui.pause(0.6)
6672
ui.say(` - [ ] Create a Dangerfile and add a few simple rules.`)
6773
await ui.pause(0.6)
68-
ui.say(` - [ ] Create a GitHub account for Danger to use, for messaging.`)
74+
ui.say(` - [ ] Potentially create a GitHub account for Danger to use, for messaging.`)
6975
await ui.pause(0.6)
70-
ui.say(` - [ ] Set up an access token for Danger.`)
76+
ui.say(` - [ ] Set up an access token for Danger to comment with.`)
7177
await ui.pause(0.6)
7278
ui.say(" - [ ] Set up Danger to run on your CI.\n")
7379

@@ -202,21 +208,20 @@ const wrapItUp = async (ui: InitUI, _state: InitState) => {
202208
await ui.pause(0.6)
203209

204210
const link = (name: string, url: string) => ui.say(" * " + ui.link(name, url))
205-
link("artsy/Emission#dangerfile.ts", "https://github.com/artsy/emission/blob/master/dangerfile.ts")
211+
link("artsy/eigen#dangerfile.ts", "https://github.com/artsy/eigen/blob/master/dangerfile.ts")
206212
link(
207-
"facebook/react-native#danger/dangerfile.js",
208-
"https://github.com/facebook/react-native/blob/master/bots/dangerfile.js"
213+
"facebook/react-native#main/packages/react-native-bots/dangerfile.js",
214+
"https://github.com/facebook/react-native/blob/main/packages/react-native-bots/dangerfile.js"
209215
)
216+
link("mui/material-ui#dangerfile.ts", "https://github.com/mui/material-ui/blob/main/dangerfile.ts#L4")
210217
link(
211-
"apollographql/apollo-client#dangerfile.ts",
212-
"https://github.com/apollographql/apollo-client/blob/master/config/dangerfile.ts"
218+
"styleguidist/react-styleguidist#dangerfile.ts",
219+
"https://github.com/styleguidist/react-styleguidist/blob/master/dangerfile.ts"
213220
)
214221
link(
215-
"styleguidist/react-styleguidist#dangerfile.js",
216-
"https://github.com/styleguidist/react-styleguidist/blob/master/dangerfile.js"
222+
"storybooks/storybook#.ci/danger/dangerfile.ts",
223+
"https://github.com/storybookjs/storybook/blob/master/.ci/danger/dangerfile.ts"
217224
)
218-
link("storybooks/storybook#dangerfle.js", "https://github.com/storybooks/storybook/blob/master/dangerfile.js")
219-
link("ReactiveX/rxjs#dangerfle.js", "https://github.com/ReactiveX/rxjs/blob/master/dangerfile.js")
220225

221226
await ui.pause(1)
222227
}
@@ -225,7 +230,9 @@ const addToCI = async (ui: InitUI, state: InitState) => {
225230
ui.header("Add to CI")
226231

227232
await ui.pause(0.6)
228-
if (state.ciType === "travis") {
233+
if (state.ciType === "gh-actions") {
234+
await githubActions(ui, state)
235+
} else if (state.ciType === "travis") {
229236
await travis(ui, state)
230237
} else if (state.ciType === "circle") {
231238
await circle(ui, state)
@@ -242,10 +249,8 @@ const thanks = async (ui: InitUI, _state: InitState) => {
242249
ui.say("and every who has sent PRs.\n")
243250
ui.say(
244251
"If you like Danger, let others know. If you want to know more, follow " +
245-
highlight("@orta") +
246-
" and " +
247-
highlight("@DangerSystems") +
248-
" on Twitter."
252+
highlight("@[email protected]") +
253+
" on Mastodon!"
249254
)
250255
ui.say("If you don't like something about Danger, help us improve the project - it's all done on volunteer time! xxx")
251256
ui.say("Remember: it's nice to be nice.\n")

source/commands/init/add-to-ci.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
11
import { InitUI, InitState, highlight } from "./interfaces"
2+
import chalk from "chalk"
3+
4+
export const githubActions = async (ui: InitUI, state: InitState) => {
5+
if (!state.isAnOSSRepo) {
6+
ui.say("For your closed-source project, we can use the default GitHub Auth token for posting.")
7+
ui.say("So, you don't need to create a bot account.")
8+
ui.pause(0.5)
9+
10+
ui.say("You will want to add a new step in an existing workflow yaml file.")
11+
ui.pause(0.2)
12+
13+
ui.say("The step should look like this:")
14+
ui.say("")
15+
ui.say("```yaml")
16+
ui.say(chalk.bold(" - name: Run Danger"))
17+
ui.say(chalk.bold(" run: yarn danger ci"))
18+
ui.say(chalk.bold(" env:"))
19+
ui.say(chalk.bold(" GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}`"))
20+
ui.say("```")
21+
ui.say("")
22+
ui.pause(1)
23+
24+
ui.say("This will run Danger on every PR, press return when you have added this to one of your workflows.")
25+
ui.waitForReturn()
26+
return
27+
}
28+
29+
const link = ui.link("issue #918.", "https://github.com/danger/danger-js/issues/918")
30+
ui.say(
31+
`For your OSS project, there are a few options you can find in ${link}. We will continue with using the new GitHub user account here.`
32+
)
33+
ui.pause(0.5)
34+
35+
ui.say("For your OSS project, you need to add a new secret to your workflow in a way that is slightly obfuscated.")
36+
ui.say("This is because GitHub Actions doesn't allow you to use the default GITHUB_TOKEN to update its own comments.")
37+
ui.pause(0.5)
38+
ui.say("This would make Danger very spammy on a Pull Request.")
39+
40+
ui.pause(0.1)
41+
ui.say("To get started, add a new step in an existing workflow file.")
42+
ui.say("The step should look like this:")
43+
ui.say("")
44+
ui.say("```yaml")
45+
ui.say(
46+
chalk.bold(`)
47+
- name: Run Danger
48+
run: |
49+
# Exposing this token is safe because the user of it has no other public repositories
50+
# and has no permission to modify this repository.
51+
TOKEN='ghp_On3bHOkcV2AmVY6'
52+
TOKEN+='56186c3b09b4f86b1c65136769'
53+
DANGER_GITHUB_API_TOKEN=$TOKEN yarn danger ci`)
54+
)
55+
ui.say("```")
56+
ui.say("")
57+
ui.pause(1)
58+
59+
ui.say("This will run Danger on every PR, press return when you have added this to one of your workflows.")
60+
ui.waitForReturn()
61+
}
262

363
export const travis = async (ui: InitUI, state: InitState) => {
464
// https://travis-ci.org/artsy/eigen/settings

source/commands/init/default-dangerfile.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ export const generateDefaultDangerfile = (state: InitState) => {
3434
3535
${rules.join("\n")}
3636
`
37-
return formatDangerfile(dangerfile, dangerfileState)
37+
return formatDangerfile(dangerfile, state, dangerfileState)
3838
}
3939

40-
export const formatDangerfile = (dangerfile: string, dangerfileState: any) => {
40+
export const formatDangerfile = (
41+
dangerfile: string,
42+
initState: InitState,
43+
dangerfileState: ReturnType<typeof generateDangerfileState>
44+
) => {
4145
if (dangerfileState.hasPrettier) {
4246
// eslint-disable-next-line @typescript-eslint/no-require-imports
4347
const { format } = require("prettier")
4448
// Get package settings
4549
const localPrettier = fs.existsSync("package.json") && JSON.parse(fs.readFileSync("package.json", "utf8")).prettier
4650
// Always include this
47-
const always = { editorconfig: true }
51+
const always = { editorconfig: true, parser: "typescript", filepath: process.cwd() + " /" + initState.filename }
4852
const settings = localPrettier ? { ...always, ...localPrettier } : always
4953

5054
return format(dangerfile, settings)

source/commands/init/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface InitState {
1717
hasSetUpAccountToken: boolean
1818

1919
repoSlug: string | null
20-
ciType: "travis" | "circle" | "unknown"
20+
ciType: "gh-actions" | "travis" | "circle" | "unknown"
2121
isGitHub: boolean
2222
}
2323

source/commands/init/state-setup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export const generateInitialState = (osProcess: NodeJS.Process): InitState => {
3838
const isBabel = checkForBabel()
3939
const hasTravis = fs.existsSync(".travis.yml")
4040
const hasCircle = fs.existsSync("circle.yml")
41-
const ciType = hasTravis ? "travis" : hasCircle ? "circle" : "unknown"
41+
const hasGitHubActions = fs.existsSync(".github/") && fs.existsSync(".github/workflows")
42+
43+
const ciType = hasGitHubActions ? "gh-actions" : hasTravis ? "travis" : hasCircle ? "circle" : "unknown"
4244
const repoSlug = getRepoSlug()
4345
const isGitHub = !!repoSlug
4446

0 commit comments

Comments
 (0)