Skip to content

Commit

Permalink
Implement commenting on failure based on config
Browse files Browse the repository at this point in the history
If there is the failure_comment_message defined in the config, notify
users via comment on failure using the configured message. Comment
only once per commit SHA and job (utilising check of previous identical Packit comment).

Fixes packit#1911
  • Loading branch information
lbarcziova committed Sep 12, 2023
1 parent 39bc4f0 commit 7174532
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ def run(self):
chroot=self.copr_event.chroot,
)
self.measure_time_after_reporting()
self.copr_build_helper.notify_about_failure_if_configured()
self.build.set_status(BuildStatus.failure)
return TaskResults(success=False, details={"msg": failed_msg})

Expand Down Expand Up @@ -370,6 +371,7 @@ def handle_srpm_end(self):
description=failed_msg,
url=url,
)
self.copr_build_helper.notify_about_failure_if_configured()
self.build.set_status(BuildStatus.failure)
self.copr_build_helper.monitor_not_submitted_copr_builds(
len(self.copr_build_helper.build_targets), "srpm_failure"
Expand Down
2 changes: 2 additions & 0 deletions packit_service/worker/handlers/koji.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ def run(self):
url=url,
chroot=build.target,
)
if self.koji_task_event.state == KojiTaskState.failed:
build_job_helper.notify_about_failure_if_configured()

koji_build_logs = KojiTaskEvent.get_koji_build_logs_url(
rpm_build_task_id=int(build.build_id),
Expand Down
4 changes: 4 additions & 0 deletions packit_service/worker/handlers/testing_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ def run(self) -> TaskResults:
success=True, details={"msg": "Testing farm results already processed"}
)

failure = False
if self.result == TestingFarmResult.running:
status = BaseCommitStatus.running
summary = self.summary or "Tests are running ..."
Expand All @@ -416,6 +417,7 @@ def run(self) -> TaskResults:
else:
status = BaseCommitStatus.failure
summary = self.summary or "Tests failed ..."
failure = True

if self.result == TestingFarmResult.running:
self.pushgateway.test_runs_started.inc()
Expand All @@ -437,6 +439,8 @@ def run(self) -> TaskResults:
else self.log_url,
links_to_external_services={"Testing Farm": self.log_url},
)
if failure:
self.testing_farm_job_helper.notify_about_failure_if_configured()

test_run_model.set_status(self.result, created=self.created)

Expand Down
22 changes: 21 additions & 1 deletion packit_service/worker/helpers/build/build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from packit_service.worker.events import EventData
from packit_service.worker.helpers.job_helper import BaseJobHelper
from packit_service.worker.monitoring import Pushgateway
from packit_service.worker.reporting import BaseCommitStatus
from packit_service.worker.reporting import BaseCommitStatus, DuplicateCheckMode
from packit_service.worker.result import TaskResults

logger = logging.getLogger(__name__)
Expand All @@ -42,6 +42,7 @@ class BaseBuildJobHelper(BaseJobHelper):
job_type_test: Optional[JobType] = None
status_name_build: str = "base-build-status"
status_name_test: str = "base-test-status"
comment_job_name: str = "jobs"

def __init__(
self,
Expand Down Expand Up @@ -754,3 +755,22 @@ def report_status_to_configured_job(
links_to_external_services=links_to_external_services,
update_feedback_time=update_feedback_time,
)

def notify_about_failure_if_configured(self):
"""
If there is a failure_comment_message configured for the job,
post a comment and include the configured message. Do not post
the comment if the last comment from the Packit user is identical.
"""
if (
configured_message := self.job_config.notifications.failure_comment_message
) is None:
return

full_message = (
f":exclamation: **One of the {self.job_type_build} failed for commit "
f"{self.db_project_event.commit_sha}**\n\n---\n\n{configured_message}"
)
self.status_reporter.comment(
full_message, duplicate_check=DuplicateCheckMode.check_last_comment
)
1 change: 1 addition & 0 deletions packit_service/worker/helpers/build/copr_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class CoprBuildJobHelper(BaseBuildJobHelper):
job_type_test = JobType.tests
status_name_build: str = "rpm-build"
status_name_test: str = "testing-farm"
comment_job_name: str = "Copr builds"

def __init__(
self,
Expand Down
1 change: 1 addition & 0 deletions packit_service/worker/helpers/build/koji_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class KojiBuildJobHelper(BaseBuildJobHelper):
job_type_test = None
status_name_build: str = "koji-build"
status_name_test: str = None
comment_job_name: str = "Koji builds"

def __init__(
self,
Expand Down
1 change: 1 addition & 0 deletions packit_service/worker/helpers/testing_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __init__(self, command_prefix: str, comment: str):

class TestingFarmJobHelper(CoprBuildJobHelper):
__test__ = False
comment_job_name: str = "Testing Farm runs"

def __init__(
self,
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from flexmock import flexmock

from packit.config.notifications import NotificationsConfig
from packit.copr_helper import CoprHelper
from packit.config import (
CommonPackageConfig,
Expand All @@ -23,6 +24,7 @@

# packit.config.aliases.get_aliases() return value example
from packit_service.worker.helpers.testing_farm import TestingFarmJobHelper
from packit_service.worker.reporting import StatusReporter, DuplicateCheckMode

ALIASES = {
"fedora-development": ["fedora-33", "fedora-rawhide"],
Expand Down Expand Up @@ -2858,3 +2860,39 @@ def test_local_project_not_called_when_initializing_api():
flexmock(LocalProject).should_receive("__init__").never()
assert copr_build_helper.api
assert copr_build_helper.api.copr_helper


def test_notify_about_failure_if_configured():
jobs = [
JobConfig(
type=JobType.copr_build,
trigger=JobConfigTriggerType.pull_request,
packages={
"packages": CommonPackageConfig(
notifications=NotificationsConfig(
failure_comment_message="ping @admin"
)
)
},
)
]
copr_build_helper = CoprBuildJobHelper(
service_config=ServiceConfig.get_service_config(),
package_config=PackageConfig(
jobs=jobs, packages={"package": CommonPackageConfig()}
),
job_config=jobs[0],
project=flexmock(),
metadata=flexmock(pr_id=1, commit_sha="123"),
db_project_event=flexmock(id=12, commit_sha="123")
.should_receive("get_project_event_object")
.and_return(flexmock(job_config_trigger_type=JobConfigTriggerType.pull_request))
.mock(),
)

flexmock(StatusReporter).should_receive("comment").with_args(
":exclamation: **One of the JobType.copr_build failed for commit 123**"
"\n\n---\n\nping @admin",
duplicate_check=DuplicateCheckMode.check_last_comment,
)
copr_build_helper.notify_about_failure_if_configured()

0 comments on commit 7174532

Please sign in to comment.