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 pull request support #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ report/
Carthage/
Build/
Licenses.plist
fastlane/test-output/
fastlane/README.md
fastlane/report.xml

2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ after_success:
- if [ "$FASTLANE_LANE" == "code_coverage" ]; then
make post_coverage;
fi
notifications:
email: false
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,16 @@ TrashCanKit().repository("nerdishbynature", name: "octokit.swift") { response in
}
}
```

### Get pull requests for a repository

```swift
TrashCanKit().pullRequests("nerdishbynature", repoSlug: "octokit.swift") { response in
switch response {
case .Success(let pullRequests, _):
// do something
case .Failure(let error):
// handle any errors
}
}
```
82 changes: 58 additions & 24 deletions TrashCanKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions TrashCanKit/PullRequests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Foundation
import RequestKit

@objc public class PullRequest: NSObject {
public var id: String
public var pullRequestDescription: String?
public var title: String?
public var state: String?
public var author: User?

public init(json: [String: AnyObject]) {
if let id = json["id"] {
self.id = String(id)
title = json["title"] as? String
pullRequestDescription = json["description"] as? String
state = json["state"] as? String
author = User(json["author"] as? [String: AnyObject] ?? [:])
} else {
id = "-1"
}
}
}

// MARK: request

public extension TrashCanKit {
public func pullRequests(owner: String, repoSlug: String, nextParameters: [String: String] = [:], completion: (response: PaginatedResponse<[PullRequest]>) -> Void) {
let router = PullRequestRouter.ReadPullRequests(configuration, owner, repoSlug, nextParameters)
router.loadJSON([String: AnyObject].self) { json, error in
if let error = error {
completion(response: PaginatedResponse.Failure(error))
}

if let json = json, values = json["values"] as? [[String: AnyObject]] {
let repos = values.map { PullRequest(json: $0) }
if let nextURL = json["next"] as? String, parameterString = nextURL.componentsSeparatedByString("?").last {
completion(response: PaginatedResponse.Success(values: repos, nextParameters: parameterString.tkk_queryParameters))
} else {
completion(response: PaginatedResponse.Success(values: repos, nextParameters: [String: String]()))
}
}
}
}
}

// MARK: Router

public enum PullRequestRouter: Router {
case ReadPullRequests(Configuration, String, String, [String: String])

public var configuration: Configuration {
switch self {
case .ReadPullRequests(let config, _, _, _): return config
}
}

public var method: HTTPMethod {
return .GET
}

public var encoding: HTTPEncoding {
return .URL
}

public var params: [String: String] {
switch self {
case .ReadPullRequests(_, _, _, let nextParameters):
return nextParameters
}
}

public var path: String {
switch self {
case .ReadPullRequests(_, let owner, let repoSlug, _):
return "/repositories/\(owner)/\(repoSlug)/pullrequests"
}
}
}
11 changes: 6 additions & 5 deletions TrashCanKit/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import Foundation
import RequestKit

@objc public class User: NSObject {
public let id: String
public var login: String?
public let id: String?
public var login: String
public var name: String?

public init(_ json: [String: AnyObject]) {
if let id = json["uuid"] as? String {
self.id = id
login = json["username"] as? String
if let username = json["username"] as? String {
id = json["uuid"] as? String
login = username
name = json["display_name"] as? String
} else {
id = "-1"
login = ""
}
}
}
Expand Down
File renamed without changes.
106 changes: 106 additions & 0 deletions TrashCanKitTests/Fixtures/PullRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"description": "![img](https://cloudup.com/c7ZJtChLw9J+)\r\n\r\n## Removing:\r\n\r\n* Notifications\r\n* Email\r\n* Change password\r\n* Sessions\r\n\r\n## Renaming: \r\n\r\n* Change username\r\n* Delete account (rename to delete team)\r\n\r\n",
"links": {
"decline": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/decline"
},
"commits": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/commits"
},
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767"
},
"comments": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/comments"
},
"patch": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/patch"
},
"merge": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/merge"
},
"html": {
"href": "https://api.bitbucket.org/bitbucket/bitbucket/pull-request/3767"
},
"activity": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/activity"
},
"diff": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/diff"
},
"approve": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/approve"
}
},
"author": {
"username": "mfrauenholtz",
"display_name": "Michael Frauenholtz",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/mfrauenholtz"
},
"avatar": {
"href": "https://bitbucket-staging-assetroot.s3.amazonaws.com/c/photos/2013/Aug/24/mfrauenholtz-avatar-1858533797-5_avatar.png"
}
}
},
"close_source_branch": true,
"title": "BB-9500: Remove certain admin links for Team accounts",
"destination": {
"commit": {
"hash": "e04099ba977c",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/commit/e04099ba977c"
}
}
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket"
},
"avatar": {
"href": "https://bitbucket-staging-assetroot.s3.amazonaws.com/c/photos/2013/Sep/27/bitbucket-logo-1832464563-7_avatar.png"
}
},
"full_name": "bitbucket/bitbucket",
"name": "bitbucket"
},
"branch": {
"name": "staging"
}
},
"reason": "",
"closed_by": null,
"source": {
"commit": {
"hash": "2a81a1edc0c2",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/commit/2a81a1edc0c2"
}
}
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket"
},
"avatar": {
"href": "https://bitbucket-staging-assetroot.s3.amazonaws.com/c/photos/2013/Sep/27/bitbucket-logo-1832464563-7_avatar.png"
}
},
"full_name": "bitbucket/bitbucket",
"name": "bitbucket"
},
"branch": {
"name": "mfrauenholtz/team-removal/admin-links"
}
},
"state": "OPEN",
"created_on": "2013-11-05T23:59:26.480984+00:00",
"updated_on": "2013-11-07T00:17:41.061613+00:00",
"merge_commit": null,
"id": 3767
}
114 changes: 114 additions & 0 deletions TrashCanKitTests/Fixtures/PullRequests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"pagelen": 1,
"next": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests?pagelen=1&page=2",
"values": [
{
"description": "![img](https://cloudup.com/c7ZJtChLw9J+)\r\n\r\n## Removing:\r\n\r\n* Notifications\r\n* Email\r\n* Change password\r\n* Sessions\r\n\r\n## Renaming: \r\n\r\n* Change username\r\n* Delete account (rename to delete team)\r\n\r\n",
"links": {
"decline": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/decline"
},
"commits": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/commits"
},
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767"
},
"comments": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/comments"
},
"patch": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/patch"
},
"merge": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/merge"
},
"html": {
"href": "https://api.bitbucket.org/bitbucket/bitbucket/pull-request/3767"
},
"activity": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/activity"
},
"diff": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/diff"
},
"approve": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/pullrequests/3767/approve"
}
},
"author": {
"username": "mfrauenholtz",
"display_name": "Michael Frauenholtz",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/mfrauenholtz"
},
"avatar": {
"href": "https://bitbucket-staging-assetroot.s3.amazonaws.com/c/photos/2013/Aug/24/mfrauenholtz-avatar-1858533797-5_avatar.png"
}
}
},
"close_source_branch": true,
"title": "BB-9500: Remove certain admin links for Team accounts",
"destination": {
"commit": {
"hash": "e04099ba977c",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/commit/e04099ba977c"
}
}
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket"
},
"avatar": {
"href": "https://bitbucket-staging-assetroot.s3.amazonaws.com/c/photos/2013/Sep/27/bitbucket-logo-1832464563-7_avatar.png"
}
},
"full_name": "bitbucket/bitbucket",
"name": "bitbucket"
},
"branch": {
"name": "staging"
}
},
"reason": "",
"closed_by": null,
"source": {
"commit": {
"hash": "2a81a1edc0c2",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket/commit/2a81a1edc0c2"
}
}
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/bitbucket"
},
"avatar": {
"href": "https://bitbucket-staging-assetroot.s3.amazonaws.com/c/photos/2013/Sep/27/bitbucket-logo-1832464563-7_avatar.png"
}
},
"full_name": "bitbucket/bitbucket",
"name": "bitbucket"
},
"branch": {
"name": "mfrauenholtz/team-removal/admin-links"
}
},
"state": "OPEN",
"created_on": "2013-11-05T23:59:26.480984+00:00",
"updated_on": "2013-11-07T00:17:41.061613+00:00",
"merge_commit": null,
"id": 3767
}
],
"page": 1,
"size": 12
}
Loading