support list releases

This commit is contained in:
Guilhem Lavaux 2025-06-09 15:04:11 +02:00
parent 5cd0635832
commit f6939e9165

View file

@ -1,7 +1,8 @@
use clap::{Parser, Subcommand};
// use time::OffsetDateTime;
use forgejo_api::Auth::Token;
use forgejo_api::Forgejo;
use forgejo_api::structs::CreateReleaseOption;
use forgejo_api::structs::{CreateReleaseOption, RepoListReleasesQuery};
use url::Url;
pub mod config;
@ -27,7 +28,7 @@ enum Commands {
#[derive(Debug, Subcommand)]
enum ReleaseSubCommands {
/// A sub-subcommand
NewRelease {
New {
alias: String,
tag: String,
#[clap(long, short)]
@ -35,9 +36,8 @@ enum ReleaseSubCommands {
},
/// Another sub-subcommand
ListReleases {
#[clap(short, long)]
output: String,
List {
alias: String,
},
}
@ -71,10 +71,10 @@ async fn handle_new_release(
let option = CreateReleaseOption {
tag_name: tag.to_string(),
name: Some(format!("Release {}", tag)),
name: Some(format!("{}", tag)),
body: Some(format!("Release notes for {}", tag)),
hide_archive_links: Some(false),
target_commitish: Some(tag.to_string()),
target_commitish: target.clone(),
draft: None,
prerelease: None,
};
@ -89,12 +89,51 @@ async fn handle_new_release(
Ok(())
}
fn handle_another_sub_sub_command(output: &str) {
println!("Executing AnotherSubSubCommand with output: {}", output);
}
async fn handle_list_releases(config: &config::Config, alias: &str) -> Result<(), String> {
println!("Listing releases for all repositories");
fn handle_another_command(option: &str) {
println!("Executing AnotherCommand with option: {}", option);
let repo = config.repos.get(alias).map_or_else(
|| Err(format!("No repository found with alias: {}", alias)),
|repo| {
println!("Using repository: {} owned by {}", repo.repo, repo.owner);
// Here you would typically call the Forgejo API to create a new release
// For example:
Ok(repo)
},
)?;
let token = Token(&repo.token);
let url = Url::parse(&repo.hostname).map_err(|e| {
eprintln!("Failed to parse URL: {}", e);
e.to_string()
})?;
let forgejo = Forgejo::new(token, url).map_err(|e| {
eprintln!("Failed to create Forgejo client: {}", e);
e.to_string()
})?;
let query = forgejo_api::structs::RepoListReleasesQuery {
draft: None,
page: None,
q: None,
pre_release: None,
limit: Some(100),
};
let result_query = forgejo.repo_list_releases(&repo.owner, &repo.repo, query).await.map_err(|e| {
eprintln!("Failed to list releases: {}", e);
e.to_string()
})?;
for release in result_query.1 {
println!(
"Release: {}, Tag: {}, Created at: {}",
release.name.unwrap_or_default(),
release.tag_name.unwrap_or("N/A".to_string()),
release.created_at.unwrap()
);
}
Ok(())
}
#[tokio::main]
@ -106,11 +145,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
match args.command {
Commands::Release(subcommand) => {
match subcommand {
ReleaseSubCommands::NewRelease { alias, tag, target } => {
ReleaseSubCommands::New { alias, tag, target } => {
handle_new_release(&config, &alias, &tag, &target).await?
}
ReleaseSubCommands::ListReleases { output } => {
handle_another_sub_sub_command(&output)
ReleaseSubCommands::List { alias } => {
handle_list_releases(&config, &alias).await?;
}
}
// Uncomment the following lines if you want to use the dispatch macro