Merge lp:~cjwatson/launchpad/webhook-payload-links into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 17788
Proposed branch: lp:~cjwatson/launchpad/webhook-payload-links
Merge into: lp:launchpad
Diff against target: 81 lines (+9/-2)
4 files modified
lib/lp/code/model/gitjob.py (+2/-0)
lib/lp/code/model/tests/test_gitjob.py (+3/-1)
lib/lp/codehosting/scanner/events.py (+3/-1)
lib/lp/codehosting/scanner/tests/test_bzrsync.py (+1/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad/webhook-payload-links
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+273087@code.launchpad.net

Commit message

Add webservice links to webhook payloads.

Description of the change

Add webservice links to webhook payloads. This makes it easier to write webhooks that respond by making webservice queries without always having to look up the object first; it also gives us a better pattern for adding webhook support to other objects that don't necessarily have an obvious equivalent of shortened_path, such as merge proposals.

The links lack host name and API version since at least the API version may vary depending on the client, but they're suitable for passing to lp.load.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/model/gitjob.py'
2--- lib/lp/code/model/gitjob.py 2015-09-01 17:10:46 +0000
3+++ lib/lp/code/model/gitjob.py 2015-10-01 15:07:28 +0000
4@@ -65,6 +65,7 @@
5 from lp.services.mail.sendmail import format_address_for_person
6 from lp.services.scripts import log
7 from lp.services.utils import text_delta
8+from lp.services.webapp.publisher import canonical_url
9 from lp.services.webhooks.interfaces import IWebhookSet
10
11
12@@ -227,6 +228,7 @@
13 if old != new:
14 ref_changes[ref] = {"old": old, "new": new}
15 return {
16+ "git_repository": canonical_url(repository, force_local_path=True),
17 "git_repository_path": repository.shortened_path,
18 "ref_changes": ref_changes,
19 }
20
21=== modified file 'lib/lp/code/model/tests/test_gitjob.py'
22--- lib/lp/code/model/tests/test_gitjob.py 2015-08-10 12:49:08 +0000
23+++ lib/lp/code/model/tests/test_gitjob.py 2015-10-01 15:07:28 +0000
24@@ -202,6 +202,7 @@
25 MatchesStructure(
26 event_type=Equals('git:push:0.1'),
27 payload=MatchesDict({
28+ 'git_repository': Equals('/' + repository.unique_name),
29 'git_repository_path': Equals(repository.unique_name),
30 'ref_changes': Equals({
31 'refs/tags/1.0': {
32@@ -230,7 +231,8 @@
33 payload = GitRefScanJob.composeWebhookPayload(
34 repository, new_refs, removed_refs)
35 self.assertEqual(
36- {'git_repository_path': repository.unique_name,
37+ {'git_repository': '/' + repository.unique_name,
38+ 'git_repository_path': repository.unique_name,
39 'ref_changes': {
40 'refs/heads/master': {
41 'old': {'commit_sha1': sha1('refs/heads/master')},
42
43=== modified file 'lib/lp/codehosting/scanner/events.py'
44--- lib/lp/codehosting/scanner/events.py 2015-09-29 00:24:29 +0000
45+++ lib/lp/codehosting/scanner/events.py 2015-10-01 15:07:28 +0000
46@@ -10,13 +10,14 @@
47 'TipChanged',
48 ]
49
50-
51 from zope.component.interfaces import (
52 IObjectEvent,
53 ObjectEvent,
54 )
55 from zope.interface import implementer
56
57+from lp.services.webapp.publisher import canonical_url
58+
59
60 class ScannerEvent(ObjectEvent):
61 """Base scanner event."""
62@@ -85,6 +86,7 @@
63 @staticmethod
64 def composeWebhookPayload(branch, old_revid, new_revid):
65 return {
66+ "bzr_branch": canonical_url(branch, force_local_path=True),
67 "bzr_branch_path": branch.shortened_path,
68 "old": {"revision_id": old_revid},
69 "new": {"revision_id": new_revid},
70
71=== modified file 'lib/lp/codehosting/scanner/tests/test_bzrsync.py'
72--- lib/lp/codehosting/scanner/tests/test_bzrsync.py 2015-09-29 00:24:29 +0000
73+++ lib/lp/codehosting/scanner/tests/test_bzrsync.py 2015-10-01 15:07:28 +0000
74@@ -769,6 +769,7 @@
75 MatchesStructure(
76 event_type=Equals("bzr:push:0.1"),
77 payload=MatchesDict({
78+ "bzr_branch": Equals("/" + self.db_branch.unique_name),
79 "bzr_branch_path": Equals(self.db_branch.shortened_path),
80 "old": Equals({"revision_id": old_revid}),
81 "new": Equals({"revision_id": new_revid}),