release_plz_core/git/
github_client.rs

1use anyhow::Context;
2use reqwest::header::{HeaderMap, HeaderValue};
3use secrecy::{ExposeSecret, SecretString};
4use url::Url;
5
6use crate::git::forge::Remote;
7
8#[derive(Debug, Clone)]
9pub struct GitHub {
10    pub remote: Remote,
11}
12
13impl GitHub {
14    pub fn new(owner: String, repo: String, token: SecretString) -> Self {
15        Self {
16            remote: Remote {
17                owner,
18                repo,
19                token,
20                base_url: "https://api.github.com".parse().unwrap(),
21            },
22        }
23    }
24
25    pub fn with_base_url(self, base_url: Url) -> Self {
26        Self {
27            remote: Remote {
28                base_url,
29                ..self.remote
30            },
31        }
32    }
33
34    pub fn default_headers(&self) -> anyhow::Result<HeaderMap> {
35        let mut headers = HeaderMap::new();
36        headers.insert(
37            reqwest::header::ACCEPT,
38            HeaderValue::from_static("application/vnd.github+json"),
39        );
40        let mut auth_header: HeaderValue = format!("Bearer {}", self.remote.token.expose_secret())
41            .parse()
42            .context("invalid GitHub token")?;
43        auth_header.set_sensitive(true);
44        headers.insert(reqwest::header::AUTHORIZATION, auth_header);
45        Ok(headers)
46    }
47}