| 1 | | import Foundation |
| 2 | | |
| 3 | | /// Where and how a branch of published runs is laid out (DESIGN.md D9). Decisions only; |
| 4 | | /// the command does the file and git work. |
| 5 | | public enum PublishedLayout { |
| 6 | | /// The folder of the branch that holds one subfolder per run. |
| 7 | | public static let runsFolder = "tests" |
| 8 | | /// How many characters of a commit name its run folder, as the report's headline |
| 9 | | /// shows it. |
| 10 | | public static let shortCommitLength = 7 |
| 11 | | /// How many runs `covane publish` keeps unless told otherwise. |
| 12 | | public static let defaultRunsToKeep = 100 |
| 13 | | |
| 14 | | /// The folder name of a commit's run. |
| 15 | | /// - Throws: `PublishError.notACommit` unless the text starts with at least |
| 16 | | /// `shortCommitLength` hexadecimal digits, so a name can never point outside the |
| 17 | | /// runs folder. |
| 18 | 6 | public static func runFolder(forCommit commit: String) throws -> String { |
| 19 | 6 | let name = commit.prefix(shortCommitLength) |
| 20 | 16 | guard name.count == shortCommitLength, name.allSatisfy(\.isHexDigit) else { |
| 21 | 4 | throw PublishError.notACommit(commit) |
| 22 | 4 | } |
| 23 | 2 | return String(name) |
| 24 | 6 | } |
| 25 | | |
| 26 | | /// The run folders to remove so that `limit` runs stay: the one just published, whatever |
| 27 | | /// its date, and the newest of the others by date. Runs without coverage count like any |
| 28 | | /// other. |
| 29 | | public static func foldersToPrune( |
| 30 | | among runs: [HistoryRun], keeping limit: Int, publishing publishedFolder: String |
| 31 | 5 | ) -> [String] { |
| 32 | 14 | let others = runs.filter { $0.folder != publishedFolder } |
| 33 | 5 | let newestFirst = others.sorted { |
| 34 | 5 | ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder) |
| 35 | 5 | } |
| 36 | 5 | return newestFirst.dropFirst(limit - 1).map(\.folder) |
| 37 | 5 | } |
| 38 | | } |
| 39 | | |
| 40 | | public enum PublishError: Error, Equatable { |
| 41 | | case notACommit(String) |
| 42 | | } |
| 43 | | |
| 44 | | extension PublishError: CustomStringConvertible { |
| 45 | 8 | public var description: String { |
| 46 | 8 | switch self { |
| 47 | 8 | case .notACommit(let text): |
| 48 | 8 | "\"\(text)\" is not a commit: it must start with at least " |
| 49 | 8 | + "\(PublishedLayout.shortCommitLength) hexadecimal digits." |
| 50 | 8 | } |
| 51 | 8 | } |
| 52 | | } |
| 53 | | |
| 54 | | /// A step outcome as GitHub Actions names it (`steps.<id>.outcome`). |
| 55 | | public enum ActionOutcome: String, Sendable, Equatable, CaseIterable { |
| 56 | | case success, failure, skipped, cancelled |
| 57 | | } |
| 58 | | |
| 59 | | /// Why a run has no coverage page, from the outcomes GitHub Actions gives its steps. |
| 60 | | public enum UnavailableReason { |
| 61 | | /// Names what failed, never where, since the page is public. Nil when both steps |
| 62 | | /// succeeded and the report can be published. |
| 63 | 8 | public static func forOutcomes(tests: ActionOutcome, report: ActionOutcome) -> String? { |
| 64 | 8 | if tests == .failure { return "The package tests failed" } |
| 65 | 6 | if tests != .success { return "The package tests did not run" } |
| 66 | 4 | if report != .success { return "The coverage report could not be built" } |
| 67 | 1 | return nil |
| 68 | 8 | } |
| 69 | | } |
| 70 | | |
| 71 | | /// The site's front page: it sends the visitor to the newest run. |
| 72 | | public struct RootRedirectPage: Sendable { |
| 73 | | private let title: String |
| 74 | | private let latestRunFolder: String |
| 75 | | |
| 76 | 2 | public init(title: String, latestRunFolder: String) { |
| 77 | 2 | self.title = title |
| 78 | 2 | self.latestRunFolder = latestRunFolder |
| 79 | 2 | } |
| 80 | | |
| 81 | 2 | public var html: String { |
| 82 | 2 | """ |
| 83 | 2 | <!doctype html> |
| 84 | 2 | <meta charset="utf-8"> |
| 85 | 2 | <title>\(escapingHTML(title))</title> |
| 86 | 2 | <meta http-equiv="refresh" content="0; url=\(PublishedLayout.runsFolder)/\(escapingHTML(latestRunFolder))/index.html"> |
| 87 | 2 | <a href="\(PublishedLayout.runsFolder)/\(escapingHTML(latestRunFolder))/index.html">Test and coverage reports</a> |
| 88 | 2 | |
| 89 | 2 | """ |
| 90 | 2 | } |
| 91 | | } |