Skip to content

Commit a278f96

Browse files
committed
Store build kind in the database
1 parent 7b7b30b commit a278f96

File tree

7 files changed

+100
-35
lines changed

7 files changed

+100
-35
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Add down migration script here
2+
ALTER TABLE build
3+
DROP COLUMN kind;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Add up migration script here
2+
ALTER TABLE build
3+
ADD COLUMN kind TEXT NOT NULL DEFAULT 'try';
4+
5+
UPDATE build
6+
SET kind =
7+
CASE
8+
WHEN build.branch LIKE '%auto' THEN 'auto'
9+
ELSE 'try'
10+
END;

src/database/client.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sqlx::PgPool;
22

33
use crate::bors::comment::CommentTag;
4-
use crate::bors::{PullRequestStatus, RollupMode};
4+
use crate::bors::{BuildKind, PullRequestStatus, RollupMode};
55
use crate::database::operations::{get_merge_queue_prs, update_pr_auto_build_id};
66
use crate::database::{
77
BuildModel, BuildStatus, CommentModel, PullRequestModel, RepoModel, TreeState, WorkflowModel,
@@ -187,8 +187,15 @@ impl PgDbClient {
187187
parent: CommitSha,
188188
) -> anyhow::Result<i32> {
189189
let mut tx = self.pool.begin().await?;
190-
let build_id =
191-
create_build(&mut *tx, &pr.repository, &branch, &commit_sha, &parent).await?;
190+
let build_id = create_build(
191+
&mut *tx,
192+
&pr.repository,
193+
&branch,
194+
BuildKind::Try,
195+
&commit_sha,
196+
&parent,
197+
)
198+
.await?;
192199
update_pr_try_build_id(&mut *tx, pr.id, build_id).await?;
193200
tx.commit().await?;
194201
Ok(build_id)
@@ -202,8 +209,15 @@ impl PgDbClient {
202209
parent: CommitSha,
203210
) -> anyhow::Result<i32> {
204211
let mut tx = self.pool.begin().await?;
205-
let build_id =
206-
create_build(&mut *tx, &pr.repository, &branch, &commit_sha, &parent).await?;
212+
let build_id = create_build(
213+
&mut *tx,
214+
&pr.repository,
215+
&branch,
216+
BuildKind::Auto,
217+
&commit_sha,
218+
&parent,
219+
)
220+
.await?;
207221
update_pr_auto_build_id(&mut *tx, pr.id, build_id).await?;
208222
tx.commit().await?;
209223
Ok(build_id)

src/database/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
str::FromStr,
55
};
66

7+
use crate::bors::BuildKind;
78
use crate::bors::comment::CommentTag;
89
use crate::{
910
bors::{PullRequestStatus, RollupMode},
@@ -340,6 +341,37 @@ pub struct BuildModel {
340341
pub created_at: DateTime<Utc>,
341342
/// The ID of the check run associated with the build.
342343
pub check_run_id: Option<i64>,
344+
/// What kind of build this is (`try` or `auto`).
345+
pub kind: BuildKind,
346+
}
347+
348+
impl sqlx::Type<sqlx::Postgres> for BuildKind {
349+
fn type_info() -> sqlx::postgres::PgTypeInfo {
350+
<String as sqlx::Type<sqlx::Postgres>>::type_info()
351+
}
352+
}
353+
354+
impl sqlx::Encode<'_, sqlx::Postgres> for BuildKind {
355+
fn encode_by_ref(
356+
&self,
357+
buf: &mut sqlx::postgres::PgArgumentBuffer,
358+
) -> Result<sqlx::encode::IsNull, BoxDynError> {
359+
let tag = match self {
360+
Self::Try => "try",
361+
Self::Auto => "auto",
362+
};
363+
<&str as sqlx::Encode<sqlx::Postgres>>::encode(tag, buf)
364+
}
365+
}
366+
367+
impl sqlx::Decode<'_, sqlx::Postgres> for BuildKind {
368+
fn decode(value: sqlx::postgres::PgValueRef<'_>) -> Result<Self, BoxDynError> {
369+
match <&str as sqlx::Decode<sqlx::Postgres>>::decode(value)? {
370+
"try" => Ok(Self::Try),
371+
"auto" => Ok(Self::Auto),
372+
kind => Err(format!("Unknown build kind: {kind}").into()),
373+
}
374+
}
343375
}
344376

345377
/// Represents a pull request.

src/database/operations.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ use chrono::DateTime;
22
use chrono::Utc;
33
use sqlx::postgres::PgExecutor;
44

5-
use crate::bors::PullRequestStatus;
6-
use crate::bors::RollupMode;
7-
use crate::bors::comment::CommentTag;
8-
use crate::database::BuildStatus;
9-
use crate::database::RepoModel;
10-
use crate::database::WorkflowModel;
11-
use crate::github::CommitSha;
12-
use crate::github::GithubRepoName;
13-
use crate::github::PullRequestNumber;
14-
use crate::utils::timing::measure_db_query;
15-
165
use super::ApprovalInfo;
176
use super::ApprovalStatus;
187
use super::Assignees;
@@ -26,6 +15,17 @@ use super::TreeState;
2615
use super::UpsertPullRequestParams;
2716
use super::WorkflowStatus;
2817
use super::WorkflowType;
18+
use crate::bors::PullRequestStatus;
19+
use crate::bors::RollupMode;
20+
use crate::bors::comment::CommentTag;
21+
use crate::database::BuildKind;
22+
use crate::database::BuildStatus;
23+
use crate::database::RepoModel;
24+
use crate::database::WorkflowModel;
25+
use crate::github::CommitSha;
26+
use crate::github::GithubRepoName;
27+
use crate::github::PullRequestNumber;
28+
use crate::utils::timing::measure_db_query;
2929
use futures::TryStreamExt;
3030

3131
pub(crate) async fn get_pull_request(
@@ -524,18 +524,20 @@ pub(crate) async fn create_build(
524524
executor: impl PgExecutor<'_>,
525525
repo: &GithubRepoName,
526526
branch: &str,
527+
kind: BuildKind,
527528
commit_sha: &CommitSha,
528529
parent: &CommitSha,
529530
) -> anyhow::Result<i32> {
530531
measure_db_query("create_build", || async {
531532
let build_id = sqlx::query_scalar!(
532533
r#"
533-
INSERT INTO build (repository, branch, commit_sha, parent, status)
534-
VALUES ($1, $2, $3, $4, $5)
534+
INSERT INTO build (repository, branch, kind, commit_sha, parent, status)
535+
VALUES ($1, $2, $3, $4, $5, $6)
535536
RETURNING id
536537
"#,
537538
repo as &GithubRepoName,
538539
branch,
540+
kind as BuildKind,
539541
commit_sha.0,
540542
parent.0,
541543
BuildStatus::Pending as BuildStatus
@@ -561,6 +563,7 @@ SELECT
561563
id,
562564
repository as "repository: GithubRepoName",
563565
branch,
566+
kind as "kind: BuildKind",
564567
commit_sha,
565568
status as "status: BuildStatus",
566569
parent,
@@ -594,6 +597,7 @@ SELECT
594597
id,
595598
repository as "repository: GithubRepoName",
596599
branch,
600+
kind as "kind: BuildKind",
597601
commit_sha,
598602
status as "status: BuildStatus",
599603
parent,
@@ -758,7 +762,8 @@ SELECT
758762
build.status,
759763
build.parent,
760764
build.created_at,
761-
build.check_run_id
765+
build.check_run_id,
766+
build.kind
762767
) AS "build!: BuildModel"
763768
FROM workflow
764769
LEFT JOIN build ON workflow.build_id = build.id
@@ -818,7 +823,8 @@ SELECT
818823
build.status,
819824
build.parent,
820825
build.created_at,
821-
build.check_run_id
826+
build.check_run_id,
827+
build.kind
822828
) AS "build!: BuildModel"
823829
FROM workflow
824830
LEFT JOIN build ON workflow.build_id = build.id
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
INSERT INTO
2-
build (repository, branch, commit_sha, status, parent)
3-
VALUES
4-
(
5-
'rust-lang/bors',
1+
INSERT INTO build (repository, branch, commit_sha, status, parent)
2+
VALUES ('rust-lang/bors',
63
'automation/bors/try',
74
'a7ec24743ca724dd4b164b3a76d29d0da9573617',
85
'pending',
9-
'8f5e9988e7aa74bffcbec51af17f541d8e7d8e3c'
10-
),
11-
(
12-
'rust-lang/cargo',
6+
'8f5e9988e7aa74bffcbec51af17f541d8e7d8e3c'),
7+
('rust-lang/cargo',
138
'automation/bors/try-merge',
149
'b3f987c12ee248ef21d37b59a40b17e93fac7c8a',
1510
'success',
16-
'c53f32bb8a51fa9fd49d7bd83eb4b15ccfd8a372'
17-
),
18-
(
19-
'rust-lang/rust',
11+
'c53f32bb8a51fa9fd49d7bd83eb4b15ccfd8a372'),
12+
('rust-lang/rust',
2013
'automation/bors/try',
2114
'4ee5a1bfc10bc49f30a8f527557ac4a93a2b9d66',
2215
'failure',
23-
'9d4e0ac0fca0d0c268be3e9d24d98e3906f0e89b'
24-
);
16+
'9d4e0ac0fca0d0c268be3e9d24d98e3906f0e89b'),
17+
('rust-lang/rust',
18+
'automation/bors/auto',
19+
'3ef2a1bfc10bc49f30a8f527557ac4a93a2b9d68',
20+
'success',
21+
'9d4e0ac0fca0d0c268be3e9d24d98e3906f0e89b');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Empty to satisfy migration tests
2+
-- We don't need to add any new data here, as the kind column has been auto-filled
3+
-- from the branch column by this migration.

0 commit comments

Comments
 (0)