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 ref missing parameter in content endpoint #58

Merged
merged 2 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 16 additions & 9 deletions Sources/Tentacle/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public final class Client {
case publicRepositories

// https://developer.github.com/v3/repos/contents/#get-contents
case content(owner: String, repository: String, path: String)
case content(owner: String, repository: String, path: String, ref: String?)

internal var path: String {
switch self {
Expand All @@ -213,13 +213,19 @@ public final class Client {
return "/orgs/\(organisation)/repos"
case .publicRepositories:
return "/repositories"
case let .content(owner, repository, path):
case let .content(owner, repository, path, _):
return "/repos/\(owner)/\(repository)/contents/\(path)"
}
}

internal var queryItems: [URLQueryItem] {
return []
switch self {
case let .content(_, _, _, ref):
return [ref]
.flatMap { $0 }
.map { URLQueryItem(name: "ref", value: $0) }
default: return []
}
}
}

Expand Down Expand Up @@ -337,12 +343,13 @@ public final class Client {
}

/// Fetch the content for a path in the repository
public func content(atPath path: String, in repository: Repository) -> SignalProducer<(Response, Content), Error> {
return fetchOne(.content(owner: repository.owner, repository: repository.name, path: path))
public func content(atPath path: String, in repository: Repository, at ref: String? = nil) -> SignalProducer<(Response, Content), Error> {
return fetchOne(.content(owner: repository.owner, repository: repository.name, path: path, ref: ref))
}

public func create(file: File, atPath path: String, in repository: Repository) -> SignalProducer<(Response, FileResponse), Error> {
return send(file, to: .content(owner: repository.owner, repository: repository.name, path: path), using: .put)
/// Create a file in a repository
public func create(file: File, atPath path: String, in repository: Repository, in branch: String? = nil) -> SignalProducer<(Response, FileResponse), Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new parameters above should follow the API Design Guidelines: https://swift.org/documentation/api-design-guidelines/

Compensate for weak type information to clarify a parameter’s role.

return send(file, to: .content(owner: repository.owner, repository: repository.name, path: path, ref: branch), using: .put)
}

/// Fetch an endpoint from the API.
Expand Down Expand Up @@ -514,8 +521,8 @@ extension Client.Endpoint: Hashable {
return organisation.hashValue
case .publicRepositories:
return "PublicRepositories".hashValue
case let .content(owner, repository, path):
return "File".hashValue ^ owner.hashValue ^ repository.hashValue ^ path.hashValue
case let .content(owner, repository, path, ref):
return "File".hashValue ^ owner.hashValue ^ repository.hashValue ^ path.hashValue ^ (ref?.hashValue ?? 0)
}
}
}
6 changes: 6 additions & 0 deletions Tentacle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
7A1F20EE1D3E862200F275F8 /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A1F20EC1D3E85BB00F275F8 /* Color.swift */; };
7A1F20F01D3E86D900F275F8 /* ColorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A1F20EF1D3E86D900F275F8 /* ColorTests.swift */; };
7A1F20F11D3E872800F275F8 /* ColorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A1F20EF1D3E86D900F275F8 /* ColorTests.swift */; };
7A22EDBC1E47CB8900524539 /* EndpointTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A22EDBB1E47CB8900524539 /* EndpointTests.swift */; };
7A22EDBD1E47CB8900524539 /* EndpointTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A22EDBB1E47CB8900524539 /* EndpointTests.swift */; };
7A27887A1D4920DA007AC936 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A2788791D4920DA007AC936 /* Comment.swift */; };
7A27887C1D49223B007AC936 /* CommentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A27887B1D49223B007AC936 /* CommentsTests.swift */; };
7A27887D1D49223B007AC936 /* CommentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A27887B1D49223B007AC936 /* CommentsTests.swift */; };
Expand Down Expand Up @@ -216,6 +218,7 @@
7A1A825D1CF3E5B60076E2DD /* PullRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PullRequest.swift; sourceTree = "<group>"; };
7A1F20EC1D3E85BB00F275F8 /* Color.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
7A1F20EF1D3E86D900F275F8 /* ColorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorTests.swift; sourceTree = "<group>"; };
7A22EDBB1E47CB8900524539 /* EndpointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EndpointTests.swift; sourceTree = "<group>"; };
7A2788791D4920DA007AC936 /* Comment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Comment.swift; sourceTree = "<group>"; };
7A27887B1D49223B007AC936 /* CommentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommentsTests.swift; sourceTree = "<group>"; };
7A3CC6361E01BC200025E711 /* repos-mdiep-Tentacle-contents-Carthage-Checkouts-ReactiveSwift.data */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "repos-mdiep-Tentacle-contents-Carthage-Checkouts-ReactiveSwift.data"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -475,6 +478,7 @@
7A1F20EF1D3E86D900F275F8 /* ColorTests.swift */,
7A27887B1D49223B007AC936 /* CommentsTests.swift */,
7A072D8D1DED11C900CFE58F /* ContentTests.swift */,
7A22EDBB1E47CB8900524539 /* EndpointTests.swift */,
BE0F409F1C89098E00E3B11A /* Fixture.swift */,
BE0F40961C8908CB00E3B11A /* Fixtures */,
BEB0765E1C8A019A00ABD373 /* GitHubErrorTests.swift */,
Expand Down Expand Up @@ -854,6 +858,7 @@
BEA86F9E1C9F7E1E0049360B /* RepositoryTests.swift in Sources */,
6190818B1C8A2DB7001BE2F8 /* (null) in Sources */,
619081891C8A2DB7001BE2F8 /* GitHubErrorTests.swift in Sources */,
7A22EDBD1E47CB8900524539 /* EndpointTests.swift in Sources */,
7A072D8F1DED11C900CFE58F /* ContentTests.swift in Sources */,
BECB8AA41CBDDF0F005D70A6 /* UserTests.swift in Sources */,
7ABFB4DE1D515A350067B500 /* RepositoryInfoTests.swift in Sources */,
Expand Down Expand Up @@ -902,6 +907,7 @@
7AAB00FD1CF51FC5005A7319 /* IssuesTests.swift in Sources */,
BE1E036B1C9AD87F001296C2 /* ResponseTests.swift in Sources */,
92F0C2BD1E391F3F004A9889 /* ArgoExtensionsTests.swift in Sources */,
7A22EDBC1E47CB8900524539 /* EndpointTests.swift in Sources */,
BEA86F9D1C9F7E1E0049360B /* RepositoryTests.swift in Sources */,
928DF22D1E0E6A8000E6DE35 /* FileResponseTests.swift in Sources */,
BEB076601C8A019E00ABD373 /* GitHubErrorTests.swift in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions Tests/TentacleTests/EndpointTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// EndpointTests.swift
// Tentacle
//
// Created by Romain Pouclet on 2017-02-05.
// Copyright © 2017 Matt Diephouse. All rights reserved.
//

import XCTest
@testable import Tentacle

class EndpointTests: XCTestCase {

func testEndpointProvidesQueryItemsWhenNeeded() {
let endpoint: Client.Endpoint = .content(owner: "palleas", repository: "romain-pouclet.com", path: "config.yml", ref: "sample-branch")
XCTAssertEqual([URLQueryItem(name: "ref", value: "sample-branch")], endpoint.queryItems)

let endpointWithoutRef: Client.Endpoint = .content(owner: "palleas", repository: "romain-pouclet.com", path: "config.yml", ref: nil)
XCTAssertEqual(0, endpointWithoutRef.queryItems.count)
}

}
2 changes: 1 addition & 1 deletion Tests/TentacleTests/Fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ struct Fixture {
let contentType = Client.APIContentType

var endpoint: Client.Endpoint {
return .content(owner: owner, repository: repository, path: path)
return .content(owner: owner, repository: repository, path: path, ref: nil)
}

init(_ server: Server, owner: String, repository: String, path: String) {
Expand Down