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

Source Code for Module buildbot.status.web.change_hook

  1  # code inspired/copied from contrib/github_buildbot 
  2  #  and inspired from code from the Chromium project 
  3  # otherwise, Andrew Melo <andrew.melo@gmail.com> wrote the rest 
  4   
  5  # but "the rest" is pretty minimal 
  6  from twisted.web import resource 
  7  from buildbot.status.builder import FAILURE 
  8  import re 
  9  from buildbot import util, interfaces 
 10  import traceback 
 11  import sys 
 12  from buildbot.process.properties import Properties 
 13  from buildbot.changes.changes import Change 
 14  from twisted.python.reflect import namedModule 
 15  from twisted.python.log import msg,err 
 16  from buildbot.util import json 
 17   
18 -class ChangeHookResource(resource.Resource):
19 # this is a cheap sort of template thingy 20 contentType = "text/html; charset=utf-8" 21 children = {}
22 - def __init__(self, dialects={}):
23 """ 24 The keys of 'dialects' select a modules to load under 25 master/buildbot/status/web/hooks/ 26 The value is passed to the module's getChanges function, providing 27 configuration options to the dialect. 28 """ 29 self.dialects = dialects
30
31 - def getChild(self, name, request):
32 return self
33
34 - def render_GET(self, request):
35 """ 36 Reponds to events and starts the build process 37 different implementations can decide on what methods they will accept 38 """ 39 return self.render_POST(request)
40
41 - def render_POST(self, request):
42 """ 43 Reponds to events and starts the build process 44 different implementations can decide on what methods they will accept 45 46 :arguments: 47 request 48 the http request object 49 """ 50 51 try: 52 changes = self.getChanges( request ) 53 except ValueError, err: 54 request.setResponseCode(400, err.args[0]) 55 return err.args[0] 56 57 msg("Payload: " + str(request.args)) 58 59 if not changes: 60 msg("No changes found") 61 return "no changes found" 62 submitted = self.submitChanges( changes, request ) 63 return json.dumps(submitted)
64 65
66 - def getChanges(self, request):
67 """ 68 Take the logic from the change hook, and then delegate it 69 to the proper handler 70 http://localhost/change_hook/DIALECT will load up 71 buildmaster/status/web/hooks/DIALECT.py 72 73 and call getChanges() 74 75 the return value is a list of changes 76 77 if DIALECT is unspecified, a sample implementation is provided 78 """ 79 uriRE = re.search(r'^/change_hook/?([a-zA-Z0-9_]*)', request.uri) 80 81 if not uriRE: 82 msg("URI doesn't match change_hook regex: %s" % request.uri) 83 raise ValueError("URI doesn't match change_hook regex: %s" % request.uri) 84 85 changes = [] 86 87 # Was there a dialect provided? 88 if uriRE.group(1): 89 dialect = uriRE.group(1) 90 else: 91 dialect = 'base' 92 93 if dialect in self.dialects.keys(): 94 msg("Attempting to load module buildbot.status.web.hooks." + dialect) 95 tempModule = namedModule('buildbot.status.web.hooks.' + dialect) 96 changes = tempModule.getChanges(request,self.dialects[dialect]) 97 msg("Got the following changes %s" % changes) 98 99 else: 100 m = "The dialect specified, '%s', wasn't whitelisted in change_hook" % dialect 101 msg(m) 102 msg("Note: if dialect is 'base' then it's possible your URL is malformed and we didn't regex it properly") 103 raise ValueError(m) 104 105 return changes
106
107 - def submitChanges(self, changes, request):
108 # get a control object 109 changeMaster = request.site.buildbot_service.master.change_svc 110 submitted = [] 111 for onechange in changes: 112 changeMaster.addChange( onechange ) 113 d = onechange.asDict() 114 msg("injected change %s" % d) 115 submitted.append(d) 116 return submitted
117