Class: BitBucket::Client::Repos::PullRequests

Inherits:
API
  • Object
show all
Defined in:
lib/bitbucket_rest_api/client/repos/pull_requests.rb

Defined Under Namespace

Classes: Comments, Commits

Constant Summary collapse

REQUIRED_PULL_REQUEST_OPTIONS =
%w[
  title
  source
]
VALID_PULL_REQUEST_PARAM_NAMES =
%w[
  title
  description
  source
  destination
  reviewers
  close_source_branch
].freeze
VALID_PULL_REQUEST_STATE_VALUES =
{
  state: ['OPEN', 'MERGED', 'DECLINED']
}

Constants included from Validations

Validations::VALID_API_KEYS

Constants included from Validations::Token

Validations::Token::TOKEN_REQUIRED, Validations::Token::TOKEN_REQUIRED_REGEXP

Constants included from BitBucket::Constants

BitBucket::Constants::ACCEPT, BitBucket::Constants::ACCEPT_CHARSET, BitBucket::Constants::CACHE_CONTROL, BitBucket::Constants::CONTENT_LENGTH, BitBucket::Constants::CONTENT_TYPE, BitBucket::Constants::DATE, BitBucket::Constants::ETAG, BitBucket::Constants::LOCATION, BitBucket::Constants::META_FIRST, BitBucket::Constants::META_LAST, BitBucket::Constants::META_NEXT, BitBucket::Constants::META_PREV, BitBucket::Constants::META_REL, BitBucket::Constants::PARAM_PAGE, BitBucket::Constants::PARAM_START_PAGE, BitBucket::Constants::QUERY_STR_SEP, BitBucket::Constants::RATELIMIT_LIMIT, BitBucket::Constants::RATELIMIT_REMAINING, BitBucket::Constants::SERVER, BitBucket::Constants::USER_AGENT

Instance Attribute Summary

Attributes inherited from API

#current_options

Instance Method Summary collapse

Methods inherited from API

#_merge_user_into_params!, #_merge_user_repo_into_params!, #_update_user_repo_params, #api_methods_in, #append_arguments, #arguments, extract_class_name, inherited, #initialize, #method_missing, namespace, #process_basic_auth, #set, #set_api_client, #setup, #with, #yield_or_eval

Methods included from BitBucket::ClassMethods

#configuration, #configure, #require_all

Methods included from Normalizer

#normalize!

Methods included from ParameterFilter

#filter!

Methods included from Validations::Required

#assert_required_keys

Methods included from Validations::Token

#validates_token_for

Methods included from Validations::Format

#assert_valid_values

Methods included from Validations::Presence

#assert_presence_of

Methods included from Request::Verbs

#delete_request, #get_request, #options_request, #patch_request, #post_request, #put_request

Methods included from Authorization

#authenticated?, #authentication, #basic_authed?

Constructor Details

This class inherits a constructor from BitBucket::API

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BitBucket::API

Instance Method Details

#activity(*args) ⇒ Object

Get a log of all activity for a pull request

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.activity 'user-name', 'repo-name'


190
191
192
193
194
195
196
197
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 190

def activity(*args)
  arguments(args, required: [:user, :repo, :pull_request_id])

  response = get_request("/repositories/#{arguments.user}/#{arguments.repo}/pullrequests/#{arguments.pull_request_id}/activity")

  return response unless block_given?
  response.each { |el| yield el }
end

#approve(user_name, repo_name, pull_request_id) ⇒ Object

Give approval on a pull request

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.approve 'user-name', 'repo-name', 'pull-request-id'


166
167
168
169
170
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 166

def approve(user_name, repo_name, pull_request_id)
  arguments(args, required: [:user, :repo, :pull_request_id])

  post_request("/repositories/#{arguments.user}/#{arguments.repo}/pullrequests/#{arguments.pull_request_id}/approve")
end

#create(*args) ⇒ Object

Create a pull request

Inputs

