launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #27180
[Merge] ~cjwatson/launchpad:charm-recipe-build-mailer into launchpad:master
Colin Watson has proposed merging ~cjwatson/launchpad:charm-recipe-build-mailer into launchpad:master with ~cjwatson/launchpad:charm-recipe-request-builds-job as a prerequisite.
Commit message:
Add charm recipe build notifications
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/403731
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:charm-recipe-build-mailer into launchpad:master.
diff --git a/lib/lp/charms/emailtemplates/charmrecipebuild-notification.txt b/lib/lp/charms/emailtemplates/charmrecipebuild-notification.txt
new file mode 100644
index 0000000..e7630e3
--- /dev/null
+++ b/lib/lp/charms/emailtemplates/charmrecipebuild-notification.txt
@@ -0,0 +1,9 @@
+ * Charm Recipe: %(recipe_name)s
+ * Project: %(project_name)s
+ * Distroseries: %(distroseries)s
+ * Architecture: %(architecturetag)s
+ * State: %(build_state)s
+ * Duration: %(build_duration)s
+ * Build Log: %(log_url)s
+ * Upload Log: %(upload_log_url)s
+ * Builder: %(builder_url)s
diff --git a/lib/lp/charms/mail/__init__.py b/lib/lp/charms/mail/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/lp/charms/mail/__init__.py
diff --git a/lib/lp/charms/mail/charmrecipebuild.py b/lib/lp/charms/mail/charmrecipebuild.py
new file mode 100644
index 0000000..4486809
--- /dev/null
+++ b/lib/lp/charms/mail/charmrecipebuild.py
@@ -0,0 +1,85 @@
+# Copyright 2021 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+from __future__ import absolute_import, print_function, unicode_literals
+
+__metaclass__ = type
+__all__ = [
+ "CharmRecipeBuildMailer",
+ ]
+
+from lp.app.browser.tales import DurationFormatterAPI
+from lp.services.config import config
+from lp.services.mail.basemailer import (
+ BaseMailer,
+ RecipientReason,
+ )
+from lp.services.webapp import canonical_url
+
+
+class CharmRecipeBuildMailer(BaseMailer):
+
+ app = "charms"
+
+ @classmethod
+ def forStatus(cls, build):
+ """Create a mailer for notifying about charm recipe build status.
+
+ :param build: The relevant build.
+ """
+ requester = build.requester
+ recipients = {requester: RecipientReason.forBuildRequester(requester)}
+ return cls(
+ "[Charm recipe build #%(build_id)d] %(build_title)s",
+ "charmrecipebuild-notification.txt", recipients,
+ config.canonical.noreply_from_address, "charm-recipe-build-status",
+ build)
+
+ def __init__(self, subject, template_name, recipients, from_address,
+ notification_type, build):
+ super(CharmRecipeBuildMailer, self).__init__(
+ subject, template_name, recipients, from_address,
+ notification_type=notification_type)
+ self.build = build
+
+ def _getHeaders(self, email, recipient):
+ """See `BaseMailer`."""
+ headers = super(CharmRecipeBuildMailer, self)._getHeaders(
+ email, recipient)
+ headers["X-Launchpad-Build-State"] = self.build.status.name
+ return headers
+
+ def _getTemplateParams(self, email, recipient):
+ """See `BaseMailer`."""
+ build = self.build
+ params = super(CharmRecipeBuildMailer, self)._getTemplateParams(
+ email, recipient)
+ params.update({
+ "architecturetag": build.distro_arch_series.architecturetag,
+ "build_duration": "",
+ "build_id": build.id,
+ "build_state": build.status.title,
+ "build_title": build.title,
+ "build_url": canonical_url(build),
+ "builder_url": "",
+ "distroseries": build.distro_series,
+ "log_url": "",
+ "project_name": build.recipe.project.name,
+ "recipe_name": build.recipe.name,
+ "upload_log_url": "",
+ })
+ if build.duration is not None:
+ duration_formatter = DurationFormatterAPI(build.duration)
+ params["build_duration"] = duration_formatter.approximateduration()
+ if build.log is not None:
+ params["log_url"] = build.log_url
+ if build.upload_log is not None:
+ params["upload_log_url"] = build.upload_log_url
+ if build.builder is not None:
+ params["builder_url"] = canonical_url(build.builder)
+ return params
+
+ def _getFooter(self, email, recipient, params):
+ """See `BaseMailer`."""
+ return ("%(build_url)s\n"
+ "%(reason)s\n" % params)
diff --git a/lib/lp/charms/model/charmrecipebuild.py b/lib/lp/charms/model/charmrecipebuild.py
index 214f084..24d7ff7 100644
--- a/lib/lp/charms/model/charmrecipebuild.py
+++ b/lib/lp/charms/model/charmrecipebuild.py
@@ -44,6 +44,7 @@ from lp.charms.interfaces.charmrecipebuild import (
ICharmRecipeBuild,
ICharmRecipeBuildSet,
)
+from lp.charms.mail.charmrecipebuild import CharmRecipeBuildMailer
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.model.person import Person
@@ -389,7 +390,8 @@ class CharmRecipeBuild(PackageBuildMixin, StormBase):
return
if self.status == BuildStatus.FULLYBUILT:
return
- # XXX cjwatson 2021-05-28: Send email notifications.
+ mailer = CharmRecipeBuildMailer.forStatus(self)
+ mailer.sendAll()
@implementer(ICharmRecipeBuildSet)
diff --git a/lib/lp/charms/tests/test_charmrecipebuild.py b/lib/lp/charms/tests/test_charmrecipebuild.py
index a6b5557..7258d3a 100644
--- a/lib/lp/charms/tests/test_charmrecipebuild.py
+++ b/lib/lp/charms/tests/test_charmrecipebuild.py
@@ -13,6 +13,7 @@ from datetime import (
)
import pytz
+import six
from testtools.matchers import Equals
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
@@ -22,6 +23,7 @@ from lp.app.errors import NotFoundError
from lp.buildmaster.enums import BuildStatus
from lp.buildmaster.interfaces.buildqueue import IBuildQueue
from lp.buildmaster.interfaces.packagebuild import IPackageBuild
+from lp.buildmaster.interfaces.processor import IProcessorSet
from lp.charms.interfaces.charmrecipe import (
CHARM_RECIPE_ALLOW_CREATE,
CHARM_RECIPE_PRIVATE_FEATURE_FLAG,
@@ -35,6 +37,7 @@ from lp.registry.enums import (
TeamMembershipPolicy,
)
from lp.registry.interfaces.series import SeriesStatus
+from lp.services.config import config
from lp.services.features.testing import FeatureFixture
from lp.services.propertycache import clear_property_cache
from lp.testing import (
@@ -43,9 +46,23 @@ from lp.testing import (
TestCaseWithFactory,
)
from lp.testing.layers import LaunchpadZopelessLayer
+from lp.testing.mail_helpers import pop_notifications
from lp.testing.matchers import HasQueryCount
+expected_body = """\
+ * Charm Recipe: charm-1
+ * Project: charm-project
+ * Distroseries: distro unstable
+ * Architecture: i386
+ * State: Failed to build
+ * Duration: 10 minutes
+ * Build Log: %s
+ * Upload Log: %s
+ * Builder: http://launchpad.test/builders/bob
+"""
+
+
class TestCharmRecipeBuild(TestCaseWithFactory):
layer = LaunchpadZopelessLayer
@@ -239,6 +256,56 @@ class TestCharmRecipeBuild(TestCaseWithFactory):
BuildStatus.BUILDING, slave_status={"revision_id": "dummy"})
self.assertEqual("dummy", self.build.revision_id)
+ def test_notify_fullybuilt(self):
+ # notify does not send mail when a recipe build completes normally.
+ build = self.factory.makeCharmRecipeBuild(
+ status=BuildStatus.FULLYBUILT)
+ build.notify()
+ self.assertEqual(0, len(pop_notifications()))
+
+ def test_notify_packagefail(self):
+ # notify sends mail when a recipe build fails.
+ person = self.factory.makePerson(name="person")
+ project = self.factory.makeProduct(name="charm-project")
+ distribution = self.factory.makeDistribution(name="distro")
+ distroseries = self.factory.makeDistroSeries(
+ distribution=distribution, name="unstable")
+ processor = getUtility(IProcessorSet).getByName("386")
+ das = self.factory.makeDistroArchSeries(
+ distroseries=distroseries, architecturetag="i386",
+ processor=processor)
+ build = self.factory.makeCharmRecipeBuild(
+ name="charm-1", requester=person, owner=person, project=project,
+ distro_arch_series=das, status=BuildStatus.FAILEDTOBUILD,
+ builder=self.factory.makeBuilder(name="bob"),
+ duration=timedelta(minutes=10))
+ build.setLog(self.factory.makeLibraryFileAlias())
+ build.notify()
+ [notification] = pop_notifications()
+ self.assertEqual(
+ config.canonical.noreply_from_address, notification["From"])
+ self.assertEqual(
+ "Person <%s>" % person.preferredemail.email, notification["To"])
+ subject = notification["Subject"].replace("\n ", " ")
+ self.assertEqual(
+ "[Charm recipe build #%d] i386 build of "
+ "/~person/charm-project/+charm/charm-1" % build.id, subject)
+ self.assertEqual(
+ "Requester", notification["X-Launchpad-Message-Rationale"])
+ self.assertEqual(person.name, notification["X-Launchpad-Message-For"])
+ self.assertEqual(
+ "charm-recipe-build-status",
+ notification["X-Launchpad-Notification-Type"])
+ self.assertEqual(
+ "FAILEDTOBUILD", notification["X-Launchpad-Build-State"])
+ body, footer = six.ensure_text(
+ notification.get_payload(decode=True)).split("\n-- \n")
+ self.assertEqual(expected_body % (build.log_url, ""), body)
+ self.assertEqual(
+ "http://launchpad.test/~person/charm-project/+charm/charm-1/"
+ "+build/%d\n"
+ "You are the requester of the build.\n" % build.id, footer)
+
def addFakeBuildLog(self, build):
build.setLog(self.factory.makeLibraryFileAlias("mybuildlog.txt"))