import Foundation enum ChangedFilesPresentation { static func facts(_ task: ThinTask) -> ThinChangeFiles? { guard !task.candidateSha.isEmpty, let step = task.steps?.last(where: { $0.specialist == "engineering" && $0.candidateSha == task.candidateSha }), step.state == "done", let facts = step.changeFiles, facts.sha == task.candidateSha, facts.base == step.baseSha else { return nil } return facts } struct Group: Identifiable { var folder: String var files: [ThinChangedFile] var id: String { folder } } static func groups(_ facts: ThinChangeFiles) -> [Group] { guard facts.entries.count > 10 else { return [Group(folder: "", files: facts.entries)] } let folders = Dictionary(grouping: facts.entries) { String($0.path.split(separator: "/").dropLast().joined(separator: "/")) } return folders.keys.sorted().map { Group(folder: $0, files: folders[$0] ?? []) } } static func fileURL(task: ThinTask, facts: ThinChangeFiles, file: ThinChangedFile) -> URL? { guard let source = task.repo else { return nil } let repo = source.hasPrefix("amadan.net/") ? String(source.dropFirst("amadan.net/".count)) : source let parts = repo.split(separator: "/", omittingEmptySubsequences: false) guard parts.count == 2, parts.allSatisfy({ $0 != "." && $0 != ".." && $0.range(of: #"^[A-Za-z0-9_.-]+$"#, options: .regularExpression) != nil }), ThinChangeFiles.path(file.path), (file.status != "deleted" || ThinChangeFiles.commit(facts.base)), ThinChangeFiles.commit(facts.sha) else { return nil } var url = URLComponents() url.scheme = "https"; url.host = "amadan.net"; url.path = "/" + repo url.queryItems = [URLQueryItem(name: "ref", value: file.status == "deleted" ? facts.base : facts.sha), URLQueryItem(name: "path", value: file.path)] // Go's query decoder treats a literal plus as a space. url.percentEncodedQuery = url.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B") return url.url } static func total(files: Int, additions: Int, deletions: Int) -> String { String.localizedStringWithFormat(Bundle.module.localizedString(forKey: "native.changes.total", value: nil, table: nil), files, additions, deletions) } static func displayPath(_ file: ThinChangedFile) -> String { VisibleCodeText.display(file.path) } static func renamedFrom(_ file: ThinChangedFile) -> String? { file.oldPath.map { String(format: EquipoStrings.text("native.changes.renamed_from"), VisibleCodeText.display($0)) } } static func status(_ file: ThinChangedFile) -> String { switch file.status { case "added": EquipoStrings.text("native.changes.added") case "deleted": EquipoStrings.text("native.changes.deleted") case "renamed": EquipoStrings.text("native.changes.renamed") default: EquipoStrings.text("native.changes.modified") } } }