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 26, 2024
2 parents dd3beaa + 8d046c3 commit 87c23cb
Show file tree
Hide file tree
Showing 20 changed files with 961 additions and 89 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_labels;
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_labels TEXT[] NOT NULL DEFAULT '{}' CHECK (array_position(added_labels, NULL) IS NULL);
26 changes: 23 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 03:01:31.500330121 +0000
+++ server/src/database/schema.rs 2024-12-25 03:01:20.600359108 +0000
@@ -1,38 +1,44 @@
+#![cfg_attr(coverage_nightly, coverage(off))]
// @generated automatically by Diesel CLI.
Expand Down Expand Up @@ -85,7 +85,27 @@
///
/// (Automatically generated by Diesel.)
status -> GithubPrStatus,
@@ -320,12 +326,13 @@
@@ -297,16 +303,16 @@
/// Its SQL type is `Nullable<Int4>`.
///
/// (Automatically generated by Diesel.)
default_priority -> Nullable<Int4>,
/// The `added_labels` column of the `github_pr` table.
///
- /// Its SQL type is `Array<Nullable<Text>>`.
+ /// Its SQL type is `Array<Text>`.
///
- /// (Automatically generated by Diesel.)
- added_labels -> Array<Nullable<Text>>,
+ /// (Manually changed from `Array<Nullable<Text>>` to `Array<Text>`)
+ added_labels -> Array<Text>,
}
}

diesel::table! {
/// Representation of the `health_check` table.
///
@@ -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_labels` column of the `github_pr` table.
///
/// Its SQL type is `Array<Nullable<Text>>`.
///
/// (Automatically generated by Diesel.)
added_labels -> Array<Nullable<Text>>,
}
}

Expand Down
17 changes: 9 additions & 8 deletions server/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
#![cfg_attr(all(coverage_nightly, test), coverage(off))]

use std::net::SocketAddr;
Expand Down Expand Up @@ -162,14 +163,14 @@ impl scuffle_bootstrap_telemetry::TelemetryConfig for Global {
}

impl scuffle_brawl::webhook::WebhookConfig for Global {
fn bind_address(&self) -> Option<SocketAddr> {
Some(self.config.github.webhook_bind)
}

fn webhook_secret(&self) -> &str {
&self.config.github.webhook_secret
}

fn bind_address(&self) -> Option<SocketAddr> {
Some(self.config.github.webhook_bind)
}

fn get_repo(
&self,
installation_id: InstallationId,
Expand All @@ -196,13 +197,13 @@ impl scuffle_brawl::webhook::WebhookConfig for Global {
Ok(())
}

fn delete_installation(&self, installation_id: InstallationId) -> anyhow::Result<()> {
self.github_service.delete_installation(installation_id);
async fn update_installation(&self, installation: Installation) -> anyhow::Result<()> {
self.github_service.update_installation(installation).await?;
Ok(())
}

async fn update_installation(&self, installation: Installation) -> anyhow::Result<()> {
self.github_service.update_installation(installation).await?;
fn delete_installation(&self, installation_id: InstallationId) -> anyhow::Result<()> {
self.github_service.delete_installation(installation_id);
Ok(())
}

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
46 changes: 31 additions & 15 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_labels: Vec<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_labels: Option<Vec<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_labels: Vec::new(),
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_labels: Some(self.added_labels.clone()),
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_labels",
"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_labels: vec![],
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_labels",
"created_at",
"updated_at"
)
Expand All @@ -302,8 +309,9 @@ mod tests {
$10,
$11,
$12,
$13
) -- binds: [1, 1, "test", "test", NotReady, 0, [], Open, "test", "test", "test", 2024-06-20T02:40:00Z, 2024-06-20T02:40:00Z]
$13,
$14
) -- 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_labels: vec![],
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_labels",
"created_at",
"updated_at"
)
Expand All @@ -403,20 +413,22 @@ mod tests {
$10,
$11,
$12,
$13
$13,
$14
) 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_labels" = $15,
"title" = $16,
"body" = $17,
"merge_status" = $18,
"assigned_ids" = $19,
"status" = $20,
"default_priority" = $21,
"merge_commit_sha" = $22,
"target_branch" = $23,
"latest_commit_sha" = $24,
"updated_at" = $25
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_labels",
"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, [], "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_labels: vec![],
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_labels: vec![],
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_labels: vec![],
default_priority: None,
merge_status: GithubPrMergeStatus::NotReady,
status: GithubPrStatus::Open,
Expand Down Expand Up @@ -674,7 +690,7 @@ mod tests {
.unwrap();

pr.update()
.body("test2".into())
.body(Cow::Borrowed("test2"))
.build()
.query()
.execute(&mut conn)
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_labels` column of the `github_pr` table.
///
/// Its SQL type is `Array<Text>`.
///
/// (Manually changed from `Array<Nullable<Text>>` to `Array<Text>`)
added_labels -> Array<Text>,
}
}

Expand Down
Loading

0 comments on commit 87c23cb

Please sign in to comment.