/
changes.py
196 lines (167 loc) · 7.55 KB
/
changes.py
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
196
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
import copy
from buildbot.data import base
from buildbot.data import sourcestamps
from buildbot.data import types
from buildbot.process import metrics
from buildbot.process.users import users
from buildbot.util import datetime2epoch
from buildbot.util import epoch2datetime
from twisted.internet import defer
from twisted.internet import reactor
from twisted.python import log
class FixerMixin(object):
@defer.inlineCallbacks
def _fixChange(self, change):
# TODO: make these mods in the DB API
if change:
change = change.copy()
change['when_timestamp'] = datetime2epoch(change['when_timestamp'])
sskey = ('sourcestamps', str(change['sourcestampid']))
change['sourcestamp'] = yield self.master.data.get(sskey)
del change['sourcestampid']
defer.returnValue(change)
class ChangeEndpoint(FixerMixin, base.Endpoint):
isCollection = False
pathPatterns = """
/changes/n:changeid
"""
def get(self, resultSpec, kwargs):
d = self.master.db.changes.getChange(kwargs['changeid'])
d.addCallback(self._fixChange)
return d
class ChangesEndpoint(FixerMixin, base.Endpoint):
isCollection = True
pathPatterns = """
/changes
/builds/n:buildid/changes
/sourcestamps/n:ssid/changes
"""
rootLinkName = 'changes'
@defer.inlineCallbacks
def get(self, resultSpec, kwargs):
buildid = kwargs.get('buildid')
ssid = kwargs.get('ssid')
if buildid is not None:
changes = yield self.master.db.changes.getChangesForBuild(buildid)
elif ssid is not None:
change = yield self.master.db.changes.getChangeFromSSid(ssid)
if change is not None:
changes = [change]
else:
changes = []
else:
changes = yield self.master.db.changes.getChanges()
changes = [(yield self._fixChange(ch)) for ch in changes]
defer.returnValue(changes)
def startConsuming(self, callback, options, kwargs):
return self.master.mq.startConsuming(callback,
('changes', None, 'new'))
class Change(base.ResourceType):
name = "change"
plural = "changes"
endpoints = [ChangeEndpoint, ChangesEndpoint]
eventPathPatterns = """
/changes/:changeid
"""
class EntityType(types.Entity):
changeid = types.Integer()
parent_changeids = types.List(of=types.Integer())
author = types.String()
files = types.List(of=types.String())
comments = types.String()
revision = types.NoneOk(types.String())
when_timestamp = types.Integer()
branch = types.NoneOk(types.String())
category = types.NoneOk(types.String())
revlink = types.NoneOk(types.String())
properties = types.SourcedProperties()
repository = types.String()
project = types.String()
codebase = types.String()
sourcestamp = sourcestamps.SourceStamp.entityType
entityType = EntityType(name)
@base.updateMethod
@defer.inlineCallbacks
def addChange(self, files=None, comments=None, author=None, revision=None,
when_timestamp=None, branch=None, category=None, revlink=u'',
properties={}, repository=u'', codebase=None, project=u'',
src=None, _reactor=reactor):
metrics.MetricCountEvent.log("added_changes", 1)
# add the source to the properties
for k in properties:
properties[k] = (properties[k], u'Change')
# get a user id
if src:
# create user object, returning a corresponding uid
uid = yield users.createUserObject(self.master,
author, src)
else:
uid = None
if callable(category):
pre_change = self.master.config.preChangeGenerator(author=author,
files=files,
comments=comments,
revision=revision,
when_timestamp=when_timestamp,
branch=branch,
revlink=revlink,
properties=properties,
repository=repository,
project=project)
category = category(pre_change)
# set the codebase, either the default, supplied, or generated
if codebase is None \
and self.master.config.codebaseGenerator is not None:
pre_change = self.master.config.preChangeGenerator(author=author,
files=files,
comments=comments,
revision=revision,
when_timestamp=when_timestamp,
branch=branch,
category=category,
revlink=revlink,
properties=properties,
repository=repository,
project=project)
codebase = self.master.config.codebaseGenerator(pre_change)
codebase = unicode(codebase)
else:
codebase = codebase or u''
# add the Change to the database
changeid = yield self.master.db.changes.addChange(
author=author,
files=files,
comments=comments,
revision=revision,
when_timestamp=epoch2datetime(when_timestamp),
branch=branch,
category=category,
revlink=revlink,
properties=properties,
repository=repository,
codebase=codebase,
project=project,
uid=uid,
_reactor=_reactor)
# get the change and munge the result for the notification
change = yield self.master.data.get(('changes', str(changeid)))
change = copy.deepcopy(change)
self.produceEvent(change, 'new')
# log, being careful to handle funny characters
msg = u"added change with revision %s to database" % (revision,)
log.msg(msg.encode('utf-8', 'replace'))
defer.returnValue(changeid)