Class: PullRequestAi::Bitbucket::Client

Inherits:
Repo::Client show all
Defined in:
lib/pull_request_ai/bitbucket/client.rb

Overview

A client to communicate with the Bitbucket API.

Instance Attribute Summary collapse

Attributes inherited from Repo::Client

#api_endpoint, #http_timeout

Instance Method Summary collapse

Methods inherited from Repo::Client

client_from_host

Constructor Details

#initialize(http_timeout: nil, api_endpoint: nil, app_password: nil, username: nil) ⇒ Client

Initializes the client.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pull_request_ai/bitbucket/client.rb', line 12

def initialize(
  http_timeout: nil,
  api_endpoint: nil,
  app_password: nil,
  username: nil
)
  super(
    http_timeout || PullRequestAi.http_timeout,
    api_endpoint || PullRequestAi.bitbucket_api_endpoint
  )
  @app_password = app_password || PullRequestAi.bitbucket_app_password
  @username = username || PullRequestAi.bitbucket_username
end

Instance Attribute Details

#app_passwordObject

Returns the value of attribute app_password.



7
8
9
# File 'lib/pull_request_ai/bitbucket/client.rb', line 7

def app_password
  @app_password
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/pull_request_ai/bitbucket/client.rb', line 8

def username
  @username
end

Instance Method Details

#open_pull_request(slug, head, base, title, description) ⇒ Object

Request to open a new Pull Request using the GitHub API. The slug combines the repository owner name and the repository name. It requires the head (destination branch), the base (current branch), the title, and a optional description. developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-post



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pull_request_ai/bitbucket/client.rb', line 77

def open_pull_request(slug, head, base, title, description)
  body = {
    title: title,
    source: {
      branch: {
        name: head
      }
    },
    destination: {
      branch: {
        name: base
      }
    },
    description: description
  }.to_json
  url = build_url(slug)
  request(:post, url, {}, body).bind do |pr|
    Dry::Monads::Success(parsed_pr_details(pr))
  end
end

#opened_pull_requests(slug, head, base) ⇒ Object

Requests the list of Open Pull Requests using the Bitbucket API. The slug combines the repository owner name and the repository name. The query contains the source and destination to filter the results.

developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pull_request_ai/bitbucket/client.rb', line 32

def opened_pull_requests(slug, head, base)
  query = {
    q: "source.branch.name = \"#{head}\" AND destination.branch.name = \"#{base}\""
  }
  url = build_url(slug)
  request(:get, url, query, {}).bind do |open_prs|
    if open_prs.empty? || open_prs['values'].empty?
      Dry::Monads::Success([])
    else
      result = open_prs['values'].map do |pr|
        parsed_pr_details(pr)
      end
      Dry::Monads::Success(result)
    end
  end
end

#update_pull_request(slug, number, base, title, description) ⇒ Object

Request to update the existing Pull Request using the Bitbucket API. The slug combines the repository owner name and the repository name. It requires the Pull Request id to modify it. The destination, title, and description can be modified. Notice: We don’t have logic to change the base on the UI. developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-put



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pull_request_ai/bitbucket/client.rb', line 56

def update_pull_request(slug, number, base, title, description)
  body = {
    title: title,
    destination: {
      branch: {
        name: base
      }
    },
    description: description
  }.to_json
  url = build_url(slug, "/#{number}")
  request(:put, url, {}, body).bind do |pr|
    Dry::Monads::Success(parsed_pr_details(pr))
  end
end