summaryrefslogtreecommitdiff
path: root/app/serializers/import/github_failure_entity.rb
blob: 8d8b16c2a6d758f0204da951b12d9882e02a6996 (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
# frozen_string_literal: true

module Import
  class GithubFailureEntity < Grape::Entity
    expose :type do |failure|
      failure.external_identifiers['object_type']
    end

    expose :title do |failure|
      title(failure)
    end

    expose :provider_url do |failure|
      build_url(failure)
    end

    expose :details do
      expose :exception_class
      expose :exception_message
      expose :correlation_id_value
      expose :source
      expose :created_at
      expose :github_identifiers do
        with_options(expose_nil: false) do
          expose(:object_type) { |failure| failure.external_identifiers['object_type'] }
          expose(:id) { |failure| failure.external_identifiers['id'] }
          expose(:db_id) { |failure| failure.external_identifiers['db_id'] }
          expose(:iid) { |failure| failure.external_identifiers['iid'] }
          expose(:title) { |failure| failure.external_identifiers['title'] }
          expose(:login) { |failure| failure.external_identifiers['login'] }
          expose(:event) { |failure| failure.external_identifiers['event'] }
          expose(:merge_request_id) { |failure| failure.external_identifiers['merge_request_id'] }
          expose(:merge_request_iid) { |failure| failure.external_identifiers['merge_request_iid'] }
          expose(:requested_reviewers) { |failure| failure.external_identifiers['requested_reviewers'] }
          expose(:note_id) { |failure| failure.external_identifiers['note_id'] }
          expose(:noteable_type) { |failure| failure.external_identifiers['noteable_type'] }
          expose(:noteable_iid) { |failure| failure.external_identifiers['noteable_iid'] }
          expose(:issuable_type) { |failure| failure.external_identifiers['issuable_type'] }
          expose(:issuable_iid) { |failure| failure.external_identifiers['issuable_iid'] }
          expose(:review_id) { |failure| failure.external_identifiers['review_id'] }
          expose(:tag) { |failure| failure.external_identifiers['tag'] }
          expose(:oid) { |failure| failure.external_identifiers['oid'] }
          expose(:size) { |failure| failure.external_identifiers['size'] }
        end
      end
    end

    private

    # rubocop:disable Metrics/CyclomaticComplexity
    def title(failure)
      gh_identifiers = failure.external_identifiers

      case gh_identifiers['object_type']
      when 'pull_request', 'issue', 'label', 'milestone'
        gh_identifiers['title']
      when 'pull_request_merged_by'
        format(s_("GithubImporter|Pull request %{pull_request_iid} merger"), pull_request_iid: gh_identifiers['iid'])
      when 'pull_request_review_request'
        format(
          s_("GithubImporter|Pull request %{pull_request_iid} review request"),
          pull_request_iid: gh_identifiers['merge_request_iid']
        )
      when 'pull_request_review'
        format(s_("GithubImporter|Pull request review %{review_id}"), review_id: gh_identifiers['review_id'])
      when 'collaborator'
        gh_identifiers['login']
      when 'protected_branch'
        gh_identifiers['id']
      when 'issue_event'
        gh_identifiers['event']
      when 'release'
        gh_identifiers['tag']
      when 'note'
        format(
          s_("GithubImporter|%{noteable_type} comment %{note_id}"),
          noteable_type: gh_identifiers['noteable_type'],
          note_id: gh_identifiers['note_id']
        )
      when 'diff_note'
        format(s_("GithubImporter|Pull request review comment %{note_id}"), note_id: gh_identifiers['note_id'])
      when 'issue_attachment'
        format(s_("GithubImporter|Issue %{issue_iid} attachment"), issue_iid: gh_identifiers['noteable_iid'])
      when 'merge_request_attachment'
        format(
          s_("GithubImporter|Merge request %{merge_request_iid} attachment"),
          merge_request_iid: gh_identifiers['noteable_iid']
        )
      when 'release_attachment'
        format(s_("GithubImporter|Release %{tag} attachment"), tag: gh_identifiers['tag'])
      when 'note_attachment'
        s_('GithubImporter|Note attachment')
      when 'lfs_object'
        gh_identifiers['oid'].to_s
      else
        ''
      end
    end

    def build_url(failure)
      project = failure.project
      gh_identifiers = failure.external_identifiers
      github_repo = project.import_source

      host = host(project.import_url)
      return '' unless host

      case gh_identifiers['object_type']
      when 'pull_request', 'pull_request_merged_by'
        # https://github.com/OWNER/REPO/pull/1
        "#{host}/#{github_repo}/pull/#{gh_identifiers['iid']}"
      when 'pull_request_review_request'
        # https://github.com/OWNER/REPO/pull/1
        "#{host}/#{github_repo}/pull/#{gh_identifiers['merge_request_iid']}"
      when 'pull_request_review'
        # https://github.com/OWNER/REPO/pull/1#pullrequestreview-1219894643
        "#{host}/#{github_repo}/pull/#{gh_identifiers['merge_request_iid']}" \
        "#pullrequestreview-#{gh_identifiers['review_id']}"
      when 'issue'
        # https://github.com/OWNER/REPO/issues/1
        "#{host}/#{github_repo}/issues/#{gh_identifiers['iid']}"
      when 'collaborator'
        # https://github.com/USER_NAME
        "#{host}/#{gh_identifiers['login']}"
      when 'protected_branch'
        branch = escape(gh_identifiers['id'])

        # https://github.com/OWNER/REPO/tree/BRANCH_NAME
        "#{host}/#{github_repo}/tree/#{branch}"
      when 'issue_event'
        # https://github.com/OWNER/REPO/issues/1#event-8356623615
        "#{host}/#{github_repo}/issues/#{gh_identifiers['issuable_iid']}#event-#{gh_identifiers['id']}"
      when 'label'
        label = escape(gh_identifiers['title'])

        # https://github.com/OWNER/REPO/labels/bug
        "#{host}/#{github_repo}/labels/#{label}"
      when 'milestone'
        # https://github.com/OWNER/REPO/milestone/1
        "#{host}/#{github_repo}/milestone/#{gh_identifiers['iid']}"
      when 'release', 'release_attachment'
        tag = escape(gh_identifiers['tag'])

        # https://github.com/OWNER/REPO/releases/tag/v1.0
        "#{host}/#{github_repo}/releases/tag/#{tag}"
      when 'note'
        # https://github.com/OWNER/REPO/issues/2#issuecomment-1480755368
        "#{host}/#{github_repo}/issues/#{gh_identifiers['noteable_iid']}#issuecomment-#{gh_identifiers['note_id']}"
      when 'diff_note'
        # https://github.com/OWNER/REPO/pull/1#discussion_r1050098241
        "#{host}/#{github_repo}/pull/#{gh_identifiers['noteable_iid']}#discussion_r#{gh_identifiers['note_id']}"
      when 'issue_attachment'
        # https://github.com/OWNER/REPO/issues/1
        "#{host}/#{github_repo}/issues/#{gh_identifiers['noteable_iid']}"
      when 'merge_request_attachment'
        # https://github.com/OWNER/REPO/pull/1
        "#{host}/#{github_repo}/pull/#{gh_identifiers['noteable_iid']}"
      when 'lfs_object', 'note_attachment'
        # we can't build url to lfs objects and note attachments
        ''
      else
        ''
      end
    end
    # rubocop:enable Metrics/CyclomaticComplexity

    def host(uri)
      parsed_uri = URI.parse(uri)
      "#{parsed_uri.scheme}://#{parsed_uri.hostname}"
    rescue URI::InvalidURIError
      nil
    end

    def escape(str)
      CGI.escape(str)
    end
  end
end