Class: Groundskeeper::Bitbucket

Inherits:
Object
  • Object
show all
Defined in:
lib/groundskeeper/bitbucket.rb

Overview

Bitbucket API wrapper

Defined Under Namespace

Classes: UnableToCreatePR

Constant Summary collapse

COMMAND =
"curl"
BITBUCKET_USERNAME =
"BITBUCKET_USERNAME"
BITBUCKET_APP_PASSWORD =
"BITBUCKET_APP_PASSWORD"
URL =
"https://api.bitbucket.org/2.0/repositories/"\
"isgmh-radd/%<repository>s/pullrequests"
CREATE_PR =
<<~REQUEST.split.join(" ")
  #{URL} -u %<username>s:%<password>s
  --request POST
  --header 'Content-Type: application/json'
  --data
  '{"title": "%<branch_name>s", "source": {"branch": {"name": "%<branch_name>s"}}}'
  --write-out ', HTTP_CODE: %%{http_code}'
REQUEST

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bitbucket) ⇒ Bitbucket

Returns a new instance of Bitbucket.



32
33
34
# File 'lib/groundskeeper/bitbucket.rb', line 32

def initialize(bitbucket)
  @bitbucket = bitbucket
end

Instance Attribute Details

#bitbucketObject (readonly)

Returns the value of attribute bitbucket.



26
27
28
# File 'lib/groundskeeper/bitbucket.rb', line 26

def bitbucket
  @bitbucket
end

Class Method Details

.buildObject



28
29
30
# File 'lib/groundskeeper/bitbucket.rb', line 28

def self.build
  new Executable.new(COMMAND)
end

Instance Method Details

#create_pr(repository:, branch_name:) ⇒ Object

rubocop:disable Metrics/MethodLength



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/groundskeeper/bitbucket.rb', line 37

def create_pr(repository:, branch_name:)
  response = bitbucket.execute(format(CREATE_PR,
                                      username: ENV[BITBUCKET_USERNAME],
                                      password: ENV[BITBUCKET_APP_PASSWORD],
                                      repository: repository,
                                      branch_name: branch_name))

  body = response.gsub(/, HTTP.*/, "")

  pr_url = nil
  begin
    pr_url = JSON.parse(body)["links"]["html"]["href"]
  rescue StandardError
    raise UnableToCreatePR, "BODY: #{response}"
  end

  open_url(pr_url)
end

#credentials?Boolean

rubocop:enable Metrics/MethodLength

Returns:

  • (Boolean)


57
58
59
# File 'lib/groundskeeper/bitbucket.rb', line 57

def credentials?
  ENV[BITBUCKET_USERNAME].present? && ENV[BITBUCKET_APP_PASSWORD].present?
end