BigW Consortium Gitlab

merge_message.rb 1.36 KB
Newer Older
Douwe Maan committed
1
class SlackService
2
  class MergeMessage < BaseMessage
3
    attr_reader :user_name
4 5 6 7 8 9
    attr_reader :project_name
    attr_reader :project_url
    attr_reader :merge_request_id
    attr_reader :source_branch
    attr_reader :target_branch
    attr_reader :state
10
    attr_reader :title
11 12

    def initialize(params)
13
      @user_name = params[:user][:username]
14 15 16 17 18 19 20 21 22
      @project_name = params[:project_name]
      @project_url = params[:project_url]

      obj_attr = params[:object_attributes]
      obj_attr = HashWithIndifferentAccess.new(obj_attr)
      @merge_request_id = obj_attr[:iid]
      @source_branch = obj_attr[:source_branch]
      @target_branch = obj_attr[:target_branch]
      @state = obj_attr[:state]
23
      @title = format_title(obj_attr[:title])
24 25 26 27 28 29 30 31 32 33 34 35
    end

    def pretext
      format(message)
    end

    def attachments
      []
    end

    private

36 37 38 39
    def format_title(title)
      '*' + title.lines.first.chomp + '*'
    end

40 41 42 43 44 45 46 47 48
    def message
      merge_request_message
    end

    def project_link
      "[#{project_name}](#{project_url})"
    end

    def merge_request_message
49
      "#{user_name} #{state} #{merge_request_link} in #{project_link}: #{title}"
50 51 52
    end

    def merge_request_link
53
      "[merge request !#{merge_request_id}](#{merge_request_url})"
54 55 56 57 58 59 60
    end

    def merge_request_url
      "#{project_url}/merge_requests/#{merge_request_id}"
    end
  end
end