Modern Apps git

← xtool.git

Fix duplicate App ID registration (#90)

Commit
efc1ed943f128e3bc98895ece3be9d4f49551faa
Author
Kabir Oberai <[email protected]>
When
2025-05-24 21:12 UTC (1 year ago)
Parents
693e60f
We pull the list of App IDs in `upsertApp` to see if we already created
the App ID. This broke when the user had too many App IDs, since DS
returned a paginated response and we only looked at the first page.

We could paginate the whole list (as seen in #89) but that's slow.
Instead, just filter the query to the expected App ID. This is a prefix
search so we might still receive multiple responses, but it's much less
likely that the user has like 10(? I'm not sure what the default/max
pagination limit is) apps registered with the XTL-ABC.com.foo.bar prefix
(they could have PlugIns but usually just a small number of them.)

This is a tentative fix for #87.

Diff

Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
diff --git a/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift b/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swiftindex 6a2af3e..8848ccc 100644--- a/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift	+++ b/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift	@@ -41,14 +41,22 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {     private func upsertApp(         bundleID: String,         entitlements: Entitlements,-        isFreeTeam: Bool,-        appIDs: [String: Components.Schemas.BundleId]+        isFreeTeam: Bool     ) async throws -> Components.Schemas.BundleId {+        let newBundleID = ProvisioningIdentifiers.identifier(fromSanitized: bundleID, context: self.context)++        let existing = try await context.developerAPIClient+            .bundleIdsGetCollection(query: .init(+                filter_lbrack_identifier_rbrack_: [bundleID]+            ))+            .ok.body.json.data+            // filter[identifier] is a prefix filter so we need to manually upgrade to equality+            .first(where: { $0.attributes?.identifier == newBundleID })+         let appID: Components.Schemas.BundleId-        if let existing = appIDs[bundleID] {+        if let existing {             appID = existing         } else {-            let newBundleID = ProvisioningIdentifiers.identifier(fromSanitized: bundleID, context: self.context)             let name = ProvisioningIdentifiers.appName(fromSanitized: bundleID)             let createResponse = try await context.developerAPIClient.bundleIdsCreateInstance(                 body: .json(@@ -150,8 +158,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {     /// that the app has).     private func addApp(         _ app: URL,-        isFreeTeam: Bool,-        appIDs: [String: Components.Schemas.BundleId]+        isFreeTeam: Bool     ) async throws -> ProvisioningInfo {         let infoURL = app.appendingPathComponent("Info.plist")         guard let data = try? Data(contentsOf: infoURL),@@ -179,7 +186,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {             entitlements = try Entitlements(entitlements: [])         } -        let appID = try await upsertApp(bundleID: bundleID, entitlements: entitlements, isFreeTeam: isFreeTeam, appIDs: appIDs)+        let appID = try await upsertApp(bundleID: bundleID, entitlements: entitlements, isFreeTeam: isFreeTeam)         let newBundleID = appID.attributes!.identifier!          let teamID = try signingInfo.certificate.teamID()@@ -225,17 +232,6 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {         )     } -    // keyed by sanitized bundle ID-    private func getCurrentAppIDs() async throws -> [String: Components.Schemas.BundleId] {-        let bundleIDs = try await context.developerAPIClient.bundleIdsGetCollection().ok.body.json.data-        let keyedIDs = bundleIDs-            .compactMap { id -> (String, Components.Schemas.BundleId)? in-                guard let bundleID = id.attributes?.identifier else { return nil }-                return (ProvisioningIdentifiers.sanitize(identifier: bundleID), id)-            }-        return Dictionary(keyedIDs, uniquingKeysWith: { $1 })-    }-     /// Registers the app + its extensions, returning the profile and entitlements of each     public func perform() async throws -> [URL: ProvisioningInfo] {         var apps: [URL] = [root]@@ -244,9 +240,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {             apps += plugins.implicitContents.filter { $0.pathExtension.lowercased() == "appex" }         } -        async let isFreeTeamTask = context.auth.team()?.isFree == true-        async let appIDsTask = getCurrentAppIDs()-        let (isFreeTeam, appIDs) = try await (isFreeTeamTask, appIDsTask)+        let isFreeTeam = try await context.auth.team()?.isFree == true          return try await withThrowingTaskGroup(             of: (URL, ProvisioningInfo).self,@@ -254,7 +248,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {         ) { group in             for app in apps {                 group.addTask {-                    let info = try await addApp(app, isFreeTeam: isFreeTeam, appIDs: appIDs)+                    let info = try await addApp(app, isFreeTeam: isFreeTeam)                     return (app, info)                 }             }
Sources/XKit/Utilities/ProvisioningIdentifiers.swift
diff --git a/Sources/XKit/Utilities/ProvisioningIdentifiers.swift b/Sources/XKit/Utilities/ProvisioningIdentifiers.swiftindex f956c5d..7f180a9 100644--- a/Sources/XKit/Utilities/ProvisioningIdentifiers.swift+++ b/Sources/XKit/Utilities/ProvisioningIdentifiers.swift@@ -21,8 +21,8 @@ extension ProvisioningIdentifiers {         return identifier.split(separator: ".").dropFirst().joined(separator: ".")     } -    static func identifier(fromSanitized sanitized: String, context: SigningContext? = nil) -> String {-        let uuid = (context?.auth.identityID ?? UUID().uuidString).split(separator: "-")[0].uppercased()+    static func identifier(fromSanitized sanitized: String, context: SigningContext) -> String {+        let uuid = context.auth.identityID.split(separator: "-")[0].uppercased()         return "\(Self.idPrefix)\(uuid).\(sanitized)"     } @@ -36,7 +36,7 @@ extension ProvisioningIdentifiers {         return sanitize(identifier: id)     } -    static func groupID(fromSanitized sanitized: String, context: SigningContext? = nil) -> DeveloperServicesAppGroup.GroupID {+    static func groupID(fromSanitized sanitized: String, context: SigningContext) -> DeveloperServicesAppGroup.GroupID {         .init(rawValue: "\(Self.groupPrefix)\(identifier(fromSanitized: sanitized, context: context))")     }