Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Swift API Client for GitHub and GitHub Enterprise

License

NotificationsYou must be signed in to change notification settings

nerdishbynature/octokit.swift

Repository files navigation

Installation

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

Authentication

Octokit supports both, GitHub and GitHub Enterprise.Authentication is handled using Configurations.

There are two types of Configurations,TokenConfiguration andOAuthConfiguration.

TokenConfiguration

TokenConfiguration is used if you are using Access Token based Authentication (e.g. the useroffered you an access token he generated on the website) or if you got an Access Token throughthe OAuth Flow

You can initialize a new config forgithub.com as follows:

letconfig=TokenConfiguration("YOUR_PRIVATE_GITHUB_TOKEN_HERE")

or for GitHub Enterprise

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

After you got your token you can use it withOctokit

Octokit(config).me(){ responseinswitch response{case.success(let user):print(user.loginasAny)case.failure(let error):print(error)}}

OAuthConfiguration

OAuthConfiguration is meant to be used, if you don't have an access token already and theuser has to login to your application. This also handles the OAuth flow.

You can authenticate an user forgithub.com as follows:

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

or for GitHub Enterprise

letconfig=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 got your config you can authenticate the user:

// AppDelegate.swiftconfig.authenticate()func application(application:UIApplication, openURL url:NSURL, sourceApplication:String?, annotation:AnyObject?)->Bool{  config.handleOpenURL(url){ configinself.loadCurrentUser(config) // purely optional of course}returnfalse}func loadCurrentUser(config:TokenConfiguration){Octokit(config).me(){ responseinswitch response{case.success(let user):print(user.login)case.failure(let error):print(error)}}}

Please note that you will be given aTokenConfiguration back from the OAuth flow.You have to store theaccessToken yourself. If you want to make further requests it is notnecessary to do the OAuth Flow again. You can just use aTokenConfiguration.

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

Users

Get a single user

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

Get the authenticated user

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

Repositories

Get a single repository

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

Get repositories of authenticated user

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

Starred Repositories

Get starred repositories of some user

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

Get starred repositories of authenticated user

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

Follower and Following

Get followers of some user

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

Get followers of authenticated user

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

Get following of some user

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

Get following of authenticated user

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

Issues

Get issues of authenticated user

Get all issues across all the authenticated user's visible repositories including owned repositories, member repositories, and organization repositories.

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

Get a single issue

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

Open a new issue

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

Edit an existing issue

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

Comment an issue

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

Edit an existing comment

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

Pull requests

Get a single pull request

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

List pull requests

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

Update an exisiting Pull Request

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

Releases

Create a new release

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){ responseinswitch response{case.success(let release):        // do something with the release        case.failure:        // handle any errors}}

About

A Swift API Client for GitHub and GitHub Enterprise

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors51


[8]ページ先頭

©2009-2025 Movatter.jp