Simply merge status logic (#1818)

This commit is contained in:
Ryan Nystrom
2018-05-19 19:07:42 -04:00
committed by GitHub
parent 344a6c652d
commit 1beccb9508
2 changed files with 27 additions and 19 deletions

View File

@@ -9,27 +9,23 @@
import Foundation
enum MergeHelper {
static func combinedMergeStatus(for states: [StatusState]) -> (state: IssueMergeSummaryModel.State, description: String) {
assert(!states.isEmpty, "Should only check merge status when there is at least one state")
let state: IssueMergeSummaryModel.State
let stateDescription: String
let failureDescription = NSLocalizedString("Some checks failed", comment: "")
switch states {
case let states where states.contains(.failure) || states.contains(.error):
state = .failure
stateDescription = failureDescription
case let states where states.contains(.pending):
state = .pending
stateDescription = NSLocalizedString("Merge status pending", comment: "")
case let states where states.containsOnly(.success):
state = .success
stateDescription = NSLocalizedString("All checks passed", comment: "")
default:
assert(false, "This should only occur when any of the `states` are of type `.expected`, which we have no clue of when it is used. The documentation (https://developer.github.com/v4/enum/statusstate/) doesn't answer that question either.")
state = .failure
stateDescription = failureDescription
var hasError = false, hasPending = false
for state in states {
switch state {
case .error, .failure, .expected, .__unknown: hasError = true
case .pending: hasPending = true
case .success: break
}
}
return (state, stateDescription)
if hasError {
return (.failure, NSLocalizedString("Some checks failed", comment: ""))
} else if hasPending {
return (.pending, NSLocalizedString("Merge status pending", comment: ""))
} else {
return (.success, NSLocalizedString("All checks passed", comment: ""))
}
}
}

View File

@@ -45,6 +45,12 @@ class MergeTests: XCTestCase {
XCTAssertEqual(
MergeHelper.combinedMergeStatus(for: [.failure, .pending]).state,
.failure)
XCTAssertEqual(
MergeHelper.combinedMergeStatus(for: [.pending, .failure]).state,
.failure)
XCTAssertEqual(
MergeHelper.combinedMergeStatus(for: [.failure, .pending, .success]).state,
.failure)
}
func test_mergeStatuses_containsFailure_andError() {
@@ -63,6 +69,12 @@ class MergeTests: XCTestCase {
XCTAssertEqual(
MergeHelper.combinedMergeStatus(for: [.pending, .success]).state,
.pending)
XCTAssertEqual(
MergeHelper.combinedMergeStatus(for: [.success, .pending]).state,
.pending)
XCTAssertEqual(
MergeHelper.combinedMergeStatus(for: [.success, .pending, .success]).state,
.pending)
}
func test_mergeStatuses_containsOnlySuccess() {