Create GitHub releases with Rust using Hubcaps

Feb 23, 2016 - Reading time: 2 minutes.

For one of my projects I need to access the GitHub API to create releases. Luckily, through reading This Week in Rust #119, I discovered Hubcaps, a library for interfacing with GitHub.

Though it lacks some documentation and is not yet fully finished, it already provides APIs for the relevant parts regarding releases.

On GitHub a release is always associated with a Git tag, but need to be specifially created to be shown on the site with the full description and optional assets attached to them. It is also possible to mark releases as a draft (then it is only visible to repo contributors) or as a pre-release, useful for alpha releases of a library or application.

Once you have a Git tag in your repository the API can be used to create an associated release using the following Rust code:

extern crate hyper; extern crate hubcaps; use std::{env, process}; use hyper::Client; use hubcaps::{Github, ReleaseOptions}; fn main () { let token = match env::var( "GITHUB_TOKEN" ). ok () { Some (token) => token, _ => { println!( "example missing GITHUB_TOKEN" ); process::exit( 1 ); } }; let client = Client::new(); let github = Github::new( "hubcaps/0.1.1" , & client, Some (token)); let user = "username" ; let repo = "my-library" ; let name = "ONE DOT OH" ; let body = "This is a long long body" ; let tag = "v1.0.0" ; let opts = ReleaseOptions::builder(tag) . name (name) . body (body) . commitish ( "master" ) . draft ( false ) . prerelease ( false ) . build (); let repo = github. repo (user, repo); let release = repo. releases (); match release. create ( & opts) { Ok ( _ ) => println!( "Release created" ), Err (e) => println!( "Failed to create release: {:?} " , e), }; }

If you clone Hubcaps and put the above code in a file named releases.rs into the examples/ folder, you can run it with cargo run --example releases . You need to get a personal access token first and set it in your environment ( export GITHUB_TOKEN=<your token here> )

Of course it has the repository and tag hard-coded, but this is easy to adapt.

The code was tested with Rust 1.6 and hubcaps 0.1.1

Updated version to work with hubcaps 0.2.0 online.