1import 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.
5public 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.
186 public static func runFolder(forCommit commit: String) throws -> String {
196 let name = commit.prefix(shortCommitLength)
2016 guard name.count == shortCommitLength, name.allSatisfy(\.isHexDigit) else {
214 throw PublishError.notACommit(commit)
224 }
232 return String(name)
246 }
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
315 ) -> [String] {
3214 let others = runs.filter { $0.folder != publishedFolder }
335 let newestFirst = others.sorted {
345 ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder)
355 }
365 return newestFirst.dropFirst(limit - 1).map(\.folder)
375 }
38}
39
40public enum PublishError: Error, Equatable {
41 case notACommit(String)
42}
43
44extension PublishError: CustomStringConvertible {
458 public var description: String {
468 switch self {
478 case .notACommit(let text):
488 "\"\(text)\" is not a commit: it must start with at least "
498 + "\(PublishedLayout.shortCommitLength) hexadecimal digits."
508 }
518 }
52}
53
54/// A step outcome as GitHub Actions names it (`steps.<id>.outcome`).
55public 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.
60public 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.
638 public static func forOutcomes(tests: ActionOutcome, report: ActionOutcome) -> String? {
648 if tests == .failure { return "The package tests failed" }
656 if tests != .success { return "The package tests did not run" }
664 if report != .success { return "The coverage report could not be built" }
671 return nil
688 }
69}
70
71/// The site's front page: it sends the visitor to the newest run.
72public struct RootRedirectPage: Sendable {
73 private let title: String
74 private let latestRunFolder: String
75
762 public init(title: String, latestRunFolder: String) {
772 self.title = title
782 self.latestRunFolder = latestRunFolder
792 }
80
812 public var html: String {
822 """
832 <!doctype html>
842 <meta charset="utf-8">
852 <title>\(escapingHTML(title))</title>
862 <meta http-equiv="refresh" content="0; url=\(PublishedLayout.runsFolder)/\(escapingHTML(latestRunFolder))/index.html">
872 <a href="\(PublishedLayout.runsFolder)/\(escapingHTML(latestRunFolder))/index.html">Test and coverage reports</a>
882
892 """
902 }
91}