Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Updates onboarding controller and views to account for `FASTLANE_CI_B…
Browse files Browse the repository at this point in the history
…OT_API_TOKEN` environment variable.
  • Loading branch information
armcburney committed Apr 12, 2018
1 parent b5f5db6 commit 5949a9c
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 56 deletions.
50 changes: 41 additions & 9 deletions app/features/onboarding/onboarding_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class OnboardingController < ControllerBase
end

get "#{HOME}/ci_bot_account" do
@progress = true if has_ci_user?
@progress = true if has_ci_user_email? && has_ci_user_password? && has_clone_user_api_token?
locals = { title: "Onboarding", variables: {} }
erb(:ci_bot_account, locals: locals, layout: FastlaneCI.default_layout)
end

get "#{HOME}/initial_clone_user" do
@progress = true if has_clone_user?
@progress = true if has_clone_user_email? && has_clone_user_api_token?
locals = { title: "Onboarding", variables: {} }
erb(:initial_clone_user, locals: locals, layout: FastlaneCI.default_layout)
end
Expand Down Expand Up @@ -91,6 +91,8 @@ class OnboardingController < ControllerBase
# 3) If the data is not valid, display an error message
post "#{HOME}/ci_bot_account" do
if valid_params?(params, post_parameter_list_for_ci_bot_user_validation)
validate_api_token_correct(params[:ci_user_api_token].strip, "ci_bot_account")

