class Changelog::Notifier::Entrypoints::Capistrano

Tries to publish the version's release note after a successful Capistrano deployment.

Public Class Methods

new(instance) click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 11
def initialize(instance)
  @capistrano = instance
end
run!(instance) click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 22
def self.run!(instance)
  entrypoint = Changelog::Notifier::Entrypoints::Capistrano.new(instance)
  entrypoint.publish_release_note
end

Public Instance Methods

publish_release_note() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 15
def publish_release_note
  ensure_release_has_changelog_file &&
    fetch_version_from_git_tags_and_commit_author &&
    parse_changelog_file &&
    run_through_adapters
end

Private Instance Methods

ensure_release_has_changelog_file() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 29
def ensure_release_has_changelog_file
  return true if File.file?('CHANGELOG.md')

  @capistrano.warn 'You repository has no CHANGELOG.md file. ' \
                   'Skipping sending release note.'

  false
end
fetch_commit_author() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 61
def fetch_commit_author
  # Retrives the current's commit author
  @author = Changelog::Notifier::GitCommitAuthorFetcher.fetch(
    capistrano: @capistrano,
    path: @git_repo_path
  )

  return true if @author

  @capistrano.warn 'Commit author cannot be found from the currernt ' \
                   ' commit. Skipping sending release note.'

  false
end
fetch_version_from_git_tags() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 46
def fetch_version_from_git_tags
  # Retrives the current's commit version tag if any
  @version = Changelog::Notifier::GitTagFetcher.fetch(
    capistrano: @capistrano,
    path: @git_repo_path
  )

  return true if @version

  @capistrano.warn 'No version tag found from the currernt commit. ' \
                   'Skipping sending release note.'

  false
end
fetch_version_from_git_tags_and_commit_author() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 38
def fetch_version_from_git_tags_and_commit_author
  @git_repo_path = @capistrano.fetch(:repo_path)

  @capistrano.info "Git dir is now #{@git_repo_path}."

  fetch_version_from_git_tags && fetch_commit_author
end
parse_changelog_file() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 76
def parse_changelog_file
  parser = Changelog::Notifier::Parsers::Markdown.new(
    File.read('CHANGELOG.md')
  )

  @release_note_hash = parser.extract(@version)

  @release_note_hash[:author] = @author
  @release_note_hash[:application] = @capistrano.fetch(
    :application,
    'the application'
  )

  true
rescue ArgumentError => error
  @capistrano.error "Parsing CHANGELOG.md failed: #{error.message}. " \
                    'Skipping sending release note.'
rescue Changelog::Notifier::ReleaseNoteNotFound
  @capistrano.error 'Parsing CHANGELOG.md failed: Missing release ' \
                    "note for version #{@version}." \
                    'Skipping sending release note.'
end
run_through_adapters() click to toggle source
# File lib/changelog/notifier/entrypoints/capistrano.rb, line 99
def run_through_adapters
  [
    Changelog::Notifier::Adapters::ActiveRecord,
    Changelog::Notifier::Adapters::Slack
  ].each do |adapter_class|
    adapter = adapter_class.new(@capistrano)

    next unless adapter.configured?

    adapter.publish!(@release_note_hash, @version)
  end
rescue StandardError => error
  @capistrano.error error.message
end