Skip to content

Commit

Permalink
Merge pull request #206 from codecov/scott/ai-pr-review-labels
Browse files Browse the repository at this point in the history
feat: Allow triggering of AI PR review based on PR labels
  • Loading branch information
scott-codecov committed Dec 6, 2023
2 parents 455f7e4 + e545e97 commit cf5611f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 12 deletions.
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ requests==2.31.0
respx==0.20.2
# via -r requirements.in
rfc3986[idna2008]==1.4.0
# via
# httpx
# rfc3986
# via httpx
rsa==4.7.2
# via google-auth
s3transfer==0.3.4
Expand Down
39 changes: 30 additions & 9 deletions tasks/sync_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,7 @@ async def run_async_within_lock(
"pull_updated": False,
"reason": "not_in_provider",
}
if pull.state == "open" and read_yaml_field(
current_yaml, ("ai_pr_review", "enabled"), False
):
log.info(
"Triggering AI PR review task", extra=dict(repoid=repoid, pullid=pullid)
)
self.app.tasks["app.tasks.ai_pr_review.AiPrReview"].apply_async(
kwargs=dict(repoid=repoid, pullid=pullid)
)
self.trigger_ai_pr_review(enriched_pull, current_yaml)
report_service = ReportService(current_yaml)
head_commit = pull.get_head_commit()
if head_commit is None:
Expand Down Expand Up @@ -408,6 +400,35 @@ def was_pr_merged_with_squash(
)
return True

def trigger_ai_pr_review(self, enriched_pull: EnrichedPull, current_yaml: UserYaml):
pull = enriched_pull.database_pull
if pull.state == "open" and read_yaml_field(
current_yaml, ("ai_pr_review", "enabled"), False
):
review_method = read_yaml_field(
current_yaml, ("ai_pr_review", "method"), "auto"
)
label_name = read_yaml_field(
current_yaml, ("ai_pr_review", "label_name"), None
)
pull_labels = enriched_pull.provider_pull.get("labels", [])
if review_method == "auto" or (
review_method == "label" and label_name in pull_labels
):
log.info(
"Triggering AI PR review task",
extra=dict(
repoid=pull.repoid,
pullid=pull.pullid,
review_method=review_method,
lbale_name=label_name,
pull_labels=pull_labels,
),
)
self.app.tasks["app.tasks.ai_pr_review.AiPrReview"].apply_async(
kwargs=dict(repoid=pull.repoid, pullid=pull.pullid)
)


RegisteredPullSyncTask = celery_app.register_task(PullSyncTask())
pull_sync_task = celery_app.tasks[RegisteredPullSyncTask.name]
81 changes: 81 additions & 0 deletions tasks/tests/unit/test_sync_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from database.tests.factories import CommitFactory, PullFactory, RepositoryFactory
from helpers.exceptions import RepositoryWithoutValidBotError
from services.repository import EnrichedPull
from services.yaml import UserYaml
from tasks.sync_pull import PullSyncTask

here = Path(__file__)
Expand Down Expand Up @@ -365,3 +366,83 @@ def test_cache_changes_stores_changed_files_in_redis_if_owner_is_whitelisted(
json.dumps(["f.py"]),
ex=86400,
)

def test_trigger_ai_pr_review(self, dbsession, mocker):
repository = RepositoryFactory.create()
dbsession.add(repository)
dbsession.flush()
base_commit = CommitFactory.create(repository=repository)
head_commit = CommitFactory.create(repository=repository)
pull = PullFactory.create(
repository=repository,
base=base_commit.commitid,
head=head_commit.commitid,
)
pullid = pull.pullid
base_commit.pullid = pullid
head_commit.pullid = pullid
dbsession.add(pull)
dbsession.add(base_commit)
dbsession.add(head_commit)
dbsession.flush()
task = PullSyncTask()
apply_async = mocker.patch.object(
task.app.tasks["app.tasks.ai_pr_review.AiPrReview"], "apply_async"
)
enriched_pull = EnrichedPull(
database_pull=pull,
provider_pull=dict(base=dict(branch="lookatthis"), labels=["ai-pr-review"]),
)
current_yaml = UserYaml.from_dict(
{
"ai_pr_review": {
"enabled": True,
"method": "label",
"label_name": "ai-pr-review",
}
}
)
task.trigger_ai_pr_review(enriched_pull, current_yaml)
apply_async.assert_called_once_with(
kwargs=dict(repoid=pull.repoid, pullid=pull.pullid)
)

apply_async.reset_mock()
current_yaml = UserYaml.from_dict(
{
"ai_pr_review": {
"enabled": True,
}
}
)
task.trigger_ai_pr_review(enriched_pull, current_yaml)
apply_async.assert_called_once_with(
kwargs=dict(repoid=pull.repoid, pullid=pull.pullid)
)

apply_async.reset_mock()
current_yaml = UserYaml.from_dict(
{
"ai_pr_review": {
"enabled": True,
"method": "auto",
}
}
)
task.trigger_ai_pr_review(enriched_pull, current_yaml)
apply_async.assert_called_once_with(
kwargs=dict(repoid=pull.repoid, pullid=pull.pullid)
)

apply_async.reset_mock()
current_yaml = UserYaml.from_dict(
{
"ai_pr_review": {
"enabled": True,
"method": "label",
"label_name": "other",
}
}
)
task.trigger_ai_pr_review(enriched_pull, current_yaml)
assert not apply_async.called

0 comments on commit cf5611f

Please sign in to comment.