Using Swift Package Manager:
import PackageDescription
let package = Package(
name: "MyAwesomeApp",
dependencies: [
.package(url: "https://github.com/nerdishbynature/octokit.swift", from: "0.11.0")
]
)
Octokit supports GitHub, and GitHub Enterprise.
Authentication is handled using a Configuration
.
There are two types of Configuration
:
TokenConfiguration
OAuthConfiguration
TokenConfiguration
is used if you are using Access Token based Authentication (e.g. the user
offered you an access token he generated on the website) or if you got an Access Token through
the OAuth Flow
You can initialize a new config for github.com
as follows:
let config = TokenConfiguration("YOUR_PRIVATE_GITHUB_TOKEN_HERE")
or for GitHub Enterprise
let config = TokenConfiguration("YOUR_PRIVATE_GITHUB_TOKEN_HERE", url: "https://github.example.com/api/v3/")
After you got your token you can use it with Octokit
let user = try await Octokit(config).me()
Use OAuthConfiguration
if the user does not already have a GitHub access token. This will guide the user through handles the OAuth flow.
You can authenticate a user for github.com
as follows:
let config = OAuthConfiguration(
token: "<Your Client ID>",
secret: "<Your Client secret>",
scopes: ["repo", "read:org"]
)
let url = config.authenticate()
or for GitHub Enterprise
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"]
)
After you have a valid configuration, you will be able to authenticate the user:
// AppDelegate.swift
config.authenticate()
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
Task {
try await config.handleOpenURL(url)
try await loadCurrentUser(config: config) // purely optional of course
}
return false
}
func loadCurrentUser(config: TokenConfiguration) async throws {
let user = try await Octokit(config).me()
print(user.login)
}
Please note that the OAuth flow will return a instance of TokenConfiguration
.
You need to store the accessToken
yourself. If you want to make further requests it is not necessary to go through the OAuth Flow again. You can just use the TokenConfiguration
that you already have.
let token = // get your token from your keychain, user defaults (not recommended) etc.
let config = TokenConfiguration(token)
let user = try await Octokit(config).user(name: "octocat")
print("User login: \(user.login!)")
let username = ... // set the username
let user = try await Octokit().user(name: username)
let user = try await Octokit().me()
let (owner, name) = ("owner", "name") // replace with actual owner and name
let repository = try await Octokit().repository(owner, name)
let repositories = try await Octokit().repositories()
let repositories = try await Octokit().stars("username")
let repositories = try await Octokit().myStars()
let users = try await Octokit().followers("username")
let users = try await Octokit().myFollowers()
let users = try await Octokit().following("username")
let users = try await Octokit().myFollowing()
Get all issues across all the authenticated user's visible repositories including owned repositories, member repositories, and organization repositories.
let issues = try await Octokit(config).myIssues()
let (owner, repo, number) = ("owner", "repo", 1347) // replace with actual owner, repo name, and issue number
let issue = try await Octokit(config).issue(owner, repository: repo, number: number)
let issue = try await Octokit(config).postIssue("owner", repository: "repo", title: "Found a bug", body: "I'm having a problem with this.", assignee: "octocat", labels: ["bug", "duplicate"])
let issue = try await Octokit(config).patchIssue("owner", repository: "repo", number: 1347, title: "Found a bug", body: "I'm having a problem with this.", assignee: "octocat", state: .Closed)
let comment = try await Octokit().commentIssue(owner: "octocat", repository: "Hello-World", number: 1, body: "Testing a comment")
let comment = try await Octokit().patchIssueComment(owner: "octocat", repository: "Hello-World", number: 1, body: "Testing a comment")
let pullRequests = try await Octokit().pullRequest(owner: "octocat", repository: "Hello-World", number: 1)
let pullRequests = try await Octokit().pullRequests(owner: "octocat", repository: "Hello-World", base: "develop", state: .Open)
let pullRequest = try await Octokit().patchPullRequest(session, owner: "octocat", repository: "Hello-World", number: 1, title: "Updated title", body: "The updated body", state: .open, base: "base-branch", mantainerCanModify: true)
let release = try await 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)