Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Srynetix committed Apr 3, 2024
1 parent b46079d commit 87f4a1d
Show file tree
Hide file tree
Showing 49 changed files with 1,298 additions and 128 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.25.0] - 2024-04-03

### Changed

- Project renamed to "prbot" for more clarity
- Use latest stable Rust version

## [0.24.0] - 2023-11-23

### Added
Expand Down
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions crates/prbot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ use use_cases::{
},
gifs::random_gif_from_query::RandomGifFromQuery,
pulls::{
add_pull_request_rule::AddPullRequestRule, apply_pull_request_rules::ApplyPullRequestRules,
automerge_pull_request::AutomergePullRequest,
determine_pull_request_merge_strategy::DeterminePullRequestMergeStrategy,
get_or_create_repository::GetOrCreateRepository, merge_pull_request::MergePullRequest,
process_pull_request_event::ProcessPullRequestEvent,
process_pull_request_opened::ProcessPullRequestOpened, set_step_label::SetStepLabel,
process_pull_request_opened::ProcessPullRequestOpened,
remove_pull_request_rule::RemovePullRequestRule,
resolve_pull_request_rules::ResolvePullRequestRules, set_step_label::SetStepLabel,
synchronize_pull_request::SynchronizePullRequest,
synchronize_pull_request_and_update_status::SynchronizePullRequestAndUpdateStatus,
try_merge_pull_request_from_status::TryMergePullRequestFromStatus,
Expand Down Expand Up @@ -66,7 +69,8 @@ module! {
GetOrCreateRepository, ProcessPullRequestEvent, HandleCheckSuiteEvent,
HandleIssueCommentEvent, HandleReviewEvent, SetPullRequestQaStatus,
UpdateStepLabelFromStatus, CreateOrUpdateCommitStatus, RenameRepository,
DetermineCommitStatus
DetermineCommitStatus, ResolvePullRequestRules, ApplyPullRequestRules,
AddPullRequestRule, RemovePullRequestRule
],
providers = []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl PostWelcomeCommentInterface for PostWelcomeComment {
) -> Result<()> {
CommentApi::post_comment(
ctx.api_service,
pr_handle.repository().owner(),
pr_handle.repository().name(),
pr_handle.repository_path().owner(),
pr_handle.repository_path().name(),
pr_handle.number(),
&format!(
":tada: Welcome, _{}_ ! :tada:\n\
Expand Down
44 changes: 44 additions & 0 deletions crates/prbot-core/src/use_cases/pulls/add_pull_request_rule.rs
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 crates/prbot-core/src/use_cases/pulls/apply_pull_request_rules.rs
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl AutomergePullRequestInterface for AutomergePullRequest {
TryMergePullRequestState::Success(strategy) => {
CommentApi::post_comment(
ctx.api_service,
pr_handle.repository().owner(),
pr_handle.repository().name(),
pr_handle.repository_path().owner(),
pr_handle.repository_path().name(),
pr_handle.number(),
&format!(
"Pull request successfully auto-merged! (strategy: '{}')",
Expand All @@ -59,8 +59,8 @@ impl AutomergePullRequestInterface for AutomergePullRequest {
TryMergePullRequestState::Error => {
CommentApi::post_comment(
ctx.api_service,
pr_handle.repository().owner(),
pr_handle.repository().name(),
pr_handle.repository_path().owner(),
pr_handle.repository_path().name(),
pr_handle.number(),
"Could not auto-merge this pull request because of an error.\nAuto-merge disabled.",
)
Expand Down
4 changes: 2 additions & 2 deletions crates/prbot-core/src/use_cases/pulls/merge_pull_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl MergePullRequestInterface for MergePullRequest {

ctx.api_service
.pulls_merge(
pr_handle.repository().owner(),
pr_handle.repository().name(),
pr_handle.repository_path().owner(),
pr_handle.repository_path().name(),
pr_handle.number(),
&commit_title,
"",
Expand Down
12 changes: 12 additions & 0 deletions crates/prbot-core/src/use_cases/pulls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
pub(crate) mod add_pull_request_rule;
pub(crate) mod apply_pull_request_rules;
pub(crate) mod automerge_pull_request;
pub(crate) mod determine_pull_request_merge_strategy;
pub(crate) mod get_or_create_repository;
pub(crate) mod merge_pull_request;
pub(crate) mod process_pull_request_event;
pub(crate) mod process_pull_request_opened;
pub(crate) mod remove_pull_request_rule;
pub(crate) mod resolve_pull_request_rules;
pub(crate) mod set_step_label;
pub(crate) mod synchronize_pull_request;
pub(crate) mod synchronize_pull_request_and_update_status;
pub(crate) mod try_merge_pull_request_from_status;
pub(crate) mod update_step_label_from_status;

pub use add_pull_request_rule::AddPullRequestRuleInterface;
pub use apply_pull_request_rules::ApplyPullRequestRulesInterface;
pub use automerge_pull_request::AutomergePullRequestInterface;
pub use determine_pull_request_merge_strategy::DeterminePullRequestMergeStrategyInterface;
pub use get_or_create_repository::GetOrCreateRepositoryInterface;
pub use merge_pull_request::MergePullRequestInterface;
pub use process_pull_request_event::ProcessPullRequestEventInterface;
pub use process_pull_request_opened::ProcessPullRequestOpenedInterface;
pub use remove_pull_request_rule::RemovePullRequestRuleInterface;
pub use resolve_pull_request_rules::ResolvePullRequestRulesInterface;
pub use set_step_label::SetStepLabelInterface;
pub use synchronize_pull_request::SynchronizePullRequestInterface;
pub use synchronize_pull_request_and_update_status::SynchronizePullRequestAndUpdateStatusInterface;
Expand All @@ -24,11 +32,15 @@ pub use update_step_label_from_status::UpdateStepLabelFromStatusInterface;

#[cfg(any(test, feature = "testkit"))]
pub use self::{
add_pull_request_rule::MockAddPullRequestRuleInterface,
apply_pull_request_rules::MockApplyPullRequestRulesInterface,
automerge_pull_request::MockAutomergePullRequestInterface,
determine_pull_request_merge_strategy::MockDeterminePullRequestMergeStrategyInterface,
get_or_create_repository::MockGetOrCreateRepositoryInterface,
merge_pull_request::MockMergePullRequestInterface,
process_pull_request_event::MockProcessPullRequestEventInterface,
remove_pull_request_rule::MockRemovePullRequestRuleInterface,
resolve_pull_request_rules::MockResolvePullRequestRulesInterface,
set_step_label::MockSetStepLabelInterface,
synchronize_pull_request::MockSynchronizePullRequestInterface,
synchronize_pull_request_and_update_status::MockSynchronizePullRequestAndUpdateStatusInterface,
Expand Down
Loading

0 comments on commit 87f4a1d

Please sign in to comment.