<commit_before>require 'net/http'
require 'uri'
require 'json'

module Viget
  module Deployment
    class SlackNotifier
      attr_reader :cap, :slack_url, :slack_channel

      def initialize(cap, slack_url, slack_channel)
        @cap           = cap
        @slack_channel = slack_channel
        @slack_url     = slack_url
      end

      def notify
        post(payload)
      end

      def payload
        {
          channel:    slack_channel,
          username:   slack_username,
          icon_emoji: slack_emoji,
          attachments: [{
            fallback:   fallback,
            title:      current_revision,
            text:       commit_message,
            title_link: commit_url,
            color:      color,
            fields: [
              deployed_by_field,
              branch_field,
              visit_site_field
            ].compact
          }]
        }
      end


      private

      def deployed_by_field
        {
          title: 'Deployed By',
          value: user,
          short: true
        }
      end

      def branch_field
        {
          title: 'Branch',
          value: "<#{branch_url}|#{branch}>",
          short: true
        }
      end

      def visit_site_field
        if slack_app_url
          {
            title: 'URL',
            value: "<#{slack_app_url}>",
            short: false
          }
        end
      end

      def slack_uri
        URI.parse(slack_url)
      end

      def slack_username
        cap.fetch(:slack_username, "#{environment} Deploy")
      end

      def slack_emoji
        cap.fetch(:slack_emoji, ':bell:')
      end

      def slack_app_url
        cap.fetch(:slack_app_url, nil)
      end

      def color
        case environment
        when 'Integration'
          '#d16d4e'
        when 'Staging'
          '#7c82d1'
        else
          '#23d15a'
        end
      end

      def fallback
        "#{current_revision}: #{commit_message}"
      end

      def environment
        cap.fetch(:stage).to_s.capitalize
      end

      def git_username
        username = cap.run_locally('git config --get user.name')

        username unless username.nil? or username == ''
      end

      def user
        git_username || ENV['USER']
      end

      def github_url
        repository_url = cap.fetch(:repository)
        path           = repository_url.gsub(%r{(^git@github\.com:?|\.git$)}, '')

        "https://github.com/#{path}"
      end

      def branch_url
        branch = cap.fetch(:branch).split('/').last

        "#{github_url}/commits/#{branch}"
      end

      def commit_url
        "#{github_url}/commit/#{current_revision}"<commit_msg>Update SlackNotifier to handle branch name as symbol<commit_after>require 'net/http'
require 'uri'
require 'json'

module Viget
  module Deployment
    class SlackNotifier
      attr_reader :cap, :slack_url, :slack_channel

      def initialize(cap, slack_url, slack_channel)
        @cap           = cap
        @slack_channel = slack_channel
        @slack_url     = slack_url
      end

      def notify
        post(payload)
      end

      def payload
        {
          channel:    slack_channel,
          username:   slack_username,
          icon_emoji: slack_emoji,
          attachments: [{
            fallback:   fallback,
            title:      current_revision,
            text:       commit_message,
            title_link: commit_url,
            color:      color,
            fields: [
              deployed_by_field,
              branch_field,
              visit_site_field
            ].compact
          }]
        }
      end


      private

      def deployed_by_field
        {
          title: 'Deployed By',
          value: user,
          short: true
        }
      end

      def branch_field
        {
          title: 'Branch',
          value: "<#{branch_url}|#{branch}>",
          short: true
        }
      end

      def visit_site_field
        if slack_app_url
          {
            title: 'URL',
            value: "<#{slack_app_url}>",
            short: false
          }
        end
      end

      def slack_uri
        URI.parse(slack_url)
      end

      def slack_username
        cap.fetch(:slack_username, "#{environment} Deploy")
      end

      def slack_emoji
        cap.fetch(:slack_emoji, ':bell:')
      end

      def slack_app_url
        cap.fetch(:slack_app_url, nil)
      end

      def color
        case environment
        when 'Integration'
          '#d16d4e'
        when 'Staging'
          '#7c82d1'
        else
          '#23d15a'
        end
      end

      def fallback
        "#{current_revision}: #{commit_message}"
      end

      def environment
        cap.fetch(:stage).to_s.capitalize
      end

      def git_username
        username = cap.run_locally('git config --get user.name')

        username unless username.nil? or username == ''
      end

      def user
        git_username || ENV['USER']
      end

      def github_url
        repository_url = cap.fetch(:repository)
        path           = repository_url.gsub(%r{(^git@github\.com:?|\.git$)}, '')

        "https://github.com/#{path}"
      end

      def branch_url
        branch = cap.fetch(:branch).to_s.split('/').last

        "#{github_url}/commits/#{branch}"
      end

      def commit_url
        "#{github_url}/commit/#{current_revision}"
{'fraction_non_alphanumeric': 0.09149962880475129, 'fraction_numerical': 0.004083147735708983, 'mean_word_length': 1.4484325306678782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2256956', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1564, 'n_words': 338}