Class: Danger::Init

Inherits:
Runner
  • Object
show all
Defined in:
lib/danger/commands/init.rb

Instance Attribute Summary collapse

Attributes inherited from Runner

#cork

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Runner

#validate!

Constructor Details

#initialize(argv) ⇒ Init

Returns a new instance of Init.



20
21
22
23
24
25
26
# File 'lib/danger/commands/init.rb', line 20

def initialize(argv)
  @bot_name = File.basename(Dir.getwd).split(".").first.capitalize + "Bot"
  super
  @ui = Interviewer.new(cork)
  ui.no_delay = argv.flag?("impatient", false)
  ui.no_waiting = argv.flag?("mousey", false)
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



11
12
13
# File 'lib/danger/commands/init.rb', line 11

def ui
  @ui
end

Class Method Details

.optionsObject



13
14
15
16
17
18
# File 'lib/danger/commands/init.rb', line 13

def self.options
  [
    ["--impatient", "'I've not got all day here. Don't add any thematic delays please.'"],
    ["--mousey", "'Don't make me press return to continue the adventure.'"]
  ].concat(super)
end

Instance Method Details

#circle_tokenObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/danger/commands/init.rb', line 231

def circle_token
  # https://circleci.com/gh/artsy/eigen/edit#env-vars
  if considered_an_oss_repo?
    ui.say "Before we start, it's important to be up-front. CircleCI only really has one option to support running Danger"
    ui.say "for forks on OSS repos. It is quite a drastic option, and I want to let you know the best place to understand"
    ui.say "the ramifications of turning on a setting I'm about to advise.\n"
    ui.link "https://circleci.com/docs/fork-pr-builds"
    ui.say "TLDR: If you have anything other than Danger config settings in CircleCI, then you should not turn on the setting."
    ui.say "I'll give you a minute to read it..."
    ui.wait_for_return

    ui.say "On danger/danger we turn on " + "Permissive building of fork pull requests".yellow + " this exposes the token to Danger"
    ui.say "You can find this setting at:"
    ui.link "https://circleci.com/gh/#{current_repo_slug}/edit#advanced-settings\n"
    ui.say "I'll hold..."
    ui.wait_for_return
  end

  ui.say "In order to expose an environment variable, go to:"
  ui.link "https://circleci.com/gh/#{current_repo_slug}/edit#env-vars"
  ui.say "The name is " + "DANGER_GITHUB_API_TOKEN".yellow + " and the value is the GitHub Personal Access Token."
end

#considered_an_oss_repo?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/danger/commands/init.rb', line 141

def considered_an_oss_repo?
  @is_open_source == "open"
end

#current_repo_slugObject



145
146
147
148
149
150
151
152
153
154
# File 'lib/danger/commands/init.rb', line 145

def current_repo_slug
  git = GitRepo.new

  author_repo_regexp = %r{(?:[/:])([^/]+/[^/]+)(?:.git)?$}
  last_git_regexp = /.git$/

  matches = git.origins.match(author_repo_regexp)

  matches ? matches[1].gsub(last_git_regexp, "").strip : "[Your/Repo]"
end

#darwin?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'lib/danger/commands/init.rb', line 293

def darwin?
  Gem::Platform.local.os == "darwin"
end

#infoObject



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/danger/commands/init.rb', line 271

def info
  ui.header "Useful info"
  ui.say "- One of the best ways to test out new rules locally is via " + "bundle exec danger pr".yellow + "."
  ui.pause 0.6
  ui.say "- You can have Danger output all of her variables to the console via the " + "--verbose".yellow + " option."
  ui.pause 0.6
  ui.say "- You can look at the following Dangerfiles to get some more ideas:"
  ui.pause 0.6
  ui.link "https://github.com/danger/danger/blob/master/Dangerfile"
  ui.link "https://github.com/artsy/eigen/blob/master/dangerfile.ts"
  ui.pause 1
end


261
262
263
264
265
266
267
268
269
# File 'lib/danger/commands/init.rb', line 261

def note_about_clicking_links
  modifier_key = "ctrl"
  clicks = "clicking"

  modifier_key = "cmd ( āŒ˜ )" if darwin?
  clicks = "double clicking" if darwin? && !ENV["ITERM_SESSION_ID"]

  ui.say "Note: Holding #{modifier_key} and #{clicks} a link will open it in your browser."
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/danger/commands/init.rb', line 28

def run
  ui.say "\nOK, thanks #{ENV['LOGNAME']}, have a seat and we'll get you started.\n".yellow
  ui.pause 1

  show_todo_state
  ui.pause 1.4

  setup_dangerfile
  
  setup_access_token
  setup_danger_ci

  info
  thanks
