File: github_buildbot.py

package info (click to toggle)
buildbot 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,080 kB
  • sloc: python: 174,183; sh: 1,204; makefile: 332; javascript: 119; xml: 16
file content (460 lines) | stat: -rwxr-xr-x 14,706 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env python
"""
github_buildbot.py is based on git_buildbot.py. Last revised on 2014-02-20.

github_buildbot.py will determine the repository information from the JSON
HTTP POST it receives from github.com and build the appropriate repository.
If your github repository is private, you must add a ssh key to the github
repository for the user who initiated the build on the worker.

This version of github_buildbot.py parses v3 of the github webhook api, with the
"application.vnd.github.v3+json" payload. Configure *only* "push" and/or
"pull_request" events to trigger this webhook.

"""

import hmac
import logging
import os
import re
import sys
from hashlib import sha1
from optparse import OptionParser

from future.utils import iteritems
from twisted.cred import credentials
from twisted.internet import reactor
from twisted.spread import pb
from twisted.web import resource
from twisted.web import server

try:
    import json
except ImportError:
    import simplejson as json


ACCEPTED = 202
BAD_REQUEST = 400
INTERNAL_SERVER_ERROR = 500
OK = 200


class GitHubBuildBot(resource.Resource):
    """
    GitHubBuildBot creates the webserver that responds to the GitHub Service
    Hook.
    """

    isLeaf = True
    master = None
    port = None

    def render_POST(self, request):
        """
        Responds only to POST events and starts the build process

        :arguments:
            request
                the http request object
        """

        # All responses are application/json
        request.setHeader(b"Content-Type", b"application/json")

        content = request.content.read()

        # Verify the message if a secret was provided
        #
        # NOTE: We always respond with '400 BAD REQUEST' if we can't
        # validate the message.  This is done to prevent malicious
        # requests from learning about why they failed to POST data
        # to us.
        if self.secret is not None:
            signature = request.getHeader(b"X-Hub-Signature")

            if signature is None:
                logging.error("Rejecting request.  Signature is missing.")
                request.setResponseCode(BAD_REQUEST)
                return json.dumps({"error": "Bad Request."})

            try:
                hash_type, hexdigest = signature.split(b"=")

            except ValueError:
                logging.error("Rejecting request.  Bad signature format.")
                request.setResponseCode(BAD_REQUEST)
                return json.dumps({"error": "Bad Request."})

            else:
                # sha1 is hard coded into github's source code so it's
                # unlikely this will ever change.
                if hash_type != b"sha1":
                    logging.error("Rejecting request.  Unexpected hash type.")
                    request.setResponseCode(BAD_REQUEST)
                    return json.dumps({"error": "Bad Request."})

                mac = hmac.new(self.secret, msg=content, digestmod=sha1)
                if mac.hexdigest() != hexdigest:
                    logging.error("Rejecting request.  Hash mismatch.")
                    request.setResponseCode(BAD_REQUEST)
                    return json.dumps({"error": "Bad Request."})

        event_type = request.getHeader(b"X-GitHub-Event")
        logging.debug(b"X-GitHub-Event: " + event_type)

        handler = getattr(self, 'handle_' + event_type.decode("ascii"), None)

        if handler is None:
            logging.info("Rejecting request. Received unsupported event %r.", event_type)
            request.setResponseCode(BAD_REQUEST)
            return json.dumps({"error": "Bad Request."})

        try:
            content_type = request.getHeader(b"Content-Type")

            if content_type == b"application/json":
                payload = json.loads(content)
            elif content_type == b"application/x-www-form-urlencoded":
                payload = json.loads(request.args["payload"][0])
            else:
                logging.info(
                    "Rejecting request.  Unknown 'Content-Type', received %r", content_type
                )
                request.setResponseCode(BAD_REQUEST)
                return json.dumps({"error": "Bad Request."})

            logging.debug("Payload: " + payload)
            repo = payload['repository']['full_name']
            repo_url = payload['repository']['html_url']
            changes = handler(payload, repo, repo_url)
            self.send_changes(changes, request)
            return server.NOT_DONE_YET

        except Exception as e:
            logging.exception(e)
            request.setResponseCode(INTERNAL_SERVER_ERROR)
            return json.dumps({"error": str(e)})

    def process_change(self, change, branch, repo, repo_url):
        files = change['added'] + change['removed'] + change['modified']
        who = ""
        if 'username' in change['author']:
            who = change['author']['username']
        else:
            who = change['author']['name']
        if 'email' in change['author']:
            who = "{} <{}>".format(who, change['author']['email'])

        comments = change['message']
        if len(comments) > 1024:
            trim = " ... (trimmed, commit message exceeds 1024 characters)"
            comments = comments[: 1024 - len(trim)] + trim

        info_change = {
            'revision': change['id'],
            'revlink': change['url'],
            'who': who,
            'comments': comments,
            'repository': repo_url,
            'files': files,
            'project': repo,
            'branch': branch,
        }

        if self.category:
            info_change['category'] = self.category

        return info_change

    def handle_ping(self, *_):
        return None

    def handle_push(self, payload, repo, repo_url):
        """
        Consumes the JSON as a python object and actually starts the build.

        :arguments:
            payload
                Python Object that represents the JSON sent by GitHub Service
                Hook.
        """
        changes = None
        refname = payload['ref']

        if self.filter_push_branch:
            if refname != f"refs/heads/{self.filter_push_branch}":
                logging.info(
                    f"Ignoring refname '{refname}': Not a push to branch '{self.filter_push_branch}'"
                )
                return changes

        m = re.match(r"^refs/(heads|tags)/(.+)$", refname)
        if not m:
            logging.info("Ignoring refname '%s': Not a branch or a tag", refname)
            return changes

        refname = m.group(2)

        if payload['deleted'] is True:
            logging.info("%r deleted, ignoring", refname)
        else:
            changes = []
            for change in payload['commits']:
                if (self.head_commit or m.group(1) == 'tags') and change['id'] != payload[
                    'head_commit'
                ]['id']:
                    continue
                changes.append(self.process_change(change, refname, repo, repo_url))
        return changes

    def handle_pull_request(self, payload, repo, repo_url):
        """
        Consumes the JSON as a python object and actually starts the build.

        :arguments:
            payload
                Python Object that represents the JSON sent by GitHub Service
                Hook.
        """
        changes = None

        branch = "refs/pull/{}/head".format(payload['number'])

        if payload['action'] not in ("opened", "synchronize"):
            logging.info("PR %r %r, ignoring", payload['number'], payload['action'])
            return None
        else:
            changes = []

            # Create a synthetic change
            change = {
                'id': payload['pull_request']['head']['sha'],
                'message': payload['pull_request']['body'],
                'timestamp': payload['pull_request']['updated_at'],
                'url': payload['pull_request']['html_url'],
                'author': {
                    'username': payload['pull_request']['user']['login'],
                },
                'added': [],
                'removed': [],
                'modified': [],
            }

            changes.append(self.process_change(change, branch, repo, repo_url))
        return changes

    def send_changes(self, changes, request):
        """
        Submit the changes, if any
        """
        if not changes:
            logging.warning("No changes found")
            request.setResponseCode(OK)
            request.write(json.dumps({"result": "No changes found."}))
            request.finish()
            return

        host, port = self.master.split(':')
        port = int(port)

        if self.auth is not None:
            auth = credentials.UsernamePassword(*self.auth.split(":"))
        else:
            auth = credentials.Anonymous()

        factory = pb.PBClientFactory()
        deferred = factory.login(auth)
        reactor.connectTCP(host, port, factory)
        deferred.addErrback(self.connectFailed, request)
        deferred.addCallback(self.connected, changes, request)

    def connectFailed(self, error, request):
        """
        If connection is failed.  Logs the error.
        """
        logging.error("Could not connect to master: %s", error.getErrorMessage())
        request.setResponseCode(INTERNAL_SERVER_ERROR)
        request.write(json.dumps({"error": "Failed to connect to buildbot master."}))
        request.finish()
        return error

    def addChange(self, _, remote, changei, src='git'):
        """
        Sends changes from the commit to the buildmaster.
        """
        logging.debug("addChange %r, %r", remote, changei)
        try:
            change = changei.next()
        except StopIteration:
            remote.broker.transport.loseConnection()
            return None

        logging.info("New revision: %s", change['revision'][:8])
        for key, value in iteritems(change):
            logging.debug("  %s: %s", key, value)

        change['src'] = src
        deferred = remote.callRemote('addChange', change)
        deferred.addCallback(self.addChange, remote, changei, src)
        return deferred

    def connected(self, remote, changes, request):
        """
        Responds to the connected event.
        """
        # By this point we've connected to buildbot so
        # we don't really need to keep github waiting any
        # longer
        request.setResponseCode(ACCEPTED)
        request.write(json.dumps({"result": "Submitting changes."}))
        request.finish()

        return self.addChange(None, remote, changes.__iter__())


