feat: Add issue new command

Command to create a new issue on a repo

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-05-08 13:03:09 +00:00
parent f1378a2eb7
commit 54f1c7e0e6
5 changed files with 330 additions and 8 deletions

View File

@@ -16,9 +16,14 @@
use convert_case::{Case, Casing};
use nostr::{
event::{EventBuilder, Tag, TagKind, TagStandard, Tags},
event::{EventBuilder, EventId, Tag, TagKind, TagStandard, Tags},
key::PublicKey,
nips::nip34::GitRepositoryAnnouncement,
nips::{
nip01::Coordinate,
nip21::Nip21,
nip34::{GitIssue, GitRepositoryAnnouncement},
},
parser::Token,
types::{RelayUrl, Url},
};
@@ -91,4 +96,63 @@ impl EventBuilder {
.tags(labels.into_iter().map(Tag::hashtag)),
)
}
/// Creates a new [`GitIssue`] event builder with the given
/// issue details.
pub fn new_git_issue(
repository: Coordinate,
content: String,
subject: Option<String>,
labels: Vec<String>,
) -> N34Result<EventBuilder> {
EventBuilder::git_issue(GitIssue {
repository,
content,
subject,
labels: labels.into_iter().map(|l| l.trim().to_owned()).collect(),
})
.map_err(N34Error::from)
}
}
/// Helper functions for [`Token`] type
#[easy_ext::ext(TokenUtils)]
impl Token<'_> {
/// Returns `Some((public_key, relays))` from the givin token if it's npub1
/// or nprofile1
pub fn extract_public_key(&self) -> Option<(PublicKey, Vec<RelayUrl>)> {
match self {
Token::Nostr(nip21) => {
match nip21 {
Nip21::Pubkey(pkey) => Some((*pkey, Vec::new())),
Nip21::Profile(profile) => Some((profile.public_key, profile.relays.clone())),
_ => None,
}
}
_ => None,
}
}
/// Returns `Some((note_id, relays))` from the givin token if it's note1 or
/// nevent1
pub fn extract_event_id(&self) -> Option<(EventId, Vec<RelayUrl>)> {
match self {
Token::Nostr(nip21) => {
match nip21 {
Nip21::EventId(event_id) => Some((*event_id, Vec::new())),
Nip21::Event(event) => Some((event.event_id, event.relays.clone())),
_ => None,
}
}
_ => None,
}
}
/// Returns `Some(hashtag)` from the givin token if it's a hashtag
pub fn extract_hashtag(&self) -> Option<String> {
match self {
Token::Hashtag(tag) => Some(tag.trim().to_owned()),
_ => None,
}
}
}

View File

@@ -14,7 +14,7 @@
// 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 std::{fmt, str::FromStr};
use std::{fmt, fs, str::FromStr, sync::atomic::Ordering};
use nostr::{
event::{Event, EventId, Kind, TagKind, TagStandard},
@@ -137,3 +137,50 @@ pub fn add_write_relays(mut vector: Vec<RelayUrl>, event: Option<&Event>) -> Vec
}
vector
}
/// Extends the given vector with read relays found in the event (if any).
pub fn add_read_relays(mut vector: Vec<RelayUrl>, event: Option<&Event>) -> Vec<RelayUrl> {
if let Some(event) = event {
vector.extend(
nip65::extract_owned_relay_list(event.clone())
.filter_map(|(r, m)| m.is_none_or(|m| m == RelayMetadata::Read).then_some(r)),
);
}
vector
}
/// Opens the user's default editor ($EDITOR) to edit a temporary file with
/// given suffix, then reads and returns the file contents. The temporary file
/// is automatically deleted.
pub fn read_editor(file_suffix: &str) -> N34Result<String> {
let Ok(editor) = std::env::var("EDITOR") else {
return Err(N34Error::EditorNotFound);
};
let temp_path = tempfile::NamedTempFile::with_suffix(file_suffix)?.into_temp_path();
// Disable the logs to not show up in a terminal text editor
crate::EDITOR_OPEN.store(true, Ordering::Relaxed);
let exit_status = std::process::Command::new(editor)
.arg(temp_path.to_str().expect("The path is valid utf8"))
.spawn()?
.wait()?;
crate::EDITOR_OPEN.store(false, Ordering::Relaxed);
if !exit_status.success() {
if let Some(code) = exit_status.code() {
tracing::warn!("The editor exit with `{code}` status")
}
}
let content = fs::read_to_string(&temp_path)
.map_err(N34Error::from)?
.trim()
.to_owned();
if content.is_empty() {
return Err(N34Error::EmptyEditorFile);
}
Ok(content)
}