Skip to content

Commit

Permalink
Apply Rubocop autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Steppat committed Jan 14, 2019
1 parent 9e1d7b4 commit 5487950
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
2 changes: 0 additions & 2 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def index
# TODO: This needs to be changed in a different PR to a filtered version.
# Therefore distinguish between admin and employee
@requests = Request.all

end

# GET /requests/1
Expand Down Expand Up @@ -147,7 +146,6 @@ def puppet_node_script(request)
end
helper_method :puppet_node_script


def puppet_name_script(request)
request.generate_puppet_name_script
end
Expand Down
41 changes: 20 additions & 21 deletions app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,35 @@ def push_to_git
g.add(name_script)
change_init_script

if g.status.untracked.length == 0 && g.status.added.length != 0
if g.status.untracked.empty? && !g.status.added.empty?
message = 'Added file and pushed to git.'
g.commit_all("Add " + node_script_filename)
g.commit_all('Add ' + node_script_filename)
g.push
elsif g.status.untracked.length == 0 && g.status.changed.length != 0
elsif g.status.untracked.empty? && !g.status.changed.empty?
message = 'Changed file and pushed to git.'
g.commit_all("Update " + node_script_filename)
g.commit_all('Update ' + node_script_filename)
g.push
else
message = "Already up to date."
message = 'Already up to date.'
end

{ notice: message }
rescue Git::GitExecuteError => e
puts e
{ alert: "Could not push to git. Please check that your ssh key and environment variables are set."}
{ alert: 'Could not push to git. Please check that your ssh key and environment variables are set.' }
ensure
FileUtils.rm_rf( path ) if File.exists?( path )
FileUtils.rm_rf(path) if File.exist?(path)
end
end

def generate_puppet_node_script
puppet_string =
'class node_vm-%s {
$admins = [%s]
$users = [%s]
'class node_vm-%s {
$admins = [%s]
$users = [%s]
realize(Accounts::Virtual[$admins], Accounts::Sudoroot[$admins])
realize(Accounts::Virtual[$users])
realize(Accounts::Virtual[$admins], Accounts::Sudoroot[$admins])
realize(Accounts::Virtual[$users])
}'
admins = users_assigned_to_requests.select(&:sudo).to_a
admins.map!(&:user)
Expand All @@ -87,16 +87,17 @@ class { node_%s: }
}'
format(puppet_script, name, name, name)
end

private

# TODO get credentials and config for currently logged in user
# TODO: get credentials and config for currently logged in user
# Clones and configures a git repository on dir.
def setup_git(path)
# Dir.mkdir(dir) unless File.exists?(dir)
uri = ENV['GIT_REPOSITORY_URL']
name = ENV['GIT_REPOSITORY_NAME']

g = Git.clone(uri, name, :path => path)
g = Git.clone(uri, name, path: path)
g.config('user.name', ENV['GITHUB_USER_NAME'])
g.config('user.email', ENV['GITHUB_USER_EMAIL'])
g
Expand All @@ -106,25 +107,24 @@ def setup_git(path)
def write_node_script(path)
puppet_string = generate_puppet_node_script
path = File.join path, ENV['GIT_REPOSITORY_NAME'], 'Node', node_script_filename
File.delete(path) if File.exists?(path)
File.delete(path) if File.exist?(path)
File.open(path, 'w') { |f| f.write(puppet_string) }
path
end

# TODO file not yet created
# TODO: file not yet created
# Creates vmname.pp
def write_name_script(path)
puppet_string = generate_puppet_name_script
path = File.join path, ENV['GIT_REPOSITORY_NAME'], 'Name', name_script_filename
File.delete(path) if File.exists?(path)
File.delete(path) if File.exist?(path)
File.open(path, 'w') { |f| f.write(puppet_string) }
path
end

# TODO logic to change init.pp for new users
# TODO: logic to change init.pp for new users
# Adapts init.pp for potential new users
def change_init_script
end
def change_init_script; end

def node_script_filename
"node_#{name}.pp"
Expand All @@ -134,7 +134,6 @@ def name_script_filename
"#{name}.pp"
end


def url(host_name)
Rails.application.routes.url_helpers.request_url self, host: host_name
end
Expand Down
8 changes: 5 additions & 3 deletions config/initializers/environment_variables.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

module EnvironmentVariablesExample
class Application < Rails::Application
config.before_configuration do
env_file = Rails.root.join("config", 'environment_variables.yml').to_s
env_file = Rails.root.join('config', 'environment_variables.yml').to_s

