phallstrom / slackistrano

Slack integration for Capistrano deployments.
MIT License
373 stars 74 forks source link

Commit log, working solution #88

Open lizdeika opened 5 years ago

lizdeika commented 5 years ago

Hi,

For anyone still struggling with outputing useful info for non technical staff:

screeny https://i.imgur.com/h0l2K9U.jpg

Snippet to add to the custom messaging class:

def payload_for_updated
  {
    attachments: [
      {
        color: 'good',
        title: "#{deployer} deployed #{application} to #{stage}",
        fields:
          [
            {
              title: 'Project',
              value: application,
              short: true
            },
            {
              title: 'Environment',
              value: stage,
              short: true
            }
          ].tap do |fields|
            if revisions_present?
              fields << {
                title: 'Log',
                value: git_log,
                short: false
              }
            end
          end,
        fallback: super[:text]
      }.tap do |attachment|
        attachment[:title_link] = deployed_link if revisions_present?
      end
    ]
  }
end

# Open3 used for safe capturing when passing ruby variables to the command
def git_log
  return unless revisions_present?

  output, _status = Open3.capture2e 'git',
                                    'log',
                                    '--no-decorate',
                                    '--oneline',
                                    '--format=format:%s',
                                    "^#{previous_revision}",
                                    current_revision
  output.strip
end

def deployed_link
  return unless revisions_present?

  "https://github.com/YOUR_ORG/YOUR_APP/compare/#{CGI.escape(previous_revision)}...#{CGI.escape(current_revision)}"
end

def revisions_present?
  current_revision && previous_revision
end

def current_revision
  fetch :current_revision
end

def previous_revision
  fetch :previous_revision
end
Blacklizardu commented 1 year ago

In git_log you can even do this to make it even prettier

def git_log
         return unless revisions_present?

         output, _status = Open3.capture2e 'git',
                                           '--no-pager',
                                           'log',
                                           '--no-merges',
                                           '--pretty=',
                                           '--format=format:%ad - *%s*%d -- _%an_',
                                           '--date-order',
                                           '--date=iso',
                                           "^#{previous_revision}",
                                           current_revision
         if output.empty?

         'No commits (empty deploy)'

         else

         output.strip

         end
       end