# Buggy code
def handle_repo_push(self, payload):
    changes = []
    project = payload["repository"]["project"]["name"]
    repo_url = payload["repository"]["links"]["self"]["href"]
    web_url = payload["repository"]["links"]["html"]["href"]

    for payload_change in payload["push"]["changes"]:
        if payload_change["new"]:
            age = "new"
            category = "push"
        else:  # when new is null the ref is deleted
            age = "old"
            category = "ref-deleted"

        commit_hash = payload_change[age]["target"]["hash"]

        if payload_change[age]["type"] == "branch":
            branch = GIT_BRANCH_REF.format(payload_change[age]["name"])
        elif payload_change[age]["type"] == "tag":
            branch = GIT_TAG_REF.format(payload_change[age]["name"])

        change = {
            "revision": commit_hash,
            "revlink": "{}/commits/{}".format(web_url, commit_hash),
            "repository": repo_url,
            "author": "{} <{}>".format(
                payload["actor"]["display_name"], payload["actor"]["username"]
            ),
            "comments": "Bitbucket Cloud commit {}".format(commit_hash),
            "branch": branch,
            "project": project,
            "category": category,
        }

        if callable(self._codebase):
            change["codebase"] = self._codebase(payload)
        elif self._codebase is not None:
            change["codebase"] = self._codebase

        changes.append(change)

    return (changes, payload["repository"]["scm"])


# Traceback (builtins.KeyError)
2019-07-02 14:33:40+0300 [_GenericHTTPChannelProtocol,13,18.234.32.226] adding changes from web hook
Traceback (most recent call last):
File "/home/buildbot/sandbox/lib/python3.5/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/buildbot/www/change_hook.py", line 106, in getAndSubmitChanges
changes, src = yield self.getChanges(request)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/twisted/internet/defer.py", line 1613, in unwindGenerator
return _cancellableInlineCallbacks(gen)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/twisted/internet/defer.py", line 1529, in _cancellableInlineCallbacks
_inlineCallbacks(None, g, status)
--- <exception caught here> ---
File "/home/buildbot/sandbox/lib/python3.5/site-packages/buildbot/www/change_hook.py", line 106, in getAndSubmitChanges
changes, src = yield self.getChanges(request)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/buildbot/www/change_hook.py", line 167, in getChanges
changes, src = yield handler.getChanges(request)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/buildbot/www/hooks/bitbucketcloud.py", line 163, in getChanges
return self.process(request)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/buildbot/www/hooks/bitbucketcloud.py", line 54, in process
return handler(payload)
File "/home/buildbot/sandbox/lib/python3.5/site-packages/buildbot/www/hooks/bitbucketcloud.py", line 97, in handle_repo_push
payload['actor']['username']),
builtins.KeyError: 'username'

# Fixed code
def handle_repo_push(self, payload):
    changes = []
    project = payload["repository"].get("project", {"name": "none"})["name"]
    repo_url = payload["repository"]["links"]["self"]["href"]
    web_url = payload["repository"]["links"]["html"]["href"]

    for payload_change in payload["push"]["changes"]:
        if payload_change["new"]:
            age = "new"
            category = "push"
        else:  # when new is null the ref is deleted
            age = "old"
            category = "ref-deleted"

        commit_hash = payload_change[age]["target"]["hash"]

        if payload_change[age]["type"] == "branch":
            branch = GIT_BRANCH_REF.format(payload_change[age]["name"])
        elif payload_change[age]["type"] == "tag":
            branch = GIT_TAG_REF.format(payload_change[age]["name"])

        change = {
            "revision": commit_hash,
            "revlink": "{}/commits/{}".format(web_url, commit_hash),
            "repository": repo_url,
            "author": "{} <{}>".format(
                payload["actor"]["display_name"], payload["actor"]["nickname"]
            ),
            "comments": "Bitbucket Cloud commit {}".format(commit_hash),
            "branch": branch,
            "project": project,
            "category": category,
        }

        if callable(self._codebase):
            change["codebase"] = self._codebase(payload)
        elif self._codebase is not None:
            change["codebase"] = self._codebase

        changes.append(change)

    return (changes, payload["repository"]["scm"])