Add a bug report hook. (#2988)

This commit is contained in:
Doug 2024-07-01 13:53:24 +01:00 committed by GitHub
parent 1178561853
commit 78b0c7068d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 33 additions and 9 deletions

View File

@ -95,7 +95,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
navigationRootCoordinator = NavigationRootCoordinator() navigationRootCoordinator = NavigationRootCoordinator()
Self.setupServiceLocator(appSettings: appSettings) Self.setupServiceLocator(appSettings: appSettings, appHooks: appHooks)
ServiceLocator.shared.analytics.startIfEnabled() ServiceLocator.shared.analytics.startIfEnabled()
@ -340,14 +340,15 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
// MARK: - Private // MARK: - Private
private static func setupServiceLocator(appSettings: AppSettings) { private static func setupServiceLocator(appSettings: AppSettings, appHooks: AppHooks) {
ServiceLocator.shared.register(userIndicatorController: UserIndicatorController()) ServiceLocator.shared.register(userIndicatorController: UserIndicatorController())
ServiceLocator.shared.register(appSettings: appSettings) ServiceLocator.shared.register(appSettings: appSettings)
ServiceLocator.shared.register(networkMonitor: NetworkMonitor()) ServiceLocator.shared.register(networkMonitor: NetworkMonitor())
ServiceLocator.shared.register(bugReportService: BugReportService(withBaseURL: appSettings.bugReportServiceBaseURL, ServiceLocator.shared.register(bugReportService: BugReportService(withBaseURL: appSettings.bugReportServiceBaseURL,
applicationId: appSettings.bugReportApplicationId, applicationId: appSettings.bugReportApplicationId,
sdkGitSHA: sdkGitSha(), sdkGitSHA: sdkGitSha(),
maxUploadSize: appSettings.bugReportMaxUploadSize)) maxUploadSize: appSettings.bugReportMaxUploadSize,
appHooks: appHooks))
let posthogAnalyticsClient = PostHogAnalyticsClient() let posthogAnalyticsClient = PostHogAnalyticsClient()
posthogAnalyticsClient.updateSuperProperties(AnalyticsEvent.SuperProperties(appPlatform: .EXI, cryptoSDK: .Rust, cryptoSDKVersion: sdkGitSha())) posthogAnalyticsClient.updateSuperProperties(AnalyticsEvent.SuperProperties(appPlatform: .EXI, cryptoSDK: .Rust, cryptoSDKVersion: sdkGitSha()))
ServiceLocator.shared.register(analytics: AnalyticsService(client: posthogAnalyticsClient, ServiceLocator.shared.register(analytics: AnalyticsService(client: posthogAnalyticsClient,

View File

@ -19,7 +19,7 @@ import Foundation
// MARK: Registration // MARK: Registration
class AppHooks: AppHooksProtocol { class AppHooks: AppHooksProtocol {
private(set) var appSettingsHook: AppSettingsHookProtocol? private var appSettingsHook: AppSettingsHookProtocol?
func registerAppSettingsHook(_ hook: AppSettingsHookProtocol) { func registerAppSettingsHook(_ hook: AppSettingsHookProtocol) {
appSettingsHook = hook appSettingsHook = hook
} }
@ -28,6 +28,16 @@ class AppHooks: AppHooksProtocol {
guard let appSettingsHook else { return appSettings } guard let appSettingsHook else { return appSettings }
return appSettingsHook.run(appSettings: appSettings) return appSettingsHook.run(appSettings: appSettings)
} }
private var bugReportHook: BugReportHookProtocol?
func registerBugReportHook(_ hook: BugReportHookProtocol) {
bugReportHook = hook
}
func runBugReportHook(_ bugReport: BugReport) -> BugReport {
guard let bugReportHook else { return bugReport }
return bugReportHook.run(bugReport: bugReport)
}
} }
protocol AppHooksProtocol { protocol AppHooksProtocol {
@ -43,3 +53,7 @@ extension AppHooksProtocol {
protocol AppSettingsHookProtocol { protocol AppSettingsHookProtocol {
func run(appSettings: AppSettings) -> AppSettings func run(appSettings: AppSettings) -> AppSettings
} }
protocol BugReportHookProtocol {
func run(bugReport: BugReport) -> BugReport
}

View File

@ -26,6 +26,9 @@ class BugReportService: NSObject, BugReportServiceProtocol {
private let sdkGitSHA: String private let sdkGitSHA: String
private let maxUploadSize: Int private let maxUploadSize: Int
private let session: URLSession private let session: URLSession
private let appHooks: AppHooks
private let progressSubject = PassthroughSubject<Double, Never>() private let progressSubject = PassthroughSubject<Double, Never>()
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
@ -35,12 +38,14 @@ class BugReportService: NSObject, BugReportServiceProtocol {
applicationId: String, applicationId: String,
sdkGitSHA: String, sdkGitSHA: String,
maxUploadSize: Int, maxUploadSize: Int,
session: URLSession = .shared) { session: URLSession = .shared,
appHooks: AppHooks) {
self.baseURL = baseURL self.baseURL = baseURL
self.applicationId = applicationId self.applicationId = applicationId
self.sdkGitSHA = sdkGitSHA self.sdkGitSHA = sdkGitSHA
self.maxUploadSize = maxUploadSize self.maxUploadSize = maxUploadSize
self.session = session self.session = session
self.appHooks = appHooks
super.init() super.init()
} }
@ -53,6 +58,8 @@ class BugReportService: NSObject, BugReportServiceProtocol {
// swiftlint:disable:next cyclomatic_complexity // swiftlint:disable:next cyclomatic_complexity
func submitBugReport(_ bugReport: BugReport, func submitBugReport(_ bugReport: BugReport,
progressListener: CurrentValueSubject<Double, Never>) async -> Result<SubmitBugReportResponse, BugReportServiceError> { progressListener: CurrentValueSubject<Double, Never>) async -> Result<SubmitBugReportResponse, BugReportServiceError> {
let bugReport = appHooks.runBugReportHook(bugReport)
var params = [ var params = [
MultipartFormData(key: "text", type: .text(value: bugReport.text)), MultipartFormData(key: "text", type: .text(value: bugReport.text)),
MultipartFormData(key: "can_contact", type: .text(value: "\(bugReport.canContact)")) MultipartFormData(key: "can_contact", type: .text(value: "\(bugReport.canContact)"))

View File

@ -26,7 +26,7 @@ struct BugReport: Equatable {
let text: String let text: String
let includeLogs: Bool let includeLogs: Bool
let canContact: Bool let canContact: Bool
let githubLabels: [String] var githubLabels: [String]
let files: [URL] let files: [URL]
} }

@ -1 +1 @@
Subproject commit e03750f654cd3800c14b41827fb3f6ab57f7d5f0 Subproject commit 9fae88678e844e9284c3b10591ac700970862d9c

View File

@ -54,7 +54,8 @@ class BugReportServiceTests: XCTestCase {
applicationId: "mock_app_id", applicationId: "mock_app_id",
sdkGitSHA: "1234", sdkGitSHA: "1234",
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize, maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize,
session: .mock) session: .mock,
appHooks: AppHooks())
XCTAssertFalse(service.crashedLastRun) XCTAssertFalse(service.crashedLastRun)
} }
@ -63,7 +64,8 @@ class BugReportServiceTests: XCTestCase {
applicationId: "mock_app_id", applicationId: "mock_app_id",
sdkGitSHA: "1234", sdkGitSHA: "1234",
maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize, maxUploadSize: ServiceLocator.shared.settings.bugReportMaxUploadSize,
session: .mock) session: .mock,
appHooks: AppHooks())
let bugReport = BugReport(userID: "@mock:client.com", let bugReport = BugReport(userID: "@mock:client.com",
deviceID: nil, deviceID: nil,