Class: GitReflow::GitServer::BitBucket::PullRequest

Inherits:
PullRequest
  • Object
show all
Defined in:
lib/git_reflow/git_server/bit_bucket/pull_request.rb

Constant Summary

Constants inherited from PullRequest

PullRequest::DEFAULT_APPROVAL_REGEX

Instance Attribute Summary

Attributes inherited from PullRequest

#base_branch_name, #build, #description, #feature_branch_name, #html_url, #number, #source_object

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PullRequest

#all_comments_addressed?, #approval_minimums_reached?, approval_regex, #approved?, #build_status, #cleanup_failure_message, #cleanup_feature_branch?, #cleanup_local_feature_branch?, #cleanup_remote_feature_branch?, #commit_message_for_merge, #deliver?, #display_pull_request_summary, #good_to_merge?, #has_comments?, #merge!, #method_missing, minimum_approvals, #rejection_message, #reviewers_pending_response

Constructor Details

#initialize(attributes) ⇒ PullRequest

Returns a new instance of PullRequest.



7
8
9
10
11
12
13
14
15
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 7

def initialize(attributes)
  self.number              = attributes.id
  self.description         = attributes.body || attributes.description
  self.html_url            = "#{attributes.source.repository.links.html.href}/pull-request/#{self.number}"
  self.feature_branch_name = attributes.source.branch.name[/[^:]+$/]
  self.base_branch_name    = attributes.destination.branch.name[/[^:]+$/]
  self.build               = Build.new
  self.source_object       = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GitReflow::GitServer::PullRequest

Class Method Details

.create(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 17

def self.create(options = {})
  self.new GitReflow.git_server.connection.repos.pull_requests.create(
    GitReflow.git_server.class.remote_user,
    GitReflow.git_server.class.remote_repo_name,
    title: options[:title],
    description: options[:body] || options[:description],
    source: {
      branch: { name: GitReflow.git_server.class.current_branch },
      repository: { full_name: "#{GitReflow.git_server.class.remote_user}/#{GitReflow.git_server.class.remote_repo_name}" }
    },
    destination: {
      branch: { name: options[:base] }
    },
    reviewers: [username: GitReflow.git_server.class.user])
end

.find_open(to: 'master', from: GitReflow.git_server.class.current_branch) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 33

def self.find_open(to: 'master', from: GitReflow.git_server.class.current_branch)
  begin
    matching_pull = GitReflow.git_server.connection.repos.pull_requests.all(GitReflow.git_server.class.remote_user, GitReflow.git_server.class.remote_repo_name, limit: 1).select do |pr|
      pr.source.branch.name == from and
      pr.destination.branch.name == to
    end.first

    if matching_pull
      self.new matching_pull
    end
  rescue ::BitBucket::Error::NotFound => e
    GitReflow.git_server.say "No BitBucket repo found for #{GitReflow.git_server.class.remote_user}/#{GitReflow.git_server.class.remote_repo_name}", :error
  rescue ::BitBucket::Error::Forbidden => e
    GitReflow.git_server.say "You don't have API access to this repo", :error
  end
end

Instance Method Details

#approvalsObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 70

def approvals
  approved  = []

  GitReflow.git_server.connection.repos.pull_requests.activity(GitReflow.git_server.class.remote_user, GitReflow.git_server.class.remote_repo_name, self.id).each do |activity|
    break unless activity.respond_to?(:approval) and activity.approval.user.username != GitReflow.git_server.class.user
    approved |= [activity.approval.user.username]
  end

  approved
end

#commentsObject



55
56
57
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 55

def comments
  GitReflow.git_server.connection.repos.pull_requests.comments.all(GitReflow.git_server.class.remote_user, GitReflow.git_server.class.remote_repo_name, self.id)
end

#commit_authorObject



50
51
52
53
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 50

def commit_author
  # use the author of the pull request
  self.author.username
end

#last_commentObject



59
60
61
62
63
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 59

def last_comment
  last_comment = comments.first
  return "" unless last_comment
  "#{last_comment.content.raw}"
end

#reviewersObject



65
66
67
68
# File 'lib/git_reflow/git_server/bit_bucket/pull_request.rb', line 65

def reviewers
  return [] unless comments.size > 0
  comments.map {|c| c.user.username }.uniq - [GitReflow.git_server.class.user]
end