Skip to content

Commit

Permalink
improved GitHub repo permission verification as pointed out by #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Honza Dvorsky committed May 1, 2015
1 parent 9639c5c commit 6642a08
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
32 changes: 32 additions & 0 deletions BuildaGitServer/GitHubServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,36 @@ extension GitHubServer {
}
}
}

/**
* GET repo metadata
*/
public func getRepo(repo: String, completion: (repo: Repo?, error: NSError?) -> ()) {

let params = [
"repo": repo
]

self.sendRequestWithMethod(.GET, endpoint: .Repos, params: params, query: nil, body: nil) {
(response, body, error) -> () in

if error != nil {
completion(repo: nil, error: error)
return
}

if let body = body as? NSDictionary {
let repository: Repo = Repo(json: body)
completion(repo: repository, error: nil)
} else {
completion(repo: nil, error: Errors.errorWithInfo("Wrong body \(body)"))
}
}
}

}





6 changes: 4 additions & 2 deletions BuildaGitServer/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
import Foundation

public class Repo : GitHubEntity {

public let name: String
public let fullName: String
public let repoUrlHTTPS: String
public let repoUrlSSH: String
public let permissions: NSDictionary

public required init(json: NSDictionary) {

self.name = json.stringForKey("name")
self.fullName = json.stringForKey("full_name")
self.repoUrlHTTPS = json.stringForKey("clone_url")
self.repoUrlSSH = json.stringForKey("ssh_url")

self.permissions = json.dictionaryForKey("permissions")

super.init(json: json)
}
}
17 changes: 14 additions & 3 deletions Buildasaur/NetworkUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@ class NetworkUtils {
if let repoName = project.githubRepoName() {

//we have a repo name
server.getOpenPullRequests(repoName, completion: { (prs, error) -> () in
server.getRepo(repoName, completion: { (repo, error) -> () in

if error != nil {
completion(success: false, error: error)
return
}

//seems like we got PRs!
completion(success: true, error: nil)
if
let repo = repo,
let readPermission = repo.permissions["pull"] as? Bool,
let writePermission = repo.permissions["push"] as? Bool
{

let hasPermissions = readPermission && writePermission

//look at the permissions in the PR metadata
completion(success: hasPermissions, error: nil)
} else {
completion(success: false, error: nil)
}
})

} else {
Expand Down

0 comments on commit 6642a08

Please sign in to comment.