end

#setup_access_tokenObject



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
137
138
139
# File 'lib/danger/commands/init.rb', line 110

def setup_access_token
  ui.header "Step 3: Configuring a GitHub Personal Access Token"

  ui.say "Here's the link, you should open this in the private session where you just created the new GitHub account"
  ui.link "https://github.com/settings/tokens/new"
  ui.pause 1

  @is_open_source = ui.ask_with_answers("For token access rights, I need to know if this is for an Open Source or Closed Source project\n", ["Open", "Closed"])

  if considered_an_oss_repo?
    ui.say "For Open Source projects, I'd recommend giving the token the smallest scope possible."
    ui.say "This means only providing access to " + "public_repo".yellow + " in the token.\n\n"
    ui.pause 1
    ui.say "This token limits Danger's abilities to just writing comments on OSS projects. I recommend"
    ui.say "this because the token can quite easily be extracted from the environment via pull requests."

    ui.say "\nIt is important that you do not store this token in your repository, as GitHub will automatically revoke it when pushed.\n"
  elsif @is_open_source == "closed"
    ui.say "For Closed Source projects, I'd recommend giving the token access to the whole repo scope."
    ui.say "This means only providing access to " + "repo".yellow + ", and its children in the token.\n\n"
    ui.pause 1
    ui.say "It's worth noting that you " + "should not".bold.white + " re-use this token for OSS repos."
    ui.say "Make a new one for those repos with just " + "public_repo".yellow + "."
    ui.pause 1
    ui.say "Additionally, don't forget to add your new GitHub account as a collaborator to your Closed Source project."
  end

  ui.say "\nšŸ‘, please press return when you have your token set up..."
  ui.wait_for_return
end

#setup_danger_ciObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/danger/commands/init.rb', line 156

def setup_danger_ci
  ui.header "Step 4: Add Danger for your CI"

  uses_travis if File.exist? ".travis.yml"
  uses_circle if File.exist? "circle.yml"
  unsure_ci unless File.exist?(".travis.yml") || File.exist?(".circle.yml")

  ui.say "\nOK, I'll give you a moment to do this..."
  ui.wait_for_return

  ui.header "Final step: exposing the GitHub token as an environment build variable."
  ui.pause 0.4
  if considered_an_oss_repo?
    ui.say "As you have an Open Source repo, this token should be considered public, otherwise you cannot"
    ui.say "run Danger on pull requests from forks, limiting its use.\n"
    ui.pause 1
  end

  travis_token if File.exist? ".travis.yml"
  circle_token if File.exist? "circle.yml"
  unsure_token unless File.exist?(".travis.yml") || File.exist?(".circle.yml")

  ui.pause 0.6
  ui.say "This is the last step, I can give you a second..."
  ui.wait_for_return
end

#setup_dangerfileObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/danger/commands/init.rb', line 56

def setup_dangerfile
  content = DangerfileGenerator.create_dangerfile(".", cork)
  File.write("Dangerfile", content)

  ui.header "Step 1: Creating a starter Dangerfile"
  ui.say "I've set up an example Dangerfile for you in this folder.\n"
  ui.pause 1

  ui.say "cat #{Dir.pwd}/Dangerfile\n".blue
  content.lines.each do |l|
    ui.say "  " + l.chomp.green
  end
  ui.say ""
  ui.pause 2

  ui.say "There's a collection of small, simple ideas in here, but Danger is about being able to easily"
  ui.say "iterate. The power comes from you having the ability to codify fixes for some of the problems"
  ui.say "that come up in day to day programming. It can be difficult to try and see those from day 1."

  ui.say "\nIf you'd like to investigate the file, and make some changes - I'll wait here,"
  ui.say "press return when you're ready to move on..."
  ui.wait_for_return
end

#setup_github_accountObject



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
# File 'lib/danger/commands/init.rb', line 80

def 
  ui.header "Step 2: Creating a GitHub account"

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

  ui.say "IMO, it's best to do this by using the private mode of your browser. Create an account like"
  ui.say "#{@bot_name}, and don't forget a cool robot avatar.\n\n"
  ui.pause 1
  ui.say "Here are great resources for creative commons images of robots:"
  ui.link "https://www.flickr.com/search/?text=robot&license=2%2C3%2C4%2C5%2C6%2C9"
  ui.link "https://www.google.com/search?q=robot&tbs=sur:fmc&tbm=isch&tbo=u&source=univ&sa=X&ved=0ahUKEwjgy8-f95jLAhWI7hoKHV_UD00QsAQIMQ&biw=1265&bih=1359"
  ui.pause 1

  if considered_an_oss_repo?
    ui.say "#{@bot_name} 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."
  else
    ui.say "#{@bot_name} will need access to your repo. Simply because the code is not available for the public"
    ui.say "to read and comment on."
  end

  ui.say ""
  note_about_clicking_links
  ui.pause 1
  ui.say "\nCool, please press return when you have your account ready (and you've verified the email...)"
  ui.wait_for_return
