Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tree endpoint #70

Merged
merged 2 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/Tentacle/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ public final class Client {
// https://developer.github.com/v3/repos/branches/#list-branches
case branches(owner: String, repository: String)

// https://developer.github.com/v3/git/trees/#get-a-tree
case tree(owner: String, repository: String, ref: String, recursive: Bool)

internal var path: String {
switch self {
case let .releaseByTagName(owner, repo, tag):
Expand Down Expand Up @@ -220,13 +223,17 @@ public final class Client {
return "/repos/\(owner)/\(repository)/contents/\(path)"
case let .branches(owner, repository):
return "/repos/\(owner)/\(repository)/branches"
case let .tree(owner, repository, ref, _):
return "repos/\(owner)/\(repository)/git/trees/\(ref)"
}
}

internal var queryItems: [URLQueryItem] {
switch self {
case let .content(_, _, _, ref?):
return [ URLQueryItem(name: "ref", value: ref) ]
case .tree(_, _, _, true):
return [ URLQueryItem(name: "recursive", value: "1") ]
default:
return []
}
Expand Down Expand Up @@ -361,6 +368,11 @@ public final class Client {
return fetchMany(.branches(owner: repository.owner, repository: repository.name), page: page, pageSize: perPage)
}

/// Get trees for a repository reference
public func trees(in repository: Repository, atRef ref: String = "HEAD", recursive: Bool = false) -> SignalProducer<(Response, Tree), Error> {
return fetchOne(.tree(owner: repository.owner, repository: repository.name, ref: ref, recursive: recursive))
}

/// Fetch an endpoint from the API.
private func fetch(_ endpoint: Endpoint, page: UInt?, pageSize: UInt?) -> SignalProducer<(Response, Any), Error> {
let url = URL(server, endpoint, page: page, pageSize: pageSize)
Expand Down Expand Up @@ -534,6 +546,8 @@ extension Client.Endpoint: Hashable {
return "File".hashValue ^ owner.hashValue ^ repository.hashValue ^ path.hashValue ^ (ref?.hashValue ?? 0)
case let .branches(owner, repository):
return "Branches".hashValue ^ owner.hashValue ^ repository.hashValue
case let .tree(owner, repository, ref, recursive):
return "Tree".hashValue ^ owner.hashValue ^ repository.hashValue ^ ref.hashValue ^ recursive.hashValue
}
}
}
140 changes: 140 additions & 0 deletions Sources/Tentacle/Tree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// Tree.swift
// Tentacle
//
// Created by David Caunt on 21/04/2017.
// Copyright © 2017 Matt Diephouse. All rights reserved.
//

import Foundation
import Argo
import Curry
import Runes

public struct Tree: CustomStringConvertible {

/// The SHA of the entry.
public let sha: SHA

/// The URL for the tree.
public let url: URL

/// The entries under this tree.
public let entries: [Entry]

/// Whether the number of entries in this tree exceeded the maximum number
/// which will be returned by the API.
public let isTruncated: Bool

public var description: String {
return "\(url)"
}

public struct Entry {

public enum EntryType {
case blob(url: URL, size: Int)
case tree(url: URL)
case commit
}

public enum Mode: String {
case file = "100644"
case executable = "100755"
case subdirectory = "040000"
case submodule = "160000"
case symlink = "120000"
}

/// The type of the entry.
public let type: EntryType

/// The SHA of the entry.
public let sha: SHA

/// The repository-relative path of the entry.
public let path: String

/// The mode of the entry.
public let mode: Mode
}
}

extension Tree: Hashable {
public static func ==(lhs: Tree, rhs: Tree) -> Bool {
return lhs.sha == rhs.sha
&& lhs.url == rhs.url
&& lhs.entries == rhs.entries
&& lhs.isTruncated == rhs.isTruncated
}

public var hashValue: Int {
return sha.hashValue
}
}

extension Tree: ResourceType {
public static func decode(_ j: JSON) -> Decoded<Tree> {
return curry(self.init)
<^> (j <| "sha" >>- toSHA)
<*> (j <| "url" >>- toURL)
<*> j <|| "tree"
<*> j <| "truncated"
}
}

extension Tree.Entry: ResourceType {
public static func decode(_ j: JSON) -> Decoded<Tree.Entry> {
return curry(self.init)
<^> Tree.Entry.EntryType.decode(j)
<*> (j <| "sha" >>- toSHA)
<*> j <| "path"
<*> j <| "mode"
}
}

extension Tree.Entry: Hashable {
public static func ==(lhs: Tree.Entry, rhs: Tree.Entry) -> Bool {
return lhs.sha == rhs.sha
}

public var hashValue: Int {
return sha.hashValue
}
}

func decodeBlob(_ j: JSON) -> Decoded<Tree.Entry.EntryType> {
return curry(Tree.Entry.EntryType.blob)
<^> j <| "url"
<*> j <| "size"
}

func decodeTree(_ j: JSON) -> Decoded<Tree.Entry.EntryType> {
return curry(Tree.Entry.EntryType.tree)
<^> j <| "url"
}

extension Tree.Entry.EntryType: Decodable {
public static func decode(_ json: JSON) -> Decoded<Tree.Entry.EntryType> {
guard case let .object(payload) = json else {
return .failure(.typeMismatch(expected: "object", actual: "\(json)"))
}

guard let type = payload["type"], case let .string(value) = type else {
return .failure(.custom("Content type is invalid"))
}

switch value {
case "blob":
return decodeBlob(json)
case "commit":
return .success(Tree.Entry.EntryType.commit)
case "tree":
return decodeTree(json)
default:
return .failure(.custom("Content type \(value) is invalid"))
}
}
}

extension Tree.Entry.Mode: Decodable {}
24 changes: 24 additions & 0 deletions Tentacle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@
BEEE47461C91BB3A000FFC21 /* ArgoExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEEE47441C91BB3A000FFC21 /* ArgoExtensions.swift */; };
BEEE474E1C92623E000FFC21 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEEE474D1C92623E000FFC21 /* Response.swift */; };
BEEE474F1C92623E000FFC21 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = BEEE474D1C92623E000FFC21 /* Response.swift */; };
BF01A25D1EAA5BEC0028ECFC /* Tree.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF01A25C1EAA5BEB0028ECFC /* Tree.swift */; };
BF01A25E1EAA5BEC0028ECFC /* Tree.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF01A25C1EAA5BEB0028ECFC /* Tree.swift */; };
BF01A2611EAA63100028ECFC /* TreeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF01A2601EAA63100028ECFC /* TreeTests.swift */; };
BF01A2621EAA63100028ECFC /* TreeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF01A2601EAA63100028ECFC /* TreeTests.swift */; };
BF5691C41EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data in Resources */ = {isa = PBXBuildFile; fileRef = BF5691C21EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data */; };
BF5691C51EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data in Resources */ = {isa = PBXBuildFile; fileRef = BF5691C21EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data */; };
BF5691C61EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response in Resources */ = {isa = PBXBuildFile; fileRef = BF5691C31EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response */; };
BF5691C71EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response in Resources */ = {isa = PBXBuildFile; fileRef = BF5691C31EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response */; };
CD1FE64F1DCF36070016639E /* HTTPStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD1FE64D1DCF35F60016639E /* HTTPStub.swift */; };
CD1FE6501DCF36070016639E /* HTTPStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD1FE64D1DCF35F60016639E /* HTTPStub.swift */; };
CD83C7BA1DCE379D00BC2608 /* Availability.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD83C7B91DCE379D00BC2608 /* Availability.swift */; };
Expand Down Expand Up @@ -322,6 +330,10 @@
BEEE47411C91B8DF000FFC21 /* ResourceType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResourceType.swift; sourceTree = "<group>"; };
BEEE47441C91BB3A000FFC21 /* ArgoExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgoExtensions.swift; sourceTree = "<group>"; };
BEEE474D1C92623E000FFC21 /* Response.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = "<group>"; };
BF01A25C1EAA5BEB0028ECFC /* Tree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tree.swift; sourceTree = "<group>"; };
BF01A2601EAA63100028ECFC /* TreeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TreeTests.swift; sourceTree = "<group>"; };
BF5691C21EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data"; sourceTree = "<group>"; };
BF5691C31EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = "repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response"; sourceTree = "<group>"; };
CD1FE64D1DCF35F60016639E /* HTTPStub.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPStub.swift; sourceTree = "<group>"; };
CD83C7B91DCE379D00BC2608 /* Availability.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Availability.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -422,6 +434,8 @@
7A3CC6591E01BC200025E711 /* repos-mdiep-NonExistent-releases-tags-tag.response */,
7A3CC65A1E01BC200025E711 /* repos-torvalds-linux-releases-tags-v4.4.data */,
7A3CC65B1E01BC200025E711 /* repos-torvalds-linux-releases-tags-v4.4.response */,
BF5691C21EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data */,
BF5691C31EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response */,
);
path = Fixtures;
sourceTree = "<group>";
Expand Down Expand Up @@ -480,6 +494,7 @@
BE88E82C1C88C94D0034A112 /* Server.swift */,
928DF23D1E1117D600E6DE35 /* Sha.swift */,
BE88E7F51C88C6B30034A112 /* Tentacle.h */,
BF01A25C1EAA5BEB0028ECFC /* Tree.swift */,
BECB8A8A1CBDCD17005D70A6 /* User.swift */,
);
name = Tentacle;
Expand Down Expand Up @@ -510,6 +525,7 @@
92F0C2B91E391088004A9889 /* FileTests.swift */,
92F0C2BC1E391F3F004A9889 /* ArgoExtensionsTests.swift */,
7A8A9D641E5547F50009DA9E /* BranchTests.swift */,
BF01A2601EAA63100028ECFC /* TreeTests.swift */,
);
name = Tests;
path = Tests/TentacleTests;
Expand Down Expand Up @@ -725,6 +741,7 @@
7A3CC68D1E01BC200025E711 /* users-Palleas-Opensource-repos.data in Resources */,
7A3CC69F1E01BC200025E711 /* repos-mdiep-MDPSplitView-releases-assets-433845.response in Resources */,
7A8A9D6A1E5548070009DA9E /* repos-Carthage-ReactiveTask-branches.data in Resources */,
BF5691C71EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response in Resources */,
7A3CC6771E01BC200025E711 /* repos-Carthage-Carthage-releases.page-1-per_page-30.response in Resources */,
7A3CC6751E01BC200025E711 /* repos-Carthage-Carthage-releases.page-1-per_page-30.data in Resources */,
7A3CC66F1E01BC200025E711 /* orgs-raccommunity-repos.response in Resources */,
Expand Down Expand Up @@ -754,6 +771,7 @@
7A8A9D6C1E5548070009DA9E /* repos-Carthage-ReactiveTask-branches.response in Resources */,
7A3CC6911E01BC200025E711 /* users-test.data in Resources */,
7A3CC68F1E01BC200025E711 /* users-Palleas-Opensource-repos.response in Resources */,
BF5691C51EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data in Resources */,
7A3CC69B1E01BC200025E711 /* repos-mdiep-Tentacle-contents-update-test-fixtures.response in Resources */,
7A3CC6851E01BC200025E711 /* repos-Palleas-opensource-Sample-repository-issues.data in Resources */,
7A3CC6831E01BC200025E711 /* repos-Palleas-Opensource-Sample-repository-issues-1-comments.response in Resources */,
Expand All @@ -780,6 +798,7 @@
7A3CC68C1E01BC200025E711 /* users-Palleas-Opensource-repos.data in Resources */,
7A3CC69E1E01BC200025E711 /* repos-mdiep-MDPSplitView-releases-assets-433845.response in Resources */,
7A8A9D691E5548070009DA9E /* repos-Carthage-ReactiveTask-branches.data in Resources */,
BF5691C61EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.response in Resources */,
7A3CC6761E01BC200025E711 /* repos-Carthage-Carthage-releases.page-1-per_page-30.response in Resources */,
7A3CC6741E01BC200025E711 /* repos-Carthage-Carthage-releases.page-1-per_page-30.data in Resources */,
7A3CC66E1E01BC200025E711 /* orgs-raccommunity-repos.response in Resources */,
Expand Down Expand Up @@ -809,6 +828,7 @@
7A8A9D6B1E5548070009DA9E /* repos-Carthage-ReactiveTask-branches.response in Resources */,
7A3CC6901E01BC200025E711 /* users-test.data in Resources */,
7A3CC68E1E01BC200025E711 /* users-Palleas-Opensource-repos.response in Resources */,
BF5691C41EAE6F9600A92D3E /* repos-Palleas-opensource-Sample-repository-git-trees-0c0dfafa361836e11aedcbb95c1f05d3f654aef0.data in Resources */,
7A3CC69A1E01BC200025E711 /* repos-mdiep-Tentacle-contents-update-test-fixtures.response in Resources */,
7A3CC6841E01BC200025E711 /* repos-Palleas-opensource-Sample-repository-issues.data in Resources */,
7A3CC6821E01BC200025E711 /* repos-Palleas-Opensource-Sample-repository-issues-1-comments.response in Resources */,
Expand Down Expand Up @@ -845,6 +865,7 @@
7A27887E1D49223E007AC936 /* Comment.swift in Sources */,
7A1F20EE1D3E862200F275F8 /* Color.swift in Sources */,
7ABFB4D81D51519B0067B500 /* RepositoryInfo.swift in Sources */,
BF01A25E1EAA5BEC0028ECFC /* Tree.swift in Sources */,
BEEE474F1C92623E000FFC21 /* Response.swift in Sources */,
927848011E0C78EA003B9EE6 /* FileResponse.swift in Sources */,
7A2F66121CF5280200463602 /* Label.swift in Sources */,
Expand Down Expand Up @@ -879,6 +900,7 @@
BEA86F9E1C9F7E1E0049360B /* RepositoryTests.swift in Sources */,
6190818B1C8A2DB7001BE2F8 /* (null) in Sources */,
619081891C8A2DB7001BE2F8 /* GitHubErrorTests.swift in Sources */,
BF01A2621EAA63100028ECFC /* TreeTests.swift in Sources */,
7A22EDBD1E47CB8900524539 /* EndpointTests.swift in Sources */,
7A072D8F1DED11C900CFE58F /* ContentTests.swift in Sources */,
BECB8AA41CBDDF0F005D70A6 /* UserTests.swift in Sources */,
Expand All @@ -905,6 +927,7 @@
7A27887A1D4920DA007AC936 /* Comment.swift in Sources */,
7A1F20ED1D3E85BB00F275F8 /* Color.swift in Sources */,
7ABFB4D71D51519B0067B500 /* RepositoryInfo.swift in Sources */,
BF01A25D1EAA5BEC0028ECFC /* Tree.swift in Sources */,
BEEE474E1C92623E000FFC21 /* Response.swift in Sources */,
927848001E0C78DB003B9EE6 /* FileResponse.swift in Sources */,
7A1A82581CF3DE4C0076E2DD /* Label.swift in Sources */,
Expand Down Expand Up @@ -943,6 +966,7 @@
BE2B6A6C1C8B854F0080BFEB /* ClientTests.swift in Sources */,
7A072D8E1DED11C900CFE58F /* ContentTests.swift in Sources */,
7A1F20F01D3E86D900F275F8 /* ColorTests.swift in Sources */,
BF01A2611EAA63100028ECFC /* TreeTests.swift in Sources */,
BEA86F9A1C9F7C230049360B /* ServerTests.swift in Sources */,
BE0F40A21C8909EB00E3B11A /* ReleaseTests.swift in Sources */,
);
Expand Down
31 changes: 30 additions & 1 deletion Tests/TentacleTests/Fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ struct Fixture {
FileForRepository.SubmoduleInTentacle,
FileForRepository.DirectoryInSampleRepository,
FileForRepository.SymlinkInSampleRepository,
BranchesForRepository.BranchesInReactiveTask
BranchesForRepository.BranchesInReactiveTask,
TreeForRepository.TreeInSampleRepository
]

