Skip to content

Commit 3ebdd66

Browse files
committed
Move Current.gitlabPipelineLimit to BuildSystem.gitlabPipelineLimit
1 parent 8f96e29 commit 3ebdd66

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

Sources/App/Commands/TriggerBuilds.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func triggerBuilds(on database: Database,
207207
AppMetrics.buildRunningJobsCount?.set(runningJobs)
208208

209209
let newJobs = ActorIsolated(0)
210+
let gitlabPipelineLimit = environment.gitlabPipelineLimit
210211

211212
await withThrowingTaskGroup(of: Void.self) { group in
212213
for pkgId in packages {
@@ -219,7 +220,7 @@ func triggerBuilds(on database: Database,
219220
group.addTask {
220221
// check if we have capacity to schedule more builds before querying for builds
221222
var newJobCount = await newJobs.value
222-
guard pendingJobs + newJobCount < Current.gitlabPipelineLimit() else {
223+
guard pendingJobs + newJobCount < gitlabPipelineLimit() else {
223224
Current.logger().info("too many pending pipelines (\(pendingJobs + newJobCount))")
224225
return
225226
}
@@ -228,7 +229,7 @@ func triggerBuilds(on database: Database,
228229
let triggers = try await findMissingBuilds(database, packageId: pkgId)
229230

230231
newJobCount = await newJobs.value
231-
guard pendingJobs + newJobCount < Current.gitlabPipelineLimit() else {
232+
guard pendingJobs + newJobCount < gitlabPipelineLimit() else {
232233
Current.logger().info("too many pending pipelines (\(pendingJobs + newJobCount))")
233234
return
234235
}

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import FoundationNetworking
2525
struct AppEnvironment: Sendable {
2626
var fileManager: FileManager
2727
var git: Git
28-
var gitlabPipelineLimit: @Sendable () -> Int
2928
var logger: @Sendable () -> Logger
3029
var setLogger: @Sendable (Logger) -> Void
3130
var shell: Shell
@@ -38,10 +37,6 @@ extension AppEnvironment {
3837
static let live = AppEnvironment(
3938
fileManager: .live,
4039
git: .live,
41-
gitlabPipelineLimit: {
42-
Environment.get("GITLAB_PIPELINE_LIMIT").flatMap(Int.init)
43-
?? Constants.defaultGitlabPipelineLimit
44-
},
4540
logger: { logger },
4641
setLogger: { logger in Self.logger = logger },
4742
shell: .live

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct EnvironmentClient {
4242
var current: @Sendable () -> Environment = { XCTFail("current"); return .development }
4343
var dbId: @Sendable () -> String?
4444
var gitlabApiToken: @Sendable () -> String?
45+
var gitlabPipelineLimit: @Sendable () -> Int = { XCTFail("gitlabPipelineLimit"); return 100 }
4546
var gitlabPipelineToken: @Sendable () -> String?
4647
var hideStagingBanner: @Sendable () -> Bool = { XCTFail("hideStagingBanner"); return Constants.defaultHideStagingBanner }
4748
var loadSPIManifest: @Sendable (String) -> SPIManifest.Manifest?
@@ -112,6 +113,10 @@ extension EnvironmentClient: DependencyKey {
112113
current: { (try? Environment.detect()) ?? .development },
113114
dbId: { Environment.get("DATABASE_ID") },
114115
gitlabApiToken: { Environment.get("GITLAB_API_TOKEN") },
116+
gitlabPipelineLimit: {
117+
Environment.get("GITLAB_PIPELINE_LIMIT").flatMap(Int.init)
118+
?? Constants.defaultGitlabPipelineLimit
119+
},
115120
gitlabPipelineToken: { Environment.get("GITLAB_PIPELINE_TOKEN") },
116121
hideStagingBanner: {
117122
Environment.get("HIDE_STAGING_BANNER").flatMap(\.asBool)

Tests/AppTests/BuildTriggerTests.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ class BuildTriggerTests: AppTestCase {
569569
$0.environment.buildTimeout = { 10 }
570570
$0.environment.buildTriggerAllowList = { [] }
571571
$0.environment.buildTriggerDownscaling = { 1 }
572+
$0.environment.gitlabPipelineLimit = { 300 }
572573
$0.environment.gitlabPipelineToken = { "pipeline token" }
573574
$0.environment.random = { @Sendable _ in 0 }
574575
$0.environment.siteURL = { "http://example.com" }
@@ -587,7 +588,6 @@ class BuildTriggerTests: AppTestCase {
587588
} operation: {
588589
// Ensure we respect the pipeline limit when triggering builds
589590
// setup
590-
Current.gitlabPipelineLimit = { 300 }
591591
var triggerCount = 0
592592
let client = MockClient { _, res in
593593
triggerCount += 1
@@ -696,6 +696,7 @@ class BuildTriggerTests: AppTestCase {
696696
$0.environment.buildTriggerAllowList = { [] }
697697
$0.environment.buildTriggerDownscaling = { 1 }
698698
$0.environment.buildTriggerLatestSwiftVersionDownscaling = { 1 }
699+
$0.environment.gitlabPipelineLimit = { 300 }
699700
$0.environment.gitlabPipelineToken = { "pipeline token" }
700701
$0.environment.random = { @Sendable _ in 0 }
701702
$0.environment.siteURL = { "http://example.com" }
@@ -714,7 +715,6 @@ class BuildTriggerTests: AppTestCase {
714715
} operation: {
715716
// Ensure we respect the pipeline limit when triggering builds for multiple package ids
716717
// setup
717-
Current.gitlabPipelineLimit = { 300 }
718718
let client = MockClient { _, res in
719719
triggerCount.withLockedValue { $0 += 1 }
720720
try? res.content.encode(
@@ -748,13 +748,13 @@ class BuildTriggerTests: AppTestCase {
748748
$0.environment.builderToken = { "builder token" }
749749
$0.environment.buildTriggerAllowList = { [] }
750750
$0.environment.buildTriggerDownscaling = { 1 }
751+
$0.environment.gitlabPipelineLimit = { 300 }
751752
$0.environment.gitlabPipelineToken = { "pipeline token" }
752753
$0.environment.random = { @Sendable _ in 0 }
753754
$0.environment.siteURL = { "http://example.com" }
754755
} operation: {
755756
// Ensure we trim builds as part of triggering
756757
// setup
757-
Current.gitlabPipelineLimit = { 300 }
758758

759759
let client = MockClient { _, _ in }
760760

@@ -789,6 +789,7 @@ class BuildTriggerTests: AppTestCase {
789789
$0.environment.buildTimeout = { 10 }
790790
$0.environment.buildTriggerAllowList = { [] }
791791
$0.environment.buildTriggerDownscaling = { 1 }
792+
$0.environment.gitlabPipelineLimit = { 300 }
792793
$0.environment.gitlabPipelineToken = { "pipeline token" }
793794
$0.environment.random = { @Sendable _ in 0 }
794795
$0.environment.siteURL = { "http://example.com" }
@@ -807,7 +808,6 @@ class BuildTriggerTests: AppTestCase {
807808
} operation: {
808809
// Ensure we trim builds as part of triggering
809810
// setup
810-
Current.gitlabPipelineLimit = { 300 }
811811
var triggerCount = 0
812812
let client = MockClient { _, res in
813813
// let the 5th trigger succeed to ensure we don't early out on errors
@@ -918,6 +918,7 @@ class BuildTriggerTests: AppTestCase {
918918
$0.environment.buildTimeout = { 10 }
919919
$0.environment.buildTriggerAllowList = { [] }
920920
$0.environment.buildTriggerDownscaling = { 1 }
921+
$0.environment.gitlabPipelineLimit = { Constants.defaultGitlabPipelineLimit }
921922
$0.environment.gitlabPipelineToken = { "pipeline token" }
922923
$0.environment.random = { @Sendable _ in 0 }
923924
$0.environment.siteURL = { "http://example.com" }
@@ -997,6 +998,7 @@ class BuildTriggerTests: AppTestCase {
997998
$0.environment.buildTimeout = { 10 }
998999
$0.environment.buildTriggerAllowList = { [] }
9991000
$0.environment.buildTriggerDownscaling = { 0.05 } // 5% downscaling rate
1001+
$0.environment.gitlabPipelineLimit = { Constants.defaultGitlabPipelineLimit }
10001002
$0.environment.gitlabPipelineToken = { "pipeline token" }
10011003
$0.environment.siteURL = { "http://example.com" }
10021004
// Use live dependency but replace actual client with a mock so we can
@@ -1075,6 +1077,7 @@ class BuildTriggerTests: AppTestCase {
10751077
$0.environment.buildTimeout = { 10 }
10761078
$0.environment.buildTriggerAllowList = { [.id0] }
10771079
$0.environment.buildTriggerDownscaling = { 0.05 } // 5% downscaling rate
1080+
$0.environment.gitlabPipelineLimit = { Constants.defaultGitlabPipelineLimit }
10781081
$0.environment.gitlabPipelineToken = { "pipeline token" }
10791082
$0.environment.siteURL = { "http://example.com" }
10801083
// Use live dependency but replace actual client with a mock so we can

Tests/AppTests/Mocks/AppEnvironment+mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extension AppEnvironment {
2424
.init(
2525
fileManager: .mock,
2626
git: .mock,
27-
gitlabPipelineLimit: { Constants.defaultGitlabPipelineLimit },
2827
logger: { logger },
2928
setLogger: { logger in Self.logger = logger },
3029
shell: .mock

0 commit comments

Comments
 (0)