Skip to content

Commit

Permalink
Merge pull request #2588 from SwiftPackageIndex/add-test-target-count…
Browse files Browse the repository at this point in the history
…-to-score
  • Loading branch information
cyndichin authored Sep 20, 2023
2 parents 4083442 + a302253 commit 519e2fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
20 changes: 17 additions & 3 deletions Sources/App/Commands/Analyze.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,26 @@ extension Analyze {
}

let versions = try await updateLatestVersions(on: tx, package: package)

let targets = await fetchTargets(on: tx, package: package)

updateScore(package: package, versions: versions)
updateScore(package: package, versions: versions, targets: targets)

await onNewVersions(client: client, logger: logger, package: package, versions: newVersions)
}
}

/// Fetch targets for a given `Package`
/// - Parameters:
/// - database: `Database` object
/// - package: `Package` object
/// - Returns: targets associated with package
private static func fetchTargets(on database: Database, package: Joined<Package, Repository>) async -> [(String, TargetType)]? {
guard let repo = package.repository, let owner = repo.owner, let repository = repo.name else {
return nil
}
return try? await API.PackageController.Target.query(on: database, owner: owner, repository: repository)
}


static func createCheckoutsDirectory(client: Client,
Expand Down Expand Up @@ -709,8 +723,8 @@ extension Analyze {
/// - Parameters:
/// - package: `Package` input
/// - versions: `[Version]` input
static func updateScore(package: Joined<Package, Repository>, versions: [Version]) {
package.model.score = Score.compute(package: package, versions: versions)
static func updateScore(package: Joined<Package, Repository>, versions: [Version], targets: [(String, TargetType)]? = nil) {
package.model.score = Score.compute(package: package, versions: versions, targets: targets)
}


Expand Down
13 changes: 10 additions & 3 deletions Sources/App/Core/Score.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum Score {
var lastActivityAt: Date?
var hasDocumentation: Bool
var numberOfContributors: Int
var hasTestTargets: Bool
}

static func compute(_ candidate: Input) -> Int {
Expand Down Expand Up @@ -85,11 +86,13 @@ enum Score {
case 5..<20: score += 5
default: score += 10
}

if candidate.hasTestTargets { score += 5 }

return score
}

static func compute(package: Joined<Package, Repository>, versions: [Version]) -> Int {
static func compute(package: Joined<Package, Repository>, versions: [Version], targets: [(String, TargetType)]? = []) -> Int {
guard
let defaultVersion = versions.latest(for: .defaultBranch),
let repo = package.repository
Expand All @@ -105,7 +108,9 @@ enum Score {
guard let authors = repo.authors else { return 0 }
return authors.authors.count + authors.numberOfContributors
}()


let hasTestTargets = !(targets?.filter { $0.1 == App.TargetType.test }.isEmpty ?? false)

return Score.compute(
.init(licenseKind: repo.license.licenseKind,
releaseCount: versions.releases.count,
Expand All @@ -114,7 +119,9 @@ enum Score {
numberOfDependencies: defaultVersion.resolvedDependencies?.count,
lastActivityAt: repo.lastActivityAt,
hasDocumentation: hasDocumentation,
numberOfContributors: numberOfContributors)
numberOfContributors: numberOfContributors,
hasTestTargets: hasTestTargets
)
)
}
}
Expand Down
68 changes: 52 additions & 16 deletions Tests/AppTests/ScoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,80 @@ class ScoreTests: AppTestCase {
isArchived: false,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
20)
XCTAssertEqual(Score.compute(.init(licenseKind: .incompatibleWithAppStore,
releaseCount: 0,
likeCount: 0,
isArchived: false,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
23)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 0,
likeCount: 0,
isArchived: false,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
30)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 10,
likeCount: 0,
isArchived: false,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
40)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 10,
likeCount: 50,
isArchived: false,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
50)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 10,
likeCount: 50,
isArchived: true,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
30)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
likeCount: 20_000,
isArchived: false,
numberOfDependencies: nil,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
87)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
likeCount: 20_000,
isArchived: false,
numberOfDependencies: 4,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
89)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
likeCount: 20_000,
isArchived: false,
numberOfDependencies: 2,
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
92)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -99,7 +108,8 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -400),
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
92)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -108,7 +118,8 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -300),
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
97)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -117,7 +128,8 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -100),
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
102)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -126,7 +138,8 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -10),
hasDocumentation: false,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
107)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -135,7 +148,8 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -10),
hasDocumentation: true,
numberOfContributors: 0)),
numberOfContributors: 0,
hasTestTargets: false)),
122)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -144,7 +158,8 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -10),
hasDocumentation: true,
numberOfContributors: 5)),
numberOfContributors: 5,
hasTestTargets: false)),
127)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
Expand All @@ -153,8 +168,29 @@ class ScoreTests: AppTestCase {
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -10),
hasDocumentation: true,
numberOfContributors: 20)),
numberOfContributors: 20,
hasTestTargets: false)),
132)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
likeCount: 20_000,
isArchived: false,
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -10),
hasDocumentation: false,
numberOfContributors: 0,
hasTestTargets: false)),
107)
XCTAssertEqual(Score.compute(.init(licenseKind: .compatibleWithAppStore,
releaseCount: 20,
likeCount: 20_000,
isArchived: false,
numberOfDependencies: 2,
lastActivityAt: Current.date().adding(days: -10),
hasDocumentation: false,
numberOfContributors: 0,
hasTestTargets: true)),
112)
}

func test_compute_package_versions() async throws {
Expand Down

0 comments on commit 519e2fd

Please sign in to comment.