Octokit.swift

安装

import PackageDescription

let package = Package(
  name: "MyAwesomeApp",
    dependencies: [
      .package(url: "https://github.com/nerdishbynature/octokit.swift", from: "0.11.0"),
    ]
  )

身份验证

Octokit 同时支持 GitHub 和 GitHub 企业版。身份验证通过配置处理。

有两种类型的配置,TokenConfigurationOAuthConfiguration

TokenConfiguration

如果您正在使用基于访问令牌的身份验证(例如,用户在网站上生成了访问令牌并提供给您),或者您通过 OAuth 流程获得了访问令牌,则可以使用 TokenConfiguration

您可以为 github.com 初始化一个新的配置,如下所示

let config = TokenConfiguration("YOUR_PRIVATE_GITHUB_TOKEN_HERE")

或者为 GitHub 企业版

let config = TokenConfiguration("YOUR_PRIVATE_GITHUB_TOKEN_HERE", url: "https://github.example.com/api/v3/")

获取令牌后,您可以将其与 Octokit 一起使用

Octokit(config).me() { response in
  switch response {
  case .success(let user):
    print(user.login as Any)
  case .failure(let error):
    print(error)
  }
}

OAuthConfiguration

如果您还没有访问令牌,并且用户必须登录到您的应用程序,则应使用 OAuthConfiguration。 这也处理 OAuth 流程。

您可以为 github.com 验证用户身份,如下所示

let config = OAuthConfiguration(token: "<Your Client ID>", secret: "<Your Client secret>", scopes: ["repo", "read:org"])
let url = config.authenticate()

或者为 GitHub 企业版

let config = OAuthConfiguration("https://github.example.com/api/v3/", webURL: "https://github.example.com/", token: "<Your Client ID>", secret: "<Your Client secret>", scopes: ["repo", "read:org"])

获取配置后,您可以验证用户身份

// AppDelegate.swift

config.authenticate()

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
  config.handleOpenURL(url) { config in
    self.loadCurrentUser(config) // purely optional of course
  }
  return false
}

func loadCurrentUser(config: TokenConfiguration) {
  Octokit(config).me() { response in
    switch response {
    case .success(let user):
      print(user.login)
    case .failure(let error):
      print(error)
    }
  }
}

请注意,您将从 OAuth 流程中获得 TokenConfiguration 返回。您必须自己存储 accessToken。 如果您想进行进一步的请求,则不必再次执行 OAuth 流程。 您只需使用 TokenConfiguration

let token = // get your token from your keychain, user defaults (not recommended) etc.
let config = TokenConfiguration(token)
Octokit(config).user(name: "octocat") { response in
  switch response {
  case .success(let user):
  	print("User login: \(user.login!)")
  case .failure(let error):
  	print("Error: \(error)")
  }
}

用户

获取单个用户

let username = ... // set the username
Octokit().user(name: username) { response in
  switch response {
    case .success(let user):
      // do something with the user
    case .failure(let error):
      // handle any errors
  }
}

获取已验证的用户

Octokit().me() { response in
  switch response {
    case .success(let user):
      // do something with the user
    case .failure(let error):
      // handle any errors
  }

代码仓库

获取单个代码仓库

let (owner, name) = ("owner", "name") // replace with actual owner and name
Octokit().repository(owner, name) { response in
  switch response {
    case .success(let repository):
      // do something with the repository
    case .failure(let error):
      // handle any errors
  }
}

获取已验证用户的代码仓库

Octokit().repositories() { response in
  switch response {
    case .success(let repository):
      // do something
    case .failure(let error):
      // handle any errors
  }
}

标星代码仓库

获取某个用户的标星代码仓库

let username = "username"
Octokit().stars(username) { response in
  switch response {
    case .success(let repositories):
      // do something with the repositories
    case .failure(let error):
      // handle any errors
  }
}

获取已验证用户的标星代码仓库

Octokit().myStars() { response in
  switch response {
    case .success(let repositories):
      // do something with the repositories
    case .failure(let error):
      // handle any errors
  }
}

关注者和关注

获取某个用户的关注者

let username = "username"
Octokit().followers(username) { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

获取已验证用户的关注者

Octokit().myFollowers() { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

获取某个用户的关注

let username = "username"
Octokit().following(username) { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

获取已验证用户的关注

Octokit().myFollowing() { response in
  switch response {
    case .success(let users):
      // do something with the users
    case .failure(let error):
      // handle any errors
  }
}

议题

获取已验证用户的议题

获取已验证用户的所有可见仓库(包括拥有的仓库、成员仓库和组织仓库)中的所有议题。

Octokit(config).myIssues() { response in
    switch response {
        case .success(let issues):
        // do something with the issues
    case .failure:
        // handle any errors
    }   
}

获取单个议题

let (owner, repo, number) = ("owner", "repo", 1347) // replace with actual owner, repo name, and issue number
Octokit(config).issue(owner, repository: repo, number: number) { response in
    switch response {
    case .success(let issue):
        // do something with the issue
    case .failure:
        // handle any errors
    }
}

创建新的议题

Octokit(config).postIssue("owner", repository: "repo", title: "Found a bug", body: "I'm having a problem with this.", assignee: "octocat", labels: ["bug", "duplicate"]) { response in
    switch response {
    case .success(let issue):
        // do something with the issue
    case .failure:
        // handle any errors
    }
}

编辑现有议题

Octokit(config).patchIssue("owner", repository: "repo", number: 1347, title: "Found a bug", body: "I'm having a problem with this.", assignee: "octocat", state: .Closed) { response in
    switch response {
    case .success(let issue):
        // do something with the issue
    case .failure:
        // handle any errors
    }
}

评论议题

Octokit().commentIssue(owner: "octocat", repository: "Hello-World", number: 1, body: "Testing a comment") { response in
    switch response {
    case .success(let comment):
        // do something with the comment
    case .failure:
        // handle any errors
    }
}

编辑现有评论

Octokit().patchIssueComment(owner: "octocat", repository: "Hello-World", number: 1, body: "Testing a comment") { response in
    switch response {
    case .success(let comment):
        // do something with the comment
    case .failure:
        // handle any errors
    }
}

拉取请求

获取单个拉取请求

Octokit().pullRequest(owner: "octocat", repository: "Hello-World", number: 1) { response in
    switch response {
        case .success(let pullRequests):
            // do something with a pull request
        case .failure:
            // handle any errors
     }
}

列出拉取请求

Octokit().pullRequests(owner: "octocat", repository: "Hello-World", base: "develop", state: Openness.Open) { response in
    switch response {
        case .success(let pullRequests):
        // do something with a pull request list
        case .failure:
        // handle any errors
    }
}

更新现有拉取请求

Octokit().patchPullRequest(session, owner: "octocat", repository: "Hello-World", number: 1, title: "Updated title", body: "The updated body", state: .open, base: "base-branch", mantainerCanModify: true) { response in
    switch response {
        case .success(let pullrequest):
        // do something with the pullrequest
        case .failure:
        // handle any errors
    }
}

发布

创建新的发布

Octokit().postRelease(owner: "octocat", repository: "Hello-World", tagName: "v1.0.0", targetCommitish: "master", name: "v1.0.0 Release", body: "The changelog of this release", prerelease: false, draft: false) { response in
	switch response {
        case .success(let release):
        // do something with the release
        case .failure:
        // handle any errors
    }
}