-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from luizmb/master
Fix SSH repos and repos with no LICENSE
- Loading branch information
Showing
5 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,22 +34,32 @@ public struct GitHubLicense: Decodable { | |
} | ||
} | ||
|
||
func githubRepository(from url: URL) -> Result<GitHubRepository, GeneratePlistError> { | ||
let gitDomain = "github.com" | ||
let gitSuffix = ".git" | ||
extension String { | ||
func replacingOccurrences(of strings: [String], with newString: String) -> String { | ||
strings.reduce(self) { partialResult, itemToReplace in | ||
partialResult.replacingOccurrences(of: itemToReplace, with: newString) | ||
} | ||
} | ||
} | ||
|
||
guard let host = url.host, | ||
host.contains(gitDomain), | ||
url.pathComponents.count >= 2, | ||
let lastPathComponent = url.pathComponents.last | ||
func githubRepository(from url: URL) -> Result<GitHubRepository, GeneratePlistError> { | ||
let cleanupList = [ | ||
"[email protected]:", | ||
"https://github.com/", | ||
"https://www.github.com/", | ||
"http://github.com/", | ||
"http://www.github.com/", | ||
".git" | ||
] | ||
|
||
let parts = url.absoluteString | ||
.replacingOccurrences(of: cleanupList, with: "") | ||
.split(separator: "/") | ||
|
||
guard | ||
let owner = parts[safe: 0].map(String.init), | ||
let name = parts[safe: 1].map(String.init) | ||
else { return .failure(.unknownRepository) } | ||
|
||
guard let owner = url.pathComponents[safe: 1], !owner.isEmpty else { | ||
return .failure(.invalidLicenseMetadataURL) | ||
} | ||
let name = lastPathComponent.contains(gitSuffix) | ||
? String(lastPathComponent.dropLast(gitSuffix.count)) | ||
: String(lastPathComponent.replacingOccurrences(of: gitSuffix, with: "")) | ||
|
||
return .success( | ||
GitHubRepository( | ||
|
@@ -80,10 +90,22 @@ public func githubLicensingAPI( | |
requester(request) | ||
.mapError(GeneratePlistError.githubAPIURLError) | ||
.flatMapResult { data, response -> Result<Data, GeneratePlistError> in | ||
guard let httpResponse = response as? HTTPURLResponse, | ||
200..<300 ~= httpResponse.statusCode else { | ||
return .failure(.githubAPIInvalidResponse(response)) | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
return .failure(.githubAPIInvalidResponse(response)) | ||
} | ||
|
||
guard httpResponse.statusCode != 404 else { | ||
return .failure(.githubLicenseNotFound) | ||
} | ||
|
||
guard httpResponse.statusCode != 403 else { | ||
return .failure(.githubAPIBudgetExceeded) | ||
} | ||
|
||
guard 200..<300 ~= httpResponse.statusCode else { | ||
return .failure(.githubAPIInvalidResponse(response)) | ||
} | ||
|
||
return .success(data) | ||
} | ||
.flatMapResult { data in | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,21 @@ class BridgeTests: XCTestCase { | |
package: "Commander", | ||
repositoryURL: URL(string: "https://github.com/kylef/Commander")!, | ||
state: .init() | ||
), | ||
.init( | ||
package: "SwiftPackageAcknowledgement", | ||
repositoryURL: URL(string: "http://github.com/teufelaudio/SwiftPackageAcknowledgement")!, | ||
state: .init() | ||
), | ||
.init( | ||
package: "FoundationExtensions", | ||
repositoryURL: URL(string: "https://www.github.com/teufelaudio/FoundationExtensions")!, | ||
state: .init() | ||
), | ||
.init( | ||
package: "NetworkExtensions", | ||
repositoryURL: URL(string: "[email protected]:teufelaudio/NetworkExtensions.git")!, | ||
state: .init() | ||
) | ||
] | ||
let content = ResolvedPackageContent( | ||
|
@@ -28,12 +43,26 @@ class BridgeTests: XCTestCase { | |
|
||
// assert | ||
let repositories = packageRepositories.map(\.repository) | ||
XCTAssertEqual(repositories.count, 5) | ||
|
||
let acknowListRepo = repositories[0] | ||
XCTAssertEqual(acknowListRepo.name, "AcknowList") | ||
XCTAssertEqual(acknowListRepo.owner, "vtourraine") | ||
|
||
let commanderRepo = repositories[1] | ||
XCTAssertEqual(commanderRepo.name, "Commander") | ||
XCTAssertEqual(commanderRepo.owner, "kylef") | ||
|
||
let spmAckRepo = repositories[2] | ||
XCTAssertEqual(spmAckRepo.name, "SwiftPackageAcknowledgement") | ||
XCTAssertEqual(spmAckRepo.owner, "teufelaudio") | ||
|
||
let foundationExtRepo = repositories[3] | ||
XCTAssertEqual(foundationExtRepo.name, "FoundationExtensions") | ||
XCTAssertEqual(foundationExtRepo.owner, "teufelaudio") | ||
|
||
let networkExtRepo = repositories[4] | ||
XCTAssertEqual(networkExtRepo.name, "NetworkExtensions") | ||
XCTAssertEqual(networkExtRepo.owner, "teufelaudio") | ||
} | ||
} |