Skip to content

Commit

Permalink
some new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsky committed Jun 7, 2022
1 parent bad7263 commit 5aebac9
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 36 deletions.
8 changes: 8 additions & 0 deletions Examples/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let package = Package(
.executable(name: "graphql", targets: ["graphql"]),
.executable(name: "advanced-authorization", targets: ["advanced-authorization"]),
.executable(name: "test-analytics", targets: ["test-analytics"]),
.executable(name: "webhooks", targets: ["webhooks"]),
],
dependencies: [
.package(name: "Buildkite", path: "../")
Expand Down Expand Up @@ -47,5 +48,12 @@ let package = Package(
],
path: "test-analytics"
),
.executableTarget(
name: "webhooks",
dependencies: [
.product(name: "Buildkite", package: "Buildkite")
],
path: "webhooks"
),
]
)
35 changes: 34 additions & 1 deletion Examples/advanced-authorization/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,40 @@ import Buildkite
import Foundation

@main struct Example {
static func main() async {
static func main() async throws {
let client = BuildkiteClient(tokens: MyTokenProvider())

let restUser = try await client.send(.me).content
let gqlUser = try await client.sendQuery(GraphQL<Query>(rawQuery: "{ viewer { user { name } } }")).viewer.user
if restUser.name == gqlUser.name {
print("Hello, \(restUser.name)! 👋🏼")
} else {
print("Hello, \(restUser.name), and also to \(gqlUser.name)! 👋🏼")
}
}
}

struct MyTokenProvider: TokenProvider {
func token(for version: APIVersion) -> String? {
switch version {
case .GraphQL.v1:
return "..."
case .REST.v2:
return "..."
default:
return nil
}
}
}

struct Query: Decodable, Equatable {
var viewer: Viewer

struct Viewer: Decodable, Equatable {
var user: User

struct User: Decodable, Equatable {
var name: String
}
}
}
59 changes: 27 additions & 32 deletions Examples/graphql/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,8 @@
import Buildkite
import Foundation

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
}
}
}
}
}

@main struct Example {
static func main() async {
static func main() async throws {
let client = BuildkiteClient(token: "...")

let query = """
Expand All @@ -49,17 +28,33 @@ struct MyPipeline: Codable {
}
"""

do {
let pipelines = try await client.sendQuery(
GraphQL<MyPipeline>(
rawQuery: query,
variables: ["first": 30]
)
let pipelines = try await client.sendQuery(
GraphQL<MyPipeline>(
rawQuery: query,
variables: ["first": 30]
)
print(pipelines)
} catch {
print(error)
exit(1)
)
print(pipelines)
}
}

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
}
}
}
}
}
6 changes: 5 additions & 1 deletion Examples/simple/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import Buildkite
import Foundation

@main struct Example {
static func main() async {
static func main() async throws {
let token = ProcessInfo.processInfo.environment["TOKEN"] ?? "..."
let client = BuildkiteClient(token: token)

let me = try await client.send(.me).content
print("Hello, \(me.name)! 👋🏼")
}
}
18 changes: 17 additions & 1 deletion Examples/test-analytics/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@ import Buildkite
import Foundation

@main struct Example {
static func main() async {
static func main() async throws {
let token = ProcessInfo.processInfo.environment["TOKEN"] ?? "..."
let client = BuildkiteClient(token: token)

let result =
try await client.send(
.uploadTestAnalytics(
[
.init(id: UUID(), history: .init(section: "http")),
.init(id: UUID(), history: .init(section: "http")),
.init(id: UUID(), history: .init(section: "http")),
],
environment: .init(ci: "buildkite", key: UUID().uuidString)
)
)
.content

print(result.runUrl)
}
}
20 changes: 20 additions & 0 deletions Examples/webhooks/Example.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Example.swift
// webhooks
//
// Created by Aaron Sky on 6/6/22.
// Copyright © 2022 Aaron Sky. All rights reserved.
//

import Buildkite
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@main struct Example {
static func main() {

}
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ test-examples:
-scheme test-analytics \
-destination "$(DESTINATION_PLATFORM_MACOS)" \
-quiet

xcodebuild build \
-scheme webhooks \
-destination "$(DESTINATION_PLATFORM_MACOS)" \
-quiet

DOC_WARNINGS := $(shell xcodebuild clean docbuild \
-scheme Buildkite \
Expand Down
38 changes: 38 additions & 0 deletions Sources/Buildkite/Models/Trace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ public struct Trace: Codable, Equatable, Identifiable {
/// History object
public var history: Span

public init(
id: UUID,
scope: String? = nil,
name: String? = nil,
identifier: String? = nil,
location: String? = nil,
fileName: String? = nil,
result: String? = nil,
failureReason: String? = nil,
history: Trace.Span
) {
self.id = id
self.scope = scope
self.name = name
self.identifier = identifier
self.location = location
self.fileName = fileName
self.result = result
self.failureReason = failureReason
self.history = history
}

public struct Span: Codable, Equatable {
/// A section category for this span, e.g. `"http"`, `"sql"`, `"sleep"`, or `"annotation"`
public var section: String
Expand All @@ -58,5 +80,21 @@ public struct Trace: Codable, Equatable, Identifiable {
public var detail: [String: String] = [:]
/// array of span objects
public var children: [Span] = []

public init(
section: String,
startAt: TimeInterval? = nil,
endAt: TimeInterval? = nil,
duration: TimeInterval? = nil,
detail: [String: String] = [:],
children: [Trace.Span] = []
) {
self.section = section
self.startAt = startAt
self.endAt = endAt
self.duration = duration
self.detail = detail
self.children = children
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ extension TestAnalytics.Resources {
public var commitSha: String?
public var message: String?
public var url: String?

public init(
ci: String,
key: String,
number: String? = nil,
jobId: String? = nil,
branch: String? = nil,
commitSha: String? = nil,
message: String? = nil,
url: String? = nil
) {
self.ci = ci
self.key = key
self.number = number
self.jobId = jobId
self.branch = branch
self.commitSha = commitSha
self.message = message
self.url = url
}
}

private enum CodingKeys: String, CodingKey {
Expand Down Expand Up @@ -66,6 +86,10 @@ extension TestAnalytics.Resources {
) {
self.body = body
}

public func transformRequest(_ request: inout URLRequest) {
request.httpMethod = "POST"
}
}
}

Expand Down

0 comments on commit 5aebac9

Please sign in to comment.