Files
CommandForge/src/CommandForge.Wpf/ViewModels/ReportBugViewModel.cs
T
poli0981andClaude Opus 5.5 cad42ac9cf fix: updates from Cloudflare R2, links to Gitea and the GitLab mirror
The GitHub account is gone, and with it the update feed and every link the
app opened. Velopack now reads https://releases.poli0981.dev/commandforge
(an HTTP source, ProductLinks.UpdateFeed); 404 / 403 / 429 / 5xx / offline
are still told apart, and their messages name "the update server" instead of
GitHub (Cloudflare's edge answers 403 when it refuses and 429 when it
limits).

ProductLinks: the source, user guide, accessibility statement and the four
legal texts on Gitea (the Legal pages read them from ProductLinks instead of
four hard-coded GitHub URLs); issues on the GitLab mirror, since Gitea's
registration is closed. Report a bug opens a new GitLab issue with the title
and the answered Bug template (.gitlab/issue_templates/Bug.md) as its
description; a test keeps the app's sections and checklist equal to the
template's.

Strings in all five languages: "Source code" (was "GitHub"), "Read the full
text online", "Open issue on GitLab", and the privacy and auto-check texts
name releases.poli0981.dev. Keys Settings_GitHub and LegalGateReadOnGitHub
become Settings_SourceCode and LegalGateReadFullText. A new test fails if any
source file links to github.com/poli0981 again.

The terms version stays 1.1: the update check sends the same request, only
to another host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 09:18:05 +07:00

176 lines
7.4 KiB
C#

