1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
51
53 return datetime.timedelta(0)
54
56
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
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
95
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
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
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