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

Refactor Capistrano3 task for Metrics/* #1498

Merged
merged 1 commit into from
Sep 30, 2022
Merged
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
60 changes: 2 additions & 58 deletions lib/new_relic/recipes/capistrano3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# frozen_string_literal: true

require 'capistrano/framework'
require_relative 'helpers/send_deployment'

namespace :newrelic do
include SendDeployment
# notifies New Relic of a deployment
desc "Record a deployment in New Relic (newrelic.com)"
task :notice_deployment do
Expand All @@ -18,62 +20,4 @@
end
end
end

def send_deployment_notification_to_newrelic
environment = fetch(:newrelic_rails_env, fetch(:rack_env, fetch(:rails_env, fetch(:stage, "production"))))

require 'new_relic/cli/command'

begin
# allow overrides to be defined for revision, description, changelog, appname, and user
rev = fetch(:newrelic_revision)
description = fetch(:newrelic_desc)
changelog = fetch(:newrelic_changelog)
appname = fetch(:newrelic_appname)
user = fetch(:newrelic_user)
license_key = fetch(:newrelic_license_key)

has_scm_from_plugin = respond_to?(:scm_plugin_installed?) && scm_plugin_installed?
has_scm_from_config = defined?(scm) && !scm.nil? && scm != :none

if has_scm_from_plugin || has_scm_from_config
changelog ||= lookup_changelog
rev ||= fetch(:current_revision)
end

new_revision = rev
deploy_options = {
:environment => environment,
:revision => new_revision,
:changelog => changelog,
:description => description,
:appname => appname,
:user => user,
:license_key => license_key
}

debug("Uploading deployment to New Relic")
deployment = NewRelic::Cli::Deployments.new(deploy_options)
deployment.run
info("Uploaded deployment information to New Relic")
rescue NewRelic::Cli::Command::CommandFailure => e
info(e.message)
rescue => e
info("Error creating New Relic deployment (#{e})\n#{e.backtrace.join("\n")}")
end
end

def lookup_changelog
previous_revision = fetch(:previous_revision)
current_revision = fetch(:current_revision)
return unless current_revision && previous_revision

debug("Retrieving changelog for New Relic Deployment details")

if Rake::Task.task_defined?("git:check")
log_command = "git --no-pager log --no-color --pretty=format:' * %an: %s' " +
"--abbrev-commit --no-merges #{previous_revision}..#{current_revision}"
`#{log_command}`
end
end
end
69 changes: 69 additions & 0 deletions lib/new_relic/recipes/helpers/send_deployment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module SendDeployment
def send_deployment_notification_to_newrelic
require 'new_relic/cli/command'
debug("Uploading deployment to New Relic")
NewRelic::Cli::Deployments.new(deploy_options).run
info("Uploaded deployment information to New Relic")
rescue NewRelic::Cli::Command::CommandFailure => e
info(e.message)
rescue => e
info("Error creating New Relic deployment (#{e})\n#{e.backtrace.join("\n")}")
end

private

def deploy_options
{
:environment => fetch_environment,
:revision => fetch_rev,
:changelog => fetch_changelog,
:description => fetch(:newrelic_desc),
:appname => fetch(:newrelic_appname),
:user => fetch(:newrelic_user),
:license_key => fetch(:newrelic_license_key)
}
end

def fetch_changelog
has_scm? ? fetch(:newrelic_changelog) : lookup_changelog
end

def fetch_environment
fetch(:newrelic_rails_env, fetch(:rack_env, fetch(:rails_env, fetch(:stage, "production"))))
end

def fetch_rev
newrelic_rev = fetch(:newrelic_revision)
has_scm? && !newrelic_rev ? fetch(:current_revision) : newrelic_rev
end

def has_scm?
has_scm_from_plugin? || has_scm_from_config?
end

def has_scm_from_config?
defined?(scm) && !scm.nil? && scm != :none
end

def has_scm_from_plugin?
respond_to?(:scm_plugin_installed?) && scm_plugin_installed?
end

def lookup_changelog
previous_revision = fetch(:previous_revision)
current_revision = fetch(:current_revision)
return unless current_revision && previous_revision

debug("Retrieving changelog for New Relic Deployment details")

if Rake::Task.task_defined?("git:check")
log_command = "git --no-pager log --no-color --pretty=format:' * %an: %s' " +
"--abbrev-commit --no-merges #{previous_revision}..#{current_revision}"
`#{log_command}`
end
end
end