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 5 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,35 @@
// 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 configs, refreshing local config files"
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
)

@OptionGroup()
var common: CommonOptions

@Option(
default: "./config/apps",
help: "Path to the Folder containing the testflight configs."
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
) 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.pullTestFlightConfigs()

configs.forEach {
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
print($0.app.name)
print($0.betagroups.count)
}

try TestFlightConfigLoader().save(configs, in: outputPath)
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
}

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

import ArgumentParser
import FileSystem
import Foundation

struct TestFlightPushCommand: CommonParsableCommand {

static var configuration = CommandConfiguration(
commandName: "push",
abstract: "Push local testflight config files to server, update server configs"
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
)

@OptionGroup()
var common: CommonOptions

@Option(
default: "./config/apps",
help: "Path to the Folder containing the testflight configs."
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
) 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()

let localConfigs = try TestFlightConfigLoader().load(appsFolderPath: inputPath)

let serverConfigs = try service.pullTestFlightConfigs()

serverConfigs.forEach { serverConfig in
guard
let localConfig = localConfigs
.first(where: { $0.app.id == serverConfig.app.id }) else {
return
}

let appId = localConfig.app.id

// 1. compare shared testers in app
let sharedTestersHandleStrategies = SyncResourceComparator(
localResources: localConfig.testers,
serverResources: serverConfig.testers
).compare()

// 1.1 handle shared testers delete only
processAppTesterStrategies(sharedTestersHandleStrategies, appId: appId)


// 2. compare beta groups
let localBetagroups = localConfig.betagroups
let serverBetagroups = serverConfig.betagroups

let betaGroupHandlingStrategies = SyncResourceComparator(
localResources: localBetagroups,
serverResources: serverBetagroups
)
.compare()

// 2.1 handle groups create, update, delete
processBetagroupsStrategies(betaGroupHandlingStrategies, appId: appId)


// 3. compare testers in group and add, delete
localBetagroups.forEach { localBetagroup in
guard let serverBetagroup = serverBetagroups
.first(where: { $0.id == localBetagroup.id } ) else {
return
}

let betagroupId = serverBetagroup.id

let localGroupTesters = localBetagroup.testers

let serverGroupTesters = serverBetagroup.testers

let testersInGroupHandlingStrategies = SyncResourceComparator(
localResources: localGroupTesters,
serverResources: serverGroupTesters
).compare()

// 3.1 handling adding/deleting testers per group
processTestersInBetaGroupStrategies(testersInGroupHandlingStrategies, betagroupId: betagroupId, appTesters: localConfig.testers)
}
}
}

func processAppTesterStrategies(_ strategies: [SyncStrategy<FileSystem.BetaTester>], appId: String) {
if dryRun {
SyncResultRenderer<FileSystem.BetaTester>().render(strategies, isDryRun: true)
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
} else {
strategies.forEach { strategy in
switch strategy {
case .delete(let betatester):
print("delete testers \(betatester) from app \(appId)")
default:
return
}
}
}

}

func processBetagroupsStrategies(_ strategies: [SyncStrategy<FileSystem.BetaGroup>], appId: String) {
if dryRun {
SyncResultRenderer<FileSystem.BetaGroup>().render(strategies, isDryRun: true)
} else {
strategies.forEach { strategy in
switch strategy {
case .create(let betagroup):
print("create new beta group \(betagroup) in app \(appId)")
case .delete(let betagroup):
print("delete betagroup \(betagroup)")
case .update(let betagroup):
print("update betagroup \(betagroup)")
}
}
}

}

func processTestersInBetaGroupStrategies(_ strategies: [SyncStrategy<BetaGroup.EmailAddress>], betagroupId: String, appTesters: [BetaTester]) {
if dryRun {
SyncResultRenderer<FileSystem.BetaGroup.EmailAddress>().render(strategies, isDryRun: true)
} else {
strategies.forEach { strategy in
switch strategy {
case .create(let email):
print("add tester with email\(email) into betagroup\(betagroupId)")
case .delete(let email):
print("delete tester with email \(email) from betagroup \(betagroupId)")
default:
return
}
}
}
}

}


