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   
 20  # but "the rest" is pretty minimal 
 21  from twisted.web import resource 
 22  import re 
 23  from twisted.python.reflect import namedModule 
 24  from twisted.python.log import msg 
 25  from buildbot.util import json 
 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
40 - def getChild(self, name, request):
41 return self
42
43 - def render_GET(self, request):
44 """ 45 Reponds to events and starts the build process 46 different implementations can decide on what methods they will accept 47 """ 48 return self.render_POST(request)
49
50 - def render_POST(self, request):
51 """ 52 Reponds to events and starts the build process 53 different implementations can decide on what methods they will accept 54 55 :arguments: 56 request 57 the http request object 58 """ 59 60 try: 61 changes = self.getChanges( request ) 62 except ValueError, err: 63 request.setResponseCode(400, err.args[0]) 64 return err.args[0] 65 66 msg("Payload: " + str(request.args)) 67 68 if not changes: 69 msg("No changes found") 70 return "no changes found" 71 submitted = self.submitChanges( changes, request ) 72 return json.dumps(submitted)
73 74
75 - def getChanges(self, request):
76 """ 77 Take the logic from the change hook, and then delegate it 78 to the proper handler 79 http://localhost/change_hook/DIALECT will load up 80 buildmaster/status/web/hooks/DIALECT.py 81 82 and call getChanges() 83 84 the return value is a list of changes 85 86 if DIALECT is unspecified, a sample implementation is provided 87 """ 88 uriRE = re.search(r'^/change_hook/?([a-zA-Z0-9_]*)', request.uri) 89 90 if not uriRE: 91 msg("URI doesn't match change_hook regex: %s" % request.uri) 92 raise ValueError("URI doesn't match change_hook regex: %s" % request.uri) 93 94 changes = [] 95 96 # Was there a dialect provided? 97 if uriRE.group(1): 98 dialect = uriRE.group(1) 99 else: 100 dialect = 'base' 101 102 if dialect in self.dialects.keys(): 103 msg("Attempting to load module buildbot.status.web.hooks." + dialect) 104 tempModule = namedModule('buildbot.status.web.hooks.' + dialect) 105 changes = tempModule.getChanges(request,self.dialects[dialect]) 106 msg("Got the following changes %s" % changes) 107 108 else: 109 m = "The dialect specified, '%s', wasn't whitelisted in change_hook" % dialect 110 msg(m) 111 msg("Note: if dialect is 'base' then it's possible your URL is malformed and we didn't regex it properly") 112 raise ValueError(m) 113 114 return changes
115
116 - def submitChanges(self, changes, request):
117 # get a control object 118 changeMaster = request.site.buildbot_service.master.change_svc 119 submitted = [] 120 for onechange in changes: 121 changeMaster.addChange( onechange ) 122 d = onechange.asDict() 123 msg("injected change %s" % d) 124 submitted.append(d) 125 return submitted
126