refactor: Support more than one naddr instead of one
This commit supports multiple naddrs for sending issues to and searching for repositories by extracting the embedded relays, and appending to the address file instead of overwriting its previous contents. A header has also been added to the `nostr-address` file to clarify its purpose. This enhancement accommodates maintainers who distribute issue tracking across multiple repositories, preventing confusion if the primary maintainer steps down. Now, issues and patches will reference all relevant repositories, not just one. Note that repositories won't automatically include all maintainers' addresses unless explicitly specified. Maintainers may manage a repository under a project-specific public key rather than individual keys. Addresses are only added to issues and patches when explicitly provided via the command or written to the address file. This prevents unintended inclusions and ensures clarity. Suggested-by: DanConwayDev <DanConwayDev@protonmail.com> Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -16,12 +16,20 @@
|
||||
|
||||
|
||||
use clap::{ArgGroup, Args};
|
||||
use nostr::{event::EventBuilder, nips::nip19::Nip19Coordinate};
|
||||
use futures::future;
|
||||
use nostr::{
|
||||
event::{EventBuilder, Tag},
|
||||
nips::nip19::Nip19Coordinate,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
cli::{CliOptions, CommandRunner, parsers},
|
||||
error::N34Result,
|
||||
nostr_utils::{NostrClient, traits::NewGitRepositoryAnnouncement, utils},
|
||||
nostr_utils::{
|
||||
NostrClient,
|
||||
traits::{NaddrsUtils, NewGitRepositoryAnnouncement, ReposUtils},
|
||||
utils,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -44,7 +52,7 @@ pub struct NewArgs {
|
||||
///
|
||||
/// If not provided, `n34` will look for the `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-OR-NIP05", long = "repo", value_parser = parsers::repo_naddr)]
|
||||
naddr: Option<Nip19Coordinate>,
|
||||
naddrs: Option<Vec<Nip19Coordinate>>,
|
||||
/// Markdown content for the issue. Cannot be used together with the
|
||||
/// `--editor` flag.
|
||||
#[arg(short, long)]
|
||||
@@ -89,24 +97,54 @@ impl CommandRunner for NewArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
let client = NostrClient::init(&options).await;
|
||||
let user_pubk = options.pubkey().await?;
|
||||
let naddr = utils::naddr_or_file(self.naddr.clone(), &utils::nostr_address_path()?)?;
|
||||
let naddrs = utils::naddrs_or_file(self.naddrs.clone(), &utils::nostr_address_path()?)?;
|
||||
let mut naddrs_iter = naddrs.clone().into_iter();
|
||||
|
||||
client.add_relays(&naddr.relays).await;
|
||||
client.add_relays(&naddrs.extract_relays()).await;
|
||||
|
||||
let relays_list = client.user_relays_list(user_pubk).await?;
|
||||
let mut write_relays =
|
||||
utils::add_write_relays(options.relays.clone(), relays_list.as_ref());
|
||||
write_relays.extend(client.fetch_repo(&naddr.coordinate).await?.relays);
|
||||
write_relays.extend(
|
||||
client
|
||||
.fetch_repos(&naddrs.into_coordinates())
|
||||
.await?
|
||||
.extract_relays(),
|
||||
);
|
||||
|
||||
let (subject, content) = self.issue_content()?;
|
||||
let content_details = client.parse_content(&content).await;
|
||||
write_relays.extend(content_details.write_relays.clone());
|
||||
|
||||
let event =
|
||||
EventBuilder::new_git_issue(naddr.coordinate.clone(), content, subject, self.label)?
|
||||
.tags(content_details.into_tags())
|
||||
.pow(options.pow)
|
||||
.build(user_pubk);
|
||||
// Include read relays for each repository owner (if found)
|
||||
write_relays.extend(
|
||||
future::join_all(
|
||||
naddrs_iter
|
||||
.clone()
|
||||
.map(|c| client.read_relays_from_user(Vec::new(), c.public_key)),
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.flatten(),
|
||||
);
|
||||
|
||||
let event = EventBuilder::new_git_issue(
|
||||
naddrs_iter
|
||||
.next()
|
||||
.expect("There is at least one address")
|
||||
.coordinate
|
||||
.clone(),
|
||||
content,
|
||||
subject,
|
||||
self.label,
|
||||
)?
|
||||
.pow(options.pow)
|
||||
.tags(content_details.into_tags())
|
||||
// p-tag the reset of the reposotoies owners
|
||||
.tags(naddrs_iter.clone().map(|n| Tag::public_key(n.public_key)))
|
||||
// a-tag the reset of the reposotoies
|
||||
.tags(naddrs_iter.map(|n| Tag::coordinate(n.coordinate, n.relays.first().cloned())))
|
||||
.build(user_pubk);
|
||||
let event_id = event.id.expect("There is an id");
|
||||
|
||||
tracing::trace!(relays = ?write_relays, "Write relays list");
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
use std::{fs, str::FromStr};
|
||||
|
||||
use clap::{ArgGroup, Args};
|
||||
use futures::future;
|
||||
use nostr::{
|
||||
event::{Event, EventBuilder, EventId, Kind, Tag},
|
||||
filter::Filter,
|
||||
@@ -31,7 +32,11 @@ use super::{CliOptions, CommandRunner};
|
||||
use crate::{
|
||||
cli::parsers,
|
||||
error::{N34Error, N34Result},
|
||||
nostr_utils::{NostrClient, utils},
|
||||
nostr_utils::{
|
||||
NostrClient,
|
||||
traits::{NaddrsUtils, ReposUtils},
|
||||
utils,
|
||||
},
|
||||
};
|
||||
|
||||
/// Length of a Nostr npub (public key) in characters.
|
||||
@@ -101,7 +106,7 @@ pub struct ReplyArgs {
|
||||
/// If not provided, `n34` will look for the `nostr-address` file and if not
|
||||
/// found, will get it from the root event if found.
|
||||
#[arg(value_name = "NADDR-OR-NIP05", long = "repo", value_parser = parsers::repo_naddr)]
|
||||
naddr: Option<Nip19Coordinate>,
|
||||
naddrs: Option<Vec<Nip19Coordinate>>,
|
||||
/// The comment (cannot be used with --editor)
|
||||
#[arg(short, long)]
|
||||
comment: Option<String>,
|
||||
@@ -116,13 +121,13 @@ impl CommandRunner for ReplyArgs {
|
||||
let client = NostrClient::init(&options).await;
|
||||
let user_pubk = options.pubkey().await?;
|
||||
|
||||
let repo_naddr = if let Some(naddr) = self.naddr {
|
||||
client.add_relays(&naddr.relays).await;
|
||||
Some(naddr.coordinate)
|
||||
let repo_naddrs = if let Some(naddrs) = self.naddrs {
|
||||
client.add_relays(&naddrs.extract_relays()).await;
|
||||
Some(naddrs)
|
||||
} else if fs::exists(&nostr_address_path).is_ok() {
|
||||
let naddr = utils::naddr_or_file(None, &nostr_address_path)?;
|
||||
client.add_relays(&naddr.relays).await;
|
||||
Some(naddr.coordinate)
|
||||
let naddrs = utils::naddrs_or_file(None, &nostr_address_path)?;
|
||||
client.add_relays(&naddrs.extract_relays()).await;
|
||||
Some(naddrs)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -139,16 +144,28 @@ impl CommandRunner for ReplyArgs {
|
||||
.ok_or(N34Error::EventNotFound)?;
|
||||
let root = client.find_root(reply_to.clone()).await?;
|
||||
|
||||
let repo_naddr = if let Some(naddr) = repo_naddr {
|
||||
naddr
|
||||
let repos_coordinate = if let Some(naddrs) = repo_naddrs {
|
||||
naddrs.into_coordinates()
|
||||
} else if let Some(ref root_event) = root {
|
||||
naddr_from_root(root_event)?
|
||||
coordinates_from_root(root_event)?
|
||||
} else {
|
||||
return Err(N34Error::NotFoundRepo);
|
||||
};
|
||||
|
||||
let repo = client.fetch_repo(&repo_naddr).await?;
|
||||
write_relays.extend(repo.relays.clone());
|
||||
let repos = client.fetch_repos(&repos_coordinate).await?;
|
||||
// Merge repository announcement relays into write relays
|
||||
write_relays.extend(repos.extract_relays());
|
||||
// Include read relays for each repository owner (if found)
|
||||
write_relays.extend(
|
||||
future::join_all(
|
||||
repos_coordinate
|
||||
.iter()
|
||||
.map(|c| client.read_relays_from_user(Vec::new(), c.public_key)),
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.flatten(),
|
||||
);
|
||||
|
||||
write_relays = client
|
||||
.read_relays_from_user(write_relays, reply_to.pubkey)
|
||||
@@ -173,11 +190,11 @@ impl CommandRunner for ReplyArgs {
|
||||
content,
|
||||
&reply_to,
|
||||
root.as_ref(),
|
||||
repo.relays.first().cloned(),
|
||||
repos.first().and_then(|r| r.relays.first()).cloned(),
|
||||
)
|
||||
.tag(Tag::public_key(repo_naddr.public_key))
|
||||
.tags(content_details.into_tags())
|
||||
.pow(options.pow)
|
||||
.tags(content_details.into_tags())
|
||||
.tag(Tag::public_key(reply_to.pubkey))
|
||||
.build(user_pubk);
|
||||
|
||||
let event_id = event.id.expect("There is an id");
|
||||
@@ -245,15 +262,19 @@ async fn quote_reply_to_content(client: &NostrClient, quoted_event: &Event) -> S
|
||||
|
||||
/// Gets the repository coordinate from a root Nostr event's tags.
|
||||
/// The event must contain a coordinate tag with GitRepoAnnouncement kind.
|
||||
fn naddr_from_root(root: &Event) -> N34Result<Coordinate> {
|
||||
Ok(root
|
||||
fn coordinates_from_root(root: &Event) -> N34Result<Vec<Coordinate>> {
|
||||
let coordinates: Vec<Coordinate> = root
|
||||
.tags
|
||||
.coordinates()
|
||||
.find(|c| c.kind == Kind::GitRepoAnnouncement)
|
||||
.ok_or_else(|| {
|
||||
N34Error::InvalidEvent(
|
||||
"The Git issue/patch does not specify a target repository".to_owned(),
|
||||
)
|
||||
})?
|
||||
.clone())
|
||||
.filter(|c| c.kind == Kind::GitRepoAnnouncement)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
if coordinates.is_empty() {
|
||||
return Err(N34Error::InvalidEvent(
|
||||
"The Git issue/patch does not specify a target repository".to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(coordinates)
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@
|
||||
// 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::fs;
|
||||
use std::{fs, io::Write};
|
||||
|
||||
use clap::Args;
|
||||
use futures::future;
|
||||
use nostr::{event::EventBuilder, key::PublicKey, types::Url};
|
||||
|
||||
use crate::{
|
||||
@@ -25,6 +26,22 @@ use crate::{
|
||||
nostr_utils::{NostrClient, traits::NewGitRepositoryAnnouncement, utils},
|
||||
};
|
||||
|
||||
/// Header written to new `nostr-address` files. Contains two trailing newline
|
||||
/// for formatting.
|
||||
const NOSTR_ADDRESS_FILE_HEADER: &str = r##"# This file contains NIP-19 `naddr` entities for repositories that accept this
|
||||
# project's issues and patches.
|
||||
#
|
||||
# The file acts as a **read-only reference** for retrieving repository relays
|
||||
# when embedded in an `naddr` and mentions those repositories when opening
|
||||
# patches or issues. Modifications here will not affect in the relays, as the
|
||||
# file is **explicitly untracked**. Its goal is to simplify contributions by
|
||||
# removing the need for manual address entry.
|
||||
#
|
||||
# Each entry must start with "naddr". Embedded relays are **strongly recommended**
|
||||
# to assist client-side discovery.
|
||||
#
|
||||
# Empty lines are ignored. Lines starting with "#" are treated as comments.
|
||||
"##;
|
||||
|
||||
/// Arguments for the `repo announce` command
|
||||
#[derive(Args, Debug)]
|
||||
@@ -66,12 +83,25 @@ impl CommandRunner for AnnounceArgs {
|
||||
let client = NostrClient::init(&options).await;
|
||||
let user_pubk = options.pubkey().await?;
|
||||
let relays_list = client.user_relays_list(user_pubk).await?;
|
||||
let write_relays = utils::add_write_relays(options.relays.clone(), relays_list.as_ref());
|
||||
let mut write_relays =
|
||||
utils::add_write_relays(options.relays.clone(), relays_list.as_ref());
|
||||
|
||||
if !self.maintainers.contains(&user_pubk) {
|
||||
self.maintainers.insert(0, user_pubk);
|
||||
}
|
||||
|
||||
// Include read relays for each maintainer (if found)
|
||||
write_relays.extend(
|
||||
future::join_all(
|
||||
self.maintainers
|
||||
.iter()
|
||||
.map(|pkey| client.read_relays_from_user(Vec::new(), *pkey)),
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.flatten(),
|
||||
);
|
||||
|
||||
let event = EventBuilder::new_git_repo(
|
||||
self.repo_id,
|
||||
self.name.map(utils::str_trim),
|
||||
@@ -90,9 +120,21 @@ impl CommandRunner for AnnounceArgs {
|
||||
let naddr = utils::repo_naddr(user_pubk, &options.relays)?;
|
||||
|
||||
if self.address_file {
|
||||
let path = std::env::current_dir()?.join(NOSTR_ADDRESS_FILE);
|
||||
tracing::info!("Create the `nostr-address` file at `{}`", path.display());
|
||||
fs::write(path, &naddr)?;
|
||||
let address_path = std::env::current_dir()?.join(NOSTR_ADDRESS_FILE);
|
||||
if !address_path.exists() {
|
||||
tracing::info!(
|
||||
"Creating new address file: '{NOSTR_ADDRESS_FILE}' at path '{}' with default \
|
||||
header",
|
||||
address_path.display()
|
||||
);
|
||||
fs::write(&address_path, NOSTR_ADDRESS_FILE_HEADER)?;
|
||||
}
|
||||
|
||||
let mut file = fs::OpenOptions::new().append(true).open(&address_path)?;
|
||||
|
||||
tracing::info!("Appending naddr '{naddr}' to address file: '{NOSTR_ADDRESS_FILE}'",);
|
||||
file.write_all(naddr.as_bytes())?;
|
||||
tracing::info!("Successfully wrote naddr to address file");
|
||||
}
|
||||
|
||||
client
|
||||
|
||||
@@ -22,7 +22,7 @@ use nostr::nips::nip19::Nip19Coordinate;
|
||||
use crate::{
|
||||
cli::{CliOptions, CommandRunner, parsers},
|
||||
error::N34Result,
|
||||
nostr_utils::{NostrClient, utils},
|
||||
nostr_utils::{NostrClient, traits::NaddrsUtils, utils},
|
||||
};
|
||||
|
||||
/// Arguments for the `repo view` command
|
||||
@@ -33,46 +33,51 @@ pub struct ViewArgs {
|
||||
///
|
||||
/// If not provided, `n34` will look for the `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-OR-NIP05", long = "repo", value_parser = parsers::repo_naddr)]
|
||||
naddr: Option<Nip19Coordinate>,
|
||||
naddrs: Option<Vec<Nip19Coordinate>>,
|
||||
}
|
||||
|
||||
impl CommandRunner for ViewArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
// FIXME: The signer is not required here
|
||||
|
||||
let naddr = utils::naddr_or_file(self.naddr, &utils::nostr_address_path()?)?;
|
||||
let naddrs = utils::naddrs_or_file(self.naddrs, &utils::nostr_address_path()?)?;
|
||||
let client = NostrClient::init(&options).await;
|
||||
client.add_relays(&naddr.relays).await;
|
||||
client.add_relays(&naddrs.extract_relays()).await;
|
||||
|
||||
let repo = client.fetch_repo(&naddr.coordinate).await?;
|
||||
let mut msg = format!("ID: {}", repo.id);
|
||||
let repos = client.fetch_repos(&naddrs.into_coordinates()).await?;
|
||||
let mut repos_details: Vec<String> = Vec::new();
|
||||
|
||||
if let Some(name) = repo.name {
|
||||
msg.push_str(&format!("\nName: {name}"));
|
||||
}
|
||||
if let Some(desc) = repo.description {
|
||||
msg.push_str(&format!("\nDescription: {desc}"));
|
||||
}
|
||||
if !repo.web.is_empty() {
|
||||
msg.push_str(&format!("\nWebpages:\n{}", format_list(repo.web)));
|
||||
}
|
||||
if !repo.clone.is_empty() {
|
||||
msg.push_str(&format!("\nClone urls:\n{}", format_list(repo.clone)));
|
||||
}
|
||||
if !repo.relays.is_empty() {
|
||||
msg.push_str(&format!("\nRelays:\n{}", format_list(repo.relays)));
|
||||
}
|
||||
if let Some(euc) = repo.euc {
|
||||
msg.push_str(&format!("\nEarliest unique commit: {euc}"));
|
||||
}
|
||||
if !repo.maintainers.is_empty() {
|
||||
msg.push_str(&format!(
|
||||
"\nMaintainers:\n{}",
|
||||
format_list(repo.maintainers)
|
||||
));
|
||||
for repo in repos {
|
||||
let mut repo_details = format!("ID: {}", repo.id);
|
||||
|
||||
if let Some(name) = repo.name {
|
||||
repo_details.push_str(&format!("\nName: {name}"));
|
||||
}
|
||||
if let Some(desc) = repo.description {
|
||||
repo_details.push_str(&format!("\nDescription: {desc}"));
|
||||
}
|
||||
if !repo.web.is_empty() {
|
||||
repo_details.push_str(&format!("\nWebpages:\n{}", format_list(repo.web)));
|
||||
}
|
||||
if !repo.clone.is_empty() {
|
||||
repo_details.push_str(&format!("\nClone urls:\n{}", format_list(repo.clone)));
|
||||
}
|
||||
if !repo.relays.is_empty() {
|
||||
repo_details.push_str(&format!("\nRelays:\n{}", format_list(repo.relays)));
|
||||
}
|
||||
if let Some(euc) = repo.euc {
|
||||
repo_details.push_str(&format!("\nEarliest unique commit: {euc}"));
|
||||
}
|
||||
if !repo.maintainers.is_empty() {
|
||||
repo_details.push_str(&format!(
|
||||
"\nMaintainers:\n{}",
|
||||
format_list(repo.maintainers)
|
||||
));
|
||||
}
|
||||
repos_details.push(repo_details);
|
||||
}
|
||||
|
||||
println!("{msg}");
|
||||
println!("{}", repos_details.join("\n----------\n"));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user