Services.environment_variable_service.write_keys_file!(
locals: format_params(
params, post_parameter_list_for_ci_bot_user_validation
Expand Down Expand Up @@ -139,6 +141,8 @@ class OnboardingController < ControllerBase

scope_validation_error = FastlaneCI::GitHubService.token_scope_validation_error(params[:clone_user_api_token])
if scope_validation_error.nil?
validate_api_token_correct(params[:clone_user_api_token].strip, "initial_clone_user")

Services.environment_variable_service.write_keys_file!(
locals: format_params(
params, post_parameter_list_for_clone_user_validation
Expand Down Expand Up @@ -207,6 +211,21 @@ class OnboardingController < ControllerBase

private

#####################################################
# @!group Redirect Helpers
#####################################################

def validate_api_token_correct(api_token, route)
if api_token.length != 40
error_message = "Invalid API token, please ensure you entered an API token and not a password"
session[:message] = error_message
logger.error(error_message)
logger.error("API tokens are generated by GitHub and are 40 characters long")

redirect("#{HOME}/#{endpoint}")
end
end

#####################################################
# @!group Data: View-specific data
#####################################################
Expand All @@ -226,15 +245,28 @@ def has_encryption_key?
end

# @return [Boolean]
def has_ci_user?
return not_nil_and_not_empty?(FastlaneCI.env.ci_user_email) &&
not_nil_and_not_empty?(FastlaneCI.env.ci_user_password)
def has_ci_user_email?
return not_nil_and_not_empty?(FastlaneCI.env.ci_user_email)
end

# @return [Boolean]
def has_ci_user_password?
return not_nil_and_not_empty?(FastlaneCI.env.ci_user_password)
end

# @return [Boolean]
def has_ci_user_api_token?
return not_nil_and_not_empty?(FastlaneCI.env.ci_user_api_token)
end

# @return [Boolean]
def has_clone_user_email?
return not_nil_and_not_empty?(FastlaneCI.env.initial_clone_email)
end

# @return [Boolean]
def has_clone_user?
return not_nil_and_not_empty?(FastlaneCI.env.initial_clone_email) &&
not_nil_and_not_empty?(FastlaneCI.env.clone_user_api_token)
def has_clone_user_api_token?
return not_nil_and_not_empty?(FastlaneCI.env.clone_user_api_token)
end

# @return [Boolean]
Expand All @@ -259,7 +291,7 @@ def post_parameter_list_for_encryption_key_validation

# @return [Set[String]]
def post_parameter_list_for_ci_bot_user_validation
return Set.new(%w(ci_user_email ci_user_password))
return Set.new(%w(ci_user_email ci_user_password ci_user_api_token))
end

# @return [Set[String]]
Expand Down
24 changes: 24 additions & 0 deletions app/features/onboarding/views/forms/_ci_bot_account.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@
placeholder="Your GitHub bot's password"
/>
</div>

<li>
Create a personal access token <a href="https://github.com/settings/tokens/new">
on GitHub</a> with <code>repo</code> permissions on your GitHub bot account:

<img src="../images/github_access_token.png" width="60%">
</li>


<li>Enter your bot user's API Token:</li>

<div class="mdl-textfield mdl-js-textfield">
<label for="input"><code>FASTLANE_CI_BOT_API_TOKEN</code></label>
<input
id="ci_user_api_token"
class="mdl-textfield__input"
type="password"
maxlength="40"
value="<%= keys[:ci_user_api_token] %>"
minlength="40"
name="ci_user_api_tokend"
placeholder="Your GitHub bot's API token"
/>
</div>
</ol>

<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
on GitHub</a> with <code>repo</code> permissions using the
<code>FASTLANE_CI_INITIAL_CLONE_EMAIL</code> you've set above:

<img src="../images/github_access_token.png" width="80%">
<img src="../images/github_access_token.png" width="60%">
</li>

<li>Enter the API token you created below:</li>
Expand Down
80 changes: 37 additions & 43 deletions app/features/onboarding/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,49 @@
<%= erb :"../../global/navigation" %>

<main class="mdl-layout__content" style="padding: 40px">
<div class="demo-card-wide mdl-card mdl-shadow--2dp" style="width: 100%">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">
Welcome to fastlane.ci 👋!
</h2>
</div>
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">
Welcome to fastlane.ci 👋!
</h2>
</div>

<div class="mdl-card__actions mdl-card--border">
<p>
<code>fastlane.ci</code> requires a few configuration values from you
to properly run the server.
</p>

<p>The following values need to be inputted to properly run the server:</p>

<ul>
<% if !has_encryption_key? %>
<li><code>FASTLANE_CI_ENCRYPTION_KEY</code></li>
<% end %>
<div class="mdl-card__supporting-text">
<p>
<code>fastlane.ci</code> requires a few configuration values from you
to properly run the server.
</p>
<% if !has_ci_user_email? %>
<li><code>FASTLANE_CI_USER</code></li>
<% end %>
The following items need to be addressed before you may properly run the
server:
</div>
<% if !has_ci_user_password? %>
<li><code>FASTLANE_CI_PASSWORD</code></li>
<% end %>
<div class="mdl-card__actions mdl-card--border">
<ul>
<% if !has_encryption_key? %>
<li>
You are missing the <code>FASTLANE_CI_ENCRYPTION_KEY</code>
environment variable.
</li>
<% end %>
<% if !has_ci_user_api_token? %>
<li><code>FASTLANE_CI_BOT_API_TOKEN</code></li>
<% end %>
<% if !has_ci_user? %>
<li>
You are missing the <code>FASTLANE_CI_USER</code> and/or
<code>FASTLANE_CI_PASSWORD</code> environment variables.
</li>
<% end %>
<% if !has_clone_user_email? %>
<li><code>FASTLANE_CI_INITIAL_CLONE_EMAIL</code></li>
<% end %>
<% if !has_clone_user? %>
<li>
You are missing the <code>FASTLANE_CI_INITIAL_CLONE_EMAIL</code>
and/or <code>FASTLANE_CI_INITIAL_CLONE_API_TOKEN</code> environment
variables.
</li>
<% end %>
<% if !has_clone_user_api_token? %>
<li><code>FASTLANE_CI_INITIAL_CLONE_API_TOKEN</code></li>
<% end %>
<% if !has_remote_github_repo? %>
<li>
You are missing the <code>FASTLANE_CI_REPO_URL</code> and/or you
didn't submit the form to create the remote repository
</li>
<% end %>
</ul>
</div>
<% if !has_remote_github_repo? %>
<li><code>FASTLANE_CI_REPO_URL</code></li>
<% end %>
</ul>
</div>

<a href="/onboarding_erb/encryption_key" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">
Expand Down
5 changes: 4 additions & 1 deletion app/services/file_writers/keys_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ def file_template
# The email address of your fastlane CI bot account
FASTLANE_CI_USER='#{locals[:ci_user_email]}'
# The encrypted API token of your fastlane CI bot account
# The password for your CI bot account
FASTLANE_CI_PASSWORD='#{locals[:ci_user_password]}'
# The encrypted API token of your fastlane CI bot account
FASTLANE_CI_BOT_API_TOKEN='#{locals[:ci_user_api_token]}'
# The git URL (https) for the configuration repo
FASTLANE_CI_REPO_URL='#{locals[:repo_url]}'
Expand Down
6 changes: 6 additions & 0 deletions app/shared/environment_variables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def all
encryption_key: encryption_key,
ci_user_email: ci_user_email,
ci_user_password: ci_user_password,
ci_user_api_token: ci_user_api_token,
repo_url: repo_url,
clone_user_email: initial_clone_email,
clone_user_api_token: clone_user_api_token
Expand All @@ -32,6 +33,11 @@ def ci_user_password
ENV["FASTLANE_CI_PASSWORD"]
end

# The API token used for the bot account
def ci_user_api_token
ENV["FASTLANE_CI_BOT_API_TOKEN"]
end

# The git URL (https) for the configuration repo
def repo_url
ENV["FASTLANE_CI_REPO_URL"]
Expand Down
5 changes: 4 additions & 1 deletion spec/fixtures/files/.fastlane/ci/.keys
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ FASTLANE_CI_ENCRYPTION_KEY='encryption_key'
# The email address of your fastlane CI bot account
FASTLANE_CI_USER='ci_user@gmail.com'

# The encrypted API token of your fastlane CI bot account
# The password for your CI bot account
FASTLANE_CI_PASSWORD='ci_bot_password'

# The encrypted API token of your fastlane CI bot account
FASTLANE_CI_BOT_API_TOKEN='bot_api_token'

# The git URL (https) for the configuration repo
FASTLANE_CI_REPO_URL='https://github.com/user_name/new_repo_name'

Expand Down
5 changes: 4 additions & 1 deletion spec/fixtures/files/templates/keys_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ FASTLANE_CI_ENCRYPTION_KEY='key'
# The email address of your fastlane CI bot account
FASTLANE_CI_USER='ci_user_email'

# The encrypted API token of your fastlane CI bot account
# The password for your CI bot account
FASTLANE_CI_PASSWORD='ci_user_password'

# The encrypted API token of your fastlane CI bot account
FASTLANE_CI_BOT_API_TOKEN='bot_api_token'

# The git URL (https) for the configuration repo
FASTLANE_CI_REPO_URL='https://github.com/user/repo'

Expand Down
1 change: 1 addition & 0 deletions spec/services/file_writers/keys_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
encryption_key: "key",
ci_user_email: "ci_user_email",
ci_user_password: "ci_user_password",
ci_user_api_token: "bot_api_token",
repo_url: "https://github.com/user/repo",
clone_user_email: "clone_user_email",
clone_user_api_token: "clone_user_api_token"
Expand Down

0 comments on commit 5949a9c

Please sign in to comment.