fix: always use Forgejo::with_user_agent

This commit is contained in:
Cyborus 2024-12-18 09:57:15 -05:00
parent f51a7093b9
commit 1b3b032d21
4 changed files with 29 additions and 17 deletions

View file

@ -145,7 +145,11 @@ async fn oauth_login(
} }
}; };
let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, host.clone())?; let api = forgejo_api::Forgejo::with_user_agent(
forgejo_api::Auth::None,
host.clone(),
crate::USER_AGENT,
)?;
let request = forgejo_api::structs::OAuthTokenRequest::Public { let request = forgejo_api::structs::OAuthTokenRequest::Public {
client_id, client_id,
code_verifier: &code_verifier, code_verifier: &code_verifier,
@ -154,9 +158,10 @@ async fn oauth_login(
}; };
let response = api.oauth_get_access_token(request).await?; let response = api.oauth_get_access_token(request).await?;
let api = forgejo_api::Forgejo::new( let api = forgejo_api::Forgejo::with_user_agent(
forgejo_api::Auth::OAuth2(&response.access_token), forgejo_api::Auth::OAuth2(&response.access_token),
host.clone(), host.clone(),
crate::USER_AGENT,
)?; )?;
let current_user = api.user_get_current().await?; let current_user = api.user_get_current().await?;
let name = current_user let name = current_user

View file

@ -54,7 +54,8 @@ impl KeyInfo {
pub async fn get_api(&mut self, url: &Url) -> eyre::Result<Forgejo> { pub async fn get_api(&mut self, url: &Url) -> eyre::Result<Forgejo> {
match self.get_login(url) { match self.get_login(url) {
Some(login) => login.api_for(url).await, Some(login) => login.api_for(url).await,
None => Forgejo::new(Auth::None, url.clone()).map_err(Into::into), None => Forgejo::with_user_agent(Auth::None, url.clone(), crate::USER_AGENT)
.map_err(Into::into),
} }
} }
@ -74,15 +75,6 @@ impl KeyInfo {
} }
} }
pub const USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
" (",
env!("CARGO_PKG_REPOSITORY"),
")"
);
#[derive(serde::Serialize, serde::Deserialize, Clone)] #[derive(serde::Serialize, serde::Deserialize, Clone)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum LoginInfo { pub enum LoginInfo {
@ -109,7 +101,8 @@ impl LoginInfo {
pub async fn api_for(&mut self, url: &Url) -> eyre::Result<Forgejo> { pub async fn api_for(&mut self, url: &Url) -> eyre::Result<Forgejo> {
match self { match self {
LoginInfo::Application { token, .. } => { LoginInfo::Application { token, .. } => {
let api = Forgejo::with_user_agent(Auth::Token(token), url.clone(), USER_AGENT)?; let api =
Forgejo::with_user_agent(Auth::Token(token), url.clone(), crate::USER_AGENT)?;
Ok(api) Ok(api)
} }
LoginInfo::OAuth { LoginInfo::OAuth {
@ -119,7 +112,7 @@ impl LoginInfo {
.. ..
} => { } => {
if time::OffsetDateTime::now_utc() >= *expires_at { if time::OffsetDateTime::now_utc() >= *expires_at {
let api = Forgejo::with_user_agent(Auth::None, url.clone(), USER_AGENT)?; let api = Forgejo::with_user_agent(Auth::None, url.clone(), crate::USER_AGENT)?;
let (client_id, client_secret) = crate::auth::get_client_info_for(url) let (client_id, client_secret) = crate::auth::get_client_info_for(url)
.ok_or_else(|| { .ok_or_else(|| {
eyre::eyre!("Can't refresh token; no client info for {url}. How did this happen?") eyre::eyre!("Can't refresh token; no client info for {url}. How did this happen?")
@ -140,7 +133,8 @@ impl LoginInfo {
); );
*expires_at = time::OffsetDateTime::now_utc() + expires_in; *expires_at = time::OffsetDateTime::now_utc() + expires_in;
} }
let api = Forgejo::with_user_agent(Auth::Token(token), url.clone(), USER_AGENT)?; let api =
Forgejo::with_user_agent(Auth::Token(token), url.clone(), crate::USER_AGENT)?;
Ok(api) Ok(api)
} }
} }

View file

@ -17,6 +17,15 @@ mod version;
mod whoami; mod whoami;
mod wiki; mod wiki;
pub const USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
" (",
env!("CARGO_PKG_REPOSITORY"),
")"
);
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct App { pub struct App {
#[clap(long, short = 'H')] #[clap(long, short = 'H')]

View file

@ -21,7 +21,7 @@ impl VersionCommand {
pub async fn run(self) -> eyre::Result<()> { pub async fn run(self) -> eyre::Result<()> {
println!("{} v{}", env!("CARGO_BIN_NAME"), env!("CARGO_PKG_VERSION")); println!("{} v{}", env!("CARGO_BIN_NAME"), env!("CARGO_PKG_VERSION"));
if self.verbose { if self.verbose {
println!("user agent: {}", crate::keys::USER_AGENT); println!("user agent: {}", crate::USER_AGENT);
println!("build type: {BUILD_TYPE}"); println!("build type: {BUILD_TYPE}");
println!(" target: {}", env!("BUILD_TARGET")); println!(" target: {}", env!("BUILD_TARGET"));
} }
@ -36,7 +36,11 @@ impl VersionCommand {
if self.check { if self.check {
let url = url::Url::parse("https://codeberg.org/")?; let url = url::Url::parse("https://codeberg.org/")?;
let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, url)?; let api = forgejo_api::Forgejo::with_user_agent(
forgejo_api::Auth::None,
url,
crate::USER_AGENT,
)?;
let latest = api let latest = api
.repo_get_latest_release("Cyborus", "forgejo-cli") .repo_get_latest_release("Cyborus", "forgejo-cli")