Welcome to mirror list, hosted at ThFree Co, Russian Federation.

builder.rb « git « flowdock « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f4428d1f42bb2aeb24b83103ef1f6c686b41a72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true
module Flowdock
  class Git
    class Commit
      def initialize(external_thread_id, thread, tags, commit)
        @commit = commit
        @external_thread_id = external_thread_id
        @thread = thread
        @tags = tags
      end

      def to_hash
        hash = {
          external_thread_id: @external_thread_id,
          event: "activity",
          author: {
            name: @commit[:author][:name],
            email: @commit[:author][:email]
          },
          title: title,
          thread: @thread,
          body: body
        }
        hash[:tags] = @tags if @tags
        encode(hash)
      end

      private

      def encode(hash)
        return hash unless "".respond_to?(:encode)

        encode_as_utf8(hash)
      end

      # This only works on Ruby 1.9
      def encode_as_utf8(obj)
        if obj.is_a? Hash
          obj.each_pair do |key, val|
            encode_as_utf8(val)
          end
        elsif obj.is_a?(Array)
          obj.each do |val|
            encode_as_utf8(val)
          end
        elsif obj.is_a?(String) && obj.encoding != Encoding::UTF_8
          unless obj.force_encoding("UTF-8").valid_encoding?
            obj.force_encoding("ISO-8859-1").encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)
          end
        end
      end

      def body
        content = @commit[:message][first_line.size..-1]
        content.strip! if content
        "<pre>#{content}</pre>" unless content.empty?
      end

      def first_line
        @first_line ||= (@commit[:message].split("\n")[0] || @commit[:message])
      end

      def title
        commit_id = @commit[:id][0, 7]
        if @commit[:url]
          "<a href=\"#{@commit[:url]}\">#{commit_id}</a> #{message_title}"
        else
          "#{commit_id} #{message_title}"
        end
      end

      def message_title
        CGI.escape_html(first_line.strip)
      end
    end

    # Class used to build Git payload
    class Builder
      include ::Gitlab::Utils::StrongMemoize

      def initialize(opts)
        @repo = opts[:repo]
        @ref = opts[:ref]
        @before = opts[:before]
        @after = opts[:after]
        @opts = opts
      end

      def commits
        @repo.commits_between(@before, @after).map do |commit|
          {
            url: @opts[:commit_url] ? @opts[:commit_url] % [commit.sha] : nil,
            id: commit.sha,
            message: commit.message,
            author: {
              name: commit.author_name,
              email: commit.author_email
            }
          }
        end
      end

      def ref_name
        @ref.to_s.sub(%r{\Arefs/(heads|tags)/}, '')
      end

      def to_hashes
        commits.map do |commit|
          Commit.new(external_thread_id, thread, @opts[:tags], commit).to_hash
        end
      end

      private

      def thread
        @thread ||= {
          title: thread_title,
          external_url: @opts[:repo_url]
        }
      end

      def permanent?
        strong_memoize(:permanent) do
          @opts[:permanent_refs].any? { |regex| regex.match(@ref) }
        end
      end

      def thread_title
        action = "updated" if permanent?
        type = @ref =~ %r(^refs/heads/) ? "branch" : "tag"

        [@opts[:repo_name], type, ref_name, action].compact.join(" ")
      end

      def external_thread_id
        @external_thread_id ||=
          if permanent?
            SecureRandom.hex
          else
            @ref
          end
      end
    end
  end
end