Skip to content

Commit

Permalink
Auto merge of #18 - troy/pr-labels, r=<dry>
Browse files Browse the repository at this point in the history
implement the pr label feature
this commit lays the ground work for adding labels to PRs depending on the CI Run Status.

The config type was already implemented but the actual labeling action was not.

Requested-by: TroyKomodo <[email protected]>
  • Loading branch information
scuffle-brawl[bot] authored Dec 25, 2024
2 parents dd3beaa + 73e2a99 commit dafb8a6
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 52 deletions.
1 change: 1 addition & 0 deletions migrations/2024-12-25-001112_label_state/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE github_pr DROP COLUMN added_label;
1 change: 1 addition & 0 deletions migrations/2024-12-25-001112_label_state/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE github_pr ADD COLUMN added_label TEXT;
6 changes: 3 additions & 3 deletions migrations/schema.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- migrations/schema.unpatched.rs 2024-12-21 01:00:50.435153015 +0000
+++ server/src/database/schema.rs 2024-12-21 00:55:41.850491800 +0000
--- migrations/schema.unpatched.rs 2024-12-25 01:43:39.324295195 +0000
+++ server/src/database/schema.rs 2024-12-25 01:43:35.592373517 +0000
@@ -1,38 +1,44 @@
+#![cfg_attr(coverage_nightly, coverage(off))]
// @generated automatically by Diesel CLI.
Expand Down Expand Up @@ -85,7 +85,7 @@
///
/// (Automatically generated by Diesel.)
status -> GithubPrStatus,
@@ -320,12 +326,13 @@
@@ -326,12 +332,13 @@
updated_at -> Timestamptz,
}
}
Expand Down
6 changes: 6 additions & 0 deletions migrations/schema.unpatched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ diesel::table! {
///
/// (Automatically generated by Diesel.)
default_priority -> Nullable<Int4>,
/// The `added_label` column of the `github_pr` table.
///
/// Its SQL type is `Nullable<Text>`.
///
/// (Automatically generated by Diesel.)
added_label -> Nullable<Text>,
}
}

