| 1 | import Foundation |
| 2 | |
| 3 | enum ChangedFilesPresentation { |
| 4 | static func facts(_ task: ThinTask) -> ThinChangeFiles? { |
| 5 | guard !task.candidateSha.isEmpty, |
| 6 | let step = task.steps?.last(where: { $0.specialist == "engineering" && $0.candidateSha == task.candidateSha }), |
| 7 | step.state == "done", let facts = step.changeFiles, facts.sha == task.candidateSha, facts.base == step.baseSha else { return nil } |
| 8 | return facts |
| 9 | } |
| 10 | struct Group: Identifiable { |
| 11 | var folder: String |
| 12 | var files: [ThinChangedFile] |
| 13 | var id: String { folder } |
| 14 | } |
| 15 | static func groups(_ facts: ThinChangeFiles) -> [Group] { |
| 16 | guard facts.entries.count > 10 else { return [Group(folder: "", files: facts.entries)] } |
| 17 | let folders = Dictionary(grouping: facts.entries) { String($0.path.split(separator: "/").dropLast().joined(separator: "/")) } |
| 18 | return folders.keys.sorted().map { Group(folder: $0, files: folders[$0] ?? []) } |
| 19 | } |
| 20 | static func fileURL(task: ThinTask, facts: ThinChangeFiles, file: ThinChangedFile) -> URL? { |
| 21 | guard let source = task.repo else { return nil } |
| 22 | let repo = source.hasPrefix("amadan.net/") ? String(source.dropFirst("amadan.net/".count)) : source |
| 23 | let parts = repo.split(separator: "/", omittingEmptySubsequences: false) |
| 24 | guard parts.count == 2, parts.allSatisfy({ |
| 25 | $0 != "." && $0 != ".." && $0.range(of: #"^[A-Za-z0-9_.-]+$"#, options: .regularExpression) != nil |
| 26 | }), ThinChangeFiles.path(file.path), |
| 27 | (file.status != "deleted" || ThinChangeFiles.commit(facts.base)), ThinChangeFiles.commit(facts.sha) else { return nil } |
| 28 | var url = URLComponents() |
| 29 | url.scheme = "https"; url.host = "amadan.net"; url.path = "/" + repo |
| 30 | url.queryItems = [URLQueryItem(name: "ref", value: file.status == "deleted" ? facts.base : facts.sha), |
| 31 | URLQueryItem(name: "path", value: file.path)] |
| 32 | // Go's query decoder treats a literal plus as a space. |
| 33 | url.percentEncodedQuery = url.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B") |
| 34 | return url.url |
| 35 | } |
| 36 | static func total(files: Int, additions: Int, deletions: Int) -> String { |
| 37 | String.localizedStringWithFormat(Bundle.module.localizedString(forKey: "native.changes.total", value: nil, table: nil), files, additions, deletions) |
| 38 | } |
| 39 | static func displayPath(_ file: ThinChangedFile) -> String { VisibleCodeText.display(file.path) } |
| 40 | static func renamedFrom(_ file: ThinChangedFile) -> String? { |
| 41 | file.oldPath.map { String(format: EquipoStrings.text("native.changes.renamed_from"), VisibleCodeText.display($0)) } |
| 42 | } |
| 43 | static func status(_ file: ThinChangedFile) -> String { |
| 44 | switch file.status { |
| 45 | case "added": EquipoStrings.text("native.changes.added") |
| 46 | case "deleted": EquipoStrings.text("native.changes.deleted") |
| 47 | case "renamed": EquipoStrings.text("native.changes.renamed") |
| 48 | default: EquipoStrings.text("native.changes.modified") |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |