summaryrefslogtreecommitdiff
path: root/lib/github/import.rb
blob: 55f8387f27a257ba85e526cbdf74c356fd17d278 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
require_relative 'error'
require_relative 'import/issue'
require_relative 'import/legacy_diff_note'
require_relative 'import/merge_request'
require_relative 'import/note'

module Github
  class Import
    include Gitlab::ShellAdapter

    attr_reader :project, :repository, :repo, :repo_url, :wiki_url,
                :options, :errors, :cached, :verbose, :last_fetched_at

    def initialize(project, options = {})
      @project = project
      @repository = project.repository
      @repo = project.import_source
      @repo_url = project.import_url
      @wiki_url = project.import_url.sub(/\.git\z/, '.wiki.git')
      @options = options.reverse_merge(token: project.import_data&.credentials&.fetch(:user))
      @verbose = options.fetch(:verbose, false)
      @cached  = Hash.new { |hash, key| hash[key] = Hash.new }
      @errors  = []
      @last_fetched_at = nil
    end

    # rubocop: disable Rails/Output
    def execute
      puts 'Fetching repository...'.color(:aqua) if verbose
      setup_and_fetch_repository
      puts 'Fetching labels...'.color(:aqua) if verbose
      fetch_labels
      puts 'Fetching milestones...'.color(:aqua) if verbose
      fetch_milestones
      puts 'Fetching pull requests...'.color(:aqua) if verbose
      fetch_pull_requests
      puts 'Fetching issues...'.color(:aqua) if verbose
      fetch_issues
      puts 'Fetching releases...'.color(:aqua) if verbose
      fetch_releases
      puts 'Cloning wiki repository...'.color(:aqua) if verbose
      fetch_wiki_repository
      puts 'Expiring repository cache...'.color(:aqua) if verbose
      expire_repository_cache

      errors.empty?
    rescue Github::RepositoryFetchError
      expire_repository_cache
      false
    ensure
      keep_track_of_errors
    end

    private

    def setup_and_fetch_repository
      begin
        project.ensure_repository
        project.repository.add_remote('github', repo_url)
        project.repository.set_import_remote_as_mirror('github')
        project.repository.add_remote_fetch_config('github', '+refs/pull/*/head:refs/merge-requests/*/head')
        fetch_remote(forced: true)
      rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e
        error(:project, repo_url, e.message)
        raise Github::RepositoryFetchError
      end
    end

    def fetch_remote(forced: false)
      @last_fetched_at = Time.now
      project.repository.fetch_remote('github', forced: forced)
    end

    def fetch_wiki_repository
      return if project.wiki.repository_exists?

      wiki_path = "#{project.disk_path}.wiki"
      gitlab_shell.import_repository(project.repository_storage_path, wiki_path, wiki_url)
    rescue Gitlab::Shell::Error => e
      # GitHub error message when the wiki repo has not been created,
      # this means that repo has wiki enabled, but have no pages. So,
      # we can skip the import.
      if e.message !~ /repository not exported/
        error(:wiki, wiki_url, e.message)
      end
    end

    def fetch_labels
      url = "/repos/#{repo}/labels"

      while url
        response = Github::Client.new(options).get(url)

        response.body.each do |raw|
          begin
            representation = Github::Representation::Label.new(raw)

            label = project.labels.find_or_create_by!(title: representation.title) do |label|
              label.color = representation.color
            end

            cached[:label_ids][representation.title] = label.id
          rescue => e
            error(:label, representation.url, e.message)
          end
        end

        url = response.rels[:next]
      end
    end

    def fetch_milestones
      url = "/repos/#{repo}/milestones"

      while url
        response = Github::Client.new(options).get(url, state: :all)

        response.body.each do |raw|
          begin
            milestone = Github::Representation::Milestone.new(raw)
            next if project.milestones.where(iid: milestone.iid).exists?

            project.milestones.create!(
              iid: milestone.iid,
              title: milestone.title,
              description: milestone.description,
              due_date: milestone.due_date,
              state: milestone.state,
              created_at: milestone.created_at,
              updated_at: milestone.updated_at
            )
          rescue => e
            error(:milestone, milestone.url, e.message)
          end
        end

        url = response.rels[:next]
      end
    end

    def fetch_pull_requests
      url = "/repos/#{repo}/pulls"

      while url
        response = Github::Client.new(options).get(url, state: :all, sort: :created, direction: :asc)

        response.body.each do |raw|
          pull_request  = Github::Representation::PullRequest.new(raw, options.merge(project: project))
          merge_request = MergeRequest.find_or_initialize_by(iid: pull_request.iid, source_project_id: project.id)
          next unless merge_request.new_record? && pull_request.valid?

          begin
            # If the PR has been created/updated after we last fetched the
            # remote, we fetch again to get the up-to-date refs.
            fetch_remote if pull_request.updated_at > last_fetched_at

            author_id   = user_id(pull_request.author, project.creator_id)
            description = format_description(pull_request.description, pull_request.author)

            merge_request.attributes = {
              iid: pull_request.iid,
              title: pull_request.title,
              description: description,
              ref_fetched: true,
              source_project: pull_request.source_project,
              source_branch: pull_request.source_branch_name,
              source_branch_sha: pull_request.source_branch_sha,
              target_project: pull_request.target_project,
              target_branch: pull_request.target_branch_name,
              target_branch_sha: pull_request.target_branch_sha,
              state: pull_request.state,
              milestone_id: milestone_id(pull_request.milestone),
              author_id: author_id,
              assignee_id: user_id(pull_request.assignee),
              created_at: pull_request.created_at,
              updated_at: pull_request.updated_at
            }

            merge_request.save!(validate: false)
            merge_request.merge_request_diffs.create

            review_comments_url = "/repos/#{repo}/pulls/#{pull_request.iid}/comments"
            fetch_comments(merge_request, :review_comment, review_comments_url, LegacyDiffNote)
          rescue => e
            error(:pull_request, pull_request.url, e.message)
          end
        end

        url = response.rels[:next]
      end
    end

    def fetch_issues
      url = "/repos/#{repo}/issues"

      while url
        response = Github::Client.new(options).get(url, state: :all, sort: :created, direction: :asc)

        response.body.each { |raw| populate_issue(raw) }

        url = response.rels[:next]
      end
    end

    def populate_issue(raw)
      representation = Github::Representation::Issue.new(raw, options)

      begin
        # Every pull request is an issue, but not every issue
        # is a pull request. For this reason, "shared" actions
        # for both features, like manipulating assignees, labels
        # and milestones, are provided within the Issues API.
        if representation.pull_request?
          return unless representation.labels? || representation.comments?

          merge_request = MergeRequest.find_by!(target_project_id: project.id, iid: representation.iid)

          if representation.labels?
            merge_request.update_attribute(:label_ids, label_ids(representation.labels))
          end

          fetch_comments_conditionally(merge_request, representation)
        else
          return if Issue.exists?(iid: representation.iid, project_id: project.id)

          author_id          = user_id(representation.author, project.creator_id)
          issue              = Issue.new
          issue.iid          = representation.iid
          issue.project_id   = project.id
          issue.title        = representation.title
          issue.description  = format_description(representation.description, representation.author)
          issue.state        = representation.state
          issue.milestone_id = milestone_id(representation.milestone)
          issue.author_id    = author_id
          issue.created_at   = representation.created_at
          issue.updated_at   = representation.updated_at
          issue.save!(validate: false)

          issue.update(
            label_ids: label_ids(representation.labels),
            assignee_ids: assignee_ids(representation.assignees))

          fetch_comments_conditionally(issue, representation)
        end
      rescue => e
        error(:issue, representation.url, e.message)
      end
    end

    def fetch_comments_conditionally(issuable, representation)
      if representation.comments?
        comments_url = "/repos/#{repo}/issues/#{issuable.iid}/comments"
        fetch_comments(issuable, :comment, comments_url)
      end
    end

    def fetch_comments(noteable, type, url, klass = Note)
      while url
        comments = Github::Client.new(options).get(url)

        ActiveRecord::Base.no_touching do
          comments.body.each do |raw|
            begin
              representation  = Github::Representation::Comment.new(raw, options)
              author_id       = user_id(representation.author, project.creator_id)

              note            = klass.new
              note.project_id = project.id
              note.noteable   = noteable
              note.note       = format_description(representation.note, representation.author)
              note.commit_id  = representation.commit_id
              note.line_code  = representation.line_code
              note.author_id  = author_id
              note.created_at = representation.created_at
              note.updated_at = representation.updated_at
              note.save!(validate: false)
            rescue => e
              error(type, representation.url, e.message)
            end
          end
        end

        url = comments.rels[:next]
      end
    end

    def fetch_releases
      url = "/repos/#{repo}/releases"

      while url
        response = Github::Client.new(options).get(url)

        response.body.each do |raw|
          representation = Github::Representation::Release.new(raw)
          next unless representation.valid?

          release = ::Release.find_or_initialize_by(project_id: project.id, tag: representation.tag)
          next unless release.new_record?

          begin
            release.description = representation.description
            release.created_at  = representation.created_at
            release.updated_at  = representation.updated_at
            release.save!(validate: false)
          rescue => e
            error(:release, representation.url, e.message)
          end
        end

        url = response.rels[:next]
      end
    end

    def label_ids(labels)
      labels.map { |label| cached[:label_ids][label.title] }.compact
    end

    def assignee_ids(assignees)
      assignees.map { |assignee| user_id(assignee) }.compact
    end

    def milestone_id(milestone)
      return unless milestone.present?

      project.milestones.select(:id).find_by(iid: milestone.iid)&.id
    end

    def user_id(user, fallback_id = nil)
      return unless user.present?
      return cached[:user_ids][user.id] if cached[:user_ids][user.id].present?

      gitlab_user_id = user_id_by_external_uid(user.id) || user_id_by_email(user.email)

      cached[:gitlab_user_ids][user.id] = gitlab_user_id.present?
      cached[:user_ids][user.id] = gitlab_user_id || fallback_id
    end

    def user_id_by_email(email)
      return nil unless email

      ::User.find_by_any_email(email)&.id
    end

    def user_id_by_external_uid(id)
      return nil unless id

      ::User.select(:id)
            .joins(:identities)
            .merge(::Identity.where(provider: :github, extern_uid: id))
            .first&.id
    end

    def format_description(body, author)
      return body if cached[:gitlab_user_ids][author.id]

      "*Created by: #{author.username}*\n\n#{body}"
    end

    def expire_repository_cache
      repository.expire_content_cache if project.repository_exists?
    end

    def keep_track_of_errors
      return unless errors.any?

      project.update_column(:import_error, {
        message: 'The remote data could not be fully imported.',
        errors: errors
      }.to_json)
    end

    def error(type, url, message)
      errors << { type: type, url: Gitlab::UrlSanitizer.sanitize(url), error: message }
    end
  end
end