Expand Down
7 changes: 4 additions & 3 deletions server/src/command/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn handle_with_pr<R: GitHubRepoClient>(
pr: PullRequest,
context: BrawlCommandContext<'_, R>,
) -> anyhow::Result<()> {
Pr::new(&pr, context.user.id, context.repo.id())
let db_pr = Pr::new(&pr, context.user.id, context.repo.id())
.upsert()
.get_result(conn)
.await
Expand All @@ -49,7 +49,7 @@ async fn handle_with_pr<R: GitHubRepoClient>(
context
.repo
.merge_workflow()
.cancel(&run, context.repo, conn)
.cancel(&run, context.repo, conn, &db_pr)
.await
.context("cancel ci run")?;

Expand Down Expand Up @@ -89,8 +89,9 @@ pub mod tests {
run: &CiRun<'_>,
repo: &impl GitHubRepoClient,
conn: &mut AsyncPgConnection,
pr: &Pr<'_>,
) -> anyhow::Result<()> {
let _ = (run, repo, conn);
let _ = (run, repo, conn, pr);
if self
.cancelled
.compare_exchange(
Expand Down
15 changes: 8 additions & 7 deletions server/src/command/dry_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ async fn handle_with_pr<R: GitHubRepoClient>(

let branch = context.repo.config().try_branch(pr.number);

let db_pr = Pr::new(&pr, context.user.id, context.repo.id())
.upsert()
.get_result(conn)
.await
.context("update pr")?;

if let Some(run) = CiRun::active(context.repo.id(), pr.number)
.get_result(conn)
.await
Expand All @@ -90,7 +96,7 @@ async fn handle_with_pr<R: GitHubRepoClient>(
context
.repo
.merge_workflow()
.cancel(&run, context.repo, conn)
.cancel(&run, context.repo, conn, &db_pr)
.await
.context("cancel ci run")?;
} else {
Expand All @@ -115,12 +121,6 @@ async fn handle_with_pr<R: GitHubRepoClient>(
}
}

let db_pr = Pr::new(&pr, context.user.id, context.repo.id())
.upsert()
.get_result(conn)
.await
.context("update pr")?;

let run = CiRun::insert(context.repo.id(), pr.number)
.base_ref(base)
.head_commit_sha(command.head_sha.as_deref().unwrap_or_else(|| pr.head.sha.as_ref()).into())
Expand Down Expand Up @@ -199,6 +199,7 @@ mod tests {
run: &CiRun<'_>,
_: &impl GitHubRepoClient,
conn: &mut AsyncPgConnection,
_: &Pr<'_>,
) -> anyhow::Result<()> {
if self
.cancelled
Expand Down
10 changes: 8 additions & 2 deletions server/src/command/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn handle_with_pr<R: GitHubRepoClient>(
.get_result(conn)
.await?;

context.repo.merge_workflow().queued(&run, context.repo).await?;
context.repo.merge_workflow().queued(&run, context.repo, conn, &db_pr).await?;

Ok(())
}
Expand All @@ -131,7 +131,13 @@ mod tests {
}

impl GitHubMergeWorkflow for MockMergeWorkFlow {
async fn queued(&self, _: &CiRun<'_>, _: &impl GitHubRepoClient) -> anyhow::Result<()> {
async fn queued(
&self,
_: &CiRun<'_>,
_: &impl GitHubRepoClient,
_: &mut AsyncPgConnection,
_: &Pr<'_>,
) -> anyhow::Result<()> {
self.queued
.compare_exchange(
false,
Expand Down
10 changes: 8 additions & 2 deletions server/src/command/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async fn handle_with_pr<R: GitHubRepoClient>(
if run.is_dry_run {
context.repo.merge_workflow().start(&run, context.repo, conn, &db_pr).await?;
} else {
context.repo.merge_workflow().queued(&run, context.repo).await?;
context.repo.merge_workflow().queued(&run, context.repo, conn, &db_pr).await?;
}

Ok(())
Expand Down Expand Up @@ -132,7 +132,13 @@ mod tests {
Ok(true)
}

async fn queued(&self, _: &CiRun<'_>, _: &impl GitHubRepoClient) -> anyhow::Result<()> {
async fn queued(
&self,
_: &CiRun<'_>,
_: &impl GitHubRepoClient,
_: &mut AsyncPgConnection,
_: &Pr<'_>,
) -> anyhow::Result<()> {
self.queued
.compare_exchange(
false,
Expand Down
38 changes: 27 additions & 11 deletions server/src/database/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Pr<'a> {
pub target_branch: Cow<'a, str>,
pub source_branch: Cow<'a, str>,
pub latest_commit_sha: Cow<'a, str>,
pub added_label: Option<Cow<'a, str>>,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
Expand All @@ -42,6 +43,7 @@ pub struct UpdatePr<'a> {
pub github_repo_id: i64,
#[builder(start_fn)]
pub github_pr_number: i32,
pub added_label: Option<Option<Cow<'a, str>>>,
pub title: Option<Cow<'a, str>>,
pub body: Option<Cow<'a, str>>,
pub merge_status: Option<GithubPrMergeStatus>,
Expand Down Expand Up @@ -110,6 +112,7 @@ impl<'a> Pr<'a> {
target_branch: Cow::Borrowed(&pr.base.ref_field),
source_branch: Cow::Borrowed(&pr.head.ref_field),
latest_commit_sha: Cow::Borrowed(&pr.head.sha),
added_label: None,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
}
Expand Down Expand Up @@ -138,6 +141,7 @@ impl<'a> Pr<'a> {
title: Some(Cow::Borrowed(self.title.as_ref())),
body: Some(Cow::Borrowed(self.body.as_ref())),
merge_status: Some(self.merge_status),
added_label: Some(self.added_label.as_deref().map(Cow::Borrowed)),
assigned_ids: Some(self.assigned_ids.clone()),
status: Some(self.status),
default_priority: Some(self.default_priority),
Expand Down Expand Up @@ -236,6 +240,7 @@ mod tests {
"github_pr"."target_branch",
"github_pr"."source_branch",
"github_pr"."latest_commit_sha",
"github_pr"."added_label",
"github_pr"."created_at",
"github_pr"."updated_at"
FROM
Expand Down Expand Up @@ -263,6 +268,7 @@ mod tests {
latest_commit_sha: Cow::Borrowed("test"),
source_branch: Cow::Borrowed("test"),
target_branch: Cow::Borrowed("test"),
added_label: None,
merge_status: GithubPrMergeStatus::NotReady,
status: GithubPrStatus::Open,
merge_commit_sha: None,
Expand All @@ -283,6 +289,7 @@ mod tests {
"target_branch",
"source_branch",
"latest_commit_sha",
"added_label",
"created_at",
"updated_at"
)
Expand All @@ -301,6 +308,7 @@ mod tests {
$9,
$10,
$11,
DEFAULT,
$12,
$13
) -- binds: [1, 1, "test", "test", NotReady, 0, [], Open, "test", "test", "test", 2024-06-20T02:40:00Z, 2024-06-20T02:40:00Z]
Expand Down Expand Up @@ -360,6 +368,7 @@ mod tests {
updated_at: chrono::DateTime::from_timestamp_nanos(1718851200000000000),
assigned_ids: vec![],
author_id: 0,
added_label: None,
default_priority: None,
latest_commit_sha: Cow::Borrowed("test"),
source_branch: Cow::Borrowed("test"),
Expand All @@ -384,6 +393,7 @@ mod tests {
"target_branch",
"source_branch",
"latest_commit_sha",
"added_label",
"created_at",
"updated_at"
)
Expand All @@ -402,21 +412,23 @@ mod tests {
$9,
$10,
$11,
DEFAULT,
$12,
$13
) ON CONFLICT ("github_repo_id", "github_pr_number") DO
UPDATE
SET
"title" = $14,
"body" = $15,
"merge_status" = $16,
"assigned_ids" = $17,
"status" = $18,
"default_priority" = $19,
"merge_commit_sha" = $20,
"target_branch" = $21,
"latest_commit_sha" = $22,
"updated_at" = $23
"added_label" = $14,
"title" = $15,
"body" = $16,
"merge_status" = $17,
"assigned_ids" = $18,
"status" = $19,
"default_priority" = $20,
"merge_commit_sha" = $21,
"target_branch" = $22,
"latest_commit_sha" = $23,
"updated_at" = $24
RETURNING
"github_pr"."github_repo_id",
"github_pr"."github_pr_number",
Expand All @@ -431,8 +443,9 @@ mod tests {
"github_pr"."target_branch",
"github_pr"."source_branch",
"github_pr"."latest_commit_sha",
"github_pr"."added_label",
"github_pr"."created_at",
"github_pr"."updated_at" -- binds: [1, 1, "test", "test", NotReady, 0, [], Open, "test", "test", "test", 2024-06-20T02:40:00Z, 2024-06-20T02:40:00Z, "test", "test", NotReady, [], Open, None, None, "test", "test", 2024-06-20T02:40:00Z]
"github_pr"."updated_at" -- binds: [1, 1, "test", "test", NotReady, 0, [], Open, "test", "test", "test", 2024-06-20T02:40:00Z, 2024-06-20T02:40:00Z, None, "test", "test", NotReady, [], Open, None, None, "test", "test", 2024-06-20T02:40:00Z]
"#,
}

Expand Down Expand Up @@ -516,6 +529,7 @@ mod tests {
title: Cow::Borrowed("test"),
assigned_ids: vec![],
author_id: 0,
added_label: None,
default_priority: None,
merge_status: GithubPrMergeStatus::NotReady,
status: GithubPrStatus::Open,
Expand Down Expand Up @@ -547,6 +561,7 @@ mod tests {
assigned_ids: vec![],
author_id: 0,
default_priority: None,
added_label: None,
merge_status: GithubPrMergeStatus::NotReady,
status: GithubPrStatus::Open,
merge_commit_sha: None,
Expand Down Expand Up @@ -584,6 +599,7 @@ mod tests {
title: Cow::Borrowed("test"),
assigned_ids: vec![],
author_id: 0,
added_label: None,
default_priority: None,
merge_status: GithubPrMergeStatus::NotReady,
status: GithubPrStatus::Open,
Expand Down
6 changes: 6 additions & 0 deletions server/src/database/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ diesel::table! {
///
/// (Automatically generated by Diesel.)
default_priority -> Nullable<Int4>,
/// The `added_label` column of the `github_pr` table.
///
/// Its SQL type is `Nullable<Text>`.
///
/// (Automatically generated by Diesel.)
added_label -> Nullable<Text>,
}
}

Expand Down
Loading

0 comments on commit dafb8a6

Please sign in to comment.