Class: Gitlab::GithubImport::Representation::PullRequest

Inherits:
Object
  • Object
show all
Includes:
ExposeAttribute, ToHash
Defined in:
lib/gitlab/github_import/representation/pull_request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExposeAttribute

#[]

Methods included from ToHash

#convert_value_for_to_hash, #to_hash

Constructor Details

#initialize(attributes) ⇒ PullRequest

attributes - A Hash containing the raw PR details. The keys of this

Hash (and any nested hashes) must be symbols.


67
68
69
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 67

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 10

def attributes
  @attributes
end

Class Method Details

.from_api_response(pr, additional_data = {}) ⇒ Object

Builds a PR from a GitHub API response.

issue - An instance of ‘Hash` containing the PR details.



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
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 21

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]

  hash = {
    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],
    merged_by: merged_by
  }

  new(hash)
end

.from_json_hash(raw_hash) ⇒ Object

Builds a new PR using a Hash that was built from a JSON payload.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 51

def self.from_json_hash(raw_hash)
  hash = Representation.symbolize_hash(raw_hash)

  hash[:state] = hash[:state].to_sym
  hash[:author] &&= Representation::User.from_json_hash(hash[:author])

  # Assignees are optional so we only convert it from a Hash if one was
  # set.
  hash[:assignee] &&= Representation::User.from_json_hash(hash[:assignee])
  hash[:merged_by] &&= Representation::User.from_json_hash(hash[:merged_by])

  new(hash)
end

Instance Method Details

#cross_project?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 101

def cross_project?
  return true unless source_repository_id

  source_repository_id != target_repository_id
end

#formatted_source_branchObject

Returns a formatted source branch.

For cross-project pull requests the branch name will be in the format ‘github/fork/owner-name/branch-name`.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 79

def formatted_source_branch
  if cross_project? && source_repository_owner
    "github/fork/#{source_repository_owner}/#{source_branch}"
  elsif source_branch == target_branch
    # Sometimes the source and target branch are the same, but GitLab
    # doesn't support this. This can happen when both the user and
    # source repository have been deleted, and the PR was submitted from
    # the fork's master branch.
    "#{source_branch}-#{iid}"
  else
    source_branch
  end
end

#github_identifiersObject



111
112
113
114
115
116
117
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 111

def github_identifiers
  {
    iid: iid,
    issuable_type: issuable_type,
    title: title
  }
end

#issuable_typeObject



107
108
109
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 107

def issuable_type
  'MergeRequest'
end

#stateObject



93
94
95
96
97
98
99
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 93

def state
  if merged_at
    :merged
  else
    attributes[:state]
  end
end

#truncated_titleObject



71
72
73
# File 'lib/gitlab/github_import/representation/pull_request.rb', line 71

def truncated_title
  title.truncate(255)
end