Package buildbot :: Package status :: Package web :: Module change_hook
[frames] | no frames]

Source Code for Module buildbot.status.web.change_hook

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15   
 16  # code inspired/copied from contrib/github_buildbot 
 17  #  and inspired from code from the Chromium project 
 18  # otherwise, Andrew Melo <andrew.melo@gmail.com> wrote the rest 
 19  # but "the rest" is pretty minimal 
 20   
 21  import re 
 22  from twisted.web import resource, server 
 23  from twisted.python.reflect import namedModule 
 24  from twisted.python import log 
 25  from twisted.internet import defer 
26 27 -class ChangeHookResource(resource.Resource):
28 # this is a cheap sort of template thingy 29 contentType = "text/html; charset=utf-8" 30 children = {}
31 - def __init__(self, dialects={}):
32 """ 33 The keys of 'dialects' select a modules to load under 34 master/buildbot/status/web/hooks/ 35 The value is passed to the module's getChanges function, providing 36 configuration options to the dialect. 37 """ 38 self.dialects = dialects 39 self.request_dialect = None
40
41 - def getChild(self, name, request):
42 return self
43
44 - def render_GET(self, request):
45 """ 46 Reponds to events and starts the build process 47 different implementations can decide on what methods they will accept 48 """ 49 return self.render_POST(request)
50
51 - def render_POST(self, request):
52 """ 53 Reponds to events and starts the build process 54 different implementations can decide on what methods they will accept 55 56 :arguments: 57 request 58 the http request object 59 """ 60 61 try: 62 changes, src = self.getChanges( request ) 63 except ValueError, err: 64 request.setResponseCode(400, err.args[0]) 65 return err.args[0] 66 except Exception: 67 log.err(None, "Exception processing web hook.") 68 msg = "Error processing changes." 69 request.setResponseCode(500, msg) 70 return msg 71 72 log.msg("Payload: " + str(request.args)) 73 74 if not changes: 75 log.msg("No changes found") 76 return "no changes found" 77 d = self.submitChanges( changes, request, src ) 78 def ok(_): 79 request.setResponseCode(202) 80 request.finish()
81 def err(_): 82 request.setResponseCode(500) 83 request.finish()
84 d.addCallbacks(ok, err) 85 return server.NOT_DONE_YET 86 87
88 - def getChanges(self, request):
89 """ 90 Take the logic from the change hook, and then delegate it 91 to the proper handler 92 http://localhost/change_hook/DIALECT will load up 93 buildmaster/status/web/hooks/DIALECT.py 94 95 and call getChanges() 96 97 the return value is a list of changes 98 99 if DIALECT is unspecified, a sample implementation is provided 100 """ 101 uriRE = re.search(r'^/change_hook/?([a-zA-Z0-9_]*)', request.uri) 102 103 if not uriRE: 104 log.msg("URI doesn't match change_hook regex: %s" % request.uri) 105 raise ValueError("URI doesn't match change_hook regex: %s" % request.uri) 106 107 changes = [] 108 src = None 109 110 # Was there a dialect provided? 111 if uriRE.group(1): 112 dialect = uriRE.group(1) 113 else: 114 dialect = 'base' 115 116 if dialect in self.dialects.keys(): 117 log.msg("Attempting to load module buildbot.status.web.hooks." + dialect) 118 tempModule = namedModule('buildbot.status.web.hooks.' + dialect) 119 changes, src = tempModule.getChanges(request,self.dialects[dialect]) 120 log.msg("Got the following changes %s" % changes) 121 self.request_dialect = dialect 122 else: 123 m = "The dialect specified, '%s', wasn't whitelisted in change_hook" % dialect 124 log.msg(m) 125 log.msg("Note: if dialect is 'base' then it's possible your URL is malformed and we didn't regex it properly") 126 raise ValueError(m) 127 128 return (changes, src)
129 130 @defer.deferredGenerator
131 - def submitChanges(self, changes, request, src):
132 master = request.site.buildbot_service.master 133 for chdict in changes: 134 wfd = defer.waitForDeferred(master.addChange(src=src, **chdict)) 135 yield wfd 136 change = wfd.getResult() 137 log.msg("injected change %s" % change)
138