Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sync testflight config command #203

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e972651
Add ./config to gitignore
DechengMa Jul 10, 2020
114d95b
Create TestFlightConfig and Loader with new Filesystem models
DechengMa Jul 10, 2020
abefab7
Create SyncResourceComparator for comparing local and server res
DechengMa Jul 10, 2020
53c6436
Create essential pull TestFlight config service func with sync commands
DechengMa Jul 10, 2020
c59e0eb
Small model and syntax tweaks on Reader and ListTesterOperation
DechengMa Jul 10, 2020
2066c54
Add testing for sync betagroup and testers comparator
DechengMa Jul 15, 2020
c298746
Make SyncStrategy confirm to Equatable
DechengMa Jul 15, 2020
6ebab0c
Syntax fixes for sync command and some other tweak for passing swiftLint
DechengMa Jul 15, 2020
ebdbae0
Create Update and Create beta group operations
DechengMa Jul 20, 2020
2a0fe03
Wire up Test Flight push command to the APIs
DechengMa Jul 20, 2020
41ca74d
Make swiftlint pass in PushCommand
DechengMa Jul 20, 2020
2ca5b7a
Add custom Hashable logic to Filesystem.BetaGroup
DechengMa Jul 20, 2020
af0b5d8
Some UI Updates in TestFlightPushCommand
DechengMa Jul 20, 2020
bec00a4
Merge branch 'master' into testflight/sync
DechengMa Jul 20, 2020
a4388f4
Add refresh local after sync completed feature
DechengMa Jul 21, 2020
d0c1fad
Merge branch 'master' into testflight/sync
DechengMa Jul 23, 2020
4ef448d
remove TestFlightConfigLoader -> add init to TestFlightConfiguration
DechengMa Jul 23, 2020
5d36cf9
Tweak ResourceComparatorCompareTests
DechengMa Jul 23, 2020
5be708c
BetaGroup && BetaTester struct let -> var, remove tester inviteType
DechengMa Jul 23, 2020
3e4f8fe
Introduce AppSyncActions and update TestFlightPushCommand
DechengMa Jul 24, 2020
92d3dc4
Add support for Creating group and add tester together
DechengMa Jul 24, 2020
b69901d
fix swift lint issues
DechengMa Jul 24, 2020
c2fe7ff
Make convenience init for BetaGroup and Testers for init sdk model
DechengMa Jul 27, 2020
0e5363c
Optimised pullTestFlightConfigurations for running request in parallel
DechengMa Jul 27, 2020
27f4ebd
Helper Text polishing. Add sync by bundleId feature for pushing
DechengMa Jul 28, 2020
47a4b8f
Optimise invite Tester to group to run in parallel
DechengMa Jul 28, 2020
49a5a8e
Operation optimisation in List and remove BetaTester
DechengMa Aug 4, 2020
b8d4f02
Optimise service pullTestFlightConfigurations to use chunk(into: )
DechengMa Aug 4, 2020
e5fca76
UI optimisation in Push and Pull command
DechengMa Aug 4, 2020
8e600f1
optimised removeTestersFromGroup to request 5 times per sec
DechengMa Aug 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ Temporary Items
/Packages
/*.xcodeproj
xcuserdata/
config/auth.yml
/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020 Itty Bitty Apps Pty Ltd

import ArgumentParser
import Foundation
import FileSystem

struct TestFlightPullCommand: CommonParsableCommand {

static var configuration = CommandConfiguration(
commandName: "pull",
abstract: "Pull down existing TestFlight configuration, refreshing local configuration files."
)

@OptionGroup()
var common: CommonOptions

@Option(
default: "./config/apps",
help: "Path to the folder containing the TestFlight configuration."
) var outputPath: String

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to filter the pulled apps by bundleId.

func run() throws {
let service = try makeService()

let configs = try service.pullTestFlightConfigurations()

try configs.save(in: outputPath)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// Copyright 2020 Itty Bitty Apps Pty Ltd

import ArgumentParser
import FileSystem
import Foundation
import Model

struct TestFlightPushCommand: CommonParsableCommand {

static var configuration = CommandConfiguration(
commandName: "push",
abstract: "Push the local configuration to TestFlight."
)

@OptionGroup()
var common: CommonOptions

@Option(
default: "./config/apps",
help: "Path to the folder containing the TestFlight configuration."
)
var inputPath: String

@Flag(help: "Perform a dry run.")
var dryRun: Bool

func run() throws {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the fundamental problem with this command is that you're executing the service requests as you're processing. This should be more functional. Eg:

compute(localState, remoteState) -> changeSet
execute(changeSet, isDryRun) -> ()

let service = try makeService()

print("Loading local TestFlight configs... \n")

let localConfigurations = try [TestFlightConfiguration](from: inputPath)

print("Loading server TestFlight configs... \n")
let serverConfigurations = try service.pullTestFlightConfigurations()

let actions = compare(
serverConfigurations: serverConfigurations,
with: localConfigurations
)

if dryRun {
render(actions: actions)
} else {
try process(actions: actions, with: service)
}

print("Refreshing local configurations...")

try service.pullTestFlightConfigurations().save(in: inputPath)

print("Refreshing completed.")
}

func render(actions: [AppSyncActions]) {
print("'Dry Run' mode activated, changes will not be applied. \n")

actions.forEach {
print("\($0.app.name ?? ""):")
// 1. app testers
print("- Testers in App: ")
$0.appTestersSyncActions.forEach { $0.render(dryRun: dryRun) }

// 2. BetaGroups in App
print("- BetaGroups in App: ")
$0.betaGroupSyncActions.forEach { $0.render(dryRun: dryRun) }

// 3. Testers in BetaGroup
print("- Testers In Beta Group: ")
$0.testerInGroupsAction.forEach {
print("\($0.betaGroup.groupName):")
$0.testerActions.forEach { $0.render(dryRun: dryRun) }
}
}
}

private func process(actions: [AppSyncActions], with service: AppStoreConnectService) throws {
try actions.forEach { appAction in
print("\(appAction.app.name ?? ""): ")
// 1. app testers
print("- Testers in App: ")
try processAppTesterActions(
appAction.appTestersSyncActions,
appId: appAction.app.id,
service: service
)

// 2. beta groups in app
print("- BetaGroups in App: ")
try processBetagroupsActions(
appAction.betaGroupSyncActions,
appId: appAction.app.id,
service: service
)

// 3. testers in beta group
print("- Testers In Beta Group: ")
try appAction.testerInGroupsAction.forEach {
try processTestersInBetaGroupActions(
$0.testerActions,
betagroupId: $0.betaGroup.id!,
appTesters: appAction.appTesters,
service: service
)
}
}
}

private func compare(
serverConfigurations: [TestFlightConfiguration],
with localConfigurations: [TestFlightConfiguration]
) -> [AppSyncActions] {
return serverConfigurations.compactMap { serverConfiguration in
guard
let localConfiguration = localConfigurations
.first(where: { $0.app.id == serverConfiguration.app.id }) else {
return nil
}

let appTesterSyncActions = SyncResourceComparator(
localResources: localConfiguration.testers,
serverResources: serverConfiguration.testers
)
.compare()

let betaGroupSyncActions = SyncResourceComparator(
localResources: localConfiguration.betagroups,
serverResources: serverConfiguration.betagroups
)
.compare()

let testerInGroupsAction = localConfiguration.betagroups.compactMap { localBetagroup -> BetaTestersInGroupActions? in
guard
let serverBetaGroup = serverConfiguration
.betagroups
.first(where: { $0.id == localBetagroup.id }) else {
return nil
}

return BetaTestersInGroupActions(
betaGroup: localBetagroup,
testerActions: SyncResourceComparator(
localResources: localBetagroup.testers,
serverResources: serverBetaGroup.testers
)
.compare()
)
}

return AppSyncActions(
app: localConfiguration.app,
appTesters: localConfiguration.testers,
appTestersSyncActions: appTesterSyncActions,
betaGroupSyncActions: betaGroupSyncActions,
testerInGroupsAction: testerInGroupsAction
)
}
}

func processAppTesterActions(_ actions: [SyncAction<FileSystem.BetaTester>], appId: String, service: AppStoreConnectService) throws {
try actions.forEach { action in
switch action {
case .delete(let betatester):
try service.removeTesterFromApp(testerEmail: betatester.email, appId: appId)
action.render(dryRun: dryRun)
default:
return
}
}
}

func processBetagroupsActions(_ actions: [SyncAction<FileSystem.BetaGroup>], appId: String, service: AppStoreConnectService) throws {
try actions.forEach { action in
switch action {
case .create(let betagroup):
_ = try service.createBetaGroup(
appId: appId,
groupName: betagroup.groupName,
publicLinkEnabled: betagroup.publicLinkEnabled ?? false,
publicLinkLimit: betagroup.publicLinkLimit
)
action.render(dryRun: dryRun)
case .delete(let betagroup):
try service.deleteBetaGroup(with: betagroup.id!)
action.render(dryRun: dryRun)
case .update(let betagroup):
try service.updateBetaGroup(betaGroup: betagroup)
action.render(dryRun: dryRun)
}
}
}

func processTestersInBetaGroupActions(
_ actions: [SyncAction<FileSystem.BetaGroup.EmailAddress>],
betagroupId: String,
appTesters: [FileSystem.BetaTester],
service: AppStoreConnectService
) throws {
let deletingEmailsWithStrategy = actions
.compactMap { (action: SyncAction<FileSystem.BetaGroup.EmailAddress>) ->
(email: String, strategy: SyncAction<FileSystem.BetaGroup.EmailAddress>)? in
if case .delete(let email) = action {
return (email, action)
}
return nil
}

try service.removeTestersFromGroup(
emails: deletingEmailsWithStrategy.map { $0.email },
groupId: betagroupId
)

deletingEmailsWithStrategy.forEach { $0.strategy.render(dryRun: dryRun) }

let creatingTestersWithStrategy = actions
.compactMap { (strategy: SyncAction<FileSystem.BetaGroup.EmailAddress>) ->
(tester: FileSystem.BetaTester, strategy: SyncAction<FileSystem.BetaGroup.EmailAddress>)? in
if case .create(let email) = strategy,
let betatester = appTesters.first(where: { $0.email == email }) {
return (betatester, strategy)
}
return nil
}

try creatingTestersWithStrategy.forEach {
try service.inviteBetaTesterToGroups(
email: $0.tester.email,
groupId: betagroupId,
firstName: $0.tester.firstName,
lastName: $0.tester.lastName
)

$0.strategy.render(dryRun: dryRun)
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2020 Itty Bitty Apps Pty Ltd

import ArgumentParser

struct TestFlightSyncCommand: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "sync",
abstract: "Sync information about testflight with provided configuration file.",
subcommands: [
TestFlightPullCommand.self,
TestFlightPushCommand.self,
]
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public struct TestFlightCommand: ParsableCommand {
TestFlightBetaTestersCommand.self,
TestFlightBuildsCommand.self,
TestFlightPreReleaseVersionCommand.self,
TestFlightSyncCommand.self,
])

public init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private typealias Filter = DownloadSalesAndTrendsReports.Filter
extension Filter.Frequency: ExpressibleByArgument, CustomStringConvertible {
private typealias AllCases = [Filter.Frequency]

public static var allCases: AllCases {
private static var allCases: AllCases {
[.DAILY, .MONTHLY, .WEEKLY, .YEARLY]
}

Expand All @@ -25,7 +25,7 @@ extension Filter.Frequency: ExpressibleByArgument, CustomStringConvertible {
extension Filter.ReportType: ExpressibleByArgument, CustomStringConvertible {
private typealias AllCases = [Filter.ReportType]

public static var allCases: AllCases {
private static var allCases: AllCases {
[.SALES, .PRE_ORDER, .NEWSSTAND, .SUBSCRIPTION, .SUBSCRIPTION_EVENT, .SUBSCRIBER]
}

Expand All @@ -41,7 +41,7 @@ extension Filter.ReportType: ExpressibleByArgument, CustomStringConvertible {
extension Filter.ReportSubType: ExpressibleByArgument, CustomStringConvertible {
private typealias AllCases = [Filter.ReportSubType]

public static var allCases: AllCases {
private static var allCases: AllCases {
[.SUMMARY, .DETAILED, .OPT_IN]
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/AppStoreConnectCLI/Model/BetaGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import AppStoreConnect_Swift_SDK
import Combine
import Foundation
import FileSystem
import struct Model.App
import struct Model.BetaGroup
import SwiftyTextTable
Expand Down Expand Up @@ -57,3 +58,15 @@ extension BetaGroup {
)
}
}

extension FileSystem.BetaGroup: SyncResourceProcessable {

var compareIdentity: String {
id ?? ""
}

var syncResultText: String {
groupName
}

}
23 changes: 23 additions & 0 deletions Sources/AppStoreConnectCLI/Model/BetaTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import AppStoreConnect_Swift_SDK
import Combine
import Foundation
import FileSystem
import struct Model.BetaTester
import SwiftyTextTable

Expand Down Expand Up @@ -58,3 +59,25 @@ extension BetaTester: ResultRenderable, TableInfoProvider {
]
}
}

extension FileSystem.BetaTester: SyncResourceProcessable {

var syncResultText: String {
email
}

var compareIdentity: String {
email
}

}

extension String: SyncResourceProcessable {
var syncResultText: String {
self
}

var compareIdentity: String {
self
}
}
Loading