Buildkite

0.1.0

A Swift library for the Buildkite REST and GraphQL APIs
aaronsky/buildkite-swift

What's New

0.1.0

2020-06-10T02:03:53Z

This is the first minor-version release of this package. While not yet committing to the stability guarantees of a a major version, this marks a period of considered stability before making changes to the API.

This release includes a small change to the data type returned by the GraphQL resource. After some research and discussion with members of the Buildkite team, it was determined that the GraphQL API offered by Buildkite is not capable of responding with data and errors simultaneously as is outlined in the specification. This means that you can write code that maps on the variant returned by GraphQL.Content, which works well with either the Combine API or the Result returned by the closure API.

Buildkite

Build Status

A Swift library and client for the Buildkite REST and GraphQL APIs.

Usage

Add the dependency to your Package.swift file:

let package = Package(
    name: "myproject",
    dependencies: [
        .package(url: "https://github.com/aaronsky/buildkite-swift.git", from: "0.0.3"),
        ],
    targets: [
        .target(
            name: "myproject",
            dependencies: ["Buildkite"]),
        ]
)

As an example, here is a way you can use the closure-based interface to list all pipelines:

import Buildkite

let client = BuildkiteClient()
client.token = "..." // Your scoped Buildkite API access token
client.send(Pipeline.Resources.List(organization: "buildkite")) { result in
    do {
        let response = try result.get()
        let pipelines = response.content
        print(pipelines)
    } catch {
        print(error)
    }
}

You can even use Combine, if you'd like!

import Buildkite

let client = BuildkiteClient()
client.token = "..." // Your scoped Buildkite API access token

var cancellables: Set<AnyCancellable> = []
client.sendPublisher(Pipeline.Resources.List(organization: "buildkite"))
    .map(\.content)
    .sink(receiveCompletion: { _ in }) { pipelines in
        print(pipelines)
    }.store(in: &cancellables)

The entire publicly documented REST API surface is supported by this package.

GraphQL

GraphQL support is present, but currently rudimentary. For example, here's what the same query as above would look like in GraphQL:

import Foundation
import Buildkite

let client = BuildkiteClient()
client.token = "..."

let query = """
query MyPipelines($first: Int!) {
    organization(slug: "buildkite") {
        pipelines(first: $first) {
            edges {
                node {
                    name
                    uuid
                }
            }
        }
    }
}
"""

struct MyPipeline: Codable {
    var organization: Organization?

    struct Organization: Codable {
        var pipelines: Pipelines

        struct Pipelines: Codable {
            var edges: [PipelineEdge]

            struct PipelineEdge: Codable {
                var node: Pipeline

                struct Pipeline: Codable {
                    var name: String
                    var uuid: UUID
                }
            }
        }
    }
}

var cancellables: Set<AnyCancellable> = []
client.sendPublisher(GraphQL<MyPipeline>(rawQuery: query, variables: ["first": 30]))
    .map(\.content)
    .sink(receiveCompletion: { _ in }) { pipelines in
        print(pipelines)
    }.store(in: &cancellables)

References

License

Buildkite for Swift is released under the BSD-2 license. See LICENSE for details.

Description

  • Swift Tools 5.2.0

Dependencies

  • None
Last updated: Tue Jan 12 2021 10:33:33 GMT-0500 (GMT-05:00)