Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookstyle Bot Auto Corrections with Cookstyle 7.1.2 #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions libraries/delivery_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,32 @@ def self.blocked_projects(node)
uri = URI.parse(change['delivery_api_url'])
http_client = Net::HTTP.new(uri.host, uri.port)

if uri.scheme == "https"
if uri.scheme == 'https'
http_client.use_ssl = true
http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
result = http_client.get(request_url, get_headers(change['token']))

case
when result.code == "404"
if result.code == '404'
Chef::Log.info("HTTP 404 recieved from #{request_url}. Please upgrade your Delivery Server.")
[]
when result.code.match(/20\d/)
elsif result.code.match(/20\d/)
JSON.parse(result.body)['blocked_projects']
else # not success or 404
error_str = "Failed request to #{request_url} returned #{result.code}"
Chef::Log.fatal(error_str)
raise BadApiResponse.new(error_str)
raise BadApiResponse, error_str
end
end

def self.get_headers(token)
{"chef-delivery-token" => token,
"chef-delivery-user" => 'builder'}
{ 'chef-delivery-token' => token,
'chef-delivery-user' => 'builder' }
end

def self.get_change_hash(node)
change_file = ::File.read(::File.expand_path('../../../../../../../change.json', node['delivery_builder']['workspace']))
change_hash = ::JSON.parse(change_file)
end

end
end
60 changes: 31 additions & 29 deletions libraries/delivery_truck_deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_search
search << " AND chef_environment:#{delivery_environment}" unless search =~ /chef_environment/

# We will search only on nodes that has push-jobs
search << " AND recipes:push-jobs*"
search << ' AND recipes:push-jobs*'
end
end
end
Expand All @@ -63,10 +63,10 @@ def dec_timeout(number)
def deploy_ccr
origin = timeout

::Chef::Log.info("Will wait up to #{timeout/60} minutes for " +
"deployment to complete...")
::Chef::Log.info("Will wait up to #{timeout / 60} minutes for " +
'deployment to complete...')

begin
loop do
# Sleep unless this is our first time through the loop.
sleep(SLEEP_TIME) unless timeout == origin

Expand All @@ -76,7 +76,7 @@ def deploy_ccr

if !nodes || nodes.empty?
# We didn't find any node to deploy. Lets skip this phase!
::Chef::Log.info("No dependency/app nodes found. Skipping phase!")
::Chef::Log.info('No dependency/app nodes found. Skipping phase!')
break
end

Expand All @@ -91,26 +91,26 @@ def deploy_ccr

# Kick off command via push.
::Chef::Log.info("Triggering #{new_resource.command} on dependency nodes " +
"with Chef Push Jobs...")
'with Chef Push Jobs...')

req = {
'command' => new_resource.command,
'nodes' => node_names
'nodes' => node_names,
}
resp = chef_server_rest.post('/pushy/jobs', req)
job_uri = resp['uri']

