diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-20 09:59:14 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-20 10:17:27 +0000 |
| commit | 316f858f223162408cfd52183ef7645828c2f480 (patch) | |
| tree | 4f275320ad776ed4fcc85a1d75b2ac2c46c63609 /src/lib | |
| parent | 8cc4a21061433c9a29e36c4f33e4d84bea1defee (diff) | |
refactor: branch_name handling
improve clarity by renaming variables and methods
defend against `branch-name` tag with an unsafe name
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/git_events.rs | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/src/lib/git_events.rs b/src/lib/git_events.rs index af469d3..559155a 100644 --- a/src/lib/git_events.rs +++ b/src/lib/git_events.rs | |||
| @@ -453,15 +453,15 @@ pub async fn generate_cover_letter_and_patch_events( | |||
| 453 | pub struct CoverLetter { | 453 | pub struct CoverLetter { |
| 454 | pub title: String, | 454 | pub title: String, |
| 455 | pub description: String, | 455 | pub description: String, |
| 456 | pub branch_name: String, | 456 | pub branch_name_without_id_or_prefix: String, |
| 457 | pub event_id: Option<nostr::EventId>, | 457 | pub event_id: Option<nostr::EventId>, |
| 458 | } | 458 | } |
| 459 | 459 | ||
| 460 | impl CoverLetter { | 460 | impl CoverLetter { |
| 461 | pub fn get_branch_name(&self) -> Result<String> { | 461 | pub fn get_branch_name_with_pr_prefix_and_shorthand_id(&self) -> Result<String> { |
| 462 | Ok(format!( | 462 | Ok(format!( |
| 463 | "pr/{}({})", | 463 | "pr/{}({})", |
| 464 | self.branch_name, | 464 | self.branch_name_without_id_or_prefix, |
| 465 | &self | 465 | &self |
| 466 | .event_id | 466 | .event_id |
| 467 | .context("proposal root event_id must be know to get it's branch name")? | 467 | .context("proposal root event_id must be know to get it's branch name")? |
| @@ -520,39 +520,33 @@ pub fn event_to_cover_letter(event: &nostr::Event) -> Result<CoverLetter> { | |||
| 520 | Ok(CoverLetter { | 520 | Ok(CoverLetter { |
| 521 | title: title.clone(), | 521 | title: title.clone(), |
| 522 | description, | 522 | description, |
| 523 | // TODO should this be prefixed by format!("{}-"e.id.to_string()[..5]?) | 523 | branch_name_without_id_or_prefix: if let Ok(name) = tag_value(event, "branch-name") { |
| 524 | branch_name: if let Ok(name) = match tag_value(event, "branch-name") { | 524 | if !name.eq("main") && !name.eq("master") { |
| 525 | Ok(name) => { | 525 | safe_branch_name_for_pr(&name) |
| 526 | if !name.eq("main") && !name.eq("master") { | 526 | } else { |
| 527 | Ok(name.chars().take(60).collect::<String>()) | 527 | safe_branch_name_for_pr(&title) |
| 528 | } else { | ||
| 529 | Err(()) | ||
| 530 | } | ||
| 531 | } | 528 | } |
| 532 | _ => Err(()), | ||
| 533 | } { | ||
| 534 | name | ||
| 535 | } else { | 529 | } else { |
| 536 | let s = title | 530 | safe_branch_name_for_pr(&title) |
| 537 | .replace(' ', "-") | 531 | }, |
| 538 | .chars() | ||
| 539 | .map(|c| { | ||
| 540 | if c.is_ascii_alphanumeric() || c.eq(&'/') { | ||
| 541 | c | ||
| 542 | } else { | ||
| 543 | '-' | ||
| 544 | } | ||
| 545 | }) | ||
| 546 | .collect(); | ||
| 547 | s | ||
| 548 | } | ||
| 549 | .chars() | ||
| 550 | .take(60) | ||
| 551 | .collect(), | ||
| 552 | event_id: Some(event.id), | 532 | event_id: Some(event.id), |
| 553 | }) | 533 | }) |
| 554 | } | 534 | } |
| 555 | 535 | ||
| 536 | fn safe_branch_name_for_pr(s: &str) -> String { | ||
| 537 | s.replace(' ', "-") | ||
| 538 | .chars() | ||
| 539 | .map(|c| { | ||
| 540 | if c.is_ascii_alphanumeric() || c.eq(&'/') { | ||
| 541 | c | ||
| 542 | } else { | ||
| 543 | '-' | ||
| 544 | } | ||
| 545 | }) | ||
| 546 | .take(60) | ||
| 547 | .collect() | ||
| 548 | } | ||
| 549 | |||
| 556 | pub fn get_most_recent_patch_with_ancestors( | 550 | pub fn get_most_recent_patch_with_ancestors( |
| 557 | mut patches: Vec<nostr::Event>, | 551 | mut patches: Vec<nostr::Event>, |
| 558 | ) -> Result<Vec<nostr::Event>> { | 552 | ) -> Result<Vec<nostr::Event>> { |
| @@ -622,9 +616,10 @@ pub fn is_event_proposal_root_for_branch( | |||
| 622 | let branch_name = branch_name_or_refstr.replace("refs/heads/", ""); | 616 | let branch_name = branch_name_or_refstr.replace("refs/heads/", ""); |
| 623 | Ok(event_to_cover_letter(e).is_ok_and(|cl| { | 617 | Ok(event_to_cover_letter(e).is_ok_and(|cl| { |
| 624 | (logged_in_user.is_some_and(|public_key| e.pubkey.eq(public_key)) | 618 | (logged_in_user.is_some_and(|public_key| e.pubkey.eq(public_key)) |
| 625 | && (branch_name.eq(&format!("pr/{}", cl.branch_name)) | 619 | && branch_name.eq(&format!("pr/{}", cl.branch_name_without_id_or_prefix))) |
| 626 | || cl.branch_name.eq(&branch_name))) | 620 | || cl |
| 627 | || cl.get_branch_name().is_ok_and(|s| s.eq(&branch_name)) | 621 | .get_branch_name_with_pr_prefix_and_shorthand_id() |
| 622 | .is_ok_and(|s| s.eq(&branch_name)) | ||
| 628 | }) && !event_is_revision_root(e)) | 623 | }) && !event_is_revision_root(e)) |
| 629 | } | 624 | } |
| 630 | 625 | ||