# Buggy code
def handle_pullrequest(self, payload, refname, category):
    pr_number = int(payload["pullrequest"]["id"])
    repo_url = payload["repository"]["links"]["self"]["href"]
    change = {
        "revision": payload["pullrequest"]["fromRef"]["commit"]["hash"],
        "revlink": payload["pullrequest"]["link"],
        "repository": repo_url,
        "author": "{} <{}>".format(
            payload["actor"]["display_name"], payload["actor"]["username"]
        ),
        "comments": "Bitbucket Cloud Pull Request #{}".format(pr_number),
        "branch": refname,
        "project": payload["repository"]["project"]["name"],
        "category": category,
        "properties": {"pullrequesturl": payload["pullrequest"]["link"]},
    }

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

    return [change], 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_pullrequest(self, payload, refname, category):
    pr_number = int(payload["pullrequest"]["id"])
    repo_url = payload["repository"]["links"]["self"]["href"]
    project = payload["repository"].get("project", {"name": "none"})["name"]
    change = {
        "revision": payload["pullrequest"]["fromRef"]["commit"]["hash"],
        "revlink": payload["pullrequest"]["link"],
        "repository": repo_url,
        "author": "{} <{}>".format(
            payload["actor"]["display_name"], payload["actor"]["nickname"]
        ),
        "comments": "Bitbucket Cloud Pull Request #{}".format(pr_number),
        "branch": refname,
        "project": project,
        "category": category,
        "properties": {"pullrequesturl": payload["pullrequest"]["link"]},
    }

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

    return [change], payload["repository"]["scm"]