The letter A styled as Alchemists logo. lchemists Syndication Icon

Putin's War on Ukraine - Watch President Zelenskyy's speech and help Ukraine fight against the senseless cruelty of a dictator!

Published October 20, 2022 Updated November 7, 2022
Ghub Icon

Ghub

0.2.0

Ghub is portmanteau (i.e. [g]it + hub = ghub) that provides a GitHub API client using a design which leverages function composition and monads. This gem is built upon the HTTP gem instead of Faraday which is what the Octokit gem uses.

Features

  • Provides an API client which is a partial implementation of GitHub’s API.

  • Provides HTTP request and response verification using Dry Schema.

  • Uses Function Composition — coupled with Transactable — to process each HTTP request and response.

Requirements

Setup

To set up the project, run:

bin/setup

Usage

All usage is via the Ghub::Client class.

Initialization

You can initialize an API client — using the defaults as described in the Environment section below — as follows:

client = Ghub::Client.new

Further customization can be done via a block:

client = Ghub::Client.new do |config|
  config.accept = "application/json"     # Uses a custom HTTP Accept header.
  config.paginate = true                 # Enabled automatic pagination.
  config.token = "ghp_abc"               # Provides a Personal Access Token (PAT).
  config.url = "https://alt.github.com"  # Uses a custom API root.
end

Environment

Environment variable support can be managed using direnv. These are the defaults:

GITHUB_API_ACCEPT=application/vnd.github+json
GITHUB_API_PAGINATE=false
GITHUB_API_TOKEN=
GITHUB_API_URL=https://api.github.com

You must provide a value for GITHUB_API_TOKEN in order to make authenticated API requests. This can be done by creating a Personal Access Token (PAT) for the value.

Endpoints

Only partial support of the various API endpoints are supported. Each section below documents usage with additional documentation, usage, parameters, responses, etc. provided by the official GitHub API documentation links.

Branch Protection

The following is an example of how to show, update, and destroy branch protection:

client.branch_protection.show "<owner>", "<repository>", "<branch>"
client.branch_protection.update "<owner>", "<repository>", "<branch>", {}
client.branch_protection.destroy "<owner>", "<repository>", "<branch>"

Branch Signature

The following is an example of how to show, create, and destroy branch signature protection:

client.branch_signature.show "<owner>", "<repository>", "<branch>"
client.branch_signature.create "<owner>", "<repository>", "<branch>"
client.branch_signature.destroy "<owner>", "<repository>", "<branch>"

Organization Members

The following is how to index organization members.

client.organization_members.index "<owner>"

Pulls

The following is how to index and show pull requests:

client.pulls.index "<owner>", "<repository>"
client.pulls.show "<owner>", "<repository>", <id>

Repositories

The following documents how to interact with repositories:

client.repositories.index "<owner>", "<repository>"
client.repositories.show "<owner>", "<repository>"

# For a user.
# Example: client.repositories.create :users, {name: "ghub-test", private: true}
client.repositories.create :users, <attributes>

# For an organization.
# Example: client.repositories.create :orgs, {name: "ghub-test", private: true}, owner: "acme"
client.repositories.create :orgs, <attributes>, owner: "<organization>"

client.repositories.patch "<owner>", {description: "For test only."}
client.repositories.destroy "<owner>", <id>

GitHub’s API design for repositories is awkward and you can see this infect the Object API, especially when creating a repository. Use :users or :orgs (can be strings) to distinguish between the two types of repository creation. The only stipulation for organization creation is that you must supply the organization name. This was done so you could use the same Object API for both.

Users

The following is how to index and show users:

client.users.index
client.users.show "<user>"

Development

To contribute, run:

git clone https://github.com/bkuhlmann/ghub
cd ghub
bin/setup

You can also use the IRB console for direct access to all objects:

bin/console

Tests

To test, run:

bundle exec rake

Credits