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 all 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,34 @@
// 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()

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

print("\nRefreshing local configurations...")
try configs.save(in: outputPath)
print("Refreshing completed.")
}

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

import ArgumentParser
import struct FileSystem.TestFlightConfiguration
import struct FileSystem.BetaGroup
import struct FileSystem.BetaTester
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

@Option(
parsing: .upToNextOption,
help: "Array of bundle IDs that uniquely identifies the apps that you would like to sync."
)
var bundleIds: [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 configurations...")
let localConfigurations = try [TestFlightConfiguration](from: inputPath, with: bundleIds)
print("Loading completed.")

print("\nLoading server TestFlight configurations...")
let serverConfigurations = try service.pullTestFlightConfigurations(with: bundleIds)
print("Loading completed.")

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

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

print("\nRefreshing local configurations...")
try service.pullTestFlightConfigurations().save(in: inputPath)
print("Refreshing completed.")
}
}

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

actions.forEach { action in
print("\n\(action.app.name ?? ""): ")

// 1. app testers
if action.appTestersSyncActions.isNotEmpty {
print("\n- Testers in App: ")
action.appTestersSyncActions.forEach { $0.render(dryRun: dryRun) }
}

// 2. BetaGroups in App
if action.betaGroupSyncActions.isNotEmpty {
print("\n- BetaGroups in App: ")
action.betaGroupSyncActions.forEach {
$0.render(dryRun: dryRun)

if case .create(let betagroup) = $0 {
action.testerInGroupsAction
.append(
.init(
betaGroup: betagroup,
testerActions: betagroup.testers.map {
SyncAction<BetaGroup.EmailAddress>.create($0)
}
)
)
}
}
}

// 3. Testers in BetaGroup
if action.testerInGroupsAction.isNotEmpty {
print("\n- Testers In Beta Group: ")
action.testerInGroupsAction.forEach {
if $0.testerActions.isNotEmpty {
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("\n\(appAction.app.name ?? ""): ")

var appAction = appAction

// 1. app testers
try processAppTesterActions(
appAction.appTestersSyncActions,
appId: appAction.app.id,
service: service
)

// 2. beta groups in app
try processBetagroupsActions(
appAction.betaGroupSyncActions,
appId: appAction.app.id,
appAction: &appAction,
service: service
)

// 3. testers in beta group
if appAction.testerInGroupsAction.isNotEmpty {
print("\n- Testers In Beta Group: ")
try appAction.testerInGroupsAction.forEach {
print("\($0.betaGroup.groupName): ")
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
}

let testerActions = SyncResourceComparator(
localResources: localBetagroup.testers,
serverResources: serverBetaGroup.testers
)
.compare()

if testerActions.isEmpty { return nil }

return BetaTestersInGroupActions(
betaGroup: localBetagroup,
testerActions: testerActions
)
}

guard appTesterSyncActions.isNotEmpty ||
betaGroupSyncActions.isNotEmpty ||
testerInGroupsAction.isNotEmpty else {
return nil
}

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

func processAppTesterActions(
_ actions: [SyncAction<BetaTester>],
appId: String,
service: AppStoreConnectService
) throws {
let testersToRemoveActionsWithEmails = actions.compactMap { action ->
(action: SyncAction<BetaTester>, email: String)? in
if case .delete(let betaTesters) = action {
return (action, betaTesters.email)
}
return nil
}

if testersToRemoveActionsWithEmails.isNotEmpty {
print("\n- Testers in App: ")
try service.removeTestersFromApp(testersEmails: testersToRemoveActionsWithEmails.map { $0.email }, appId: appId)

testersToRemoveActionsWithEmails.map { $0.action }.forEach { $0.render(dryRun: dryRun) }
}
}

func processBetagroupsActions(
_ actions: [SyncAction<BetaGroup>],
appId: String,
appAction: inout AppSyncActions,
service: AppStoreConnectService
) throws {
if actions.isNotEmpty {
print("\n- BetaGroups in App: ")

try actions.forEach { action in
switch action {
case .create(let betagroup):
let newCreatedBetaGroup = try service.createBetaGroup(
appId: appId,
groupName: betagroup.groupName,
publicLinkEnabled: betagroup.publicLinkEnabled ?? false,
publicLinkLimit: betagroup.publicLinkLimit
)
action.render(dryRun: dryRun)

if betagroup.testers.isNotEmpty {
appAction.testerInGroupsAction
.append(
.init(
betaGroup: newCreatedBetaGroup,
testerActions: betagroup.testers.map {
SyncAction<BetaGroup.EmailAddress>.create($0)
}
)
)
}

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<BetaGroup.EmailAddress>],
betagroupId: String,
appTesters: [BetaTester],
service: AppStoreConnectService
) throws {
let deletingEmailsWithStrategy: [(email: String, strategy: SyncAction<BetaGroup.EmailAddress>)] = actions
.compactMap { action in
if case .delete(let email) = action {
return (email, action)
}
return nil
}

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

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

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

if creatingTestersWithStrategy.isNotEmpty {
try service.inviteTestersToGroup(
betaTesters: creatingTestersWithStrategy.map { $0.tester },
groupId: betagroupId
)

creatingTestersWithStrategy.forEach { $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
11 changes: 11 additions & 0 deletions Sources/AppStoreConnectCLI/Helpers/Array+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2020 Itty Bitty Apps Pty Ltd

import Foundation

extension Array {
func chunked(into size: Int) -> [[Element]] {
return stride(from: 0, to: count, by: size).map {
Array(self[$0 ..< Swift.min($0 + size, count)])
}
}
}
6 changes: 6 additions & 0 deletions Sources/AppStoreConnectCLI/Helpers/Publisher+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ extension Publisher {
}
}
}

extension Sequence where Element: Publisher {
func merge() -> Publishers.MergeMany<Element> {
Publishers.MergeMany(self)
}
}
Loading