knope 0.23.0

A command line tool for automating common development tasks
#![allow(dead_code)]
//! Shared helpers for GitHub and Gitea integration test modules.

use std::{path::Path, process::Command};

/// Redact any embedded credentials from text that may contain URLs,
/// to avoid leaking tokens in test failure messages.
pub(crate) fn redact_url_credentials(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut remaining = s;
    while let Some(scheme_end) = remaining.find("://") {
        result.push_str(&remaining[..scheme_end + 3]);
        remaining = &remaining[scheme_end + 3..];
        if let Some(at_pos) = remaining.find('@') {
            result.push_str("<redacted>");
            remaining = &remaining[at_pos..];
        }
    }
    result.push_str(remaining);
    result
}

#[allow(clippy::expect_used)]
pub(crate) fn push_branch(dir: &Path, branch: &str) {
    let output = Command::new("git")
        .args(["push", "--set-upstream", "origin", branch, "--force"])
        .current_dir(dir)
        .output()
        .expect("Failed to run git push");
    assert!(
        output.status.success(),
        "git push failed:\nstdout: {}\nstderr: {}",
        redact_url_credentials(&String::from_utf8_lossy(&output.stdout)),
        redact_url_credentials(&String::from_utf8_lossy(&output.stderr))
    );
}