Skip to content

Commit e3ce42a

Browse files
committed
wip
1 parent 27f1f8f commit e3ce42a

File tree

10 files changed

+587
-545
lines changed

10 files changed

+587
-545
lines changed

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct AppEnvironment: Sendable {
2929
var awsDocsBucket: @Sendable () -> String?
3030
var awsReadmeBucket: @Sendable () -> String?
3131
var awsSecretAccessKey: @Sendable () -> String?
32-
var builderToken: @Sendable () -> String?
3332
var buildTriggerAllowList: @Sendable () -> [Package.Id]
3433
var buildTriggerDownscaling: @Sendable () -> Double
3534
var buildTriggerLatestSwiftVersionDownscaling: @Sendable () -> Double
@@ -111,7 +110,6 @@ extension AppEnvironment {
111110
awsDocsBucket: { Environment.get("AWS_DOCS_BUCKET") },
112111
awsReadmeBucket: { Environment.get("AWS_README_BUCKET") },
113112
awsSecretAccessKey: { Environment.get("AWS_SECRET_ACCESS_KEY") },
114-
builderToken: { Environment.get("BUILDER_TOKEN") },
115113
buildTriggerAllowList: {
116114
Environment.get("BUILD_TRIGGER_ALLOW_LIST")
117115
.map { Data($0.utf8) }

Sources/App/Core/Authentication/User.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import Authentication
16+
import Dependencies
1617
import JWTKit
1718
import Vapor
1819
import VaporToOpenAPI
@@ -55,7 +56,8 @@ extension User {
5556

5657
struct BuilderAuthenticator: AsyncBearerAuthenticator {
5758
func authenticate(bearer: BearerAuthorization, for request: Request) async throws {
58-
if let builderToken = Current.builderToken(),
59+
@Dependency(\.environment) var environment
60+
if let builderToken = environment.builderToken(),
5961
bearer.token == builderToken {
6062
request.auth.login(User.builder)
6163
}

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct EnvironmentClient {
2323
// regarding the use of XCTFail here.
2424
var allowBuildTriggers: @Sendable () -> Bool = { XCTFail(#function); return true }
2525
var allowSocialPosts: @Sendable () -> Bool = { XCTFail(#function); return true }
26+
var builderToken: @Sendable () -> String?
2627
var buildTimeout: @Sendable () -> Int = { XCTFail(#function); return 10 }
2728
// We're not defaulting current to XCTFail, because its use is too pervasive and would require the vast
2829
// majority of tests to be wrapped with `withDependencies`.
@@ -46,6 +47,7 @@ extension EnvironmentClient: DependencyKey {
4647
.flatMap(\.asBool)
4748
?? Constants.defaultAllowSocialPosts
4849
},
50+
builderToken: { Environment.get("BUILDER_TOKEN") },
4951
buildTimeout: { Environment.get("BUILD_TIMEOUT").flatMap(Int.init) ?? 10 },
5052
current: { (try? Environment.detect()) ?? .development },
5153
mastodonCredentials: {

Sources/App/Core/Gitlab.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension Gitlab.Builder {
8080
@Dependency(\.environment) var environment
8181

8282
guard let pipelineToken = Current.gitlabPipelineToken(),
83-
let builderToken = Current.builderToken()
83+
let builderToken = environment.builderToken()
8484
else { throw Gitlab.Error.missingToken }
8585
guard let awsDocsBucket = Current.awsDocsBucket() else {
8686
throw Gitlab.Error.missingConfiguration("AWS_DOCS_BUCKET")

Tests/AppTests/ApiTests.swift

Lines changed: 499 additions & 464 deletions
Large diffs are not rendered by default.

Tests/AppTests/BuildTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ class BuildTests: AppTestCase {
130130

131131
func test_trigger() async throws {
132132
try await withDependencies {
133+
$0.environment.builderToken = { "builder token" }
133134
$0.environment.buildTimeout = { 10 }
134135
} operation: {
135-
Current.builderToken = { "builder token" }
136136
Current.gitlabPipelineToken = { "pipeline token" }
137137
Current.siteURL = { "http://example.com" }
138138
// setup
@@ -197,11 +197,11 @@ class BuildTests: AppTestCase {
197197

198198
func test_trigger_isDocBuild() async throws {
199199
try await withDependencies {
200+
$0.environment.builderToken = { "builder token" }
200201
$0.environment.buildTimeout = { 10 }
201202
} operation: {
202203
// Same test as test_trigger above, except we trigger with isDocBuild: true
203204
// and expect a 15m TIMEOUT instead of 10m
204-
Current.builderToken = { "builder token" }
205205
Current.gitlabPipelineToken = { "pipeline token" }
206206
Current.siteURL = { "http://example.com" }
207207
// setup
@@ -210,7 +210,7 @@ class BuildTests: AppTestCase {
210210
try await v.save(on: app.db)
211211
let buildId = UUID()
212212
let versionID = try XCTUnwrap(v.id)
213-
213+
214214
// Use live dependency but replace actual client with a mock so we can
215215
// assert on the details being sent without actually making a request
216216
Current.triggerBuild = { client, buildId, cloneURL, isDocBuild, platform, ref, swiftVersion, versionID in
@@ -235,7 +235,7 @@ class BuildTests: AppTestCase {
235235
XCTAssertNotNil(response)
236236
XCTAssertEqual(response?.variables["TIMEOUT"], "15m")
237237
}
238-
238+
239239
// MUT
240240
let res = try await Build.trigger(database: app.db,
241241
client: client,
@@ -244,7 +244,7 @@ class BuildTests: AppTestCase {
244244
platform: .macosXcodebuild,
245245
swiftVersion: .init(5, 2, 4),
246246
versionId: versionID)
247-
247+
248248
// validate
249249
XCTAssertTrue(called)
250250
XCTAssertEqual(res.status, .created)

Tests/AppTests/BuildTriggerTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ class BuildTriggerTests: AppTestCase {
329329

330330
func test_triggerBuildsUnchecked() async throws {
331331
try await withDependencies {
332+
$0.environment.builderToken = { "builder token" }
332333
$0.environment.buildTimeout = { 10 }
333334
} operation: {
334335
// setup
335-
Current.builderToken = { "builder token" }
336336
Current.gitlabPipelineToken = { "pipeline token" }
337337
Current.siteURL = { "http://example.com" }
338338

@@ -390,11 +390,11 @@ class BuildTriggerTests: AppTestCase {
390390

391391
func test_triggerBuildsUnchecked_supported() async throws {
392392
try await withDependencies {
393+
$0.environment.builderToken = { "builder token" }
393394
$0.environment.buildTimeout = { 10 }
394395
} operation: {
395396
// Explicitly test the full range of all currently triggered platforms and swift versions
396397
// setup
397-
Current.builderToken = { "builder token" }
398398
Current.gitlabPipelineToken = { "pipeline token" }
399399
Current.siteURL = { "http://example.com" }
400400

@@ -470,6 +470,7 @@ class BuildTriggerTests: AppTestCase {
470470

471471
func test_triggerBuildsUnchecked_build_exists() async throws {
472472
try await withDependencies {
473+
$0.environment.builderToken = { "builder token" }
473474
$0.environment.buildTimeout = { 10 }
474475
} operation: {
475476
// Tests error handling when a build record already exists and `create` raises a
@@ -483,7 +484,6 @@ class BuildTriggerTests: AppTestCase {
483484
// being completely ignored because the command errors out.
484485
// See https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/2237
485486
// setup
486-
Current.builderToken = { "builder token" }
487487
Current.gitlabPipelineToken = { "pipeline token" }
488488
Current.siteURL = { "http://example.com" }
489489

@@ -550,11 +550,11 @@ class BuildTriggerTests: AppTestCase {
550550
func test_triggerBuilds_checked() async throws {
551551
try await withDependencies {
552552
$0.environment.allowBuildTriggers = { true }
553+
$0.environment.builderToken = { "builder token" }
553554
$0.environment.buildTimeout = { 10 }
554555
} operation: {
555556
// Ensure we respect the pipeline limit when triggering builds
556557
// setup
557-
Current.builderToken = { "builder token" }
558558
Current.gitlabPipelineToken = { "pipeline token" }
559559
Current.siteURL = { "http://example.com" }
560560
Current.gitlabPipelineLimit = { 300 }
@@ -662,11 +662,11 @@ class BuildTriggerTests: AppTestCase {
662662
func test_triggerBuilds_multiplePackages() async throws {
663663
try await withDependencies {
664664
$0.environment.allowBuildTriggers = { true }
665+
$0.environment.builderToken = { "builder token" }
665666
$0.environment.buildTimeout = { 10 }
666667
} operation: {
667668
// Ensure we respect the pipeline limit when triggering builds for multiple package ids
668669
// setup
669-
Current.builderToken = { "builder token" }
670670
Current.gitlabPipelineToken = { "pipeline token" }
671671
Current.siteURL = { "http://example.com" }
672672
Current.gitlabPipelineLimit = { 300 }
@@ -712,10 +712,10 @@ class BuildTriggerTests: AppTestCase {
712712
func test_triggerBuilds_trimming() async throws {
713713
try await withDependencies {
714714
$0.environment.allowBuildTriggers = { true }
715+
$0.environment.builderToken = { "builder token" }
715716
} operation: {
716717
// Ensure we trim builds as part of triggering
717718
// setup
718-
Current.builderToken = { "builder token" }
719719
Current.gitlabPipelineToken = { "pipeline token" }
720720
Current.siteURL = { "http://example.com" }
721721
Current.gitlabPipelineLimit = { 300 }
@@ -747,11 +747,11 @@ class BuildTriggerTests: AppTestCase {
747747
func test_triggerBuilds_error() async throws {
748748
try await withDependencies {
749749
$0.environment.allowBuildTriggers = { true }
750+
$0.environment.builderToken = { "builder token" }
750751
$0.environment.buildTimeout = { 10 }
751752
} operation: {
752753
// Ensure we trim builds as part of triggering
753754
// setup
754-
Current.builderToken = { "builder token" }
755755
Current.gitlabPipelineToken = { "pipeline token" }
756756
Current.siteURL = { "http://example.com" }
757757
Current.gitlabPipelineLimit = { 300 }
@@ -843,11 +843,11 @@ class BuildTriggerTests: AppTestCase {
843843

844844
func test_override_switch() async throws {
845845
try await withDependencies {
846+
$0.environment.builderToken = { "builder token" }
846847
$0.environment.buildTimeout = { 10 }
847848
} operation: {
848849
// Ensure don't trigger if the override is off
849850
// setup
850-
Current.builderToken = { "builder token" }
851851
Current.gitlabPipelineToken = { "pipeline token" }
852852
Current.siteURL = { "http://example.com" }
853853
// Use live dependency but replace actual client with a mock so we can
@@ -917,11 +917,11 @@ class BuildTriggerTests: AppTestCase {
917917
func test_downscaling() async throws {
918918
try await withDependencies {
919919
$0.environment.allowBuildTriggers = { true }
920+
$0.environment.builderToken = { "builder token" }
920921
$0.environment.buildTimeout = { 10 }
921922
} operation: {
922923
// Test build trigger downscaling behaviour
923924
// setup
924-
Current.builderToken = { "builder token" }
925925
Current.gitlabPipelineToken = { "pipeline token" }
926926
Current.siteURL = { "http://example.com" }
927927
Current.buildTriggerDownscaling = { 0.05 } // 5% downscaling rate
@@ -990,11 +990,11 @@ class BuildTriggerTests: AppTestCase {
990990
func test_downscaling_allow_list_override() async throws {
991991
try await withDependencies {
992992
$0.environment.allowBuildTriggers = { true }
993+
$0.environment.builderToken = { "builder token" }
993994
$0.environment.buildTimeout = { 10 }
994995
} operation: {
995996
// Test build trigger downscaling behaviour for allow-listed packages
996997
// setup
997-
Current.builderToken = { "builder token" }
998998
Current.gitlabPipelineToken = { "pipeline token" }
999999
Current.siteURL = { "http://example.com" }
10001000
Current.buildTriggerDownscaling = { 0.05 } // 5% downscaling rate

Tests/AppTests/GitlabBuilderTests.swift

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ class GitlabBuilderTests: AppTestCase {
5555

5656
func test_triggerBuild() async throws {
5757
try await withDependencies {
58+
$0.environment.builderToken = { "builder token" }
5859
$0.environment.buildTimeout = { 10 }
5960
} operation: {
6061
Current.awsDocsBucket = { "docs-bucket" }
61-
Current.builderToken = { "builder token" }
6262
Current.gitlabPipelineToken = { "pipeline token" }
6363
Current.siteURL = { "http://example.com" }
6464
let buildId = UUID()
@@ -104,13 +104,13 @@ class GitlabBuilderTests: AppTestCase {
104104

105105
func test_issue_588() async throws {
106106
try await withDependencies {
107+
$0.environment.builderToken = { "builder token" }
107108
$0.environment.buildTimeout = { 10 }
108109
} operation: {
109110
Current.awsDocsBucket = { "docs-bucket" }
110-
Current.builderToken = { "builder token" }
111111
Current.gitlabPipelineToken = { "pipeline token" }
112112
Current.siteURL = { "http://example.com" }
113-
113+
114114
var called = false
115115
let client = MockClient { req, res in
116116
called = true
@@ -122,7 +122,7 @@ class GitlabBuilderTests: AppTestCase {
122122
.flatMap { $0.variables["SWIFT_VERSION"] }
123123
XCTAssertEqual(swiftVersion, "6.0")
124124
}
125-
125+
126126
// MUT
127127
_ = try await Gitlab.Builder.triggerBuild(client: client,
128128
buildId: .id0,
@@ -176,41 +176,44 @@ class LiveGitlabBuilderTests: AppTestCase {
176176
"This is a live trigger test for end-to-end testing of pre-release builder versions"
177177
)
178178

179-
// set build branch to trigger on
180-
Gitlab.Builder.branch = "main"
179+
try await withDependencies {
180+
$0.environment.builderToken = {
181+
// Set this to a valid value if you want to report build results back to the server
182+
ProcessInfo.processInfo.environment["LIVE_BUILDER_TOKEN"]
183+
}
184+
} operation: {
185+
// set build branch to trigger on
186+
Gitlab.Builder.branch = "main"
187+
188+
// make sure environment variables are configured for live access
189+
Current.awsDocsBucket = { "spi-dev-docs" }
190+
Current.gitlabPipelineToken = {
191+
// This Gitlab token is required in order to trigger the pipeline
192+
ProcessInfo.processInfo.environment["LIVE_GITLAB_PIPELINE_TOKEN"]
193+
}
194+
Current.siteURL = { "https://staging.swiftpackageindex.com" }
181195

182-
// make sure environment variables are configured for live access
183-
Current.awsDocsBucket = { "spi-dev-docs" }
184-
Current.builderToken = {
185-
// Set this to a valid value if you want to report build results back to the server
186-
ProcessInfo.processInfo.environment["LIVE_BUILDER_TOKEN"]
187-
}
188-
Current.gitlabPipelineToken = {
189-
// This Gitlab token is required in order to trigger the pipeline
190-
ProcessInfo.processInfo.environment["LIVE_GITLAB_PIPELINE_TOKEN"]
196+
let buildId = UUID()
197+
198+
// use a valid uuid from a live db if reporting back should succeed
199+
// SemanticVersion 0.3.2 on staging
200+
let versionID = UUID(uuidString: "93d8c545-15c4-43c2-946f-1b625e2596f9")!
201+
202+
// MUT
203+
let res = try await Gitlab.Builder.triggerBuild(
204+
client: app.client,
205+
buildId: buildId,
206+
cloneURL: "https://github.com/SwiftPackageIndex/SemanticVersion.git",
207+
isDocBuild: false,
208+
platform: .macosSpm,
209+
reference: .tag(.init(0, 3, 2)),
210+
swiftVersion: .v4,
211+
versionID: versionID)
212+
213+
print("status: \(res.status)")
214+
print("buildId: \(buildId)")
215+
print("webUrl: \(res.webUrl)")
191216
}
192-
Current.siteURL = { "https://staging.swiftpackageindex.com" }
193-
194-
let buildId = UUID()
195-
196-
// use a valid uuid from a live db if reporting back should succeed
197-
// SemanticVersion 0.3.2 on staging
198-
let versionID = UUID(uuidString: "93d8c545-15c4-43c2-946f-1b625e2596f9")!
199-
200-
// MUT
201-
let res = try await Gitlab.Builder.triggerBuild(
202-
client: app.client,
203-
buildId: buildId,
204-
cloneURL: "https://github.com/SwiftPackageIndex/SemanticVersion.git",
205-
isDocBuild: false,
206-
platform: .macosSpm,
207-
reference: .tag(.init(0, 3, 2)),
208-
swiftVersion: .v4,
209-
versionID: versionID)
210-
211-
print("status: \(res.status)")
212-
print("buildId: \(buildId)")
213-
print("webUrl: \(res.webUrl)")
214217
}
215218

216219
}

0 commit comments

Comments
 (0)