Class: CiToolkit::BitriseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ci_toolkit/bitrise_client.rb

Overview

client to use with the Bitrise API

Constant Summary collapse

API_VERSION =
"v0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { build_number: ENV.fetch("BITRISE_BUILD_NUMBER", nil), token: ENV.fetch("BITRISE_TOKEN", nil), app_slug: ENV.fetch("BITRISE_APP_SLUG", nil), build_slug: ENV.fetch("BITRISE_BUILD_SLUG", nil) }, faraday = nil) ⇒ BitriseClient

noinspection Metrics/ParameterLists



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ci_toolkit/bitrise_client.rb', line 13

def initialize(options = {
  build_number: ENV.fetch("BITRISE_BUILD_NUMBER", nil),
  token: ENV.fetch("BITRISE_TOKEN", nil),
  app_slug: ENV.fetch("BITRISE_APP_SLUG", nil),
  build_slug: ENV.fetch("BITRISE_BUILD_SLUG", nil)
},
               faraday = nil)
  @build_number = options[:build_number].to_i
  @token = options[:token]
  @app_slug = options[:app_slug]
  @build_slug = options[:build_slug]
  @connection = faraday || create_connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/ci_toolkit/bitrise_client.rb', line 10

def connection
  @connection
end

Instance Method Details

#abort_pull_request_builds(pull_request, branch, commit = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ci_toolkit/bitrise_client.rb', line 54

def abort_pull_request_builds(pull_request, branch, commit = nil)
  find_pull_request_builds(pull_request, branch, commit).each do |build|
    next if build["slug"] == @build_slug

    @connection.post("/#{API_VERSION}/apps/#{@app_slug}/builds/#{build["slug"]}/abort", {
                       abort_reason: "Aborting due to other build failed for pull request #{pull_request}"
                     })
  end
end

#create_connectionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ci_toolkit/bitrise_client.rb', line 27

def create_connection
  connection = Faraday.new(
    url: "https://api.bitrise.io",
    headers: { "Content-Type" => "application/json", "Authorization" => @token }
  )
  connection.use Faraday::Request::UrlEncoded
  connection.use Faraday::Request::Retry
  connection.use FaradayMiddleware::EncodeJson
  connection.use FaradayMiddleware::ParseJson
  connection.use FaradayMiddleware::FollowRedirects

  connection
end

#create_pull_request_build(pull_request, branch, commit, workflow) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ci_toolkit/bitrise_client.rb', line 41

def create_pull_request_build(pull_request, branch, commit, workflow)
  @connection.post("/#{API_VERSION}/apps/#{@app_slug}/builds", {
                     hook_info: { type: "bitrise" },
                     build_params: {
                       branch:,
                       branch_dest: "develop",
                       pull_request_id: pull_request,
                       workflow_id: workflow,
                       commit_hash: commit
                     }
                   })
end

#filter_builds_by_commit(builds, commit) ⇒ Object



75
76
77
78
# File 'lib/ci_toolkit/bitrise_client.rb', line 75

def filter_builds_by_commit(builds, commit)
  builds&.select! { |build| build["commit_hash"] == commit && build["build_number"] != @build_number }
  builds || []
end

#find_pull_request_builds(pull_request, branch, commit = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/ci_toolkit/bitrise_client.rb', line 64

def find_pull_request_builds(pull_request, branch, commit = nil)
  response = @connection.get("/#{API_VERSION}/apps/#{@app_slug}/builds", {
                               branch:,
                               pull_request_id: pull_request.to_i,
                               status: 0 # status: 0 == not finished
                             })
  builds = response.body["data"] || []
  builds = filter_builds_by_commit(builds, commit) unless commit.nil?
  builds
end