Skip to content

Commit

Permalink
Link to the “current” docs from the package page.
Browse files Browse the repository at this point in the history
  • Loading branch information
daveverwer committed May 21, 2024
1 parent 8659466 commit ba08aa1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension API.PackageController.GetRoute {
var isArchived: Bool
var hasBinaryTargets: Bool
var homepageUrl: String?
var documentationTarget: DocumentationTarget? = nil
var currentDocumentationTarget: DocumentationTarget? = nil
var weightedKeywords: [WeightedKeyword]
var releaseReferences: [App.Version.Kind: App.Reference]
var fundingLinks: [FundingLink]
Expand Down Expand Up @@ -74,7 +74,7 @@ extension API.PackageController.GetRoute {
isArchived: Bool,
hasBinaryTargets: Bool = false,
homepageUrl: String? = nil,
documentationTarget: DocumentationTarget? = nil,
currentDocumentationTarget: DocumentationTarget? = nil,
weightedKeywords: [WeightedKeyword] = [],
defaultBranchReference: App.Reference,
releaseReference: App.Reference?,
Expand Down Expand Up @@ -107,7 +107,7 @@ extension API.PackageController.GetRoute {
self.isArchived = isArchived
self.hasBinaryTargets = hasBinaryTargets
self.homepageUrl = homepageUrl
self.documentationTarget = documentationTarget
self.currentDocumentationTarget = currentDocumentationTarget
self.weightedKeywords = weightedKeywords
self.releaseReferences = {
var refs = [App.Version.Kind.defaultBranch: defaultBranchReference]
Expand Down Expand Up @@ -167,7 +167,7 @@ extension API.PackageController.GetRoute {
isArchived: repository.isArchived,
hasBinaryTargets: result.defaultBranchVersion.hasBinaryTargets,
homepageUrl: repository.homepageUrl,
documentationTarget: result.canonicalDocumentationTarget(),
currentDocumentationTarget: result.currentDocumentationTarget(),
weightedKeywords: weightedKeywords,
defaultBranchReference: result.defaultBranchVersion.reference,
releaseReference: result.releaseVersion?.reference,
Expand Down
12 changes: 12 additions & 0 deletions Sources/App/Controllers/PackageController+PackageResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,16 @@ extension PackageController.PackageResult {
preReleaseVersion?.model
].canonicalDocumentationTarget()
}

func currentDocumentationTarget() -> DocumentationTarget? {
guard let target = canonicalDocumentationTarget()
else { return nil }

switch target {
case .external:
return target
case .internal(_, let archive):
return .internal(docVersion: .current(referencing: nil), archive: archive)
}
}
}
2 changes: 1 addition & 1 deletion Sources/App/Views/PackageController/PackageShow+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ extension PackageShow {
)
)
},
.unwrap(model.documentationTarget) { target in
.unwrap(model.currentDocumentationTarget) { target in
.li(
.a(
.href(SiteURL.relativeURL(owner: model.repositoryOwner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class API_PackageController_GetRoute_ModelTests: SnapshotTestCase {
weightedKeywords: []))

// validate
XCTAssertEqual(model.documentationTarget, .internal(docVersion: .reference("main"), archive: "archive1"))
XCTAssertEqual(model.currentDocumentationTarget, .internal(docVersion: .reference("main"), archive: "archive1"))
}

func test_init_external_documentation() async throws {
Expand All @@ -111,7 +111,7 @@ class API_PackageController_GetRoute_ModelTests: SnapshotTestCase {
weightedKeywords: []))

// validate
XCTAssertEqual(model.documentationTarget, .external(url: "https://example.com/package/documentation"))
XCTAssertEqual(model.currentDocumentationTarget, .external(url: "https://example.com/package/documentation"))
}

func test_gitHubOwnerUrl() throws {
Expand Down
84 changes: 84 additions & 0 deletions Tests/AppTests/PackageResultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,88 @@ class PackageResultTests: AppTestCase {
XCTAssertEqual(res.canonicalDocumentationTarget(), nil)
}
}

func test_currentDocumentationTarget() async throws {
do {
// Test package with branch docs and stable version docs
let pkg = try savePackage(on: app.db, "1".url)
try await Repository(package: pkg,
defaultBranch: "main",
forks: 42,
license: .mit,
name: "bar1",
owner: "foo",
stars: 17,
summary: "summary").save(on: app.db)
try await App.Version(package: pkg,
docArchives: [.init(name: "archive1", title: "Archive 1")],
latest: .defaultBranch,
reference: .branch("main")).save(on: app.db)
try await App.Version(package: pkg,
// Note the name change on the default branch. The current should point to this archive.
docArchives: [.init(name: "archive2", title: "Archive 2")],
latest: .release,
reference: .tag(1, 2, 3)).save(on: app.db)
}

do {
// Test package with only branch docs hosted externally.
let pkg = try savePackage(on: app.db, "2".url)
try await Repository(package: pkg,
defaultBranch: "main",
forks: 42,
license: .mit,
name: "bar2",
owner: "foo",
stars: 17,
summary: "summary").save(on: app.db)
try await App.Version(package: pkg,
latest: .defaultBranch,
reference: .branch("main"),
spiManifest: .init(externalLinks: .init(documentation: "https://example.com"))).save(on: app.db)
}

do {
// Test package with no documentation
let pkg = try savePackage(on: app.db, "3".url)
try await Repository(package: pkg,
defaultBranch: "main",
forks: 42,
license: .mit,
name: "bar3",
owner: "foo",
stars: 17,
summary: "summary").save(on: app.db)
try await App.Version(package: pkg,
latest: .defaultBranch,
reference: .branch("main")).save(on: app.db)
}

do {
// Testing for internally hosted documentation pointing at the "current".
let res = try await PackageController.PackageResult.query(on: app.db, owner: "foo", repository: "bar1")
let currentTarget = try XCTUnwrap(res.currentDocumentationTarget())

// Validaton
XCTAssertEqual(currentTarget, .internal(docVersion: .current(referencing: nil), archive: "archive2"))
}

do {
// Testing for `.external` case pass-through.
let res = try await PackageController.PackageResult.query(on: app.db, owner: "foo", repository: "bar2")
let currentTarget = try XCTUnwrap(res.currentDocumentationTarget())

// Validaton
XCTAssertEqual(currentTarget, .external(url: "https://example.com"))
}

do {
// Testing for "no documentation" pass-through.
let res = try await PackageController.PackageResult.query(on: app.db, owner: "foo", repository: "bar3")

// Validaton
XCTAssertNil(res.currentDocumentationTarget())
}
}

}
2 changes: 1 addition & 1 deletion Tests/AppTests/WebpageSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class WebpageSnapshotTests: SnapshotTestCase {

func test_PackageShowView_with_documentation_link() throws {
var model = API.PackageController.GetRoute.Model.mock
model.documentationTarget = .internal(docVersion: .reference("main"), archive: "archive")
model.currentDocumentationTarget = .internal(docVersion: .reference("main"), archive: "archive")
let page = { PackageShow.View(path: "", model: model, packageSchema: .mock).document() }

assertSnapshot(matching: page, as: .html)
Expand Down

0 comments on commit ba08aa1

Please sign in to comment.