7 releases

0.1.6 Feb 9, 2024
0.1.5 Aug 31, 2023
0.1.4 Jun 1, 2023
0.1.3 May 29, 2023
0.1.2 Apr 22, 2023

#154 in HTTP server

Download history 42/week @ 2023-10-28 41/week @ 2023-11-04 39/week @ 2023-11-11 44/week @ 2023-11-18 67/week @ 2023-11-25 49/week @ 2023-12-02 51/week @ 2023-12-09 45/week @ 2023-12-16 49/week @ 2023-12-23 26/week @ 2023-12-30 34/week @ 2024-01-06 42/week @ 2024-01-13 34/week @ 2024-01-20 40/week @ 2024-01-27 55/week @ 2024-02-03 88/week @ 2024-02-10

225 downloads per month
Used in 9 crates (3 directly)

MIT/Apache

84KB
2.5K SLoC

Octocrate (Octocat + Crate)

A Github API library based on Rust

octocrate MIT

Install

Install Octocrate with cargo:

cargo add octocrate

Or modify your Cargo.toml to include octocrate as a dependency:

[dependencies]
octocrate = "0.1.2"

Usage

Github API

use octocrate::{GithubPersonalAccessToken, GithubAPI};

#[tokio::main]
async fn main() {
    let token = GithubPersonalAccessToken::new("YOUR_PERSONAL_ACCESS_TOKEN");

    let api = GithubAPI::with_token(token);

    let repositories = api
        .repositories
        .list_user_repositories("panghu-huang")
        .send()
        .await
        .unwrap();

    println!("Repositories: {:#?}", repositories);
}

Github App

use octocrate::{GithubApp};

#[tokio::main]
async fn main() {
  let app = GithubApp::builder()
    .app_id("GITHUB_APP_ID")
    .private_key("GITHUB_APP_PRIVATE_KEY")
    .build()
    .unwrap();

  let installation = app
    .get_repository_installation("panghu-huang", "octocrate")
    .await
    .unwrap();

  let api = app.get_api(installation.id).await.unwrap();

  let repository = api
    .repositories
    .get_repository("panghu-huang", "octocrate")
    .send()
    .await
    .unwrap();

  println!("Repository: {:?}", repository);
}

Github Webhook Server

Here's a simple example showing how to create a Github webhook server and handle the issue comment event. The webhook-server feature needs to be enabled

use octocrate::{GithubApp, GithubWebhookEvent, WebhookServer};

#[tokio::main]
async fn main() {
  let mut server = WebhookServer::builder()
    .app_id("GITHUB_APP_ID")
    .private_key("GITHUB_APP_PRIVATE_KEY")
    .build()
    .unwrap();

  server
    .on_webhook_event(|event, api| {
      match event {
        GithubWebhookEvent::IssueComment(evt) => {
          // ... handle issue comment event
        }
        _ => {}
      };

      Ok(())
    })
    .start()
    .await
    .unwrap();
}

The github_app.start() method opens a web server to listen for webhook requests from Github, the default path is /github/webhook

Examples

For more examples, please refer to: examples

Dependencies

~13–28MB
~535K SLoC