/// Returns the fixture for the given URL, or nil if no such fixture exists.
Expand Down Expand Up @@ -380,4 +381,32 @@ struct Fixture {
}

}

struct TreeForRepository: EndpointFixtureType {
static let TreeInSampleRepository = TreeForRepository(.dotCom, owner: "Palleas-opensource", repository: "Sample-repository",
ref: "0c0dfafa361836e11aedcbb95c1f05d3f654aef0", recursive: false)

let server: Server
let page: UInt? = nil
let pageSize: UInt? = nil

let owner: String
let repository: String
let ref: String
let recursive: Bool

let contentType = Client.APIContentType

var endpoint: Client.Endpoint {
return .tree(owner: owner, repository: repository, ref: ref, recursive: recursive)
}

init(_ server: Server, owner: String, repository: String, ref: String, recursive: Bool) {
self.server = server
self.owner = owner
self.repository = repository
self.ref = ref
self.recursive = recursive
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"tree" : [
{
"path" : "Directory",
"url" : "https:\/\/api.github.com\/repos\/Palleas-opensource\/Sample-repository\/git\/trees\/5bfad2b3f8e483b6b173d8aaff19597e84626f15",
"type" : "tree",
"mode" : "040000",
"sha" : "5bfad2b3f8e483b6b173d8aaff19597e84626f15"
},
{
"path" : "README.markdown",
"size" : 18,
"url" : "https:\/\/api.github.com\/repos\/Palleas-opensource\/Sample-repository\/git\/blobs\/c3eb8708a0a5aaa4f685aab24ef6403fbfd28efc",
"type" : "blob",
"mode" : "100644",
"sha" : "c3eb8708a0a5aaa4f685aab24ef6403fbfd28efc"
},
{
"path" : "Tentacle",
"type" : "commit",
"mode" : "160000",
"sha" : "7a84505a3c553fd8e2879cfa63753b0cd212feb8"
},
{
"path" : "say",
"size" : 12,
"url" : "https:\/\/api.github.com\/repos\/Palleas-opensource\/Sample-repository\/git\/blobs\/1e3f1fd0bc1f65cf4701c217f4d1fd9a3cd50721",
"type" : "blob",
"mode" : "120000",
"sha" : "1e3f1fd0bc1f65cf4701c217f4d1fd9a3cd50721"
}
],
"url" : "https:\/\/api.github.com\/repos\/Palleas-opensource\/Sample-repository\/git\/trees\/0c0dfafa361836e11aedcbb95c1f05d3f654aef0",
"truncated" : false,
"sha" : "0c0dfafa361836e11aedcbb95c1f05d3f654aef0"
}
Binary file not shown.
Loading