Skip to content

Commit 78a0112

Browse files
authored
Merge pull request #650 from reddevilmidzy/duration
Include build duration in success comment
2 parents bbe007d + f1127fc commit 78a0112

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/bors/comment.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ pub fn auto_build_succeeded_comment(
409409
approved_by: &str,
410410
merge_sha: &CommitSha,
411411
base_ref: &str,
412+
duration: Option<Duration>,
412413
) -> Comment {
413414
workflows.sort_by(|a, b| a.name.cmp(&b.name));
414415
let urls = workflows
@@ -417,10 +418,17 @@ pub fn auto_build_succeeded_comment(
417418
.collect::<Vec<_>>()
418419
.join(", ");
419420

421+
let duration = if let Some(dur) = duration {
422+
format_duration(dur)
423+
} else {
424+
"Unknown duration".to_string()
425+
};
426+
420427
Comment {
421428
text: format!(
422429
r#":sunny: Test successful - {urls}
423430
Approved by: `{approved_by}`
431+
Duration: `{duration}`
424432
Pushing {merge_sha} to `{base_ref}`..."#
425433
),
426434
metadata: Some(CommentMetadata::BuildCompleted {
@@ -431,6 +439,27 @@ Pushing {merge_sha} to `{base_ref}`..."#
431439
}
432440
}
433441

442+
fn format_duration(duration: Duration) -> String {
443+
let total_secs = duration.as_secs();
444+
let hours = total_secs / 3600;
445+
let minutes = (total_secs % 3600) / 60;
446+
let seconds = total_secs % 60;
447+
448+
let mut res = String::new();
449+
450+
if hours > 0 {
451+
res.push_str(&format!("{hours}h "));
452+
}
453+
if minutes > 0 {
454+
res.push_str(&format!("{minutes}m "));
455+
}
456+
if seconds > 0 {
457+
res.push_str(&format!("{seconds}s"));
458+
}
459+
460+
res.trim().to_string()
461+
}
462+
434463
pub fn auto_build_push_failed_comment(error: &str) -> Comment {
435464
Comment::new(format!(
436465
":eyes: Test was successful, but fast-forwarding failed: {error}"

src/bors/handlers/refresh.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ auto_build_failed = ["+failed"]
475475
insta::assert_snapshot!(comment, @r#"
476476
:sunny: Test successful - [Workflow1](https://github.com/rust-lang/borstest/actions/runs/1)
477477
Approved by: `default-user`
478+
Duration: `1h`
478479
Pushing merge-0-pr-1-d7d45f1f-reauthored-to-bors to `main`...
479480
<!-- homu: {"type":"BuildCompleted","base_ref":"main","merge_sha":"merge-0-pr-1-d7d45f1f-reauthored-to-bors"} -->
480481
"#);
@@ -506,6 +507,7 @@ auto_build_failed = ["+failed"]
506507
insta::assert_snapshot!(comment, @r#"
507508
:sunny: Test successful - [Workflow1](https://github.com/rust-lang/borstest/actions/runs/1)
508509
Approved by: `default-user`
510+
Duration: `1h`
509511
Pushing merge-0-pr-1-d7d45f1f-reauthored-to-bors to `main`...
510512
<!-- homu: {"type":"BuildCompleted","base_ref":"main","merge_sha":"merge-0-pr-1-d7d45f1f-reauthored-to-bors"} -->
511513
"#);

src/bors/merge_queue.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ async fn handle_successful_build(
244244
&approval_info.approver,
245245
&commit_sha,
246246
&pr.base_branch,
247+
auto_build.duration.map(|d| d.inner()),
247248
);
248249

249250
if let Err(error) = repo
@@ -793,6 +794,7 @@ merge_queue_enabled = false
793794
@r#"
794795
:sunny: Test successful - [Workflow1](https://github.com/rust-lang/borstest/actions/runs/1)
795796
Approved by: `default-user`
797+
Duration: `1h`
796798
Pushing merge-0-pr-1-d7d45f1f-reauthored-to-bors to `main`...
797799
<!-- homu: {"type":"BuildCompleted","base_ref":"main","merge_sha":"merge-0-pr-1-d7d45f1f-reauthored-to-bors"} -->
798800
"#
@@ -802,6 +804,84 @@ merge_queue_enabled = false
802804
.await;
803805
}
804806

807+
#[sqlx::test]
808+
async fn auto_build_success_comment_duration_hours_minutes(pool: sqlx::PgPool) {
809+
// Test with hours and minutes (4800 seconds = 1h 20m)
810+
run_test(pool, async |ctx: &mut BorsTester| {
811+
ctx.approve(()).await?;
812+
ctx.start_auto_build(()).await?;
813+
let w1 = ctx.auto_workflow();
814+
ctx.modify_workflow(w1, |w| w.set_duration(Duration::from_secs(4800)));
815+
ctx.workflow_full_success(w1).await?;
816+
ctx.run_merge_queue_until_merge_attempt().await;
817+
818+
insta::assert_snapshot!(
819+
ctx.get_next_comment_text(()).await?,
820+
@r#"
821+
:sunny: Test successful - [Workflow1](https://github.com/rust-lang/borstest/actions/runs/1)
822+
Approved by: `default-user`
823+
Duration: `1h 20m`
824+
Pushing merge-0-pr-1-d7d45f1f-reauthored-to-bors to `main`...
825+
<!-- homu: {"type":"BuildCompleted","base_ref":"main","merge_sha":"merge-0-pr-1-d7d45f1f-reauthored-to-bors"} -->
826+
"#
827+
);
828+
Ok(())
829+
})
830+
.await;
831+
}
832+
833+
#[sqlx::test]
834+
async fn auto_build_success_comment_duration_all_units(pool: sqlx::PgPool) {
835+
// Test with hours, minutes and seconds (12345 seconds = 3h 25m 45s)
836+
run_test(pool, async |ctx: &mut BorsTester| {
837+
ctx.approve(()).await?;
838+
ctx.start_auto_build(()).await?;
839+
let w1 = ctx.auto_workflow();
840+
ctx.modify_workflow(w1, |w| w.set_duration(Duration::from_secs(12345)));
841+
ctx.workflow_full_success(w1).await?;
842+
ctx.run_merge_queue_until_merge_attempt().await;
843+
844+
insta::assert_snapshot!(
845+
ctx.get_next_comment_text(()).await?,
846+
@r#"
847+
:sunny: Test successful - [Workflow1](https://github.com/rust-lang/borstest/actions/runs/1)
848+
Approved by: `default-user`
849+
Duration: `3h 25m 45s`
850+
Pushing merge-0-pr-1-d7d45f1f-reauthored-to-bors to `main`...
851+
<!-- homu: {"type":"BuildCompleted","base_ref":"main","merge_sha":"merge-0-pr-1-d7d45f1f-reauthored-to-bors"} -->
852+
"#
853+
);
854+
Ok(())
855+
})
856+
.await;
857+
}
858+
859+
#[sqlx::test]
860+
async fn auto_build_success_comment_duration_seconds_only(pool: sqlx::PgPool) {
861+
// Test with just seconds (45 seconds)
862+
run_test(pool, async |ctx: &mut BorsTester| {
863+
ctx.approve(()).await?;
864+
ctx.start_auto_build(()).await?;
865+
let w1 = ctx.auto_workflow();
866+
ctx.modify_workflow(w1, |w| w.set_duration(Duration::from_secs(45)));
867+
ctx.workflow_full_success(w1).await?;
868+
ctx.run_merge_queue_until_merge_attempt().await;
869+
870+
insta::assert_snapshot!(
871+
ctx.get_next_comment_text(()).await?,
872+
@r#"
873+
:sunny: Test successful - [Workflow1](https://github.com/rust-lang/borstest/actions/runs/1)
874+
Approved by: `default-user`
875+
Duration: `45s`
876+
Pushing merge-0-pr-1-d7d45f1f-reauthored-to-bors to `main`...
877+
<!-- homu: {"type":"BuildCompleted","base_ref":"main","merge_sha":"merge-0-pr-1-d7d45f1f-reauthored-to-bors"} -->
878+
"#
879+
);
880+
Ok(())
881+
})
882+
.await;
883+
}
884+
805885
#[sqlx::test]
806886
async fn auto_build_failure_comment(pool: sqlx::PgPool) {
807887
run_test(pool, async |ctx: &mut BorsTester| {

0 commit comments

Comments
 (0)