def setup_options():
    """
    The main event loop that starts the server and configures it.
    """
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)

    parser.add_option(
        "-p",
        "--port",
        help="Port the HTTP server listens to for the GitHub Service Hook [default: %default]",
        default=9001,
        type=int,
        dest="port",
    )

    parser.add_option(
        "-m",
        "--buildmaster",
        help="Buildbot Master host and port. ie: localhost:9989 [default: %default]",
        default="localhost:9989",
        dest="buildmaster",
    )

    parser.add_option(
        "--auth",
        help="The username and password, separated by a colon, "
        "to use when connecting to buildbot over the "
        "perspective broker.",
        default="change:changepw",
        dest="auth",
    )

    parser.add_option(
        "--head-commit", action="store_true", help="If set, only trigger builds for commits at head"
    )

    parser.add_option(
        "--secret",
        help="If provided then use the X-Hub-Signature header "
        "to verify that the request is coming from "
        "github. [default: %default]",
        default=None,
        dest="secret",
    )

    parser.add_option(
        "-l",
        "--log",
        help="The absolute path, including filename, to save the "
        "log to [default: %default].  This may also be "
        "'stdout' indicating logs should output directly to "
        "standard output instead.",
        default="github_buildbot.log",
        dest="log",
    )

    parser.add_option(
        "-L",
        "--level",
        help="The logging level: debug, info, warn, error, fatal [default: %default]",
        default='warn',
        dest="level",
        choices=("debug", "info", "warn", "error", "fatal"),
    )

    parser.add_option(
        "-g",
        "--github",
        help="The github server.  Changing this is useful if"
        " you've specified a specific HOST handle in "
        "~/.ssh/config for github [default: %default]",
        default='github.com',
        dest="github",
    )

    parser.add_option(
        "--pidfile",
        help="Write the process identifier (PID) to this "
        "file on start. The file is removed on clean "
        "exit. [default: %default]",
        default=None,
        dest="pidfile",
    )

    parser.add_option(
        "--category", help="Category for the build change", default=None, dest="category"
    )

    parser.add_option(
        "--filter-push-branch",
        help="Only trigger builds for pushes to a given branch name.",
        default=None,
        dest="filter_push_branch",
    )

    (options, _) = parser.parse_args()

    if options.auth is not None and ":" not in options.auth:
        parser.error("--auth did not contain ':'")

    if options.pidfile:
        with open(options.pidfile, 'w') as f:
            f.write(str(os.getpid()))

    filename = options.log
    log_format = "%(asctime)s - %(levelname)s - %(message)s"
    if options.log != "stdout":
        logging.basicConfig(
            filename=filename, format=log_format, level=logging._levelNames[options.level.upper()]
        )
    else:
        logging.basicConfig(
            format=log_format,
            handlers=[logging.StreamHandler(stream=sys.stdout)],
            level=logging._levelNames[options.level.upper()],
        )

    return options


def run_hook(options):
    github_bot = GitHubBuildBot()
    github_bot.github = options.github
    github_bot.master = options.buildmaster
    github_bot.secret = options.secret
    github_bot.auth = options.auth
    github_bot.head_commit = options.head_commit
    github_bot.category = options.category
    github_bot.filter_push_branch = options.filter_push_branch

    site = server.Site(github_bot)
    reactor.listenTCP(options.port, site)

    reactor.run()


def main():
    options = setup_options()

    run_hook(options)


if __name__ == '__main__':
    main()