From 939e197031b88cb195421526fe148ce910a69d0e Mon Sep 17 00:00:00 2001 From: Luiz Rodrigo Martins Barbosa Date: Tue, 23 Nov 2021 11:09:42 +0100 Subject: [PATCH] Fix Git Auth request --- Sources/Models/GitHub.swift | 40 ++++++------ .../BridgeTests.swift | 2 + .../GithubTests.swift | 62 +++++++++++++++++++ 3 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 Tests/SwiftPackageAcknowledgementTests/GithubTests.swift diff --git a/Sources/Models/GitHub.swift b/Sources/Models/GitHub.swift index a990c57..1e35b05 100644 --- a/Sources/Models/GitHub.swift +++ b/Sources/Models/GitHub.swift @@ -8,29 +8,29 @@ import Helper public typealias GitHubRepository = (owner: String, name: String) public struct GitHubLicense: Decodable { - let name: String - let path: String - let sha: String - let size: Int - let url: URL? - let htmlUrl: URL? - let gitUrl: URL? - let downloadUrl: URL - let type: String? - let content: String? - let encoding: String? - let license: LicenseDetails + public let name: String + public let path: String + public let sha: String + public let size: Int + public let url: URL? + public let htmlUrl: URL? + public let gitUrl: URL? + public let downloadUrl: URL + public let type: String? + public let content: String? + public let encoding: String? + public let license: LicenseDetails - var licenseName: String { + public var licenseName: String { license.name } - struct LicenseDetails: Decodable { - let key: String - let name: String - let spdx_id: String? - let url: URL? - let node_id: String? + public struct LicenseDetails: Decodable { + public let key: String + public let name: String + public let spdxId: String? + public let url: URL? + public let nodeId: String? } } @@ -70,7 +70,7 @@ public func githubLicensingAPI( var request = URLRequest(url: url) zip(githubClientID, githubClientSecret) - .flatMap { "\($0):\($0)".data(using: .utf8) } + .flatMap { (clientId, clientSecret) in "\(clientId):\(clientSecret)".data(using: .utf8) } .map { "Basic \($0.base64EncodedString())" } .analysis(ifSome: { request.addValue($0, forHTTPHeaderField: "Authorization") }, ifNone: { }) diff --git a/Tests/SwiftPackageAcknowledgementTests/BridgeTests.swift b/Tests/SwiftPackageAcknowledgementTests/BridgeTests.swift index cc6d45e..78f151d 100644 --- a/Tests/SwiftPackageAcknowledgementTests/BridgeTests.swift +++ b/Tests/SwiftPackageAcknowledgementTests/BridgeTests.swift @@ -1,3 +1,5 @@ +// Copyright © 2021 Lautsprecher Teufel GmbH. All rights reserved. + import Models import XCTest diff --git a/Tests/SwiftPackageAcknowledgementTests/GithubTests.swift b/Tests/SwiftPackageAcknowledgementTests/GithubTests.swift new file mode 100644 index 0000000..2dc36bc --- /dev/null +++ b/Tests/SwiftPackageAcknowledgementTests/GithubTests.swift @@ -0,0 +1,62 @@ +// Copyright © 2021 Lautsprecher Teufel GmbH. All rights reserved. + +import Foundation + +import Models +import XCTest + +class GithubTests: XCTestCase { + func test_github_license_decode() throws { + let data = try XCTUnwrap(json.data(using: .utf8)) + let decoder = JSONDecoder() + decoder.keyDecodingStrategy = .convertFromSnakeCase + + let sut = try decoder.decode(GitHubLicense.self, from: data) + XCTAssertEqual(sut.name, "LICENSE") + XCTAssertEqual(sut.path, "LICENSE") + XCTAssertEqual(sut.sha, "261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64") + XCTAssertEqual(sut.size, 11357) + XCTAssertEqual(sut.url, try XCTUnwrap(URL(string: "https://api.github.com/repos/SwiftRex/SwiftRex/contents/LICENSE?ref=develop"))) + XCTAssertEqual(sut.htmlUrl, try XCTUnwrap(URL(string: "https://github.com/SwiftRex/SwiftRex/blob/develop/LICENSE"))) + XCTAssertEqual(sut.gitUrl, try XCTUnwrap(URL(string: "https://api.github.com/repos/SwiftRex/SwiftRex/git/blobs/261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64"))) + XCTAssertEqual(sut.downloadUrl, try XCTUnwrap(URL(string: "https://raw.githubusercontent.com/SwiftRex/SwiftRex/develop/LICENSE"))) + XCTAssertEqual(sut.type, "file") + XCTAssertEqual(sut.content, "GVyIHRoZSBMaWNlbnNlLgo=\n") + XCTAssertEqual(sut.encoding, "base64") + XCTAssertEqual(sut.license.key, "apache-2.0") + XCTAssertEqual(sut.license.name, "Apache License 2.0") + XCTAssertEqual(sut.license.spdxId, "Apache-2.0") + XCTAssertEqual(sut.license.url, try XCTUnwrap(URL(string: "https://api.github.com/licenses/apache-2.0"))) + XCTAssertEqual(sut.license.nodeId, "MDc6TGljZW5zZTI=") + } +} + + +let json = +""" +{ + "name": "LICENSE", + "path": "LICENSE", + "sha": "261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64", + "size": 11357, + "url": "https://api.github.com/repos/SwiftRex/SwiftRex/contents/LICENSE?ref=develop", + "html_url": "https://github.com/SwiftRex/SwiftRex/blob/develop/LICENSE", + "git_url": "https://api.github.com/repos/SwiftRex/SwiftRex/git/blobs/261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64", + "download_url": "https://raw.githubusercontent.com/SwiftRex/SwiftRex/develop/LICENSE", + "type": "file", + "content": "GVyIHRoZSBMaWNlbnNlLgo=\\n", + "encoding": "base64", + "_links": { + "self": "https://api.github.com/repos/SwiftRex/SwiftRex/contents/LICENSE?ref=develop", + "git": "https://api.github.com/repos/SwiftRex/SwiftRex/git/blobs/261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64", + "html": "https://github.com/SwiftRex/SwiftRex/blob/develop/LICENSE" + }, + "license": { + "key": "apache-2.0", + "name": "Apache License 2.0", + "spdx_id": "Apache-2.0", + "url": "https://api.github.com/licenses/apache-2.0", + "node_id": "MDc6TGljZW5zZTI=" + } +} +"""