~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
= The BugTrackerPerson interface =

The IBugTrackerPerson interfaces allows Launchpad to link Persons to
bugtrackers. BugTrackerPersons are created using the
linkPersonToSelf() method of IBugTracker.

    >>> from zope.component import getUtility
    >>> from lp.bugs.interfaces.bugtracker import (
    ...     BugTrackerType)
    >>> from lp.registry.interfaces.person import (
    ...     IPersonSet)
    >>> from lp.bugs.tests.externalbugtracker import (
    ...     new_bugtracker)

    >>> sample_person = getUtility(IPersonSet).getByName('name12')

    >>> bugtracker = new_bugtracker(BugTrackerType.BUGZILLA)

We'll rename the bugtracker to make the tests more readable.

    >>> from canonical.database.sqlbase import commit
    >>> from lp.testing.layers import LaunchpadZopelessLayer
    >>> LaunchpadZopelessLayer.switchDbUser('launchpad')

    >>> bugtracker.name = 'bugzilla-checkwatches'
    >>> commit()

    >>> from lp.services.config import config
    >>> LaunchpadZopelessLayer.switchDbUser(config.checkwatches.dbuser)

    >>> bugtracker_person = bugtracker.linkPersonToSelf(
    ...         'some-name-i-made-up', sample_person)

    >>> print bugtracker_person.name
    some-name-i-made-up

    >>> print bugtracker_person.person.name
    name12

    >>> print bugtracker_person.bugtracker.name
    bugzilla-checkwatches

A name can only be registered with a bugtracker once. Trying to link a
new person to a bugtracker using an existing name will cause an error.

    >>> foo_bar = getUtility(IPersonSet).getByName('name16')
    >>> bugtracker_person = bugtracker.linkPersonToSelf(
    ...         'some-name-i-made-up', foo_bar)
    Traceback (most recent call last):
      ...
    BugTrackerPersonAlreadyExists: Name 'some-name-i-made-up' is already in
    use for bugtracker 'bugzilla-checkwatches'.

The BugTrackerPerson record for a given name on a given bugtracker can
be retrieved by calling BugTracker.getLinkedPersonByName().

    >>> bugtracker_person = bugtracker.getLinkedPersonByName(
    ...     'some-name-i-made-up')

    >>> print bugtracker_person.name
    some-name-i-made-up

    >>> print bugtracker_person.person.name
    name12


== ensurePersonForSelf() ==

IBugTracker has a method, ensurePersonForSelf(), which is
responsible for returning the correct BugTrackerPerson for a given
remote username on on a given bugtracker.

Passing a new remote user's details to ensurePersonForSelf() will
return a new Person record.

    >>> from lp.registry.interfaces.person import (
    ...     PersonCreationRationale)

    >>> print getUtility(IPersonSet).getByEmail('new.person@example.com')
    None

    >>> new_person = bugtracker.ensurePersonForSelf(
    ...     display_name='New Person', email='new.person@example.com',
    ...     rationale=PersonCreationRationale.BUGIMPORT,
    ...     creation_comment='whilst testing ensurePersonForSelf().')

    >>> print new_person.displayname
    New Person

There won't be a BugTrackerPerson record linking 'New Person' to the
bugtracker since we have an email address for 'New Person'. That means
that we can always retrieve them reliably when we encounter them in a
remote bugtracker.

    >>> bugtracker_person = bugtracker.getLinkedPersonByName('New Person')
    >>> print bugtracker_person
    None

Calling ensurePersonForSelf() with the same details will return the
same person.

    >>> other_person = bugtracker.ensurePersonForSelf(
    ...     display_name='New Person',
    ...     email='new.person@example.com',
    ...     rationale=PersonCreationRationale.BUGIMPORT,
    ...     creation_comment='whilst testing ensurePersonForSelf().')

    >>> print other_person.name
    new-person

    >>> print new_person.name
    new-person

ensurePersonForSelf() can also handle remote users whose email
addresses aren't provided.

    >>> noemail_person = bugtracker.ensurePersonForSelf(
    ...     display_name='no-email-person',
    ...     email=None, rationale=PersonCreationRationale.BUGIMPORT,
    ...     creation_comment='whilst testing ensurePersonForSelf().')

    >>> print noemail_person.name
    no-email-person-bugzilla-checkwatches

A BugTrackerPerson record will have been created to map
'no-email-person' on our example bugtracker to
'no-email-person-bugzilla-checkwatches-1' in Launchpad.

    >>> bugtracker_person = bugtracker.getLinkedPersonByName(
    ...     'no-email-person')

    >>> bugtracker_person.person == noemail_person
    True

ensurePersonForSelf() handles situations in which bugtrackers have
been renamed, too, and avoids name collisions when doing so.

We'll create a person, 'noemail,' on our example bugtracker.

    >>> new_person = bugtracker.ensurePersonForSelf(
    ...     display_name='noemail',
    ...     email=None, rationale=PersonCreationRationale.BUGIMPORT,
    ...     creation_comment='whilst testing.')

    >>> print new_person.name
    noemail-bugzilla-checkwatches

    >>> bugtracker_person = bugtracker.getLinkedPersonByName('noemail')

    >>> print bugtracker_person.bugtracker.name
    bugzilla-checkwatches

    >>> print bugtracker_person.person.name
    noemail-bugzilla-checkwatches

    >>> commit()

If we rename the BugTracker and then create another with the same name,
calling ensurePersonForSelf() for 'noemail' on that BugTracker
should produce a new Person rather than re-using the existing one.

    >>> other_bug_tracker = new_bugtracker(BugTrackerType.BUGZILLA)

    >>> LaunchpadZopelessLayer.switchDbUser('launchpad')

    >>> bugtracker.name = 'bugzilla-checkwatches-renamed'
    >>> commit()

    >>> other_bug_tracker.name = 'bugzilla-checkwatches'
    >>> commit()

    >>> LaunchpadZopelessLayer.switchDbUser(config.checkwatches.dbuser)

A new Person has been created for 'noemail' on other_bug_tracker, even
though that bug tracker's name is the same as one from which we've
imported previously.

    >>> from lp.bugs.interfaces.bugtracker import IBugTrackerSet
    >>> bugtracker = getUtility(IBugTrackerSet).get(bugtracker.id)
    >>> other_bugtracker = getUtility(IBugTrackerSet).get(
    ...     other_bug_tracker.id)

    >>> original_bugtracker_person = bugtracker.getLinkedPersonByName(
    ...     'noemail')

    >>> new_person = other_bugtracker.ensurePersonForSelf(
    ...     'noemail', None, PersonCreationRationale.BUGIMPORT,
    ...     'while testing, again')

    >>> print original_bugtracker_person.person.name
    noemail-bugzilla-checkwatches

    >>> print new_person.name
    noemail-bugzilla-checkwatches-1