forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

commit_status.rs190 lines, 6.7 KiBHistoryRawDownload
1//! Commit status indicators. `commit_status` is the combined state as an icon, with a dropdown of
2//! every context (the commit list, commit page and PR commits tab). `checks_summary` is the line in
3//! the pull request merge box.
4
5use forge_core::store::commit_status::{CommitStatus, StatusState, combine};
6use topcoat::Result;
7use topcoat::context::Cx;
8use topcoat::view::{View, component, view};
9
10use crate::i18n::Lang;
11use crate::i18n::code::{self as g, K};
12
13/// The language of the status indicators. It defaults to English, so a caller that does not pass
14/// one (the pull request page, for now) keeps its English text.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct StatusLang(pub Lang);
17
18impl Default for StatusLang {
19 fn default() -> Self {
20 StatusLang(Lang::En)
21 }
22}
23
24/// One context's report, prepared for display.
25#[derive(Clone)]
26pub struct StatusItem {
27 pub context: String,
28 /// The state's icon, and the CSS classes that colour it.
29 pub icon: &'static str,
30 pub icon_class: String,
31 pub description: String,
32 pub has_description: bool,
33 pub link: String,
34 pub has_link: bool,
35}
36
37impl From<&CommitStatus> for StatusItem {
38 fn from(status: &CommitStatus) -> Self {
39 let description = status.description.clone().unwrap_or_default();
40 let link = status.target_url.clone().unwrap_or_default();
41 Self {
42 context: status.context.clone(),
43 icon: icon(status.state),
44 icon_class: format!("status-icon status-{}", css(status.state)),
45 has_description: !description.is_empty(),
46 description,
47 has_link: !link.is_empty(),
48 link,
49 }
50 }
51}
52
53/// Everything the indicators show about one commit.
54#[derive(Clone, Default)]
55pub struct StatusSummary {
56 /// Combined state, `None` when the commit has no statuses.
57 pub state: Option<StatusState>,
58 pub items: Vec<StatusItem>,
59}
60
61impl StatusSummary {
62 pub fn from_rows(rows: &[CommitStatus]) -> Self {
63 Self {
64 state: combine(rows.iter().map(|row| row.state)),
65 items: rows.iter().map(StatusItem::from).collect(),
66 }
67 }
68}
69
70/// The icon for a state: check, cross or dot.
71pub fn icon(state: StatusState) -> &'static str {
72 match state {
73 StatusState::Success => "✓",
74 StatusState::Failure | StatusState::Error => "✗",
75 StatusState::Pending => "●",
76 }
77}
78
79/// CSS modifier for a state. `error` shares the failure colour.
80pub fn css(state: StatusState) -> &'static str {
81 match state {
82 StatusState::Success => "success",
83 StatusState::Failure | StatusState::Error => "failure",
84 StatusState::Pending => "pending",
85 }
86}
87
88/// The combined icon with a dropdown listing every context. Renders nothing when the commit has
89/// no statuses.
90#[component]
91pub async fn commit_status(
92 cx: &Cx,
93 summary: StatusSummary,
94 #[default] lang: StatusLang,
95) -> Result<impl View> {
96 let lang = lang.0;
97 let has_state = summary.state.is_some();
98 let state = summary.state.unwrap_or(StatusState::Pending);
99 let icon_text = icon(state);
100 let css_class = format!("status-icon status-{}", css(state));
101 let title = format!(
102 "{}{}",
103 g::t(lang, K::CommitStatusPrefix),
104 g::state_word(lang, state)
105 );
106 let items = summary.items;
107 Ok(view! {
108 if has_state {
109 <details class="commit-status">
110 <summary class=(css_class.as_str()) title=(title.as_str()) aria-label=(title.as_str())>
111 <span aria-hidden="true">(icon_text)</span>
112 </summary>
113 status_list(items: items, lang: lang)
114 </details>
115 }
116 })
117}
118
119/// The merge box line: "All checks have passed", "Some checks failed" or "Checks pending", with
120/// the contexts beneath it.
121#[component]
122pub async fn checks_summary(
123 cx: &Cx,
124 summary: StatusSummary,
125 #[default] lang: StatusLang,
126) -> Result<impl View> {
127 let lang = lang.0;
128 let (text, state) = match summary.state {
129 None => (g::t(lang, K::NoChecksReported), None),
130 Some(StatusState::Success) => (g::t(lang, K::AllChecksPassed), Some(StatusState::Success)),
131 Some(StatusState::Pending) => (g::t(lang, K::ChecksPending), Some(StatusState::Pending)),
132 Some(StatusState::Failure) | Some(StatusState::Error) => {
133 (g::t(lang, K::SomeChecksFailed), Some(StatusState::Failure))
134 }
135 };
136 let icon_text = state.map_or("", icon);
137 let css_class = format!("checks-summary status-{}", state.map_or("none", css));
138 let items = summary.items;
139 let has_items = !items.is_empty();
140 Ok(view! {
141 <div class=(css_class.as_str())>
142 <span class="status-icon" aria-hidden="true">(icon_text)</span>
143 <strong>(text)</strong>
144 if has_items {
145 <details class="checks-details">
146 <summary class="act">(g::t(lang, K::ShowChecks))</summary>
147 status_list(items: items, lang: lang)
148 </details>
149 }
150 </div>
151 })
152}
153
154/// A status description in the viewer's language. CI's own descriptions are stored in English;
155/// what an outside tool reports stays as it sent it.
156fn description_text(lang: Lang, text: &str) -> &str {
157 use crate::i18n::s;
158 match text {
159 "Waiting for approval" => s(lang, "Waiting for approval", "Onay bekliyor"),
160 "Waiting for other jobs" => s(lang, "Waiting for other jobs", "Diğer işleri bekliyor"),
161 "Queued" => s(lang, "Queued", "Sırada"),
162 "Running" => s(lang, "Running", "Çalışıyor"),
163 "Succeeded" => s(lang, "Succeeded", "Başarılı"),
164 "Failed" => s(lang, "Failed", "Başarısız"),
165 "Cancelled" => s(lang, "Cancelled", "İptal edildi"),
166 "Errored" => s(lang, "Errored", "Hata verdi"),
167 other => other,
168 }
169}
170
171/// The contexts of one commit: icon, context name, description and a details link.
172#[component]
173pub async fn status_list(cx: &Cx, items: Vec<StatusItem>, lang: Lang) -> Result<impl View> {
174 Ok(view! {
175 <ul class="status-list">
176 for item in items.iter() {
177 <li class="status-item">
178 <span class=(item.icon_class.as_str())>(item.icon)</span>
179 <code>(item.context.as_str())</code>
180 if item.has_description {
181 <span class="hint">(description_text(lang, item.description.as_str()))</span>
182 }
183 if item.has_link {
184 <a class="act" href=(item.link.as_str()) rel="nofollow noopener noreferrer">(g::t(lang, K::DetailsLink))</a>
185 }
186 </li>
187 }
188 </ul>
189 })
190}