Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from valen90/feature/report-only-if-in-notifyR…
Browse files Browse the repository at this point in the history
…eleaseStages

Only report when current stage is in notifyReleaseStages
  • Loading branch information
steffendsommer authored Apr 12, 2017
2 parents 64518c8 + 7810043 commit 111a868
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Sources/Bugsnag/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import HTTP

public protocol ConfigurationType {
var apiKey: String { get }
var notifyReleaseStages: [String] { get }
var notifyReleaseStages: [String]? { get }
var endpoint: String { get }
var filters: [String] { get }

Expand All @@ -28,7 +28,7 @@ public struct Configuration: ConfigurationType {
}

public let apiKey: String
public let notifyReleaseStages: [String]
public let notifyReleaseStages: [String]?
public let endpoint: String
public let filters: [String]

Expand Down
16 changes: 7 additions & 9 deletions Sources/Bugsnag/PayloadTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Stacked
import HTTP

public protocol PayloadTransformerType {
var environment: Environment { get }
var apiKey: String { get }

func payloadFor(
message: String,
metadata: Node?,
Expand All @@ -13,14 +16,9 @@ public protocol PayloadTransformerType {
}

internal struct PayloadTransformer: PayloadTransformerType {
let drop: Droplet
let config: ConfigurationType
let environment: Environment
let apiKey: String

init(drop: Droplet, config: ConfigurationType) {
self.drop = drop
self.config = config
}

internal func payloadFor(
message: String,
metadata: Node?,
Expand Down Expand Up @@ -48,7 +46,7 @@ internal struct PayloadTransformer: PayloadTransformerType {
])

let app: Node = Node([
"releaseStage": Node(drop.environment.description),
"releaseStage": Node(environment.description),
"type": "Vapor"
])

Expand Down Expand Up @@ -91,7 +89,7 @@ internal struct PayloadTransformer: PayloadTransformerType {
])

return try JSON(node: [
"apiKey": self.config.apiKey,
"apiKey": apiKey,
"notifier": Node([
"name": "Bugsnag Vapor",
"version": "1.0.11",
Expand Down
16 changes: 13 additions & 3 deletions Sources/Bugsnag/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public final class Reporter: ReporterType {
config: config
)
self.payloadTransformer = transformer ?? PayloadTransformer(
drop: drop,
config: config
environment: drop.environment,
apiKey: config.apiKey
)
}

Expand All @@ -54,7 +54,10 @@ public final class Reporter: ReporterType {
completion complete: (() -> ())?
) throws {
if let error = error as? AbortError {
guard error.metadata?["report"]?.bool ?? true else {
guard
error.metadata?["report"]?.bool ?? true,
shouldNotifyForReleaseStage()
else {
return
}
try self.report(
Expand Down Expand Up @@ -100,4 +103,11 @@ public final class Reporter: ReporterType {
if let complete = complete { complete() }
}
}

private func shouldNotifyForReleaseStage() -> Bool {
guard let notifyReleaseStages = config.notifyReleaseStages else {
return true
}
return notifyReleaseStages.contains(drop.environment.description)
}
}
11 changes: 8 additions & 3 deletions Tests/BugsnagTests/Mocks/ConfigurationMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import Bugsnag

internal class ConfigurationMock: ConfigurationType {
let apiKey = "1337"
let notifyReleaseStages: [String] = []
let notifyReleaseStages: [String]?
let endpoint = "some-endpoint"
let filters: [String] = ["someFilter"]

required init(drop: Droplet) throws {}
init() {}
required convenience init(drop: Droplet) throws {
self.init()
}

init(releaseStages: [String]? = ["mock-environment"]) {
notifyReleaseStages = releaseStages
}
}
12 changes: 6 additions & 6 deletions Tests/BugsnagTests/Mocks/PayloadTransformerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Bugsnag
import HTTP

internal class PayloadTransformerMock: PayloadTransformerType {
let drop: Droplet
let config: ConfigurationType
var lastPayloadData: (message: String, metadata: Node?, request: Request?, severity: Severity, filters: [String])? = nil
let environment: Environment
let apiKey: String

var lastPayloadData: (message: String, metadata: Node?, request: Request?, severity: Severity, filters: [String])? = nil

required init(drop: Droplet, config: ConfigurationType) {
self.drop = drop
self.config = config
init(environment: Environment, apiKey: String) {
self.environment = environment
self.apiKey = apiKey
}

internal func payloadFor(
Expand Down
11 changes: 1 addition & 10 deletions Tests/BugsnagTests/PayloadTransformerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ class PayloadTransformerTests: XCTestCase {
]

override func setUp() {
let drop = Droplet(
arguments: nil,
workDir: nil,
environment: Environment.custom("mock-environment"),
config: nil,
localization: nil,
log: nil
)
let config = ConfigurationMock()
self.payloadTransformer = PayloadTransformer(drop: drop, config: config)
self.payloadTransformer = PayloadTransformer(environment: .custom("mock-environment"), apiKey: "1337")
let req = try! Request(method: .get, uri: "http://some-random-url.com/payload-test")
req.parameters = ["url": "value"]
req.query = ["query": "value"]
Expand Down
106 changes: 101 additions & 5 deletions Tests/BugsnagTests/ReporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ class ReporterTests: XCTestCase {
("testThatThePayloadGetsSubmitted", testThatThePayloadGetsSubmitted),
("testThatFiltersComeFromConfig", testThatFiltersComeFromConfig),
("testSeverityGetsDefaultValue", testSeverityGetsDefaultValue),
("testSeverityGetsGivenValue", testSeverityGetsGivenValue)
("testSeverityGetsGivenValue", testSeverityGetsGivenValue),
("testErrorNotReportedWhenEnvironmentNotInNotifyReleaseStages", testErrorNotReportedWhenEnvironmentNotInNotifyReleaseStages),
("testErrorReportedWhenEnvironmentInNotifyReleaseStages", testErrorReportedWhenEnvironmentInNotifyReleaseStages),
("testErrorNotBeingReportedWhenEmptyReleaseStages", testErrorNotBeingReportedWhenEmptyReleaseStages),
("testErrorBeingReportedWhenNilReleaseStages", testErrorBeingReportedWhenNilReleaseStages)
]


override func setUp() {
let drop = Droplet()
let drop = Droplet(
arguments: nil,
workDir: nil,
environment: .custom("mock-environment"),
config: nil,
localization: nil,
log: nil
)
let config = ConfigurationMock()
self.connectionManager = ConnectionManagerMock(drop: drop, config: config)
self.payloadTransformer = PayloadTransformerMock(drop: drop, config: config)
self.payloadTransformer = PayloadTransformerMock(
environment: drop.environment,
apiKey: "1337"
)
self.reporter = Reporter(
drop: drop,
config: config,
Expand Down Expand Up @@ -167,7 +180,6 @@ class ReporterTests: XCTestCase {
waitForExpectations(timeout: 1)
}


func testThatFiltersComeFromConfig() {
let req = try! Request(method: .get, uri: "some-random-uri")

Expand All @@ -192,6 +204,90 @@ class ReporterTests: XCTestCase {
XCTAssertEqual(self.payloadTransformer.lastPayloadData?.3, Severity.info)
}

func testErrorNotReportedWhenEnvironmentNotInNotifyReleaseStages() {
let drop = Droplet(
arguments: nil,
workDir: nil,
environment: .development, //currentEnvironment = "development"
config: nil,
localization: nil,
log: nil
)
let conf = ConfigurationMock() //notifyReleaseStages = ["mock-environment"]
let repo = Reporter(
drop: drop,
config: conf,
connectionManager: self.connectionManager,
transformer: self.payloadTransformer
)
try! repo.report(error: Abort.badRequest, request: nil)
XCTAssertNil(self.payloadTransformer.lastPayloadData)

}

func testErrorReportedWhenEnvironmentInNotifyReleaseStages() {
let drop = Droplet(
arguments: nil,
workDir: nil,
environment: .custom("mock-environment"), //currentEnvironment = "mock-environment"
config: nil,
localization: nil,
log: nil
)
let conf = ConfigurationMock() //notifyReleaseStages = ["mock-environment"]
let repo = Reporter(
drop: drop,
config: conf,
connectionManager: self.connectionManager,
transformer: self.payloadTransformer
)
try! repo.report(error: Abort.badRequest, request: nil)
XCTAssertNotNil(self.payloadTransformer.lastPayloadData)

}

func testErrorNotBeingReportedWhenEmptyReleaseStages() {
let drop = Droplet(
arguments: nil,
workDir: nil,
environment: .custom("mock-environment"), //currentEnvironment = "mock-environment"
config: nil,
localization: nil,
log: nil
)
let conf = ConfigurationMock(releaseStages: []) //notifyReleaseStages = []
let repo = Reporter(
drop: drop,
config: conf,
connectionManager: self.connectionManager,
transformer: self.payloadTransformer
)
try! repo.report(error: Abort.badRequest, request: nil)
XCTAssertNil(self.payloadTransformer.lastPayloadData)

}

func testErrorBeingReportedWhenNilReleaseStages() {
let drop = Droplet(
arguments: nil,
workDir: nil,
environment: .custom("mock-environment"), //currentEnvironment = "mock-environment"
config: nil,
localization: nil,
log: nil
)
let conf = ConfigurationMock(releaseStages: nil) //notifyReleaseStages = nil
let repo = Reporter(
drop: drop,
config: conf,
connectionManager: self.connectionManager,
transformer: self.payloadTransformer
)
try! repo.report(error: Abort.badRequest, request: nil)
XCTAssertNotNil(self.payloadTransformer.lastPayloadData)

}

// MARK: - Submission

func testThatThePayloadGetsSubmitted() {
Expand Down

0 comments on commit 111a868

Please sign in to comment.