Class: UnofficialBuildkiteClient

Inherits:
Object
  • Object
show all
Defined in:
lib/unofficial_buildkite_client.rb,
lib/unofficial_buildkite_client/version.rb,
lib/unofficial_buildkite_client/http_client.rb

Defined Under Namespace

Classes: Error, HttpClient

Constant Summary collapse

GRAPHQL_ENDPOINT =
"https://graphql.buildkite.com/v1"
INTERNAL_API_HOST =
"https://buildkite.com"
VERSION =
"0.4.0"

Instance Method Summary collapse

Constructor Details

#initialize(access_token: ENV["BUILDKITE_ACCESS_TOKEN"], org_slug: nil, pipeline_slug: nil, logger: Logger.new(STDERR)) ⇒ UnofficialBuildkiteClient

Returns a new instance of UnofficialBuildkiteClient.



11
12
13
14
15
# File 'lib/unofficial_buildkite_client.rb', line 11

def initialize(access_token: ENV["BUILDKITE_ACCESS_TOKEN"], org_slug: nil, pipeline_slug: nil, logger: Logger.new(STDERR))
  @client = HttpClient.new(authorization_header: "Bearer #{access_token}", logger: logger)
  @org_slug = org_slug
  @pipeline_slug = pipeline_slug
end

Instance Method Details

#fetch_artifact(org_slug: @org_slug, pipeline_slug: @pipeline_slug, build_number:, job_id:, artifact_id:) ⇒ Object



63
64
65
# File 'lib/unofficial_buildkite_client.rb', line 63

def fetch_artifact(org_slug: @org_slug, pipeline_slug: @pipeline_slug, build_number:, job_id:, artifact_id:)
  @client.request(:get, "#{INTERNAL_API_HOST}/organizations/#{org_slug}/pipelines/#{pipeline_slug}/builds/#{build_number}/jobs/#{job_id}/artifacts/#{artifact_id}", json: false)
end

#fetch_artifacts(org_slug: @org_slug, pipeline_slug: @pipeline_slug, build_number:, job_id:) ⇒ Object



59
60
61
# File 'lib/unofficial_buildkite_client.rb', line 59

def fetch_artifacts(org_slug: @org_slug, pipeline_slug: @pipeline_slug, build_number:, job_id:)
  @client.request(:get, "#{INTERNAL_API_HOST}/organizations/#{org_slug}/pipelines/#{pipeline_slug}/builds/#{build_number}/jobs/#{job_id}/artifacts")
end

#fetch_build(org_slug: @org_slug, pipeline_slug: @pipeline_slug, number:) ⇒ Object



55
56
57
# File 'lib/unofficial_buildkite_client.rb', line 55

def fetch_build(org_slug: @org_slug, pipeline_slug: @pipeline_slug, number:)
  @client.request(:get, "#{INTERNAL_API_HOST}/#{org_slug}/#{pipeline_slug}/builds/#{number}")
end

#fetch_builds(org_slug: @org_slug, pipeline_slug: @pipeline_slug, created_at_from: nil, first:, state: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/unofficial_buildkite_client.rb', line 17

def fetch_builds(org_slug: @org_slug, pipeline_slug: @pipeline_slug, created_at_from: nil, first:, state: nil)
  variables = {slug: "#{org_slug}/#{pipeline_slug}", createdAtFrom: created_at_from, first: first, state: state}

  post_graphql(<<~GRAPHQL, variables: variables).dig(:data, :pipeline, :builds, :edges).map {|b| b[:node] }
    query ($createdAtFrom: DateTime, $slug: ID!, $first: Int, $state: [BuildStates!]) {
      pipeline(slug: $slug) {
        builds(
          first: $first
          state: $state
          createdAtFrom: $createdAtFrom
        ) {
          edges {
            node {
              branch
              canceledAt
              commit
              createdAt
              env
              finishedAt
              id
              message
              number
              scheduledAt
              source {
                name
              }
              startedAt
              state
              url
              uuid
            }
          }
        }
      }
    }
  GRAPHQL
end

#fetch_log(org_slug: @org_slug, pipeline_slug: @pipeline_slug, build_number:, job_id:) ⇒ Object



67
68
69
# File 'lib/unofficial_buildkite_client.rb', line 67

def fetch_log(org_slug: @org_slug, pipeline_slug: @pipeline_slug, build_number:, job_id:)
  @client.request(:get, "#{INTERNAL_API_HOST}/organizations/#{org_slug}/pipelines/#{pipeline_slug}/builds/#{build_number}/jobs/#{job_id}/log")
end

#post_graphql(query, variables: {}) ⇒ Object



71
72
73
# File 'lib/unofficial_buildkite_client.rb', line 71

def post_graphql(query, variables: {})
  @client.request(:post, GRAPHQL_ENDPOINT, params: {query: query, variables: variables})
end