<tt>:title</tt> - Required string
<tt>:description</tt> - Optional string
<tt>:source</tt> - Required hash - The source branch name and/or repository (for example, { develop)
* <tt>{ "branch": { "name": "REQUIRED branch_name" }, "repository": { "full_name": "owner/repo_slug" } }</tt>
<tt>:destination</tt> - Optional hash - The destination branch or commit
* <tt>{ "branch": { "name": "branch_name" }, "commit": { "hash": "name" } }</tt>
<tt>:reviewers</tt> - Optional array - Users currently reviewiing the pull
* <tt>[{ "username": "accountname" }]</tt>

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.create 'user-name', 'repo-name',
  "title" => "Fixes a bug",
  "description" => "Fixes not being able to see anything.",
  "source" => { "branch" => { "name" => "bug-fixes" } },
  "destination" => { "branch" => { "name" => "master" } },
  "reviewers" => [ { "username" => "octocat" } ],
  "close_source_branch" => true


93
94
95
96
97
98
99
100
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 93

def create(*args)
  arguments(args, required: [:user, :repo]) do
    permit VALID_PULL_REQUEST_PARAM_NAMES
    assert_required REQUIRED_PULL_REQUEST_OPTIONS
  end

  post_request("/repositories/#{arguments.user}/#{arguments.repo.downcase}/pullrequests", arguments.params)
end

#decline(user_name, repo_name, pull_request_id) ⇒ Object Also known as: reject

Decline or reject a single pull request

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.reject 'user-name', 'repo-name', 'pull-request-id'


153
154
155
156
157
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 153

def decline(user_name, repo_name, pull_request_id)
  arguments(args, required: [:user, :repo, :pull_request_id])

  post_request("/repositories/#{arguments.user}/#{arguments.repo}/pullrequests/#{arguments.pull_request_id}/decline")
end

#diff(user_name, repo_name, pull_request_id) ⇒ Object

Get the diff for a pull request

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.diff 'user-name', 'repo-name', 'pull-request-id'


178
179
180
181
182
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 178

def diff(user_name, repo_name, pull_request_id)
  arguments(args, required: [:user, :repo, :pull_request_id])

  get_request("/repositories/#{arguments.user}/#{arguments.repo}/pullrequests/#{arguments.pull_request_id}/diff")
end

#get(*args) ⇒ Object Also known as: find

Get a single pull request

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.find 'user-name', 'repo-name', 'pull-request-id'


63
64
65
66
67
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 63

def get(*args)
  arguments(args, required: [:user, :repo, :pull_request_id])

  get_request("/repositories/#{arguments.user}/#{arguments.repo.downcase}/pullrequests/#{arguments.pull_request_id}", arguments.params)
end

#list(*args) ⇒ Object Also known as: all

List pull requests for a repository

Inputs

<tt>:state</tt> - Optional - State of the pull request (OPEN, MERGED, DECLINED)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 40

def list(*args)
  arguments(args, required: [:user, :repo], optional: [:state])
  params = arguments.params
  user   = arguments.user
  repo   = arguments.repo

  params['state'] ||= 'OPEN'
  # Bitbucket requires the state to be all caps or it returns all
  params['state']   = params['state'].upcase

  response = get_request("/repositories/#{user}/#{repo}/pullrequests/", params)

  return response unless block_given?
  response.each { |el| yield el }
end

#update(*args) ⇒ Object Also known as: edit

Edit a pull request

Inputs

<tt>:title</tt> - Required string
<tt>:description</tt> - Optional string
<tt>:destination</tt> - Optional hash - The destination branch or commit
* <tt>{ "branch": { "name": "branch_name" }, "commit": { "hash": "name" } }</tt>
<tt>:reviewers</tt> - Optional array - Users currently reviewiing the pull
* <tt>[{ "username": "accountname" }]</tt>

Examples

bitbucket = BitBucket.new
bitbucket.repos.pull_requests.update 'user-name', 'repo-name', 'pull-request-id',
  "title" => "Fixes a bug",
  "description" => "Fixes not being able to see anything.",
  "destination" => { "branch" => { "name" => "master" } },
  "reviewers" => [ { "username" => "octocat" } ],
  "close_source_branch" => true


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bitbucket_rest_api/client/repos/pull_requests.rb', line 121

def update(*args)
  arguments(args, required: [:user, :repo, :pull_request_id]) do
    permit VALID_PULL_REQUEST_PARAM_NAMES
  end

  user            = arguments.user
  repo            = arguments.repo
  pull_request_id = arguments.pull_request_id

  # BitBucket will drop any data if it is not included, so we have to check for pre-existing data
  existing_pull = get(user, repo, pull_request_id)
  existing_pull_data = {
    'title' => existing_pull.title,
    'description' => existing_pull.description,
    'destination' => {
      'branch' => existing_pull.destination.branch
    },
    'reviewers' => existing_pull.reviewers,
    'close_source_branch' => existing_pull.close_source_branch
  }
  params = normalize!(existing_pull_data).merge!(normalize!(arguments.params))

  put_request("/repositories/#{user}/#{repo.downcase}/pullrequests/#{pull_request_id}/", params)
end