A Github API library based on Rust
Install Octocrate
with cargo
:
bash
cargo install octocrate
```rust use octocrate::{GithubPersonalAccessToken, GithubAPI};
async fn main() { let token = GithubPersonalAccessToken::new("YOURPERSONALACCESS_TOKEN");
let api = GithubAPI::with_token(token);
let repositories = api
.repositories
.list_user_repositories("panghu-huang")
.send()
.await
.unwrap();
println!("Repositories: {:#?}", repositories);
} ```
```rust use octocrate::{GithubApp};
async fn main() { let app = GithubApp::builder() .appid("GITHUBAPPID") .privatekey("GITHUBAPPPRIVATE_KEY") .build() .unwrap();
let installation = app .getrepositoryinstallation("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); } ```
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
```rust use octocrate::{GithubApp, GithubWebhookEvent, WebhookServer};
async fn main() { let mut server = WebhookServer::builder() .appid("GITHUBAPPID") .privatekey("GITHUBAPPPRIVATE_KEY") .build() .unwrap();
server .onwebhookevent(|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
For more examples, please refer to: examples