using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using CommandForge.Application;
using CommandForge.Application.Ports;
using CommandForge.Desktop.Resources;
using CommandForge.Desktop.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace CommandForge.Desktop.ViewModels;
/// <summary>
/// Drives the Report-a-bug dialog: shows auto-gathered environment, exports logs, and opens a
/// prefilled issue on the GitLab mirror. User-initiated only — nothing is sent automatically (Privacy).
/// </summary>
public sealed partial class ReportBugViewModel : ObservableObject
{
// Browsers and servers cap URL length; the body beyond this is left for the user to paste.
private const int MaxBodyCharsInUrl = 4000;
/// <summary>The issue template the report follows: .gitlab/issue_templates/Bug.md on the GitLab mirror.</summary>
public const string BugReportTemplate = "Bug";
/// <summary>The checklist at the end of the Bug template, left for the reporter to tick.</summary>
public static readonly string[] Confirmations =
[
"- [ ] I'm using the latest release.",
"- [ ] This is not a security vulnerability (those go to " + ProductLinks.SecurityEmail + ").",
];
private readonly ILogMaintenance _maintenance;
private readonly IDialogService _dialogs;
private readonly IFileDialogService _files;
private readonly IShellService _shell;
private readonly string? _errorCode;
public ReportBugViewModel(
ILogMaintenance maintenance,
IDialogService dialogs,
IFileDialogService files,
IShellService shell,
string? errorCode = null,
string? errorDetails = null)
{
ArgumentNullException.ThrowIfNull(maintenance);
ArgumentNullException.ThrowIfNull(dialogs);
ArgumentNullException.ThrowIfNull(files);
ArgumentNullException.ThrowIfNull(shell);
_maintenance = maintenance;
_dialogs = dialogs;
_files = files;
_shell = shell;
_errorCode = errorCode;
EnvironmentText = BuildEnvironment(AppVersion(), RuntimeInformation.OSDescription, RuntimeInformation.OSArchitecture);
_description = errorDetails ?? string.Empty;
}
public string EnvironmentText { get; }
[ObservableProperty]
private string _description;
public event Action? CloseRequested;
[RelayCommand]
private async Task ExportLogAsync()
{
var title = Strings.Get("ReportBug_ExportLog");
var path = _files.PickSaveFile(title, Strings.Get("FileFilter_Zip"), $"commandforge-logs-{DateTime.Now:yyyyMMdd-HHmmss}.zip");
if (path is null)
{
return;
}
try
{
await _maintenance.ExportZipAsync(path);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
await _dialogs.ShowMessageAsync(title, string.Format(CultureInfo.CurrentCulture, Strings.Get("File_SaveFailed"), ex.Message), DialogKind.Error);
return;
}
await _dialogs.ShowMessageAsync(
Strings.Get("ReportBug_Title"),
string.Format(CultureInfo.CurrentCulture, Strings.Get("Log_ExportSuccess"), path)
+ Environment.NewLine + Strings.Get("ReportBug_ReviewReminder"));
}
[RelayCommand]
private void OpenIssue()
=> _shell.TryOpen(BuildIssueUrl(
BuildTitle(),
AppVersion(),
$"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})",
Description,
_errorCode));
/// <summary>
/// Pure: the address of a new issue on the GitLab mirror with its title and description filled
/// in. The description is the Bug template (.gitlab/issue_templates/Bug.md) with its sections
/// answered; GitLab shows it as is (a template picked in the URL would replace it).
/// </summary>
public static string BuildIssueUrl(string title, string appVersion, string windowsVersion, string? userDescription, string? errorCode)
{
ArgumentNullException.ThrowIfNull(title);
ArgumentNullException.ThrowIfNull(appVersion);
ArgumentNullException.ThrowIfNull(windowsVersion);
(string Field, string Value)[] fields =
[
("issue[title]", title),
("issue[description]", BuildIssueBody(appVersion, windowsVersion, userDescription, errorCode)),
];
return ProductLinks.NewIssue + "?" + string.Join('&', fields.Select(field => Uri.EscapeDataString(field.Field) + "=" + Uri.EscapeDataString(field.Value)));
}
/// <summary>Pure: the Bug template's sections, in its order, with what the app knows filled in.</summary>
public static string BuildIssueBody(string appVersion, string windowsVersion, string? userDescription, string? errorCode)
{
ArgumentNullException.ThrowIfNull(appVersion);
ArgumentNullException.ThrowIfNull(windowsVersion);
string[] lines =
[
"## What happened?", string.Empty, TruncateForUrl(BuildWhatHappened(userDescription, errorCode)), string.Empty,
"## Steps to reproduce", string.Empty, "1. ", string.Empty,
"## CommandForge version", string.Empty, appVersion, string.Empty,
"## Windows version / build", string.Empty, windowsVersion, string.Empty,
"## Logs (optional)", string.Empty, "_Attach the exported .zip here. Review its contents first — it may contain command output._", string.Empty,
"## Confirmations", string.Empty, .. Confirmations,
];
return string.Join('\n', lines);
}
/// <summary>Pure: the form's "What happened?" text: the user's words (or a prompt) and a crash's error code.</summary>
public static string BuildWhatHappened(string? userDescription, string? errorCode)
{
var text = string.IsNullOrWhiteSpace(userDescription) ? "_Describe what happened._" : userDescription.Trim();
return string.IsNullOrWhiteSpace(errorCode)
? text
: text + Environment.NewLine + Environment.NewLine + "Error code: " + errorCode;
}
/// <summary>Pure: keeps a prefilled issue body within URL limits (a full stack trace can exceed them).</summary>
public static string TruncateForUrl(string body)
{
ArgumentNullException.ThrowIfNull(body);
return body.Length <= MaxBodyCharsInUrl
? body
: body[..MaxBodyCharsInUrl] + Environment.NewLine + Environment.NewLine + "_(truncated — please paste the rest of the details here)_";
}
[RelayCommand]
private void Close() => CloseRequested?.Invoke();
/// <summary>Pure: assembles the environment block (testable).</summary>
public static string BuildEnvironment(string appVersion, string osDescription, Architecture osArchitecture)
{
var builder = new StringBuilder();
builder.AppendLine($"App version: {appVersion}");
builder.AppendLine($"OS: {osDescription}");
builder.AppendLine($"Architecture: {osArchitecture}");
return builder.ToString();
}
private string BuildTitle() => _errorCode is null ? "[Bug] " : $"[Crash {_errorCode}] ";
private static string AppVersion()
{
var version = typeof(ReportBugViewModel).Assembly.GetName().Version;
return version is null ? "—" : $"{version.Major}.{version.Minor}.{version.Build}";
}
}