Skip to content

Commit

Permalink
Use configured failure message as suffix in comment for downstream jo…
Browse files Browse the repository at this point in the history
…bs (#2199)

Use configured failure message as suffix in comment for downstream jobs

When creating issues/commenting in issue_repository, use the configured notifications.failure_comment.message in downstrem jobs. Followup of #2182

RELEASE NOTES BEGIN
You can now configure notifications.failure_comment.message also for downstream jobs, where the configured message will be used as an extension of the comment added by default by Packit.
RELEASE NOTES END

Reviewed-by: František Lachman <[email protected]>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Sep 26, 2023
2 parents 9a5e671 + 24c02fc commit 1623608
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packit_service/worker/handlers/bodhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
GetBranchesFromIssueMixin,
PackitAPIWithDownstreamMixin,
)
from packit_service.worker.reporting import report_in_issue_repository
from packit_service.worker.reporting import (
report_in_issue_repository,
update_message_with_configured_failure_comment_message,
)
from packit_service.worker.result import TaskResults

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -141,6 +144,10 @@ def report_in_issue_repository(
f"{body}\n{trigger_type_description}\n\n{msg_retrigger}{MSG_GET_IN_TOUCH}\n"
)

body_msg = update_message_with_configured_failure_comment_message(
body_msg, self.job_config
)

report_in_issue_repository(
issue_repository=self.job_config.issue_repository,
service_config=self.service_config,
Expand Down
28 changes: 24 additions & 4 deletions packit_service/worker/handlers/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@
PackitAPIWithDownstreamMixin,
GetSyncReleaseTagMixin,
)
from packit_service.worker.reporting import BaseCommitStatus, report_in_issue_repository
from packit_service.worker.reporting import (
BaseCommitStatus,
report_in_issue_repository,
update_message_with_configured_failure_comment_message,
)
from packit_service.worker.result import TaskResults

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -494,6 +498,10 @@ def _report_errors_for_each_branch(self, message: str) -> None:
)
body_msg = f"{message}{msg_retrigger}\n"

body_msg = update_message_with_configured_failure_comment_message(
body_msg, self.job_config
)

PackageConfigGetter.create_issue_if_needed(
project=self.project,
title=f"{self.job_name_for_reporting.capitalize()} failed for "
Expand Down Expand Up @@ -570,13 +578,22 @@ def get_resolved_bugs(self) -> List[str]:
return bugs.split(",")

def _report_errors_for_each_branch(self, message: str) -> None:
body_msg = (
f"{message}\n\n---\n\n*Get in [touch with us]({CONTACTS_URL}) if you "
f"need some help.*\n"
)
long_message = update_message_with_configured_failure_comment_message(
body_msg, self.job_config
)
short_message = update_message_with_configured_failure_comment_message(
message, self.job_config
)
report_in_issue_repository(
issue_repository=self.job_config.issue_repository,
service_config=self.service_config,
title=f"Pull from upstream failed for release {self.tag}",
message=message
+ f"\n\n---\n\n*Get in [touch with us]({CONTACTS_URL}) if you need some help.*",
comment_to_existing=message,
message=long_message,
comment_to_existing=short_message,
)

def run(self) -> TaskResults:
Expand Down Expand Up @@ -665,6 +682,9 @@ def report_in_issue_repository(self, branch: str, ex: PackitException) -> None:
body_msg = (
f"{body}\n{trigger_type_description}\n\n{msg_retrigger}{MSG_GET_IN_TOUCH}\n"
)
body_msg = update_message_with_configured_failure_comment_message(
body_msg, self.job_config
)

report_in_issue_repository(
issue_repository=self.job_config.issue_repository,
Expand Down
16 changes: 16 additions & 0 deletions packit_service/worker/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from ogr.services.gitlab import GitlabProject
from ogr.services.pagure import PagureProject
from packit.config import JobConfig

from packit_service.config import ServiceConfig, PackageConfigGetter
from packit_service.constants import (
Expand Down Expand Up @@ -569,3 +570,18 @@ def report_in_issue_repository(
message=message,
comment_to_existing=comment_to_existing,
)


def update_message_with_configured_failure_comment_message(
comment: str, job_config: JobConfig
) -> str:
"""
If there is the notifications.failure_comment.message present in the configuration,
append it to the existing message.
"""
configured_failure_message = (
f"\n\n---\n{configured_message}"
if (configured_message := job_config.notifications.failure_comment.message)
else ""
)
return f"{comment}{configured_failure_message}"
13 changes: 8 additions & 5 deletions tests/unit/test_distgit.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
import json
import pytest

from flexmock import flexmock
import pytest
from fasjson_client import Client
from flexmock import flexmock

from ogr.services.github import GithubService
from packit.api import PackitAPI
from packit.config.notifications import NotificationsConfig
from packit_service.config import PackageConfigGetter
from packit_service.worker.events.event import EventData
from packit_service.worker.handlers.distgit import (
ProposeDownstreamHandler,
DownstreamKojiBuildHandler,
AbstractSyncReleaseHandler,
PullFromUpstreamHandler,
)
from packit_service.worker.events.event import EventData
from packit_service.config import PackageConfigGetter


def test_create_one_issue_for_pr():
Expand Down Expand Up @@ -48,7 +49,9 @@ def test_create_one_issue_for_pr():
]
)
flexmock(ProposeDownstreamHandler).should_receive("project").and_return(project)
handler = ProposeDownstreamHandler(None, None, {}, flexmock())
handler = ProposeDownstreamHandler(
None, flexmock(notifications=NotificationsConfig()), {}, flexmock()
)
handler._report_errors_for_each_branch(
{
"f34": "Propose downstream failed for release 056",
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
)
from ogr.services.gitlab import GitlabProject
from ogr.services.pagure import PagureProject
from packit.config.notifications import (
NotificationsConfig,
FailureCommentNotificationsConfig,
)
from packit_service.worker import reporting

from packit_service.worker.reporting import (
Expand All @@ -23,6 +27,7 @@
StatusReporterGitlab,
StatusReporterGithubChecks,
DuplicateCheckMode,
update_message_with_configured_failure_comment_message,
)

create_table_content = StatusReporterGithubChecks._create_table
Expand Down Expand Up @@ -634,3 +639,24 @@ def test_comment(pr_id, commit_sha, duplicate_check, existing_comments, should_c
act_upon.should_receive("commit_comment").never()

reporter.comment(body="foo", duplicate_check=duplicate_check)


@pytest.mark.parametrize(
"comment,configured_message,result",
[
("Some comment", None, "Some comment"),
("Some comment", "hello @admin", "Some comment\n\n---\nhello @admin"),
],
)
def test_update_message_with_configured_failure_comment_message(
comment, configured_message, result
):
job_config = flexmock(
notifications=NotificationsConfig(
failure_comment=FailureCommentNotificationsConfig(configured_message)
)
)
assert (
update_message_with_configured_failure_comment_message(comment, job_config)
== result
)

0 comments on commit 1623608

Please sign in to comment.