Skip to content

Commit

Permalink
Implement commenting on failure based on config (#2182)
Browse files Browse the repository at this point in the history
Implement commenting on failure based on config

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 #1911
TODO:

 Update or write new documentation in packit/packit.dev. (packit/packit.dev#735)


RELEASE NOTES BEGIN
We have introduced a new configuration option notifications.failure_comment.message that enables notifying users on failure via a comment using the configured message.
RELEASE NOTES END

Reviewed-by: Laura Barcziová
Reviewed-by: František Lachman <[email protected]>
Reviewed-by: Martin Pitt
  • Loading branch information
softwarefactory-project-zuul[bot] authored Sep 13, 2023
2 parents 39bc4f0 + c2dbd61 commit 3f0fe2d
Show file tree
Hide file tree
Showing 5 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
20 changes: 19 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 Down Expand Up @@ -754,3 +754,21 @@ 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 not (
configured_message := self.job_config.notifications.failure_comment.message
):
return

formatted_message = configured_message.format(
commit_sha=self.db_project_event.commit_sha
)
self.status_reporter.comment(
formatted_message, duplicate_check=DuplicateCheckMode.check_last_comment
)
43 changes: 43 additions & 0 deletions tests/unit/test_build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import pytest
from flexmock import flexmock

from packit.config.notifications import (
NotificationsConfig,
FailureCommentNotificationsConfig,
)
from packit.copr_helper import CoprHelper
from packit.config import (
CommonPackageConfig,
Expand All @@ -23,6 +27,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 +2863,41 @@ 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=FailureCommentNotificationsConfig(
"One of the Copr builds failed for "
"commit {commit_sha}, 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(
"One of the Copr builds failed for commit 123, ping @admin",
duplicate_check=DuplicateCheckMode.check_last_comment,
)
copr_build_helper.notify_about_failure_if_configured()

0 comments on commit 3f0fe2d

Please sign in to comment.