Skip to content

Commit c372f18

Browse files
committed
Append workflow links to auto build started comments
1 parent 56dd684 commit c372f18

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

src/bors/comment.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum CommentMetadata {
2828
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2929
pub enum CommentTag {
3030
TryBuildStarted,
31+
AutoBuildStarted,
3132
}
3233

3334
impl Comment {
@@ -199,6 +200,10 @@ pub fn try_build_started_comment(
199200
}
200201

201202
pub fn append_workflow_links_to_comment(comment_content: &mut String, workflow_urls: Vec<String>) {
203+
if !comment_content.ends_with("\n") {
204+
comment_content.push('\n');
205+
}
206+
202207
if workflow_urls.len() == 1 {
203208
comment_content.push_str(&format!("\n**Workflow**: {}", workflow_urls[0]));
204209
} else {

src/bors/handlers/workflow.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::PgDbClient;
2+
use crate::bors::BuildKind;
23
use crate::bors::comment::{
34
CommentTag, append_workflow_links_to_comment, build_failed_comment, try_build_succeeded_comment,
45
};
56
use crate::bors::event::{WorkflowRunCompleted, WorkflowRunStarted};
67
use crate::bors::handlers::labels::handle_label_trigger;
78
use crate::bors::handlers::{hide_try_build_started_comments, is_bors_observed_branch};
89
use crate::bors::merge_queue::MergeQueueSender;
9-
use crate::bors::{BuildKind, TRY_BRANCH_NAME};
1010
use crate::bors::{FailedWorkflowRun, RepositoryState, WorkflowRun};
1111
use crate::database::{
1212
BuildModel, BuildStatus, PullRequestModel, QueueStatus, WorkflowModel, WorkflowStatus,
@@ -66,14 +66,12 @@ pub(super) async fn handle_workflow_started(
6666
)
6767
.await?;
6868

69-
if build.branch == TRY_BRANCH_NAME {
70-
add_workflow_links_to_try_build_start_comment(repo, db, &build, payload).await?;
71-
}
69+
add_workflow_links_to_build_start_comment(repo, db, &build, payload).await?;
7270

7371
Ok(())
7472
}
7573

76-
async fn add_workflow_links_to_try_build_start_comment(
74+
async fn add_workflow_links_to_build_start_comment(
7775
repo: Arc<RepositoryState>,
7876
db: Arc<PgDbClient>,
7977
build: &BuildModel,
@@ -83,27 +81,31 @@ async fn add_workflow_links_to_try_build_start_comment(
8381
tracing::warn!("PR for build not found");
8482
return Ok(());
8583
};
84+
85+
let tag = match build.kind {
86+
BuildKind::Try => CommentTag::TryBuildStarted,
87+
BuildKind::Auto => CommentTag::AutoBuildStarted,
88+
};
8689
let comments = db
87-
.get_tagged_bot_comments(&payload.repository, pr.number, CommentTag::TryBuildStarted)
90+
.get_tagged_bot_comments(&payload.repository, pr.number, tag)
8891
.await?;
8992

90-
let Some(try_build_comment) = comments.last() else {
91-
tracing::warn!("No try build comment found for PR");
93+
let Some(build_started_comment) = comments.last() else {
94+
tracing::warn!("No build started comment found for PR");
9295
return Ok(());
9396
};
9497

9598
let workflows = db.get_workflow_urls_for_build(build).await?;
96-
9799
if !workflows.is_empty() {
98100
let mut comment_content = repo
99101
.client
100-
.get_comment_content(&try_build_comment.node_id)
102+
.get_comment_content(&build_started_comment.node_id)
101103
.await?;
102104

103105
append_workflow_links_to_comment(&mut comment_content, workflows);
104106

105107
repo.client
106-
.update_comment_content(&try_build_comment.node_id, &comment_content)
108+
.update_comment_content(&build_started_comment.node_id, &comment_content)
107109
.await?;
108110
}
109111

@@ -154,6 +156,7 @@ pub(super) async fn handle_workflow_completed(
154156
)
155157
.await
156158
}
159+
157160
/// Attempt to complete a pending build after a workflow run has been completed.
158161
/// We assume that the status of the completed workflow run has already been updated in the
159162
/// database.
@@ -176,7 +179,7 @@ async fn maybe_complete_build(
176179
.await?
177180
else {
178181
tracing::warn!(
179-
"Received check suite finished for an unknown build: {}",
182+
"Received workflow finished for an unknown build: {}",
180183
payload.commit_sha
181184
);
182185
return Ok(());

src/bors/merge_queue.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use tracing::Instrument;
88

99
use crate::BorsContext;
1010
use crate::bors::comment::{
11-
auto_build_push_failed_comment, auto_build_started_comment, auto_build_succeeded_comment,
12-
merge_conflict_comment,
11+
CommentTag, auto_build_push_failed_comment, auto_build_started_comment,
12+
auto_build_succeeded_comment, merge_conflict_comment,
1313
};
1414
use crate::bors::{AUTO_BRANCH_NAME, PullRequestStatus, RepositoryState};
1515
use crate::database::{
@@ -209,6 +209,7 @@ async fn handle_successful_build(
209209

210210
/// Handle starting a new auto build for an approved PR.
211211
/// Returns true if the queue should break, false to continue.
212+
#[tracing::instrument(skip(repo, ctx, pr))]
212213
async fn handle_start_auto_build(
213214
repo: &RepositoryState,
214215
ctx: &BorsContext,
@@ -387,11 +388,24 @@ async fn start_auto_build(
387388

388389
// 5. Post status comment
389390
let comment = auto_build_started_comment(&head_sha, &merge_sha);
390-
if let Err(error) = client.post_comment(pr.number, comment).await {
391-
tracing::error!(
392-
"Failed to post auto build started comment on PR {}: {error:?}",
393-
pr.number
394-
);
391+
match client.post_comment(pr.number, comment).await {
392+
Ok(comment) => {
393+
if let Err(error) = ctx
394+
.db
395+
.record_tagged_bot_comment(
396+
repo.repository(),
397+
pr.number,
398+
CommentTag::AutoBuildStarted,
399+
&comment.node_id,
400+
)
401+
.await
402+
{
403+
tracing::error!("Cannot tag auto build started comment: {error:?}",);
404+
}
405+
}
406+
Err(error) => {
407+
tracing::error!("Failed to post auto build started comment: {error:?}",);
408+
}
395409
};
396410

397411
Ok(())
@@ -1134,4 +1148,29 @@ auto_build_failed = ["+foo", "+bar", "-baz"]
11341148
})
11351149
.await;
11361150
}
1151+
1152+
#[sqlx::test]
1153+
async fn update_auto_build_started_comment_after_workflow_starts(pool: sqlx::PgPool) {
1154+
run_test(pool, async |tester: &mut BorsTester| {
1155+
tester.approve(()).await?;
1156+
tester.process_merge_queue().await;
1157+
let comment = tester.get_next_comment(()).await?;
1158+
1159+
tester.workflow_start(tester.auto_branch().await).await?;
1160+
1161+
// Check that the comment text has been updated with a link to the started workflow
1162+
let updated_comment = tester
1163+
.get_comment_by_node_id(&comment.node_id.unwrap())
1164+
.await
1165+
.unwrap();
1166+
insta::assert_snapshot!(updated_comment.content, @r"
1167+
:hourglass: Testing commit pr-1-sha with merge merge-0-pr-1...
1168+
1169+
**Workflow**: https://github.com/rust-lang/borstest/actions/runs/1
1170+
");
1171+
1172+
Ok(())
1173+
})
1174+
.await;
1175+
}
11371176
}

src/database/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ impl sqlx::Encode<'_, sqlx::Postgres> for CommentTag {
622622
) -> Result<sqlx::encode::IsNull, BoxDynError> {
623623
let tag = match self {
624624
CommentTag::TryBuildStarted => "TryBuildStarted",
625+
CommentTag::AutoBuildStarted => "AutoBuildStarted",
625626
};
626627
<&str as sqlx::Encode<sqlx::Postgres>>::encode(tag, buf)
627628
}
@@ -631,6 +632,7 @@ impl sqlx::Decode<'_, sqlx::Postgres> for CommentTag {
631632
fn decode(value: sqlx::postgres::PgValueRef<'_>) -> Result<Self, BoxDynError> {
632633
match <&str as sqlx::Decode<sqlx::Postgres>>::decode(value)? {
633634
"TryBuildStarted" => Ok(CommentTag::TryBuildStarted),
635+
"AutoBuildStarted" => Ok(CommentTag::AutoBuildStarted),
634636
tag => Err(format!("Unknown comment tag: {tag}").into()),
635637
}
636638
}

0 commit comments

Comments
 (0)