Skip to content

Commit

Permalink
Merge pull request #2717 from SwiftPackageIndex/move-funding-type
Browse files Browse the repository at this point in the history
Move funding type to top level
  • Loading branch information
daveverwer authored Nov 15, 2023
2 parents e223c50 + b7bd083 commit 8ba8c11
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 54 deletions.
5 changes: 2 additions & 3 deletions Sources/App/Commands/Ingest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func updateRepository(on database: Database,

repository.defaultBranch = repoMetadata.defaultBranch
repository.forks = repoMetadata.forkCount
repository.fundingLinks = repoMetadata.fundingLinks ?? []
repository.fundingLinks = repoMetadata.fundingLinks?.map(FundingLink.init(from:)) ?? []
repository.homepageUrl = repoMetadata.homepageUrl?.trimmed
repository.isArchived = repoMetadata.isArchived
repository.isInOrganization = repoMetadata.isInOrganization
Expand All @@ -217,8 +217,7 @@ func updateRepository(on database: Database,
repository.ownerAvatarUrl = repoMetadata.owner.avatarUrl
repository.s3Readme = s3Readme
repository.readmeHtmlUrl = readmeInfo?.htmlUrl
repository.releases = metadata.repository?.releases.nodes
.map(Release.init(from:)) ?? []
repository.releases = repoMetadata.releases.nodes.map(Release.init(from:))
repository.stars = repoMetadata.stargazerCount
repository.summary = repoMetadata.description

Expand Down
68 changes: 20 additions & 48 deletions Sources/App/Core/Github.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,53 +249,6 @@ extension Github {
var htmlUrl: String
}

struct FundingLink: Codable, Equatable {

enum Platform: String, Codable {
case communityBridge
case customUrl
case gitHub
case issueHunt
case koFi
case lfxCrowdfunding
case liberaPay
case openCollective
case otechie
case patreon
case tideLift

private static let gitHubApiEncodings: [String: Platform] = [
"COMMUNITY_BRIDGE": .communityBridge,
"CUSTOM": .customUrl,
"GITHUB": .gitHub,
"ISSUEHUNT": .issueHunt,
"KO_FI": .koFi,
"LFX_CROWDFUNDING": .lfxCrowdfunding,
"LIBERAPAY": .liberaPay,
"OPEN_COLLECTIVE": .openCollective,
"OTECHIE": .otechie,
"PATREON": .patreon,
"TIDELIFT": .tideLift
]

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let stringValue = try container.decode(String.self)

if let value = Platform(rawValue: stringValue) {
self = value
} else if let value = Platform.gitHubApiEncodings[stringValue] {
self = value
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to decode \(stringValue)")
}
}
}

var platform: Platform
var url: String
}

struct Metadata: Decodable, Equatable {
static func query(owner: String, repository: String) -> GraphQLQuery {
// Go to https://developer.github.com/v4/explorer/ to run query manually
Expand Down Expand Up @@ -389,7 +342,7 @@ extension Github {
var defaultBranchRef: DefaultBranchRef?
var description: String?
var forkCount: Int
var fundingLinks: [FundingLink]?
var fundingLinks: [FundingLinkNode]?
var homepageUrl: String?
var isArchived: Bool
// periphery:ignore
Expand Down Expand Up @@ -417,6 +370,25 @@ extension Github {
}
}

struct FundingLinkNode: Codable, Equatable {
enum Platform: String, Codable {
case communityBridge = "COMMUNITYBRIDGE"
case customUrl = "CUSTOMURL"
case gitHub = "GITHUB"
case issueHunt = "ISSUEHUNT"
case koFi = "KOFI"
case lfxCrowdfunding = "LFXCROWDFUNDING"
case liberaPay = "LIBERAPAY"
case openCollective = "OPENCOLLECTIVE"
case otechie = "OTECHIE"
case patreon = "PATREON"
case tideLift = "TIDELIFT"
}

var platform: Platform
var url: String
}

struct IssueNodes: Decodable, Equatable {
var nodes: [IssueNode]

Expand Down
73 changes: 73 additions & 0 deletions Sources/App/Models/FundingLink.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation


struct FundingLink: Codable, Equatable {
enum Platform: String, Codable {
case communityBridge
case customUrl
case gitHub
case issueHunt
case koFi
case lfxCrowdfunding
case liberaPay
case openCollective
case otechie
case patreon
case tideLift
}

var platform: Platform
var url: String
}


extension FundingLink {
init(from node: Github.Metadata.FundingLinkNode) {
platform = .init(from: node.platform)
url = node.url
}
}


extension FundingLink.Platform {
init(from platform: Github.Metadata.FundingLinkNode.Platform) {
switch platform {
case .communityBridge:
self = .communityBridge
case .customUrl:
self = .customUrl
case .gitHub:
self = .gitHub
case .issueHunt:
self = .issueHunt
case .koFi:
self = .koFi
case .lfxCrowdfunding:
self = .lfxCrowdfunding
case .liberaPay:
self = .liberaPay
case .openCollective:
self = .openCollective
case .otechie:
self = .otechie
case .patreon:
self = .patreon
case .tideLift:
self = .tideLift
}
}
}
4 changes: 2 additions & 2 deletions Sources/App/Models/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class Repository: Model, Content {
var forks: Int

@Field(key: "funding_links")
var fundingLinks: [Github.FundingLink]
var fundingLinks: [FundingLink]

@Field(key: "homepage_url")
var homepageUrl: String?
Expand Down Expand Up @@ -134,7 +134,7 @@ final class Repository: Model, Content {
defaultBranch: String? = nil,
firstCommitDate: Date? = nil,
forks: Int = 0,
fundingLinks: [Github.FundingLink] = [],
fundingLinks: [FundingLink] = [],
forkedFrom: Repository? = nil,
homepageUrl: String? = nil,
isArchived: Bool = false,
Expand Down
10 changes: 10 additions & 0 deletions Tests/AppTests/Fixtures/github-graphql-resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,16 @@
},
"description": "Elegant HTTP Networking in Swift",
"forkCount": 6727,
"fundingLinks": [
{
"platform": "GITHUB",
"url": "https://github.com/Alamofire"
},
{
"platform": "GITHUB",
"url": "https://github.com/jshier"
}
],
"isArchived": false,
"isFork": false,
"licenseInfo": {
Expand Down
4 changes: 4 additions & 0 deletions Tests/AppTests/GithubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class GithubTests: AppTestCase {
XCTAssertEqual(res.repository?.closedPullRequests.nodes.first!.closedAt,
iso8601.date(from: "2021-05-28T15:50:17Z"))
XCTAssertEqual(res.repository?.forkCount, 6727)
XCTAssertEqual(res.repository?.fundingLinks, [
.init(platform: .gitHub, url: "https://github.com/Alamofire"),
.init(platform: .gitHub, url: "https://github.com/jshier"),
])
XCTAssertEqual(res.repository?.mergedPullRequests.nodes.first!.closedAt,
iso8601.date(from: "2021-06-07T22:47:01Z"))
XCTAssertEqual(res.repository?.name, "Alamofire")
Expand Down
2 changes: 1 addition & 1 deletion Tests/AppTests/Mocks/GithubMetadata+mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extension Github.Metadata {

init(defaultBranch: String,
forks: Int,
fundingLinks: [Github.FundingLink] = [],
fundingLinks: [FundingLinkNode] = [],
homepageUrl: String?,
isInOrganization: Bool,
issuesClosedAtDates: [Date],
Expand Down

0 comments on commit 8ba8c11

Please sign in to comment.