end

#show_todo_stateObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/danger/commands/init.rb', line 44

def show_todo_state
  ui.say "We need to do the following:\n"
  ui.pause 0.6
  ui.say " - [ ] Create a Dangerfile and add a few simple rules."
  ui.pause 0.6
  ui.say " - [#{@account_created ? 'x' : ' '}] Create a GitHub account for Danger to use, for messaging."
  ui.pause 0.6
  ui.say " - [ ] Set up an access token for Danger."
  ui.pause 0.6
  ui.say " - [ ] Set up Danger to run on your CI.\n\n"
end

#thanksObject



284
285
286
287
288
289
290
291
# File 'lib/danger/commands/init.rb', line 284

def thanks
  ui.say "\n\nšŸŽ‰"
  ui.pause 0.6

  ui.say "And you're good to go. Danger is a collaboration between Orta Therox, Gem 'Danger' McShane and Felix Krause."
  ui.say "If you like Danger, let others know. If you want to know more, follow " + "@orta".yellow + " and " + "@KrauseFx".yellow + " on Twitter."
  ui.say "If you don't like Danger, help us improve the project! xxx"
end

#travis_tokenObject



221
222
223
224
225
226
227
228
229
# File 'lib/danger/commands/init.rb', line 221

def travis_token
  # https://travis-ci.org/artsy/eigen/settings
  ui.say "In order to add an environment variable, go to:"
  ui.link "https://travis-ci.org/#{current_repo_slug}/settings"
  ui.say "\nThe name is " + "DANGER_GITHUB_API_TOKEN".yellow + " and the value is the GitHub Personal Access Token."
  if @is_open_source
    ui.say 'Make sure to have "Display value in build log" enabled.'
  end
end

#unsure_ciObject



214
215
216
217
218
219
# File 'lib/danger/commands/init.rb', line 214

def unsure_ci
  danger = "bundle exec danger".yellow
  ui.say "As I'm not sure what CI you want to run Danger on based on the files in your repo, I'll just offer some generic"
  ui.say "advice. You want to run " + danger + " after your tests have finished running, it should still be during the testing"
  ui.say "process so the build can fail."
end

#unsure_tokenObject



254
255
256
257
258
259
# File 'lib/danger/commands/init.rb', line 254

def unsure_token
  ui.say "You need to expose a token called " + "DANGER_GITHUB_API_TOKEN".yellow + " and the value is the GitHub Personal Access Token."
  ui.say "Depending on the CI system, this may need to be done on the machine (in the " + "~/.bashprofile".yellow + ") or in a web UI somewhere."
  ui.say "We have a guide for all supported CI systems on danger.systems:"
  ui.link "https://danger.systems/guides/getting_started.html#setting-up-danger-to-run-on-your-ci"
end

#uses_circleObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/danger/commands/init.rb', line 196

def uses_circle
  danger = "- bundle exec danger".yellow
  config = YAML.load(File.read("circle.yml"))

  if config.kind_of?(Hash) && config["test"]
    if config["test"]["post"]
      ui.say "Add " + danger + " as a new step in the " + "test:post:".yellow + " section of your circle.yml file."
    else
      ui.say "Add " + danger + " as a new step in the " + "test:override:".yellow + " section of your circle.yml file."
    end
  else
    ui.say "Add this to the bottom of your circle.yml file:"
    ui.say "test:".green
    ui.say "  post:".green
    ui.say "    #{danger}".green
  end
end

#uses_travisObject



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/danger/commands/init.rb', line 183

def uses_travis
  danger = "bundle exec danger".yellow
  config = YAML.load(File.read(".travis.yml"))
  if config.kind_of?(Hash) && config["script"]
    ui.say "Add " + "- ".yellow + danger + " as a new step in the " + "script".yellow + " section of your .travis.yml file."
  else
    ui.say "I'd recommend adding " + "before_script: ".yellow + danger + " to the script section of your .travis.yml file."
  end

  ui.pause 1
  ui.say "You shouldn't use " + "after_success, after_failure, after_script".red + " as they cannot fail your builds."
end