feat: New issue {reopen,close,resolve} commands to manage issue status
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
65
src/cli/commands/issue/close.rs
Normal file
65
src/cli/commands/issue/close.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
|
||||
// Copyright (C) 2025 Awiteb <a@4rs.nl>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||
|
||||
use clap::Args;
|
||||
|
||||
use super::IssueStatus;
|
||||
use crate::{
|
||||
cli::{
|
||||
CliOptions,
|
||||
traits::CommandRunner,
|
||||
types::{NaddrOrSet, NostrEvent},
|
||||
},
|
||||
error::{N34Error, N34Result},
|
||||
};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct CloseArgs {
|
||||
/// Repository address in `naddr` format (`naddr1...`), NIP-05 format
|
||||
/// (`4rs.nl/n34` or `_@4rs.nl/n34`), or a set name like `kernel`.
|
||||
///
|
||||
/// If omitted, looks for a `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-NIP05-OR-SET", long = "repo")]
|
||||
naddrs: Option<Vec<NaddrOrSet>>,
|
||||
/// The opened issue id to close it
|
||||
issue_id: NostrEvent,
|
||||
}
|
||||
|
||||
impl CommandRunner for CloseArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
crate::cli::common_commands::issue_status_command(
|
||||
options,
|
||||
self.issue_id,
|
||||
self.naddrs,
|
||||
IssueStatus::Closed,
|
||||
|issue_status| {
|
||||
if issue_status.is_closed() {
|
||||
return Err(N34Error::InvalidStatus(
|
||||
"You can't close an already closed issue".to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
if issue_status.is_resolved() {
|
||||
return Err(N34Error::InvalidStatus(
|
||||
"You can't close a resolved issue".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -14,17 +14,27 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||
|
||||
/// `issue close` subcommand
|
||||
mod close;
|
||||
/// `issue new` subcommand
|
||||
mod new;
|
||||
/// `issue reopen` subcommand
|
||||
mod reopen;
|
||||
/// `issue resolve` subcommand
|
||||
mod resolve;
|
||||
/// `issue view` subcommand
|
||||
mod view;
|
||||
|
||||
use clap::Subcommand;
|
||||
use nostr::event::Kind;
|
||||
|
||||
use self::close::CloseArgs;
|
||||
use self::new::NewArgs;
|
||||
use self::reopen::ReopenArgs;
|
||||
use self::resolve::ResolveArgs;
|
||||
use self::view::ViewArgs;
|
||||
use super::{CliOptions, CommandRunner};
|
||||
use crate::error::N34Result;
|
||||
use crate::error::{N34Error, N34Result};
|
||||
|
||||
/// Prefix used for git issue alt.
|
||||
pub const ISSUE_ALT_PREFIX: &str = "git issue: ";
|
||||
@@ -35,10 +45,66 @@ pub enum IssueSubcommands {
|
||||
New(NewArgs),
|
||||
/// View an issue by its ID
|
||||
View(ViewArgs),
|
||||
/// Reopens a closed issue.
|
||||
Reopen(ReopenArgs),
|
||||
/// Closes an open issue.
|
||||
Close(CloseArgs),
|
||||
/// Resolves an open issue.
|
||||
Resolve(ResolveArgs),
|
||||
}
|
||||
|
||||
/// Possible states for a Git issue
|
||||
#[derive(Debug)]
|
||||
pub enum IssueStatus {
|
||||
/// The issue is currently open
|
||||
Open,
|
||||
/// The issue has been resolved
|
||||
Resolved,
|
||||
/// The issue has been closed
|
||||
Closed,
|
||||
}
|
||||
|
||||
impl IssueStatus {
|
||||
/// Maps the issue status to its corresponding Nostr kind.
|
||||
pub fn kind(&self) -> Kind {
|
||||
match self {
|
||||
Self::Open => Kind::GitStatusOpen,
|
||||
Self::Resolved => Kind::GitStatusApplied,
|
||||
Self::Closed => Kind::GitStatusClosed,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the issue is open.
|
||||
pub fn is_open(&self) -> bool {
|
||||
matches!(self, Self::Open)
|
||||
}
|
||||
|
||||
/// Check if the issue is resolved.
|
||||
pub fn is_resolved(&self) -> bool {
|
||||
matches!(self, Self::Resolved)
|
||||
}
|
||||
|
||||
/// Check if the issue is closed.
|
||||
pub fn is_closed(&self) -> bool {
|
||||
matches!(self, Self::Closed)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Kind> for IssueStatus {
|
||||
type Error = N34Error;
|
||||
|
||||
fn try_from(kind: Kind) -> Result<Self, Self::Error> {
|
||||
match kind {
|
||||
Kind::GitStatusOpen => Ok(Self::Open),
|
||||
Kind::GitStatusApplied => Ok(Self::Resolved),
|
||||
Kind::GitStatusClosed => Ok(Self::Closed),
|
||||
_ => Err(N34Error::InvalidIssueStatus(kind)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRunner for IssueSubcommands {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
crate::run_command!(self, options, & New View)
|
||||
crate::run_command!(self, options, & New View Reopen Close Resolve)
|
||||
}
|
||||
}
|
||||
|
||||
65
src/cli/commands/issue/reopen.rs
Normal file
65
src/cli/commands/issue/reopen.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
|
||||
// Copyright (C) 2025 Awiteb <a@4rs.nl>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||
|
||||
use clap::Args;
|
||||
|
||||
use super::IssueStatus;
|
||||
use crate::{
|
||||
cli::{
|
||||
CliOptions,
|
||||
traits::CommandRunner,
|
||||
types::{NaddrOrSet, NostrEvent},
|
||||
},
|
||||
error::{N34Error, N34Result},
|
||||
};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ReopenArgs {
|
||||
/// Repository address in `naddr` format (`naddr1...`), NIP-05 format
|
||||
/// (`4rs.nl/n34` or `_@4rs.nl/n34`), or a set name like `kernel`.
|
||||
///
|
||||
/// If omitted, looks for a `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-NIP05-OR-SET", long = "repo")]
|
||||
naddrs: Option<Vec<NaddrOrSet>>,
|
||||
/// The closed issue id to reopen it
|
||||
issue_id: NostrEvent,
|
||||
}
|
||||
|
||||
impl CommandRunner for ReopenArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
crate::cli::common_commands::issue_status_command(
|
||||
options,
|
||||
self.issue_id,
|
||||
self.naddrs,
|
||||
IssueStatus::Open,
|
||||
|issue_status| {
|
||||
if issue_status.is_open() {
|
||||
return Err(N34Error::InvalidStatus(
|
||||
"You can't reopen an opened issue".to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
if issue_status.is_resolved() {
|
||||
return Err(N34Error::InvalidStatus(
|
||||
"You can't open a resolved issue".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
59
src/cli/commands/issue/resolve.rs
Normal file
59
src/cli/commands/issue/resolve.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
|
||||
// Copyright (C) 2025 Awiteb <a@4rs.nl>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||
|
||||
use clap::Args;
|
||||
|
||||
use super::IssueStatus;
|
||||
use crate::{
|
||||
cli::{
|
||||
CliOptions,
|
||||
traits::CommandRunner,
|
||||
types::{NaddrOrSet, NostrEvent},
|
||||
},
|
||||
error::{N34Error, N34Result},
|
||||
};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
pub struct ResolveArgs {
|
||||
/// Repository address in `naddr` format (`naddr1...`), NIP-05 format
|
||||
/// (`4rs.nl/n34` or `_@4rs.nl/n34`), or a set name like `kernel`.
|
||||
///
|
||||
/// If omitted, looks for a `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-NIP05-OR-SET", long = "repo")]
|
||||
naddrs: Option<Vec<NaddrOrSet>>,
|
||||
/// The opened issue id to resolve it
|
||||
issue_id: NostrEvent,
|
||||
}
|
||||
|
||||
impl CommandRunner for ResolveArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
crate::cli::common_commands::issue_status_command(
|
||||
options,
|
||||
self.issue_id,
|
||||
self.naddrs,
|
||||
IssueStatus::Resolved,
|
||||
|issue_status| {
|
||||
if issue_status.is_resolved() {
|
||||
return Err(N34Error::InvalidStatus(
|
||||
"You can't resolve an resolved issue".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user