Swiftpack.co - Package - nerdishbynature/octokit.swift
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

Octokit.swift

Build Status CocoaPods codecov.io

Installation

import PackageDescription

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

Authentication

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

There are two types of Configurations, TokenConfiguration and OAuthConfiguration.

TokenConfiguration

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

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

OAuthConfiguration

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

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

// 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)
    }
  }
}

Please note that you will be given a TokenConfiguration back from the OAuth flow. You have to store the accessToken yourself. If you want to make further requests it is not necessary to do the OAuth Flow again. You can just use a 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)")
  }
}

Users

Get a single user

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
  }
}

Get the authenticated user

Octokit().me() { response in
  switch 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 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
  }
}

Get repositories of authenticated user

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

Starred Repositories

Get starred repositories of some user

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
  }
}

Get starred repositories of authenticated user

Octokit().myStars() { response in
  switch 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

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
  }
}

Get followers of authenticated user

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

Get following of some user

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
  }
}

Get following of authenticated user

Octokit().myFollowing() { response in
  switch 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() { response in
    switch 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 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
    }
}

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"]) { response in
    switch 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) { response in
    switch 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") { response in
    switch 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) { response in
    switch response {
        case .success(let pullRequests):
            // do something with a pull request
        case .failure:
            // handle any errors
     }
}

List a pull requests

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
    }
}

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

Github

link
Stars: 315

Releases

SwiftPM 5.2 - 2020-03-31T09:45:07

  • SwiftPM Changes 5.2. #102 #103
    @bofeizhu @f-meloni @pietbrauer

OMG It's been so long - 2019-12-16T15:52:14

  • Allow GitHub API calls to be discarded #70
    @orta
  • Fixed GitHub spelling #71
    @sdaniel55
  • Update Readme.md #72
    @petrpavlik
  • Fixed issue of nil htmlURL #74
    @stephenyao
  • Bump Xcode, RequestKit and Swift versions #76
    @rubiojr
  • Resolved problems when posting Issues #77
    @vincode-io
  • Added support for Gists #78.
    @vincode-io
  • Add methods to get and create labels #80
    @robmathers
  • Add labels parameter to postIssue #81
    @robmathers
  • Allow requestkit versions until next major #83
    @f-meloni
  • Add Swift 5.1 support on Linux #85
    @ellneal

It's Linux not Linux - 2018-11-12T12:30:08

  • Make Framework Linux compatible
    #69 @f-meloni
  • Support for Swift PM #68 @shkhaliq

- 2018-03-27T13:00:39

  • Update to Swift 4
    #66 @pietbrauer

- 2017-08-25T11:40:51

  • Add basic PullRequest Support
    #62 @polok

- 2017-03-29T19:09:05

  • New binary with Swift 3.1 compatibility

- 2017-02-22T14:34:53

  • Replace naive parameter parsing for redirect URLs
    #59 @pietbrauer
  • Update to recommended project settings
    #58 #60 @russellbstephens @pietbrauer

- 2017-02-09T13:33:46

  • Cherry pick bug fixes from Swift 2.3 branch
    #57 @pietbrauer

Bug fixes and performance improvements - 2016-10-06T11:35:19

  • Fixes a critical bug where repos would not be loaded

Let there be Swift 3 - 2016-09-20T14:35:50

Intended to be used with Xcode 8.0 and Swift 3.0

Swift 2.2/2.3 - 2016-09-20T12:12:57

Intended to be used with Xcode 7.3.1 or Xcode 8.0 and Swift 2.2 or 2.3

AppStore compliant - 2016-04-21T00:36:51

  • Add allow_signup=false to the authenticate URL so the signup button is hidden on iPads (makes apps not being rejected)
    #42 @pietbrauer

Xcode 7.3 - 2016-03-24T07:04:44

  • Update Xcode to 7.3
    #39 @pietbrauer

All the Platforms - 2016-02-25T05:45:33

  • Add support for all platforms: Mac OS X, tvOS and watchOS
    #8 @pietbrauer

- 2015-12-13T05:55:52

  • Update RequestKit
    #30 @pietbrauer

- 2015-11-26T02:25:06

  • Add lastPush and htmlURL to Repositories
    #22 @phatblat
  • Load repositories for arbitrary user
    #21 @phatblat
  • Add podspec
    #26 @nwest
  • Remove routers from public API
    #28 @nwest
  • Documentation for public API
    #27 @nwest

Note: This release was altered on Dec 11, 2015 to include .bcsymbolmap needed to submit builds to the AppStore with Bitcode enabled

All I see is stars and swirls - 2015-10-29T11:40:12

  • Move generic components to RequestKit so TrashCanKit can use it as well #14
  • Add support for fetching starred repositories #17

Gimme that URL - 2015-09-21T12:56:47

  • Add possibility to get the authentication URL, e.g. for presenting your own SFSafariViewController because Apple doesn't know the concept of OAuth. (I am sorry if you are working there and I stepped on your toe, I am sure there is some reason to do it like that)

- 2015-09-10T19:12:47

  • Add Objective-C compatibility again

Xcode 7 & Swift 2 - 2015-09-10T10:35:04

  • Add support for Xcode 7 and Swift 2 #11

0.2.5 - 2015-08-31T07:50:50

  • Add additional perPage parameter to get repositories (defaults to 100)

- 2015-08-26T12:58:29

  • Add pagination for repositories list

Post Public Key - 2015-08-26T06:30:23

  • Add possibility to post a Public Key #8

Objectve-C - 2015-08-25T12:01:30

Classes are the new structs - 2015-08-25T11:56:19

  • Use class for Repository and User for objc-compability

Restart all the things - 2015-08-25T07:01:04

  • Remove Alamofire dependency #5
  • Solidify JSON Parsing #6
  • Add Carthage support #7