Skip to content

Commit 0a56ff7

Browse files
author
Koenraad Verheyden
committed
When a build fails, display a message that links to last successful build
1 parent 9bfa9ea commit 0a56ff7

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/test/fakes.rs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ impl<'db> FakeRelease<'db> {
7171
self
7272
}
7373

74+
pub(crate) fn build_result_successful(mut self, new: bool) -> Self {
75+
self.build_result.successful = new;
76+
self
77+
}
78+
7479
pub(crate) fn create(self) -> Result<i32, Error> {
7580
let tempdir = tempdir::TempDir::new("docs.rs-fake")?;
7681

src/web/crate_details.rs

+65
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct CrateDetails {
3131
rustdoc: Option<String>, // this is description_long in database
3232
release_time: time::Timespec,
3333
build_status: bool,
34+
last_successful_build: Option<String>,
3435
rustdoc_status: bool,
3536
repository_url: Option<String>,
3637
homepage_url: Option<String>,
@@ -69,6 +70,7 @@ impl ToJson for CrateDetails {
6970
m.insert("release_time".to_string(),
7071
duration_to_str(self.release_time).to_json());
7172
m.insert("build_status".to_string(), self.build_status.to_json());
73+
m.insert("last_successful_build".to_string(), self.last_successful_build.to_json());
7274
m.insert("rustdoc_status".to_string(), self.rustdoc_status.to_json());
7375
m.insert("repository_url".to_string(), self.repository_url.to_json());
7476
m.insert("homepage_url".to_string(), self.homepage_url.to_json());
@@ -172,6 +174,7 @@ impl CrateDetails {
172174
rustdoc: rows.get(0).get(8),
173175
release_time: rows.get(0).get(9),
174176
build_status: rows.get(0).get(10),
177+
last_successful_build: None,
175178
rustdoc_status: rows.get(0).get(11),
176179
repository_url: rows.get(0).get(12),
177180
homepage_url: rows.get(0).get(13),
@@ -215,6 +218,23 @@ impl CrateDetails {
215218
crate_details.owners.push((row.get(0), row.get(1)));
216219
}
217220

221+
// retrieve last successful build if build failed
222+
if crate_details.build_status == false {
223+
let rows = conn.query(
224+
"SELECT version
225+
FROM releases
226+
INNER JOIN crates ON releases.crate_id = crates.id
227+
WHERE build_status = true AND yanked = false AND crates.name = $1
228+
ORDER BY release_time desc
229+
LIMIT 1;",
230+
&[&name],
231+
).unwrap();
232+
233+
if rows.len() >= 1 {
234+
crate_details.last_successful_build = Some(rows.get(0).get(0));
235+
}
236+
}
237+
218238
Some(crate_details)
219239
}
220240
}
@@ -252,3 +272,48 @@ pub fn crate_details_handler(req: &mut Request) -> IronResult<Response> {
252272
}
253273
}
254274
}
275+
276+
#[cfg(test)]
277+
mod tests {
278+
use super::*;
279+
use failure::Error;
280+
281+
#[test]
282+
fn test_last_successful_build() {
283+
fn last_successful_build_equals(
284+
conn: &Connection,
285+
package: &str,
286+
version: &str,
287+
expected_last_successful_build: Option<String>
288+
) -> Result<bool, Error> {
289+
290+
let details = CrateDetails::new(conn, package, version)
291+
.ok_or(failure::err_msg("could not fetch crate details"))?;
292+
293+
Ok(details.last_successful_build == expected_last_successful_build)
294+
};
295+
296+
crate::test::with_database(|db| {
297+
// Create some releases in the database, of which the last release failed to build
298+
db.fake_release()
299+
.name("foo")
300+
.version("0.0.1")
301+
.create()?;
302+
db.fake_release()
303+
.name("foo")
304+
.version("0.0.2")
305+
.create()?;
306+
db.fake_release()
307+
.name("foo")
308+
.version("0.0.3")
309+
.build_result_successful(false)
310+
.create()?;
311+
312+
assert!(last_successful_build_equals(&db.conn(), "foo", "0.0.1", None)?);
313+
assert!(last_successful_build_equals(&db.conn(), "foo", "0.0.2", None)?);
314+
assert!(last_successful_build_equals(&db.conn(), "foo", "0.0.3", Some("0.0.2".to_string()))?);
315+
316+
Ok(())
317+
});
318+
}
319+
}

templates/crate_details.hbs

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
{{else}}
6464
{{#unless build_status}}
6565
<div class="warning">docs.rs failed to build {{name}}-{{version}}<br>Please check the <a href="/crate/{{name}}/{{version}}/builds">build logs</a> and, if you believe this is docs.rs' fault, <a href="https://github.com/rust-lang/docs.rs/issues/new/choose">open an issue</a>.</div>
66+
{{#if last_successful_build}}
67+
<div class="info">Visit the last successful build: <a href="/crate/{{name}}/{{last_successful_build}}">{{name}}-{{last_successful_build}}</a></div>
68+
{{/if}}
6669
{{else}}
6770
{{#unless rustdoc_status}}
6871
<div class="warning">{{name}}-{{version}} doesn't have any documentation.</div>

templates/style.scss

+7-2
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,10 @@ div.cratesfyi-package-container-rustdoc {
706706
margin-bottom: 10px;
707707
}
708708

709-
div.warning {
709+
div.info {
710710
font-family: $font-family-sans;
711711
border-radius: 4px;
712-
background-color: lighten($color-type, 45%);
712+
background-color: $color-background-code;
713713
padding: .4em 1em;
714714
text-align: center;
715715
margin-bottom: 10px;
@@ -720,6 +720,11 @@ div.warning {
720720
}
721721
}
722722

723+
div.warning {
724+
@extend div.info;
725+
background-color: lighten($color-type, 45%);
726+
}
727+
723728
div.search-page-search-form {
724729
padding: .4em 1em;
725730
text-align: center;

0 commit comments

Comments
 (0)