Skip to main content

subspace_client_github/
issue_report.rs

1//! Reviewed issue reports for the repository the app names. No UI or image
2//! I/O.
3
4mod browser;
5mod diagnostics;
6mod draft;
7mod error;
8mod http;
9mod repository;
10mod submission;
11
12pub use browser::{BrowserFallback, BrowserMode, MAX_BROWSER_URL_BYTES};
13pub use diagnostics::Diagnostics;
14pub use draft::{Draft, IssueType};
15pub use error::{Error, ErrorKind};
16pub use repository::IssueRepository;
17pub use submission::{CreatedIssue, Outcome, ReportPlan, TransportKind};
18pub use subspace_core::diagnostics::IssuePaths;
19
20const MAX_BODY_CHARS: usize = 65_536;
21
22/// Immutable text snapshot. Editing the form requires preparing a new snapshot.
23#[derive(Debug, Clone)]
24// qual:allow(srp, god_struct) reason: "One validated report snapshot owns its title, exact body, type and browser fallback projection together."
25pub struct PreparedReport {
26    title: String,
27    body: String,
28    user_body: String,
29    diagnostic_lines: Vec<String>,
30    issue_type: IssueType,
31    product_id: String,
32    repository: IssueRepository,
33}
34
35impl PreparedReport {
36    // qual:allow(dry, boilerplate) reason: "The validated title must remain immutable after user review."
37    pub fn title(&self) -> &str {
38        &self.title
39    }
40
41    // qual:allow(dry, boilerplate) reason: "The exact reviewed body cannot be replaced independently of its fallback projection."
42    pub fn body(&self) -> &str {
43        &self.body
44    }
45
46    // qual:allow(dry, boilerplate) reason: "The type remains bound to the reviewed Markdown and requested labels."
47    pub fn issue_type(&self) -> IssueType {
48        self.issue_type
49    }
50
51    /// The repository the report files into.
52    // qual:allow(dry, boilerplate) reason: "The repository stays bound to the reviewed report it was prepared for."
53    pub fn repository(&self) -> &IssueRepository {
54        &self.repository
55    }
56
57    pub fn requested_labels(&self) -> [String; 2] {
58        [
59            self.issue_type.label().into(),
60            format!("source:{}", self.product_id),
61        ]
62    }
63
64    pub fn browser_fallback(&self) -> BrowserFallback {
65        browser::prepare(self)
66    }
67}
68
69/// Validate form fields and construct the exact API title and Markdown body
70/// of a report into `repository`.
71pub fn prepare(
72    draft: &Draft,
73    diagnostics: &Diagnostics,
74    repository: &IssueRepository,
75) -> Result<PreparedReport, Error> {
76    let title = draft::validated_title(draft)?;
77    let diagnostic_lines = diagnostics::lines(diagnostics)?;
78    let user_body = draft::body(draft);
79    let body = body_with_diagnostics(&user_body, &diagnostic_lines);
80    if body.chars().count() > MAX_BODY_CHARS {
81        return Err(ErrorKind::BodyTooLong.into());
82    }
83    Ok(PreparedReport {
84        title,
85        body,
86        user_body,
87        diagnostic_lines,
88        issue_type: draft.issue_type(),
89        product_id: diagnostics.product_id().into(),
90        repository: repository.clone(),
91    })
92}
93
94fn body_with_diagnostics(user_body: &str, lines: &[String]) -> String {
95    if lines.is_empty() {
96        return user_body.into();
97    }
98    format!("{user_body}\n\n## Diagnostics\n\n{}", lines.join("\n"))
99}
100
101#[cfg(test)]
102mod tests;