Skip to content

Commit

Permalink
More init
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Dec 5, 2017
1 parent 4277d35 commit 8450f27
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
45 changes: 23 additions & 22 deletions source/commands/danger-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ const go = async (app: App) => {
const highlight = chalk.bold.yellow

const showTodoState = async (ui: InitUI, state: InitState) => {
ui.say("We need to do the following:\n")
ui.say("Welcome to Danger Init - this will take you through setting up Danger for this project.")
ui.say("There are four main steps we need to do:\n")
await ui.pause(0.6)
ui.say(` - [${state.hasCreatedDangerfile ? "x" : " "}] Create a Dangerfile and add a few simple rules.`)
await ui.pause(0.6)
ui.say(` - [${state.hasSetUpAccount ? "x" : " "}] Create a GitHub account for Danger to use, for messaging.`)
await ui.pause(0.6)
ui.say(` - [${state.hasSetUpAccountToken ? "x" : " "}] Set up an access token for Danger.`)
await ui.pause(0.6)
ui.say(" - [ ] Set up Danger to run on your CI.\n\n")
ui.say(" - [ ] Set up Danger to run on your CI.\n")
}

const setupDangerfile = async (ui: InitUI, state: InitState) => {
Expand Down Expand Up @@ -153,17 +154,17 @@ const setupGitHubAccount = async (ui: InitUI, state: InitState) => {
ui.header("Step 2: Creating a GitHub account")

ui.say("In order to get the most out of Danger, I'd recommend giving it the ability to post in")
ui.say("the code-review comment section.\n")
ui.say("the code-review comment section.")

ui.say("\n" + ui.link("GitHub Home", "https://github.com") + "\n")

await ui.pause(1)

ui.say(
`IMO, it's best to do this by using the private mode of your browser. Create an account like: ${highlight(
state.botName
)},`
)
ui.say(`and don't forget a cool robot avatar too.\n`)
ui.say(`IMO, it's best to do this by using the private mode of your browser.`)
ui.say(`Create an account like: ${highlight(state.botName)} and don't forget a cool robot avatar too.\n`)

await ui.pause(1)
ui.say("Here are great resources for creative commons images of robots:")
ui.say("Here are great resources for creative-commons images of robots:\n")
const flickr = ui.link("flickr", "https://www.flickr.com/search/?text=robot&license=2%2C3%2C4%2C5%2C6%2C9")
const googImages = ui.link(
"googleimages",
Expand All @@ -174,6 +175,9 @@ const setupGitHubAccount = async (ui: InitUI, state: InitState) => {
ui.say("")
await ui.pause(1)

noteAboutClickingLinks(ui, state)
await ui.pause(1)

if (state.isAnOSSRepo) {
ui.say(`${state.botName} does not need privileged access to your repo or org. This is because Danger will only`)
ui.say("be writing comments, and you do not need special access for that.")
Expand All @@ -182,7 +186,6 @@ const setupGitHubAccount = async (ui: InitUI, state: InitState) => {
ui.say("to read and comment on.")
}

// note_about_clicking_links
await ui.pause(1)
ui.say("\nCool, please press return when you have your account ready (and you've verified the email...)")
ui.waitForReturn()
Expand All @@ -195,11 +198,9 @@ const setupGHAccessToken = async (ui: InitUI, state: InitState) => {
ui.say("\n" + ui.link("New GitHub Token", "https://github.com/settings/tokens/new"))
await ui.pause(1)

state.isAnOSSRepo =
ui.askWithAnswers(
"For token access rights, I need to know if this is for an Open Source or private project\n",
["Open Source", "Private Repo"]
) === "Open Source"
ui.say("For token access rights, I need to know if this is for an Open Source or private project\n")
// TODO: Check for this via the API instead!
state.isAnOSSRepo = ui.askWithAnswers("", ["Open Source", "Private Repo"]) === "Open Source"

if (state.isAnOSSRepo) {
ui.say("\n\nFor Open Source projects, I'd recommend giving the token the smallest scope possible.")
Expand Down Expand Up @@ -228,12 +229,12 @@ const setupGHAccessToken = async (ui: InitUI, state: InitState) => {
ui.waitForReturn()
}

// const noteAboutClickingLinks = (ui: InitUI, state: State) => {
// const modifier_key = state.isMac ? "cmd ( ⌘ )" : "ctrl"
// const clicks = state.isWindows ? "clicking" : "double clicking"

// ui.say(`Note: Holding ${modifier_key} and ${clicks} a link will open it in your browser.`)
// }
const noteAboutClickingLinks = (ui: InitUI, state: InitState) => {
const modifier_key = state.isMac ? "cmd ( ⌘ )" : "ctrl"
const clicks = state.isWindows || state.supportsHLinks ? "clicking" : "double clicking"
const sidenote = chalk.italic.bold("Sidenote: ")
ui.say(`${sidenote} Holding ${modifier_key} and ${clicks} a link will open it in your browser.\n`)
}

const wrapItUp = async (ui: InitUI, _state: InitState) => {
ui.header("Useful info")
Expand Down
43 changes: 41 additions & 2 deletions source/commands/init/default-dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InitState } from "../danger-init"

const generateDangerfileState = () => ({
hasCHANGELOG: fs.existsSync("CHANGELOG.md"),
hasSeparateTestFolder: fs.existsSync("tests"),
hasPrettier: fs.existsSync("node_modules/.bin/prettier") || fs.existsSync("node_modules/.bin/prettier.bin"),
})

Expand All @@ -12,15 +13,31 @@ export const generateDefaultDangerfile = (state: InitState) => {
let rules: string[] = []

rules.push(descriptionRule)

if (dangerfileState.hasCHANGELOG) {
rules.push(changelogRule)
}

if (!state.isAnOSSRepo) {
rules.push(assignSomeone)
}

if (!dangerfileState.hasSeparateTestFolder) {
if (fs.existsSync("src")) {
rules.push(checkSeparateTestsFolder("src"))
} else if (fs.existsSync("lib")) {
rules.push(checkSeparateTestsFolder("lib"))
}
}

const dangerfile = `${createImport(state)}
${rules.join("\n")}
`
return formatDangerfile(dangerfile, dangerfileState)
}

export const formatDangerfile = (dangerfile: string, dangerfileState: any) => {
if (dangerfileState.hasPrettier) {
// tslint:disable-next-line:no-require-imports
const { format } = require("prettier")
Expand All @@ -41,16 +58,38 @@ export const createImport = (state: InitState) => {
export const changelogRule = `
// Check for a CHANGELOG entry
const hasChangelog = danger.git.modified_files.includes('CHANGELOG.md')
const isTrivial = (danger.github.pr.body + danger.github.pr.title).includes('#trivial')
const description = danger.github.pr.body + danger.github.pr.title
const isTrivial = description.includes('#trivial')
if (!hasChangelog && !isTrivial) {
warn('Please add a changelog entry for your changes.')
}
`

export const descriptionRule = `
// Check for description
// No PR is too small to include a decription of why you made a change
if (danger.github.pr.body.length < 10) {
warn('Please include a description of your PR changes.');
}
`

export const assignSomeone = `
// Check that someone has been assigned to this PR
if (danger.github.pr.assignee === null) {
warn('Please assign someone to merge this PR, and optionally include people who should review.');
}
`
// For projects not using Jest
export const checkSeparateTestsFolder = (src: string) => `
// Request changes to ${src} also include changes to tests.
const allChangedFiles = danger.git.modified_files.concat(danger.git.created_files)
const modifiedAppFiles = allChangedFiles.filter(p => includes(p, '${src}/'))
const modifiedTestFiles = allChangedFiles.filter(p => includes(p, 'test/'))
const hasAppChanges = modifiedAppFiles.length > 0;
const hasTestChanges = modifiedTestFiles.length > 0;
if (hasAppChanges && !hasTestChanges) {
warn('This PR does not include any changes to tests, even though it affects app code.');
}
`

0 comments on commit 8450f27

Please sign in to comment.