git-next-forge-github 0.14.1

GitHub support for git-next, the trunk-based development manager
Documentation
use std::collections::HashMap;

//
use git_next_core::{git, webhook, ApiToken, BranchName};

mod authorisation;
mod list;
mod parser;
mod register;
mod unregister;

pub use authorisation::is_authorised;
pub use list::list;
pub use parser::parse_body;
pub use register::register;
pub use unregister::unregister;

#[cfg(test)]
pub use authorisation::sign_body;

pub fn headers(token: &ApiToken) -> HashMap<String, String> {
    use secrecy::ExposeSecret;

    HashMap::from([
        (
            "Accept".to_string(),
            "application/vnd.github+json".to_string(),
        ),
        (
            "User-Agent".to_string(),
            format!("git-next/server/{}", clap::crate_version!()),
        ),
        (
            "Authorization".to_string(),
            format!("Bearer {}", token.expose_secret()),
        ),
        ("X-GitHub-Api-Version".to_string(), "2022-11-28".to_string()),
    ])
}

#[derive(Debug, serde::Deserialize)]
pub struct Push {
    #[serde(rename = "ref")]
    reference: String,
    after: String,
    head_commit: HeadCommit,
}
#[derive(Debug, serde::Deserialize)]
pub struct HeadCommit {
    message: String,
}

impl TryFrom<Push> for webhook::Push {
    type Error = git::forge::webhook::Error;
    fn try_from(push: Push) -> Result<Self, Self::Error> {
        let branch = push
            .reference
            .splitn(3, '/') // should be of the form 'refs/heads/branchname'
            .nth(2)
            .map(BranchName::new)
            .ok_or(git::forge::webhook::Error::UnknownBranch(push.reference))?;
        Ok(Self::new(branch, push.after, push.head_commit.message))
    }
}