func testPrint<T: Codable>(json: T) {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let json = try! jsonEncoder.encode(json) // swiftlint:disable:this force_try
print(String(data: json, encoding: .utf8)!)
}
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
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
]
)
}
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
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
])

public init() {
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
}
}
29 changes: 29 additions & 0 deletions Sources/AppStoreConnectCLI/Readers and Renderers/Renderers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,32 @@ extension ResultRenderable where Self: TableInfoProvider {
return table.render()
}
}

protocol SyncResultRenderable: Equatable {
var syncResultText: String { get }
}

struct SyncResultRenderer<T: SyncResultRenderable> {
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 printing the results as we execute the commands not after.


func render(_ strategy: [SyncStrategy<T>], isDryRun: Bool) {
strategy.forEach { renderResultText($0, isDryRun) }
}

func render(_ strategy: SyncStrategy<T>, isDryRun: Bool) {
renderResultText(strategy, isDryRun)
}

private func renderResultText(_ strategy: SyncStrategy<T>, _ isDryRun: Bool) {
let resultText: String
switch strategy {
case .create(let input):
resultText = "➕ \(input.syncResultText)"
case .delete(let input):
resultText = "➖ \(input.syncResultText)"
case .update(let input):
resultText = "⬆️ \(input.syncResultText)"
}

print("\(isDryRun ? "" : "✅") \(resultText)")
}
}
57 changes: 57 additions & 0 deletions Sources/AppStoreConnectCLI/Services/AppStoreConnectService.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 Model

class AppStoreConnectService {
Expand Down Expand Up @@ -798,6 +799,62 @@ class AppStoreConnectService {
.await()
}

func pullTestFlightConfigs() throws -> [TestFlightConfiguration] {
DechengMa marked this conversation as resolved.
Show resolved Hide resolved

let apps = try listApps(bundleIds: [], names: [], skus: [], limit: nil)
Copy link
Member

Choose a reason for hiding this comment

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

We will need to support pulling a subset of apps (filtered by bundleIds probably).



testPrint(json: apps)

return try apps.map {
let testers: [FileSystem.BetaTester] = try ListBetaTestersOperation(options:
.init(appIds: [$0.id])
)
.execute(with: requestor)
.await()
.map{
FileSystem.BetaTester(
email: ($0.betaTester.attributes?.email)!,
firstName: $0.betaTester.attributes?.firstName,
lastName: $0.betaTester.attributes?.lastName,
inviteType: $0.betaTester.attributes?.inviteType?.rawValue
)
}

testPrint(json: testers)

let betagroups = try ListBetaGroupsOperation(
options: .init(appIds: [$0.id], names: [], sort: nil)
)
.execute(with: requestor)
.await()
DechengMa marked this conversation as resolved.
Show resolved Hide resolved
.map { output -> FileSystem.BetaGroup in
let testersEmails = try ListBetaTestersOperation(
options: .init(groupIds: [output.betaGroup.id])
)
.execute(with: requestor)
.await()
.compactMap { $0.betaTester.attributes?.email }

return FileSystem.BetaGroup(
id: output.betaGroup.id,
groupName: (output.betaGroup.attributes?.name)!,
isInternal: output.betaGroup.attributes?.isInternalGroup,
publicLink: output.betaGroup.attributes?.publicLink,
publicLinkEnabled: output.betaGroup.attributes?.publicLinkEnabled,
publicLinkLimit: output.betaGroup.attributes?.publicLinkLimit,
publicLinkLimitEnabled: output.betaGroup.attributes?.publicLinkLimitEnabled,
creationDate: output.betaGroup.attributes?.createdDate?.formattedDate,
testers: testersEmails
)
}

testPrint(json: betagroups)

return TestFlightConfiguration(app: $0, testers: testers, betagroups: betagroups)
}
}

/// Make a request for something `Decodable`.
///
/// - Parameters:
Expand Down
Loading