Package buildbot :: Package status :: Package web :: Package hooks :: Module github
[frames] | no frames]

Source Code for Module buildbot.status.web.hooks.github

  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  #!/usr/bin/env python 
 17  """ 
 18  github_buildbot.py is based on git_buildbot.py 
 19   
 20  github_buildbot.py will determine the repository information from the JSON  
 21  HTTP POST it receives from github.com and build the appropriate repository. 
 22  If your github repository is private, you must add a ssh key to the github 
 23  repository for the user who initiated the build on the buildslave. 
 24   
 25  """ 
 26   
 27  import re 
 28  import datetime 
 29  from twisted.python import log 
 30  import calendar 
 31   
 32  try: 
 33      import json 
 34      assert json 
 35  except ImportError: 
 36      import simplejson as json 
 37   
 38  # python is silly about how it handles timezones 
39 -class fixedOffset(datetime.tzinfo):
40 """ 41 fixed offset timezone 42 """
43 - def __init__(self, minutes, hours, offsetSign = 1):
44 self.minutes = int(minutes) * offsetSign 45 self.hours = int(hours) * offsetSign 46 self.offset = datetime.timedelta(minutes = self.minutes, 47 hours = self.hours)
48
49 - def utcoffset(self, dt):
50 return self.offset
51
52 - def dst(self, dt):
53 return datetime.timedelta(0)
54
55 -def convertTime(myTestTimestamp):
56 #"1970-01-01T00:00:00+00:00" 57 matcher = re.compile(r'(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)([-+])(\d\d):(\d\d)') 58 result = matcher.match(myTestTimestamp) 59 (year, month, day, hour, minute, second, offsetsign, houroffset, minoffset) = \ 60 result.groups() 61 if offsetsign == '+': 62 offsetsign = 1 63 else: 64 offsetsign = -1 65 66 offsetTimezone = fixedOffset( minoffset, houroffset, offsetsign ) 67 myDatetime = datetime.datetime( int(year), 68 int(month), 69 int(day), 70 int(hour), 71 int(minute), 72 int(second), 73 0, 74 offsetTimezone) 75 return calendar.timegm( myDatetime.utctimetuple() )
76
77 -def getChanges(request, options = None):
78 """ 79 Reponds only to POST events and starts the build process 80 81 :arguments: 82 request 83 the http request object 84 """ 85 payload = json.loads(request.args['payload'][0]) 86 user = payload['repository']['owner']['name'] 87 repo = payload['repository']['name'] 88 repo_url = payload['repository']['url'] 89 project = request.args.get('project', None) 90 if project: 91 project = project[0] 92 elif project is None: 93 project = '' 94 # This field is unused: 95 #private = payload['repository']['private'] 96 changes = process_change(payload, user, repo, repo_url, project) 97 log.msg("Received %s changes from github" % len(changes)) 98 return (changes, 'git')
99
100 -def process_change(payload, user, repo, repo_url, project):
101 """ 102 Consumes the JSON as a python object and actually starts the build. 103 104 :arguments: 105 payload 106 Python Object that represents the JSON sent by GitHub Service 107 Hook. 108 """ 109 changes = [] 110 newrev = payload['after'] 111 refname = payload['ref'] 112 113 # We only care about regular heads, i.e. branches 114 match = re.match(r"^refs\/heads\/(.+)$", refname) 115 if not match: 116 log.msg("Ignoring refname `%s': Not a branch" % refname) 117 return [] 118 119 branch = match.group(1) 120 if re.match(r"^0*$", newrev): 121 log.msg("Branch `%s' deleted, ignoring" % branch) 122 return [] 123 else: 124 for commit in payload['commits']: 125 files = [] 126 if 'added' in commit: 127 files.extend(commit['added']) 128 if 'modified' in commit: 129 files.extend(commit['modified']) 130 if 'removed' in commit: 131 files.extend(commit['removed']) 132 when = convertTime( commit['timestamp']) 133 log.msg("New revision: %s" % commit['id'][:8]) 134 chdict = dict( 135 who = commit['author']['name'] 136 + " <" + commit['author']['email'] + ">", 137 files = files, 138 comments = commit['message'], 139 revision = commit['id'], 140 when = when, 141 branch = branch, 142 revlink = commit['url'], 143 repository = repo_url, 144 project = project) 145 changes.append(chdict) 146 return changes
147