Sha256: 25cdf4383060c2ac44739ed4de78ec0c5cd5110075a0a7c8dc26a4920ade6ccc
Contents?: true
Size: 1.83 KB
Versions: 8
Compression:
Stored size: 1.83 KB
Contents
# frozen_string_literal: true require "json" module Groundskeeper # Bitbucket API wrapper class Bitbucket # Bitbucket API Error class UnableToCreatePR < StandardError end 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 attr_reader :bitbucket def self.build new Executable.new(COMMAND) end def initialize(bitbucket) @bitbucket = bitbucket end # rubocop:disable Metrics/MethodLength def create_pr(repository:, branch_name:) response = bitbucket.execute(format(CREATE_PR, username: ENV.fetch(BITBUCKET_USERNAME, nil), password: ENV.fetch(BITBUCKET_APP_PASSWORD, nil), 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 # rubocop:enable Metrics/MethodLength def credentials? !ENV.fetch(BITBUCKET_USERNAME, "").empty? && !ENV.fetch(BITBUCKET_APP_PASSWORD, "").empty? end private # :nocov: def open_url(url) `open #{url}` end # :nocov: end end
Version data entries
8 entries across 8 versions & 1 rubygems