-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
1,298 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
crates/prbot-core/src/use_cases/pulls/add_pull_request_rule.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use async_trait::async_trait; | ||
use prbot_models::{PullRequestRule, Repository, RuleAction, RuleCondition}; | ||
use shaku::{Component, Interface}; | ||
|
||
use crate::{CoreContext, Result}; | ||
|
||
#[cfg_attr(any(test, feature = "testkit"), mockall::automock)] | ||
#[async_trait] | ||
pub trait AddPullRequestRuleInterface: Interface { | ||
async fn run<'a>( | ||
&self, | ||
ctx: &CoreContext<'a>, | ||
repository: &Repository, | ||
name: String, | ||
conditions: Vec<RuleCondition>, | ||
actions: Vec<RuleAction>, | ||
) -> Result<PullRequestRule>; | ||
} | ||
|
||
#[derive(Component)] | ||
#[shaku(interface = AddPullRequestRuleInterface)] | ||
pub(crate) struct AddPullRequestRule; | ||
|
||
#[async_trait] | ||
impl AddPullRequestRuleInterface for AddPullRequestRule { | ||
async fn run<'a>( | ||
&self, | ||
ctx: &CoreContext<'a>, | ||
repository: &Repository, | ||
name: String, | ||
conditions: Vec<RuleCondition>, | ||
actions: Vec<RuleAction>, | ||
) -> Result<PullRequestRule> { | ||
let rule = PullRequestRule { | ||
repository_id: repository.id, | ||
name, | ||
conditions, | ||
actions, | ||
}; | ||
|
||
let rule = ctx.db_service.pull_request_rules_create(rule).await?; | ||
Ok(rule) | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
crates/prbot-core/src/use_cases/pulls/apply_pull_request_rules.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
use async_trait::async_trait; | ||
use prbot_models::{PullRequestHandle, PullRequestRule, QaStatus, RuleAction}; | ||
use shaku::{Component, Interface}; | ||
use tracing::info; | ||
|
||
use crate::{CoreContext, Result}; | ||
|
||
#[cfg_attr(any(test, feature = "testkit"), mockall::automock)] | ||
#[async_trait] | ||
pub trait ApplyPullRequestRulesInterface: Interface { | ||
async fn run<'a>( | ||
&self, | ||
ctx: &CoreContext<'a>, | ||
pr_handle: &PullRequestHandle, | ||
rules: Vec<PullRequestRule>, | ||
) -> Result<()>; | ||
} | ||
|
||
#[derive(Component)] | ||
#[shaku(interface = ApplyPullRequestRulesInterface)] | ||
pub(crate) struct ApplyPullRequestRules; | ||
|
||
#[async_trait] | ||
impl ApplyPullRequestRulesInterface for ApplyPullRequestRules { | ||
async fn run<'a>( | ||
&self, | ||
ctx: &CoreContext<'a>, | ||
pr_handle: &PullRequestHandle, | ||
rules: Vec<PullRequestRule>, | ||
) -> Result<()> { | ||
for rule in rules { | ||
for action in rule.actions { | ||
self.apply_action(ctx, pr_handle, action).await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl ApplyPullRequestRules { | ||
async fn apply_action( | ||
&self, | ||
ctx: &CoreContext<'_>, | ||
pr_handle: &PullRequestHandle, | ||
action: RuleAction, | ||
) -> Result<()> { | ||
info!( | ||
action = ?action, | ||
pr_handle = ?pr_handle, | ||
"Applying rule action" | ||
); | ||
|
||
match action { | ||
RuleAction::SetAutomerge(value) => { | ||
ctx.db_service | ||
.pull_requests_set_automerge( | ||
pr_handle.owner(), | ||
pr_handle.name(), | ||
pr_handle.number(), | ||
value, | ||
) | ||
.await?; | ||
} | ||
RuleAction::SetChecksEnabled(value) => { | ||
ctx.db_service | ||
.pull_requests_set_checks_enabled( | ||
pr_handle.owner(), | ||
pr_handle.name(), | ||
pr_handle.number(), | ||
value, | ||
) | ||
.await?; | ||
} | ||
RuleAction::SetNeededReviewers(value) => { | ||
ctx.db_service | ||
.pull_requests_set_needed_reviewers_count( | ||
pr_handle.owner(), | ||
pr_handle.name(), | ||
pr_handle.number(), | ||
value, | ||
) | ||
.await?; | ||
} | ||
RuleAction::SetQaEnabled(value) => { | ||
ctx.db_service | ||
.pull_requests_set_qa_status( | ||
pr_handle.owner(), | ||
pr_handle.name(), | ||
pr_handle.number(), | ||
if value { | ||
QaStatus::Waiting | ||
} else { | ||
QaStatus::Skipped | ||
}, | ||
) | ||
.await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use prbot_database_interface::DbService; | ||
use prbot_models::{PullRequest, PullRequestRule, Repository, RuleAction, RuleCondition}; | ||
|
||
use super::{ApplyPullRequestRules, ApplyPullRequestRulesInterface}; | ||
use crate::context::tests::CoreContextTest; | ||
|
||
#[tokio::test] | ||
async fn apply() { | ||
let ctx = CoreContextTest::new(); | ||
let repo = ctx | ||
.db_service | ||
.repositories_create(Repository { | ||
owner: "me".into(), | ||
name: "name".into(), | ||
..Default::default() | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let pr = ctx | ||
.db_service | ||
.pull_requests_create(PullRequest { | ||
repository_id: repo.id, | ||
automerge: false, | ||
..Default::default() | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(pr.automerge, false); | ||
|
||
let rule1 = ctx | ||
.db_service | ||
.pull_request_rules_create(PullRequestRule { | ||
repository_id: repo.id, | ||
name: "Rule 1".into(), | ||
actions: vec![RuleAction::SetAutomerge(true)], | ||
conditions: vec![RuleCondition::Author("me".into())], | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let handle = ("me", "name", pr.number).into(); | ||
|
||
ApplyPullRequestRules | ||
.run(&ctx.as_context(), &handle, vec![rule1]) | ||
.await | ||
.unwrap(); | ||
|
||
let pr = ctx | ||
.db_service | ||
.pull_requests_get("me", "name", pr.number) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
assert_eq!(pr.automerge, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.