Skip to content

Commit

Permalink
IOS-8495: Start caching responses in release scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
JPedroAmorim committed Apr 30, 2024
1 parent 7905418 commit 53b0b8e
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ transifex/download/*
## Release scripts
scripts/ReleaseScripts/*/.build
scripts/ReleaseScripts/*/.swiftpm
scripts/ReleaseScripts/Resources/values.env
scripts/ReleaseScripts/Resources/values.env
scripts/ReleaseScripts/Resources/cache.json
2 changes: 1 addition & 1 deletion scripts/ReleaseScripts/AnnounceRelease/Sources/slack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private func sendReleaseCandidateMessage(input: UserInput, channelId: String) as
<\(input.jiraReleasePackageLink)|\(input.version) JIRA Release Package>
*Release notes*:
- \(input.releaseNotes)
\(input.releaseNotes)
"""
]

Expand Down
13 changes: 12 additions & 1 deletion scripts/ReleaseScripts/AnnounceRelease/Sources/userInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ struct UserInput {
}

func userInput() throws -> UserInput {
let version = try majorMinorInput("Enter the version number you're releasing (format: '[major].[minor]'):")
let version = if let version = readFromCache(key: .version) {
version
} else {
try majorMinorInput("Enter the version number you're releasing (format: '[major].[minor]'):")
}

let sdkVersion = try majorMinorPatchInput(submoduleMessage(.sdk))
writeToCache(key: .sdkVersion, value: sdkVersion)

let chatVersion = try majorMinorPatchInput(submoduleMessage(.chatSDK))
writeToCache(key: .chatVersion, value: chatVersion)

let releaseNotes = try releaseNotesInput(defaultReleaseNotes: "Bug fixes and performance improvements.")
writeToCache(key: .releaseNotes, value: releaseNotes)

let jiraReleasePackageLink = urlInput("Enter the Jira release package link for \(version) - (The one you created on 'Monday - Prepare Code Freeze'):")
let testFlightLink = urlInput("Enter the TestFlight link for this build")
let nextVersion = try majorMinorInput("Enter the next release version number (format: '[major].[minor]'):")
Expand Down
6 changes: 5 additions & 1 deletion scripts/ReleaseScripts/CreateRelease/Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ setVerbose()

do {
log("Started execution")
let version = try majorMinorInput("Enter the version number you're releasing (format: '[major].[minor]'):")
let version = if let version = readFromCache(key: .version) {
version
} else {
try majorMinorInput("Enter the version number you're releasing (format: '[major].[minor]'):")
}

log("Creating release branch and pushing to origin")
let releaseBranch = try createReleaseBranchAndPushToOrigin(version: version)
Expand Down
10 changes: 8 additions & 2 deletions scripts/ReleaseScripts/PrepareHotfix/Sources/userInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ struct UserInput {
}

func userInput() throws -> UserInput {
let tagMessage = "Enter the version number (format: '[major].[minor]' or '[major].[minor].[patch]') of the version you're creating a hotfix for:"
let tag = try majorMinorOrMajorMinorPatchInput(tagMessage)
let tag = if let tag = readFromCache(key: .version) {
tag
} else {
try majorMinorOrMajorMinorPatchInput(
"Enter the version number (format: '[major].[minor]' or '[major].[minor].[patch]') of the version you're creating a hotfix for:"
)
}

let hotfixVersion = try majorMinorOrMajorMinorPatchInput("Enter the hotfix version number (format: '[major].[minor].[patch]'):")
writeToCache(key: .version, value: hotfixVersion)

return .init(tag: tag, hotfixVersion: hotfixVersion)
}
5 changes: 5 additions & 0 deletions scripts/ReleaseScripts/PrepareRelease/Sources/userInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ struct UserInput {

func userInput() throws -> UserInput {
let version = try majorMinorInput("Enter the version number you're releasing (format: '[major].[minor]'):")
writeToCache(key: .version, value: version)

let sdkHash = try commitHashFromUser(submodule: .sdk)
writeToCache(key: .sdkHash, value: sdkHash)

let chatHash = try commitHashFromUser(submodule: .chatSDK)
writeToCache(key: .chatHash, value: chatHash)

return .init(version: version, sdkHash: sdkHash, chatHash: chatHash)
}
Expand Down
67 changes: 67 additions & 0 deletions scripts/ReleaseScripts/SharedReleaseScript/Sources/cache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Foundation

enum InputCacheError: Error {
case failedToDecodeJson
}

public enum InputCacheKeys: String {
case version
case sdkHash
case sdkVersion
case chatHash
case chatVersion
case releaseNotes
}

public func writeToCache(key: InputCacheKeys, value: String) {
do {
var cache = try decodeCache()
cache[key.rawValue] = value
try writeToURL(cache: cache)
} catch {
print("Non-fatal: failed to write \(key.rawValue):\(value) pair in cache.json with error \(String(describing: error))")
}
}

public func readFromCache(key: InputCacheKeys) -> String? {
do {
let cache = try decodeCache()
return cache[key.rawValue]
} catch {
print("Non-fatal: failed to read \(key.rawValue) from cache.json with error \(String(describing: error))")
return nil
}
}

private func decodeCache() throws -> [String: String] {
try changeCurrentWorkDirectoryToRootDirectory()

let cacheURL = try ensureCacheFile()
let cacheData = try Data(contentsOf: cacheURL)
let cache = try JSONSerialization.jsonObject(with: cacheData) as? [String: String]

guard let cache else {
throw InputCacheError.failedToDecodeJson
}

return cache
}

private let cacheURL = URL(fileURLWithPath: "scripts/ReleaseScripts/Resources/cache.json")

private func ensureCacheFile() throws -> URL {
let fileManager = FileManager.default

if !fileManager.fileExists(atPath: cacheURL.path) {
fileManager.createFile(atPath: cacheURL.path, contents: nil, attributes: nil)
let emptyCache: [String: String] = [:]
try writeToURL(cache: emptyCache)
}

return cacheURL
}

private func writeToURL(cache: [String: String]) throws {
let jsonData = try JSONSerialization.data(withJSONObject: cache, options: [])
try jsonData.write(to: cacheURL, options: [])
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import Foundation

public enum FileSystemError: Error {
case cannotChangeToRootDirectory
case cannotChangeToDirectory(directory: AvailableDirectories, path: String)
}

private let fileManager = FileManager.default

public enum AvailableDirectories {
case root
case currentScriptDirectory

var isInRootDirectory: Bool {
if case .root = self {
return true
} else {
return false
}
}
}

private var currentDirectory: AvailableDirectories = .currentScriptDirectory

public func changeCurrentWorkDirectoryToRootDirectory() throws {
guard !currentDirectory.isInRootDirectory else { return }
let rootDirectoryPath = "../../../"
let fileManager = FileManager.default
try changeDirectory(path: rootDirectoryPath, directory: .root)
}

guard fileManager.changeCurrentDirectoryPath(rootDirectoryPath) else {
throw FileSystemError.cannotChangeToRootDirectory
private func changeDirectory(path: String, directory: AvailableDirectories) throws {
guard fileManager.changeCurrentDirectoryPath(path) else {
throw FileSystemError.cannotChangeToDirectory(directory: directory, path: path)
}

currentDirectory = directory
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,33 @@ public struct ReleaseInput {
}

public func releaseInput() throws -> ReleaseInput {
let version = try majorMinorOrMajorMinorPatchInput("Enter the version number you're releasing (format: '[major].[minor]' or '[major].[minor].[patch]'):")
let message = try releaseNotesInput(defaultReleaseNotes: defaultReleaseNotesInput())
let version = if let version = readFromCache(key: .version) {
version
} else {
try majorMinorInput("Enter the version number you're releasing (format: '[major].[minor]'):")
}

let message = if let message = readFromCache(key: .releaseNotes) {
message
} else {
try releaseNotesInput(defaultReleaseNotes: defaultReleaseNotesInput())
}

return .init(version: version, message: message)
}

private func defaultReleaseNotesInput() throws -> String {
let sdkVersion = try submoduleVersionInput(.sdk)
let chatVersion = try submoduleVersionInput(.chatSDK)
let sdkVersion = if let sdkVersion = readFromCache(key: .sdkVersion) {
sdkVersion
} else {
try submoduleVersionInput(.sdk)
}

let chatVersion = if let chatVersion = readFromCache(key: .chatVersion) {
chatVersion
} else {
try submoduleVersionInput(.chatSDK)
}

let defaultReleaseNotes =
"""
Expand Down

0 comments on commit 53b0b8e

Please sign in to comment.