Skip to content

Commit 01f8d42

Browse files
committed
More Build related conversions
1 parent 7c02e39 commit 01f8d42

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

Sources/App/Controllers/BuildController.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,15 @@ import Vapor
1919

2020
enum BuildController {
2121
@Sendable
22-
static func show(req: Request) throws -> EventLoopFuture<HTML> {
22+
static func show(req: Request) async throws -> HTML {
2323
guard let id = req.parameters.get("id"),
2424
let buildId = UUID.init(uuidString: id)
25-
else { return req.eventLoop.future(error: Abort(.notFound)) }
25+
else { throw Abort(.notFound) }
2626

27-
return BuildResult.query(on: req.db, buildId: buildId)
28-
.flatMap { result in
29-
Build.fetchLogs(client: req.client, logUrl: result.build.logUrl)
30-
.map { (result, $0) }
31-
}
32-
.map { BuildShow.Model.init(result: $0, logs: $1) }
33-
.unwrap(or: Abort(.notFound))
34-
.map {
35-
BuildShow.View(path: req.url.path, model: $0).document()
36-
}
27+
let result = try await BuildResult.query(on: req.db, buildId: buildId)
28+
let logs = try await Build.fetchLogs(client: req.client, logUrl: result.build.logUrl)
29+
guard let model = BuildShow.Model(result: result, logs: logs) else { throw Abort(.notFound) }
30+
return BuildShow.View(path: req.url.path, model: model).document()
3731
}
3832

3933
}

Sources/App/Core/Query+Support/Joined4+BuildResult.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ extension BuildResult {
3434
)
3535
}
3636

37-
static func query(on database: Database, buildId: Build.Id) -> EventLoopFuture<Self> {
38-
query(on: database)
37+
static func query(on database: Database, buildId: Build.Id) async throws -> Self {
38+
try await query(on: database)
3939
.filter(\.$id == buildId)
4040
.first()
4141
.unwrap(or: Abort(.notFound))

Sources/App/Models/Build.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,9 @@ extension Build {
277277

278278
extension Build {
279279

280-
static func fetchLogs(client: Client, logUrl: String?) -> EventLoopFuture<String?> {
281-
guard let logUrl = logUrl else {
282-
return client.eventLoop.future(nil)
283-
}
284-
return client.get(URI(string: logUrl))
285-
.map { $0.body?.asString() }
280+
static func fetchLogs(client: Client, logUrl: String?) async throws -> String? {
281+
guard let logUrl = logUrl else { return nil }
282+
return try await client.get(URI(string: logUrl)).body?.asString()
286283
}
287284

288285
}

Tests/AppTests/BuildResultTests.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@ import XCTVapor
1919

2020
class BuildResultTests: AppTestCase {
2121

22-
func test_query() throws {
22+
func test_query() async throws {
2323
// setup
2424
let pkg = try savePackage(on: app.db, "1".url)
2525
let repo = try Repository(package: pkg)
26-
try repo.save(on: app.db).wait()
26+
try await repo.save(on: app.db)
2727
let version = try Version(package: pkg)
28-
try version.save(on: app.db).wait()
28+
try await version.save(on: app.db)
2929
let build = try Build(version: version, platform: .iOS, status: .ok, swiftVersion: .init(5, 3, 0))
30-
try build.save(on: app.db).wait()
30+
try await build.save(on: app.db)
3131

3232
// MUT
33-
let res = try BuildResult
34-
.query(on: app.db, buildId: build.id!)
35-
.wait()
33+
let res = try await BuildResult.query(on: app.db, buildId: build.id!)
3634

3735
// validate
3836
XCTAssertEqual(res.build.id, build.id)

Tests/AppTests/BuildShowModelTests.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,23 @@ class BuildShowModelTests: AppTestCase {
2929
XCTAssertEqual(Model.mock.packageURL, "/foo/bar")
3030
}
3131

32-
func test_init() throws {
32+
func test_init() async throws {
3333
// setup
3434
let pkg = try savePackage(on: app.db, "1".url)
35-
try Repository(package: pkg,
35+
try await Repository(package: pkg,
3636
defaultBranch: "main",
3737
forks: 42,
3838
license: .mit,
3939
name: "bar",
4040
owner: "foo",
4141
stars: 17,
42-
summary: "summary").save(on: app.db).wait()
42+
summary: "summary").save(on: app.db)
4343
let version = try Version(id: UUID(), package: pkg, packageName: "Bar", reference: .branch("main"))
44-
try version.save(on: app.db).wait()
44+
try await version.save(on: app.db)
4545
let buildId = UUID()
46-
try Build(id: buildId, version: version, platform: .iOS, status: .ok, swiftVersion: .v3)
47-
.save(on: app.db).wait()
48-
let result = try BuildResult
49-
.query(on: app.db, buildId: buildId)
50-
.wait()
46+
try await Build(id: buildId, version: version, platform: .iOS, status: .ok, swiftVersion: .v3)
47+
.save(on: app.db)
48+
let result = try await BuildResult.query(on: app.db, buildId: buildId)
5149

5250
// MUT
5351
let model = BuildShow.Model(result: result, logs: "logs")

0 commit comments

Comments
 (0)