if File.exists?(env_file)
if File.exist?(env_file)
YAML.load_file(env_file)[Rails.env].each do |key, value|
ENV[key.to_s] = value
end
end
end
end
end
end
2 changes: 2 additions & 0 deletions db/migrate/20181216161526_add_application_name_to_requests.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddApplicationNameToRequests < ActiveRecord::Migration[5.2]
def change
add_column :requests, :application_name, :string
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/requests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@
describe 'POST #push_to_git' do
before do
request = Request.create! valid_attributes
allow(request).to receive(:push_to_git) {{ notice: "Successfully pushed to git." }}
request_class = class_double("Request").
as_stubbed_const(:transfer_nested_constants => true)
allow(request).to receive(:push_to_git).and_return(notice: 'Successfully pushed to git.')
request_class = class_double('Request')
.as_stubbed_const(transfer_nested_constants: true)

expect(request_class).to receive(:find) { request }
end
Expand Down
42 changes: 21 additions & 21 deletions spec/models/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,36 @@

context 'push to git' do
before do
@git = double()
@status = double()
@git = double
@status = double
allow(@git).to receive(:config).with('user.name', 'test_user_name')
allow(@git).to receive(:config).with('user.email', 'test_user_email')
allow(@git).to receive(:status) {@status}
allow(@git).to receive(:status) { @status }
allow(@git).to receive(:add)
allow(@git).to receive(:commit_all)
allow(@git).to receive(:push)

file = double('file')
allow(File).to receive(:open) {file}
allow(File).to receive(:open) { file }
allow(file).to receive(:write)

git_class = class_double("Git").
as_stubbed_const(:transfer_nested_constants => true)
git_class = class_double('Git')
.as_stubbed_const(transfer_nested_constants: true)

allow(git_class).to receive(:clone) { @git }
end

context 'with a new puppet script' do
before do
allow(@status).to receive(:untracked) {[]}
allow(@status).to receive(:changed) {[]}
allow(@status).to receive(:added) {['added_file']}
allow(@status).to receive(:untracked).and_return([])
allow(@status).to receive(:changed).and_return([])
allow(@status).to receive(:added).and_return(['added_file'])
end

it 'correctly calls git' do
expect(@git).to receive(:config).with('user.name', 'test_user_name')
expect(@git).to receive(:config).with('user.email', 'test_user_email')
expect(@git).to receive(:status).exactly(2).times {@status}
expect(@git).to receive(:status).twice { @status }
expect(@status).to receive(:untracked).once
expect(@status).to receive(:added).once
expect(@status).not_to receive(:changed)
Expand All @@ -116,21 +116,21 @@
end

it 'returns a success message' do
expect(request.push_to_git).to eq({notice: "Added file and pushed to git."})
expect(request.push_to_git).to eq(notice: 'Added file and pushed to git.')
end
end

context 'with a changed puppet script' do
before do
allow(@status).to receive(:untracked) {[]}
allow(@status).to receive(:changed) {['changed_file']}
allow(@status).to receive(:added) {[]}
allow(@status).to receive(:untracked).and_return([])
allow(@status).to receive(:changed).and_return(['changed_file'])
allow(@status).to receive(:added).and_return([])
end

it 'correctly calls git' do
expect(@git).to receive(:config).with('user.name', 'test_user_name')
expect(@git).to receive(:config).with('user.email', 'test_user_email')
expect(@git).to receive(:status).exactly(4).times {@status}
expect(@git).to receive(:status).exactly(4).times { @status }
expect(@status).to receive(:untracked).twice
expect(@status).to receive(:added).once
expect(@status).to receive(:changed).once
Expand All @@ -142,21 +142,21 @@
end

it 'returns a success message' do
expect(request.push_to_git).to eq({notice: "Changed file and pushed to git."})
expect(request.push_to_git).to eq(notice: 'Changed file and pushed to git.')
end
end

context 'without any changes' do
before do
allow(@status).to receive(:untracked) {[]}
allow(@status).to receive(:changed) {[]}
allow(@status).to receive(:added) {[]}
allow(@status).to receive(:untracked).and_return([])
allow(@status).to receive(:changed).and_return([])
allow(@status).to receive(:added).and_return([])
end

it 'correctly calls git' do
expect(@git).to receive(:config).with('user.name', 'test_user_name')
expect(@git).to receive(:config).with('user.email', 'test_user_email')
expect(@git).to receive(:status).exactly(4).times {@status}
expect(@git).to receive(:status).exactly(4).times { @status }
expect(@status).to receive(:untracked).twice
expect(@status).to receive(:added).once
expect(@status).to receive(:changed).once
Expand All @@ -168,7 +168,7 @@
end

it 'returns a success message' do
expect(request.push_to_git).to eq({notice: "Already up to date."})
expect(request.push_to_git).to eq(notice: 'Already up to date.')
end
end
end
Expand Down

0 comments on commit 5487950

Please sign in to comment.