unless job_uri
# We were not able to start the push job.
::Chef::Log.info("Could not start push job. " +
::Chef::Log.info('Could not start push job. ' +
"Will try again in #{SLEEP_TIME} seconds...")
next
end

::Chef::Log.info("Started push job with id: #{job_uri[-32,32]}")
previous_state = "initialized"
begin
sleep(PUSH_SLEEP_TIME) unless previous_state == "initialized"
::Chef::Log.info("Started push job with id: #{job_uri[-32, 32]}")
previous_state = 'initialized'
loop do
sleep(PUSH_SLEEP_TIME) unless previous_state == 'initialized'
job = chef_server_rest.get_rest(job_uri)
case job['status']
when 'new'
Expand Down Expand Up @@ -146,38 +146,40 @@ def deploy_ccr
## Check for success
if finished && job['nodes']['succeeded'] &&
job['nodes']['succeeded'].size == nodes.size
::Chef::Log.info("Deployment complete in " +
"#{(origin-timeout)/60} minutes. " +
"Deploy Successful!")
::Chef::Log.info('Deployment complete in ' +
"#{(origin - timeout) / 60} minutes. " +
'Deploy Successful!')
break
elsif finished == true && job['nodes']['failed'] || job['nodes']['unavailable']
::Chef::Log.info("Deployment failed on the following nodes with status: ")
::Chef::Log.info('Deployment failed on the following nodes with status: ')
::Chef::Log.info(" => Failed: #{job['nodes']['failed']}.") if job['nodes']['failed']
::Chef::Log.info(" => Unavailable: #{job['nodes']['unavailable']}.") if job['nodes']['unavailable']
raise "Deployment failed! Not all nodes were successful."
raise 'Deployment failed! Not all nodes were successful.'
end

dec_timeout(PUSH_SLEEP_TIME)
end until timeout <= 0
break if timeout <= 0
end

break if finished

## If we make it here and we are past our timeout the job timed out
## waiting for the push job.
if timeout <= 0
::Chef::Log.error("Timed out after #{origin/60} minutes waiting "+
"for push job. Deploy Failed...")
raise "Timeout waiting for deploy..."
::Chef::Log.error("Timed out after #{origin / 60} minutes waiting " +
'for push job. Deploy Failed...')
raise 'Timeout waiting for deploy...'
end

dec_timeout(SLEEP_TIME)
end while timeout > 0
break unless timeout > 0
end

## If we make it here and we are past our timeout the job timed out.
if timeout <= 0
::Chef::Log.error("Timed out after #{origin/60} minutes waiting "+
"for deployment to complete. Deploy Failed...")
raise "Timeout waiting for deploy..."
::Chef::Log.error("Timed out after #{origin / 60} minutes waiting " +
'for deployment to complete. Deploy Failed...')
raise 'Timeout waiting for deploy...'
end

# we survived
Expand All @@ -194,12 +196,12 @@ class DeliveryTruckDeploy < Chef::Resource::LWRPBase

default_action :run

attribute :command, :kind_of => String, :default => 'chef-client'
attribute :timeout, :kind_of => Integer, :default => 30 * 60 # 30 mins
attribute :search, :kind_of => String
attribute :command, kind_of: String, default: 'chef-client'
attribute :timeout, kind_of: Integer, default: 30 * 60 # 30 mins
attribute :search, kind_of: String

self.resource_name = :delivery_truck_deploy
def initialize(name, run_context=nil)
def initialize(name, run_context = nil)
super
@provider = Chef::Provider::DeliveryTruckDeploy
end
Expand Down
6 changes: 3 additions & 3 deletions libraries/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
require_relative 'helpers_deploy'

# And these mix the DSL methods into the Chef infrastructure
Chef::Recipe.send(:include, DeliveryTruck::DSL)
Chef::Resource.send(:include, DeliveryTruck::DSL)
Chef::Provider.send(:include, DeliveryTruck::DSL)
Chef::DSL::Recipe.include DeliveryTruck::DSL
Chef::Resource.include DeliveryTruck::DSL
Chef::DSL::Recipe.include DeliveryTruck::DSL
74 changes: 33 additions & 41 deletions libraries/helpers_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,34 @@ module Lint
# @param node [Chef::Node] Chef Node object
# @return [String]
def foodcritic_tags(node)
begin
config = node['delivery']['config']['delivery-truck']['lint']['foodcritic']
config = node['delivery']['config']['delivery-truck']['lint']['foodcritic']

# We ignore these two by default since they search for the presence of
# `issues_url` and `source_url` in the metadata.rb. Those fields will
# only be populated by cookbooks uploading to Supermarket.
default_ignore = "-t ~FC064 -t ~FC065"
# We ignore these two by default since they search for the presence of
# `issues_url` and `source_url` in the metadata.rb. Those fields will
# only be populated by cookbooks uploading to Supermarket.
default_ignore = '-t ~FC064 -t ~FC065'

# Do not ignore these rules if the cookbook will be share to Supermarket
if ::DeliveryTruck::Helpers::Publish.share_cookbook_to_supermarket?(node)
default_ignore = ""
end
# Do not ignore these rules if the cookbook will be share to Supermarket
if ::DeliveryTruck::Helpers::Publish.share_cookbook_to_supermarket?(node)
default_ignore = ''
end

case
when config.nil?
default_ignore
when config['only_rules'] && !config['only_rules'].empty?
"-t " + config['only_rules'].join(",")
when config['ignore_rules'].nil?
default_ignore
when config['ignore_rules'].empty?
# They can set ignore_rules to an empty Array to disable these defaults
""
when config['ignore_rules'] && !config['ignore_rules'].empty?
"-t ~" + config['ignore_rules'].join(" -t ~")
else
""
end
rescue
""
if config.nil?
default_ignore
elsif config['only_rules'] && !config['only_rules'].empty?
'-t ' + config['only_rules'].join(',')
elsif config['ignore_rules'].nil?
default_ignore
elsif config['ignore_rules'].empty?
# They can set ignore_rules to an empty Array to disable these defaults
''
elsif config['ignore_rules'] && !config['ignore_rules'].empty?
'-t ~' + config['ignore_rules'].join(' -t ~')
else
''
end
rescue
''
end

# Based on the properties in the Delivery Config, create the --excludes
Expand All @@ -65,17 +62,14 @@ def foodcritic_tags(node)
# @param node [Chef::Node] Chef Node object
# @return [String]
def foodcritic_excludes(node)
begin
config = node['delivery']['config']['delivery-truck']['lint']['foodcritic']
case
when config['excludes'] && !config['excludes'].empty?
"--exclude " + config['excludes'].join(" --exclude ")
else
""
end
rescue
""
config = node['delivery']['config']['delivery-truck']['lint']['foodcritic']
if config['excludes'] && !config['excludes'].empty?
'--exclude ' + config['excludes'].join(' --exclude ')
else
''
end
rescue
''
end

# Based on the properties in the Delivery Config, create the --epic_fail
Expand All @@ -85,15 +79,15 @@ def foodcritic_excludes(node)
# @return [String]
def foodcritic_fail_tags(node)
config = node['delivery']['config']['delivery-truck']['lint']['foodcritic']
case
when config['fail_tags'] && !config['fail_tags'].empty?
if config['fail_tags'] && !config['fail_tags'].empty?
'-f ' + config['fail_tags'].join(',')
else
'-f correctness'
end
rescue
'-f correctness'
end

# Read the Delivery Config to see if the user has indicated they want to
# run cookstyle tests on their cookbook
#
Expand All @@ -104,12 +98,10 @@ def cookstyle_enabled?(node)
rescue
false
end

end
end

module DSL

# Return the applicable tags for foodcritic runs
def foodcritic_tags
DeliveryTruck::Helpers::Lint.foodcritic_tags(node)
Expand Down
26 changes: 12 additions & 14 deletions libraries/helpers_provision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def handle_acceptance_pinnings(node, acceptance_env_name, get_all_project_cookbo
promote_cookbook_versions(union_env, acceptance_env)

if acceptance_env.override_attributes && !acceptance_env.override_attributes.empty? &&
acceptance_env.override_attributes['applications'] != nil
!acceptance_env.override_attributes['applications'].nil?
union_apps = union_env.override_attributes['applications']
acceptance_env.override_attributes['applications'].merge!(union_apps) unless union_apps.nil?
else
Expand Down Expand Up @@ -173,7 +173,7 @@ def cleanup_union_changes(union_env, node)
# so we promote all cookbook_versions, default_attributes, and
# override_attributes (not just for the current project, but everything
# in from_env).
def handle_delivered_pinnings(node)
def handle_delivered_pinnings(_node)
to_env_name = 'delivered'
from_env_name = 'rehearsal'

Expand Down Expand Up @@ -211,7 +211,7 @@ def project_name(node)
def fetch_or_create_environment(env_name)
env = Chef::Environment.load(env_name)
rescue Net::HTTPServerException => http_e
raise http_e unless http_e.response.code == "404"
raise http_e unless http_e.response.code == '404'
chef_log.info("Creating Environment #{env_name}")
env = Chef::Environment.new()
env.name(env_name)
Expand Down Expand Up @@ -324,11 +324,10 @@ def promote_project_cookbooks(node, promoted_from_env, promoted_on_env, project_
all_project_cookbooks.each do |pin|
from_v = promoted_from_env.cookbook_versions[pin]
to_v = promoted_on_env.cookbook_versions[pin]
if from_v
chef_log.info("Promoting #{pin} @ #{from_v} from #{promoted_from_env.name}" +
" to #{promoted_on_env.name} was @ #{to_v}.")
promoted_on_env.cookbook_versions[pin] = from_v
end
next unless from_v
chef_log.info("Promoting #{pin} @ #{from_v} from #{promoted_from_env.name}" +
" to #{promoted_on_env.name} was @ #{to_v}.")
promoted_on_env.cookbook_versions[pin] = from_v
end
end

Expand All @@ -347,11 +346,10 @@ def promote_project_apps(node, promoted_from_env, promoted_on_env)
node['delivery']['project_apps'].each do |app|
from_v = promoted_from_env.override_attributes['applications'][app]
to_v = promoted_on_env.override_attributes['applications'][app]
if from_v
chef_log.info("Promoting #{app} @ #{from_v} from #{promoted_from_env.name}" +
" to #{promoted_on_env.name} was @ #{to_v}.")
promoted_on_env.override_attributes['applications'][app] = from_v
end
next unless from_v
chef_log.info("Promoting #{app} @ #{from_v} from #{promoted_from_env.name}" +
" to #{promoted_on_env.name} was @ #{to_v}.")
promoted_on_env.override_attributes['applications'][app] = from_v
end
end

Expand All @@ -377,7 +375,7 @@ def promote_unblocked_cookbooks_and_applications(promoted_from_env, promoted_on_
promoted_from_env.default_attributes['delivery']['project_artifacts'].each do |project_name, project_contents|
if blocked.include?(project_name)
chef_log.info("Project #{project_name} is currently blocked." +
"not promoting its cookbooks or applications")
'not promoting its cookbooks or applications')
else
chef_log.info("Promoting cookbooks and applications for project #{project_name}")

Expand Down
2 changes: 1 addition & 1 deletion libraries/helpers_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def bumped_version?(path, node)
templates\/.*
).join('|')

clean_relative_dir = relative_dir == "." ? "" : Regexp.escape("#{relative_dir}/")
clean_relative_dir = relative_dir == '.' ? '' : Regexp.escape("#{relative_dir}/")

if modified_files.any? { |f| /^#{clean_relative_dir}(#{files_to_check})/ =~ f }
base = change.merge_sha.empty? ? "origin/#{change.pipeline}" : "#{change.merge_sha}~1"
Expand Down
10 changes: 0 additions & 10 deletions libraries/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,3 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

if defined?(ChefSpec)
def create_chef_environment(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:chef_environment, :create, resource_name)
end

def run_delivery_truck_deploy(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:delivery_truck_deploy, :run, resource_name)
end
end
Loading