Skip to content

Commit

Permalink
Bugreport auto issue (#862)
Browse files Browse the repository at this point in the history
* adds urlencoding as a dependency and generates github issue URLs based on sysinfo

* removes support for multiple templates, sending reports only to 3-bug-report

* makes the cli more clear about adding sensitive information to the issue

* makes the cli output prettier while respecting stuff like no_color

* changes color to Cyan

* applies code improvements

* runs cargo fmt
  • Loading branch information
PedroTurik authored Aug 21, 2024
1 parent c2c106a commit 74635ee
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ rustc_version = "0.4.0"
rayon = "1.8.0"
anstyle = "1.0.6"
anstream = "0.6.13"
urlencoding = "2.1.3"
cargo-config2 = "0.1.26"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
84 changes: 74 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

use std::{env, path::PathBuf};

use anstyle::{AnsiColor, Color, Reset, Style};
use cargo_config2::Config;
use cargo_semver_checks::{
GlobalConfig, PackageSelection, ReleaseType, Rustdoc, ScopeSelection, SemverQuery,
};
use clap::{Args, Parser, Subcommand};
use std::io::Write;

#[cfg(test)]
mod snapshot_tests;
Expand All @@ -16,16 +19,10 @@ fn main() {
let Cargo::SemverChecks(args) = Cargo::parse();

configure_color(args.color_choice);
let mut config = GlobalConfig::new();

if args.bugreport {
use bugreport::{bugreport, collector::*, format::Markdown};
bugreport!()
.info(SoftwareVersion::default())
.info(OperatingSystem::default())
.info(CommandLine::default())
.info(CommandOutput::new("cargo version", "cargo", &["-V"]))
.info(CompileTimeInformation::default())
.print::<Markdown>();
print_issue_url(&mut config);
std::process::exit(0);
} else if args.list {
exit_on_error(true, || {
Expand Down Expand Up @@ -93,8 +90,6 @@ fn main() {
None => args.check_release,
};

let mut config = GlobalConfig::new();

config.set_log_level(check_release.verbosity.log_level());

let check: cargo_semver_checks::Check = check_release.into();
Expand Down Expand Up @@ -146,6 +141,75 @@ fn configure_color(cli_choice: Option<clap::ColorChoice>) {
choice.write_global();
}

fn print_issue_url(config: &mut GlobalConfig) {
use bugreport::{bugreport, collector::*, format::Markdown};
let other_bug_url: &str = "https://github.com/obi1kenobi/cargo-semver-checks/issues/new?labels=C-bug&template=3-bug-report.yml";

let mut bug_report = bugreport!()
.info(SoftwareVersion::default())
.info(OperatingSystem::default())
.info(CommandLine::default())
.info(CommandOutput::new("cargo version", "cargo", &["-V"]))
.info(CompileTimeInformation::default());

let bold_cyan = Style::new()
.bold()
.fg_color(Some(Color::Ansi(AnsiColor::Cyan)));

writeln!(
config.stdout(),
"{bold_cyan}\
System information:{Reset}\n\
-------------------"
)
.expect("Failed to print bug report system information to stdout");
bug_report.print::<Markdown>();

let bug_report = bug_report.format::<Markdown>();
let bug_report_url = urlencoding::encode(&bug_report);

let cargo_config = match Config::load() {
Ok(c) => toml::to_string(&c).unwrap_or_else(|s| {
writeln!(
config.stderr(),
"Error serializing cargo build configuration: {}",
s
)
.expect("Failed to print error");
String::default()
}),
Err(e) => {
writeln!(
config.stderr(),
"Error loading cargo build configuration: {}",
e
)
.expect("Failed to print error");
String::default()
}
};

writeln!(
config.stdout(),
"{bold_cyan}\
Cargo build configuration:{Reset}\n\
--------------------------\n\
{cargo_config}"
)
.expect("Failed to print bug report Cargo configuration to stdout");

let cargo_config_url: String = urlencoding::encode(&cargo_config).into_owned();

let bold = Style::new().bold();
writeln!(
config.stdout(),
"{bold}Please file an issue on GitHub reporting your bug.\n\
Consider adding the diagnostic information above, either manually or automatically through the link below:{Reset}\n\n\
{other_bug_url}&sys-info={bug_report_url}&build-config={cargo_config_url}",
)
.expect("Failed to print bug report generated github issue link");
}

#[derive(Debug, Parser)]
#[command(name = "cargo")]
#[command(bin_name = "cargo")]
Expand Down

0 comments on commit 74635ee

Please sign in to comment.