summaryrefslogtreecommitdiff
path: root/lib/api/github/entities.rb
blob: c28a0b8eb7e544bbc7cc35f298bb61628ccffcd2 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# frozen_string_literal: true

# Simplified version of Github API entities.
# It's mainly used to mimic Github API and integrate with Jira Development Panel.
#
module API
  module Github
    module Entities
      class Repository < Grape::Entity
        expose :id
        expose :owner do |project, options|
          root_namespace = options[:root_namespace] || project.root_namespace

          { login: root_namespace.path }
        end
        expose :name do |project, options|
          ::Gitlab::Jira::Dvcs.encode_project_name(project)
        end
      end

      class BranchCommit < Grape::Entity
        expose :id, as: :sha
        expose :type do |_|
          'commit'
        end
      end

      class RepoCommit < Grape::Entity
        expose :id, as: :sha
        expose :author do |commit|
          {
            login: commit.author&.username,
            email: commit.author_email
          }
        end
        expose :committer do |commit|
          {
            login: commit.author&.username,
            email: commit.committer_email
          }
        end
        expose :commit do |commit|
          {
            author: {
              name: commit.author_name,
              email: commit.author_email,
              date: commit.authored_date.iso8601,
              type: 'User'
            },
            committer: {
              name: commit.committer_name,
              email: commit.committer_email,
              date: commit.committed_date.iso8601,
              type: 'User'
            },
            message: commit.safe_message
          }
        end
        expose :parents do |commit|
          commit.parent_ids.map { |id| { sha: id } }
        end
        expose :files do |commit|
          commit.diffs.diff_files.flat_map do |diff|
            additions = diff.added_lines
            deletions = diff.removed_lines

            if diff.new_file?
              {
                status: 'added',
                filename: diff.new_path,
                additions: additions,
                changes: additions
              }
            elsif diff.deleted_file?
              {
                status: 'removed',
                filename: diff.old_path,
                deletions: deletions,
                changes: deletions
              }
            elsif diff.renamed_file?
              [
                {
                  status: 'removed',
                  filename: diff.old_path,
                  deletions: deletions,
                  changes: deletions
                },
                {
                  status: 'added',
                  filename: diff.new_path,
                  additions: additions,
                  changes: additions
                }
              ]
            else
              {
                status: 'modified',
                filename: diff.new_path,
                additions: additions,
                deletions: deletions,
                changes: (additions + deletions)
              }
            end
          end
        end
      end

      class Branch < Grape::Entity
        expose :name

        expose :commit, using: BranchCommit do |repo_branch, options|
          options[:project].repository.commit(repo_branch.dereferenced_target)
        end
      end

      class User < Grape::Entity
        expose :id
        expose :username, as: :login
        expose :user_url, as: :url
        expose :user_url, as: :html_url
        expose :avatar_url

        private

        def user_url
          Gitlab::Routing.url_helpers.user_url(object)
        end
      end

      class NoteableComment < Grape::Entity
        expose :id
        expose :author, as: :user, using: User
        expose :note, as: :body
        expose :created_at
      end

      class PullRequest < Grape::Entity
        expose :title
        expose :assignee, using: User do |merge_request|
          merge_request.assignee
        end
        expose :author, as: :user, using: User
        expose :created_at
        expose :description, as: :body
        # Since Jira service requests `/repos/-/jira/pulls` (without project
        # scope), we need to make it work with ID instead IID.
        expose :id, as: :number
        # GitHub doesn't have a "merged" or "closed" state. It's just "open" or
        # "closed".
        expose :state do |merge_request|
          case merge_request.state
          when 'opened', 'locked'
            'open'
          when 'merged'
            'closed'
          else
            merge_request.state
          end
        end
        expose :merged?, as: :merged
        expose :merged_at do |merge_request|
          merge_request.metrics&.merged_at
        end
        expose :closed_at do |merge_request|
          merge_request.metrics&.latest_closed_at
        end
        expose :updated_at
        expose :html_url do |merge_request|
          Gitlab::UrlBuilder.build(merge_request)
        end
        expose :head do
          expose :source_branch, as: :label
          expose :source_branch, as: :ref
          expose :source_project, as: :repo, using: Repository
        end
        expose :base do
          expose :target_branch, as: :label
          expose :target_branch, as: :ref
          expose :target_project, as: :repo, using: Repository
        end
      end

      class PullRequestPayload < Grape::Entity
        expose :action do |merge_request|
          case merge_request.state
          when 'merged', 'closed'
            'closed'
          else
            'opened'
          end
        end

        expose :id
        expose :pull_request, using: PullRequest do |merge_request|
          merge_request
        end
      end

      class PullRequestEvent < Grape::Entity
        expose :id do |merge_request|
          updated_at = merge_request.updated_at.to_i
          "#{merge_request.id}-#{updated_at}"
        end
        expose :type do |_merge_request|
          'PullRequestEvent'
        end
        expose :updated_at, as: :created_at
        expose :payload, using: PullRequestPayload do |merge_request|
          # The merge request data is used by PullRequestPayload and PullRequest, so we just provide it
          # here. Otherwise Grape::Entity would try to access a field "payload" on Merge Request.
          merge_request
        end
      end
    end
  end
end