diff options
Diffstat (limited to 'lib/gitlab/github_import/representation')
10 files changed, 152 insertions, 128 deletions
diff --git a/lib/gitlab/github_import/representation/diff_note.rb b/lib/gitlab/github_import/representation/diff_note.rb index 64aa6ea5cb4..f3be90834c7 100644 --- a/lib/gitlab/github_import/representation/diff_note.rb +++ b/lib/gitlab/github_import/representation/diff_note.rb @@ -19,33 +19,33 @@ module Gitlab # Builds a diff note from a GitHub API response. # - # note - An instance of `Sawyer::Resource` containing the note details. + # note - An instance of `Hash` containing the note details. def self.from_api_response(note, additional_data = {}) - matches = note.html_url.match(NOTEABLE_ID_REGEX) + matches = note[:html_url].match(NOTEABLE_ID_REGEX) unless matches raise( ArgumentError, - "The note URL #{note.html_url.inspect} is not supported" + "The note URL #{note[:html_url].inspect} is not supported" ) end - user = Representation::User.from_api_response(note.user) if note.user + user = Representation::User.from_api_response(note[:user]) if note[:user] hash = { noteable_id: matches[:iid].to_i, - file_path: note.path, - commit_id: note.commit_id, - original_commit_id: note.original_commit_id, - diff_hunk: note.diff_hunk, + file_path: note[:path], + commit_id: note[:commit_id], + original_commit_id: note[:original_commit_id], + diff_hunk: note[:diff_hunk], author: user, - note: note.body, - created_at: note.created_at, - updated_at: note.updated_at, - note_id: note.id, - end_line: note.line, - start_line: note.start_line, - side: note.side, - in_reply_to_id: note.in_reply_to_id + note: note[:body], + created_at: note[:created_at], + updated_at: note[:updated_at], + note_id: note[:id], + end_line: note[:line], + start_line: note[:start_line], + side: note[:side], + in_reply_to_id: note[:in_reply_to_id] } new(hash) diff --git a/lib/gitlab/github_import/representation/issue.rb b/lib/gitlab/github_import/representation/issue.rb index 9d457ec1c2f..e878aeaf3b9 100644 --- a/lib/gitlab/github_import/representation/issue.rb +++ b/lib/gitlab/github_import/representation/issue.rb @@ -15,28 +15,28 @@ module Gitlab # Builds an issue from a GitHub API response. # - # issue - An instance of `Sawyer::Resource` containing the issue + # issue - An instance of `Hash` containing the issue # details. def self.from_api_response(issue, additional_data = {}) user = - if issue.user - Representation::User.from_api_response(issue.user) + if issue[:user] + Representation::User.from_api_response(issue[:user]) end hash = { - iid: issue.number, - title: issue.title, - description: issue.body, - milestone_number: issue.milestone&.number, - state: issue.state == 'open' ? :opened : :closed, - assignees: issue.assignees.map do |u| + iid: issue[:number], + title: issue[:title], + description: issue[:body], + milestone_number: issue.dig(:milestone, :number), + state: issue[:state] == 'open' ? :opened : :closed, + assignees: issue[:assignees].map do |u| Representation::User.from_api_response(u) end, - label_names: issue.labels.map(&:name), + label_names: issue[:labels].map { _1[:name] }, author: user, - created_at: issue.created_at, - updated_at: issue.updated_at, - pull_request: issue.pull_request ? true : false, + created_at: issue[:created_at], + updated_at: issue[:updated_at], + pull_request: issue[:pull_request] ? true : false, work_item_type_id: additional_data[:work_item_type_id] } diff --git a/lib/gitlab/github_import/representation/issue_event.rb b/lib/gitlab/github_import/representation/issue_event.rb index 89271a7dcd6..39a23c016ce 100644 --- a/lib/gitlab/github_import/representation/issue_event.rb +++ b/lib/gitlab/github_import/representation/issue_event.rb @@ -34,23 +34,23 @@ module Gitlab class << self # Builds an event from a GitHub API response. # - # event - An instance of `Sawyer::Resource` containing the event details. + # event - An instance of `Hash` containing the event details. def from_api_response(event, additional_data = {}) new( - id: event.id, - actor: user_representation(event.actor), - event: event.event, - commit_id: event.commit_id, - label_title: event.label && event.label[:name], - old_title: event.rename && event.rename[:from], - new_title: event.rename && event.rename[:to], - milestone_title: event.milestone && event.milestone[:title], - issue: event.issue&.to_h&.symbolize_keys, - source: event.source, - assignee: user_representation(event.assignee), - requested_reviewer: user_representation(event.requested_reviewer), - review_requester: user_representation(event.review_requester), - created_at: event.created_at + id: event[:id], + actor: user_representation(event[:actor]), + event: event[:event], + commit_id: event[:commit_id], + label_title: event.dig(:label, :name), + old_title: event.dig(:rename, :from), + new_title: event.dig(:rename, :to), + milestone_title: event.dig(:milestone, :title), + issue: event[:issue], + source: event[:source], + assignee: user_representation(event[:assignee]), + requested_reviewer: user_representation(event[:requested_reviewer]), + review_requester: user_representation(event[:review_requester]), + created_at: event[:created_at] ) end diff --git a/lib/gitlab/github_import/representation/note.rb b/lib/gitlab/github_import/representation/note.rb index ae56c370b19..14379e8a4e9 100644 --- a/lib/gitlab/github_import/representation/note.rb +++ b/lib/gitlab/github_import/representation/note.rb @@ -16,14 +16,14 @@ module Gitlab # Builds a note from a GitHub API response. # - # note - An instance of `Sawyer::Resource` containing the note details. + # note - An instance of `Hash` containing the note details. def self.from_api_response(note, additional_data = {}) - matches = note.html_url.match(NOTEABLE_TYPE_REGEX) + matches = note[:html_url].match(NOTEABLE_TYPE_REGEX) if !matches || !matches[:type] raise( ArgumentError, - "The note URL #{note.html_url.inspect} is not supported" + "The note URL #{note[:html_url].inspect} is not supported" ) end @@ -34,15 +34,15 @@ module Gitlab 'Issue' end - user = Representation::User.from_api_response(note.user) if note.user + user = Representation::User.from_api_response(note[:user]) if note[:user] hash = { noteable_type: noteable_type, noteable_id: matches[:iid].to_i, author: user, - note: note.body, - created_at: note.created_at, - updated_at: note.updated_at, - note_id: note.id + note: note[:body], + created_at: note[:created_at], + updated_at: note[:updated_at], + note_id: note[:id] } new(hash) diff --git a/lib/gitlab/github_import/representation/note_text.rb b/lib/gitlab/github_import/representation/note_text.rb new file mode 100644 index 00000000000..505d7d805d3 --- /dev/null +++ b/lib/gitlab/github_import/representation/note_text.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# This class only partly represents MODELS_ALLOWLIST records from DB and +# is used to connect ReleasesAttachmentsImporter, NotesAttachmentsImporter etc. +# with NoteAttachmentsImporter without modifying ObjectImporter a lot. +# Attachments are inside release's `description`. +module Gitlab + module GithubImport + module Representation + class NoteText + include ToHash + include ExposeAttribute + + MODELS_ALLOWLIST = [::Release, ::Note, ::Issue, ::MergeRequest].freeze + ModelNotSupported = Class.new(StandardError) + + attr_reader :attributes + + expose_attribute :record_db_id, :record_type, :text + + class << self + # Builds a note text representation from DB record of Note or Release. + # + # record - An instance of `Note`, `Release`, `Issue`, `MergeRequest` model + def from_db_record(record) + check_record_class!(record) + + record_type = record.class.name + # only column for note is different along MODELS_ALLOWLIST + text = record.is_a?(::Note) ? record.note : record.description + new( + record_db_id: record.id, + record_type: record_type, + text: text + ) + end + + def from_json_hash(raw_hash) + new Representation.symbolize_hash(raw_hash) + end + + private + + def check_record_class!(record) + raise ModelNotSupported, record.class.name if MODELS_ALLOWLIST.exclude?(record.class) + end + end + + # attributes - A Hash containing the event details. The keys of this + # Hash (and any nested hashes) must be symbols. + def initialize(attributes) + @attributes = attributes + end + + def github_identifiers + { db_id: record_db_id } + end + end + end + end +end diff --git a/lib/gitlab/github_import/representation/protected_branch.rb b/lib/gitlab/github_import/representation/protected_branch.rb index b80b7cf1076..07a607ae70d 100644 --- a/lib/gitlab/github_import/representation/protected_branch.rb +++ b/lib/gitlab/github_import/representation/protected_branch.rb @@ -9,18 +9,22 @@ module Gitlab attr_reader :attributes - expose_attribute :id, :allow_force_pushes + expose_attribute :id, :allow_force_pushes, :required_conversation_resolution, :required_signatures, + :required_pull_request_reviews # Builds a Branch Protection info from a GitHub API response. # Resource structure details: # https://docs.github.com/en/rest/branches/branch-protection#get-branch-protection - # branch_protection - An instance of `Sawyer::Resource` containing the protection details. + # branch_protection - An instance of `Hash` containing the protection details. def self.from_api_response(branch_protection, _additional_object_data = {}) - branch_name = branch_protection.url.match(%r{/branches/(\S{1,255})/protection$})[1] + branch_name = branch_protection[:url].match(%r{/branches/(\S{1,255})/protection$})[1] hash = { id: branch_name, - allow_force_pushes: branch_protection.allow_force_pushes.enabled + allow_force_pushes: branch_protection.dig(:allow_force_pushes, :enabled), + required_conversation_resolution: branch_protection.dig(:required_conversation_resolution, :enabled), + required_signatures: branch_protection.dig(:required_signatures, :enabled), + required_pull_request_reviews: branch_protection[:required_pull_request_reviews].present? } new(hash) diff --git a/lib/gitlab/github_import/representation/pull_request.rb b/lib/gitlab/github_import/representation/pull_request.rb index 2adac2af502..4b8ae1f8eab 100644 --- a/lib/gitlab/github_import/representation/pull_request.rb +++ b/lib/gitlab/github_import/representation/pull_request.rb @@ -17,30 +17,30 @@ module Gitlab # Builds a PR from a GitHub API response. # - # issue - An instance of `Sawyer::Resource` containing the PR details. + # issue - An instance of `Hash` containing the PR details. def self.from_api_response(pr, additional_data = {}) - assignee = Representation::User.from_api_response(pr.assignee) if pr.assignee - user = Representation::User.from_api_response(pr.user) if pr.user - merged_by = Representation::User.from_api_response(pr.merged_by) if pr.merged_by + assignee = Representation::User.from_api_response(pr[:assignee]) if pr[:assignee] + user = Representation::User.from_api_response(pr[:user]) if pr[:user] + merged_by = Representation::User.from_api_response(pr[:merged_by]) if pr[:merged_by] hash = { - iid: pr.number, - title: pr.title, - description: pr.body, - source_branch: pr.head.ref, - target_branch: pr.base.ref, - source_branch_sha: pr.head.sha, - target_branch_sha: pr.base.sha, - source_repository_id: pr.head&.repo&.id, - target_repository_id: pr.base&.repo&.id, - source_repository_owner: pr.head&.user&.login, - state: pr.state == 'open' ? :opened : :closed, - milestone_number: pr.milestone&.number, + iid: pr[:number], + title: pr[:title], + description: pr[:body], + source_branch: pr.dig(:head, :ref), + target_branch: pr.dig(:base, :ref), + source_branch_sha: pr.dig(:head, :sha), + target_branch_sha: pr.dig(:base, :sha), + source_repository_id: pr.dig(:head, :repo, :id), + target_repository_id: pr.dig(:base, :repo, :id), + source_repository_owner: pr.dig(:head, :user, :login), + state: pr[:state] == 'open' ? :opened : :closed, + milestone_number: pr.dig(:milestone, :number), author: user, assignee: assignee, - created_at: pr.created_at, - updated_at: pr.updated_at, - merged_at: pr.merged_at, + created_at: pr[:created_at], + updated_at: pr[:updated_at], + merged_at: pr[:merged_at], merged_by: merged_by } diff --git a/lib/gitlab/github_import/representation/pull_request_review.rb b/lib/gitlab/github_import/representation/pull_request_review.rb index 8a7ecf0c588..8fb57ae89a4 100644 --- a/lib/gitlab/github_import/representation/pull_request_review.rb +++ b/lib/gitlab/github_import/representation/pull_request_review.rb @@ -11,16 +11,19 @@ module Gitlab expose_attribute :author, :note, :review_type, :submitted_at, :merge_request_id, :review_id + # Builds a PullRequestReview from a GitHub API response. + # + # review - An instance of `Hash` containing the note details. def self.from_api_response(review, additional_data = {}) - user = Representation::User.from_api_response(review.user) if review.user + user = Representation::User.from_api_response(review[:user]) if review[:user] new( - merge_request_id: review.merge_request_id, + merge_request_id: review[:merge_request_id], author: user, - note: review.body, - review_type: review.state, - submitted_at: review.submitted_at, - review_id: review.id + note: review[:body], + review_type: review[:state], + submitted_at: review[:submitted_at], + review_id: review[:id] ) end diff --git a/lib/gitlab/github_import/representation/release_attachments.rb b/lib/gitlab/github_import/representation/release_attachments.rb deleted file mode 100644 index fd272be2405..00000000000 --- a/lib/gitlab/github_import/representation/release_attachments.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true - -# This class only partly represents Release record from DB and -# is used to connect ReleasesAttachmentsImporter with ReleaseAttachmentsImporter -# without modifying ObjectImporter a lot. -# Attachments are inside release's `description`. -module Gitlab - module GithubImport - module Representation - class ReleaseAttachments - include ToHash - include ExposeAttribute - - attr_reader :attributes - - expose_attribute :release_db_id, :description - - # Builds a event from a GitHub API response. - # - # release - An instance of `Release` model. - def self.from_db_record(release) - new( - release_db_id: release.id, - description: release.description - ) - end - - def self.from_json_hash(raw_hash) - new Representation.symbolize_hash(raw_hash) - end - - # attributes - A Hash containing the event details. The keys of this - # Hash (and any nested hashes) must be symbols. - def initialize(attributes) - @attributes = attributes - end - - def github_identifiers - { db_id: release_db_id } - end - end - end - end -end diff --git a/lib/gitlab/github_import/representation/user.rb b/lib/gitlab/github_import/representation/user.rb index 4ef916cc41c..02cbe037384 100644 --- a/lib/gitlab/github_import/representation/user.rb +++ b/lib/gitlab/github_import/representation/user.rb @@ -13,11 +13,11 @@ module Gitlab # Builds a user from a GitHub API response. # - # user - An instance of `Sawyer::Resource` containing the user details. + # user - An instance of `Hash` containing the user details. def self.from_api_response(user, additional_data = {}) new( - id: user.id, - login: user.login + id: user[:id], + login: user[:login] ) end |