We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Log scroll bug

* [x] I have created a file in the `master/buildbot/newsfragment` directory (and read the `README.txt` in that directory)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `master/buildbot/changes/bitbucket.py`
Content:
```
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 import time
16 from datetime import datetime
17 
18 from twisted.internet import defer
19 from twisted.internet import reactor
20 from twisted.python import log
21 from twisted.web import client
22 
23 from buildbot.changes import base
24 from buildbot.util import ascii2unicode
25 from buildbot.util import datetime2epoch
26 from buildbot.util import deferredLocked
27 from buildbot.util import epoch2datetime
28 from buildbot.util import json
29 
30 
31 class BitbucketPullrequestPoller(base.PollingChangeSource):
32 
33     compare_attrs = ("owner", "slug", "branch",
34                      "pollInterval", "useTimestamps",
35                      "category", "project", "pollAtLaunch")
36 
37     db_class_name = 'BitbucketPullrequestPoller'
38 
39     def __init__(self, owner, slug,
40                  branch=None,
41                  pollInterval=10 * 60,
42                  useTimestamps=True,
43                  category=None,
44                  project='',
45                  pullrequest_filter=True,
46                  encoding='utf-8',
47                  pollAtLaunch=False
48                  ):
49 
50         self.owner = owner
51         self.slug = slug
52         self.branch = branch
53         base.PollingChangeSource.__init__(
54             self, name='/'.join([owner, slug]), pollInterval=pollInterval, pollAtLaunch=pollAtLaunch)
55         self.encoding = encoding
56 
57         if hasattr(pullrequest_filter, '__call__'):
58             self.pullrequest_filter = pullrequest_filter
59         else:
60             self.pullrequest_filter = (lambda _: pullrequest_filter)
61 
62         self.lastChange = time.time()
63         self.lastPoll = time.time()
64         self.useTimestamps = useTimestamps
65         self.category = category if callable(
66             category) else ascii2unicode(category)
67         self.project = ascii2unicode(project)
68         self.initLock = defer.DeferredLock()
69 
70     def describe(self):
71         return "BitbucketPullrequestPoller watching the "\
72             "Bitbucket repository %s/%s, branch: %s" % (
73                 self.owner, self.slug, self.branch)
74 
75     @deferredLocked('initLock')
76     def poll(self):
77         d = self._getChanges()
78         d.addCallback(self._processChanges)
79         d.addErrback(self._processChangesFailure)
80         return d
81 
82     def _getChanges(self):
83         self.lastPoll = time.time()
84         log.msg("BitbucketPullrequestPoller: polling "
85                 "Bitbucket repository %s/%s, branch: %s" % (self.owner, self.slug, self.branch))
86         url = "https://bitbucket.org/api/2.0/repositories/%s/%s/pullrequests" % (
87             self.owner, self.slug)
88         return client.getPage(url, timeout=self.pollInterval)
89 
90     @defer.inlineCallbacks
91     def _processChanges(self, page):
92         result = json.loads(page, encoding=self.encoding)
93         for pr in result['values']:
94             branch = pr['source']['branch']['name']
95             nr = int(pr['id'])
96             # Note that this is a short hash. The full length hash can be accessed via the
97             # commit api resource but we want to avoid requesting multiple pages as long as
98             # we are not sure that the pull request is new or updated.
99             revision = pr['source']['commit']['hash']
100 
101             # check branch
102             if not self.branch or branch in self.branch:
103                 current = yield self._getCurrentRev(nr)
104 
105                 if not current or current != revision:
106                     # parse pull request api page (required for the filter)
107                     page = yield client.getPage(str(pr['links']['self']['href']))
108                     pr_json = json.loads(page, encoding=self.encoding)
109 
110                     # filter pull requests by user function
111                     if not self.pullrequest_filter(pr_json):
112                         log.msg('pull request does not match filter')
113                         continue
114 
115                     # access additional information
116                     author = pr['author']['display_name']
117                     prlink = pr['links']['html']['href']
118                     # Get time updated time. Note that the timezone offset is
119                     # ignored.
120                     if self.useTimestamps:
121                         updated = datetime.strptime(
122                             pr['updated_on'].split('.')[0],
123                             '%Y-%m-%dT%H:%M:%S')
124                     else:
125                         updated = epoch2datetime(reactor.seconds())
126                     title = pr['title']
127                     # parse commit api page
128                     page = yield client.getPage(str(pr['source']['commit']['links']['self']['href']))
129                     commit_json = json.loads(page, encoding=self.encoding)
130                     # use the full-length hash from now on
131                     revision = commit_json['hash']
132                     revlink = commit_json['links']['html']['href']
133                     # parse repo api page
134                     page = yield client.getPage(str(pr['source']['repository']['links']['self']['href']))
135                     repo_json = json.loads(page, encoding=self.encoding)
136                     repo = repo_json['links']['html']['href']
137 
138                     # update database
139                     yield self._setCurrentRev(nr, revision)
140                     # emit the change
141                     yield self.master.data.updates.addChange(
142                         author=ascii2unicode(author),
143                         revision=ascii2unicode(revision),
144                         revlink=ascii2unicode(revlink),
145                         comments=u'pull-request #%d: %s\n%s' % (
146                             nr, title, prlink),
147                         when_timestamp=datetime2epoch(updated),
148                         branch=self.branch,
149                         category=self.category,
150                         project=self.project,
151                         repository=ascii2unicode(repo),
152                         src=u'bitbucket',
153                     )
154 
155     def _processChangesFailure(self, f):
156         log.msg('BitbucketPullrequestPoller: json api poll failed')
157         log.err(f)
158         # eat the failure to continue along the defered chain - we still want
159         # to catch up
160         return None
161 
162     def _getCurrentRev(self, pr_id):
163         # Return a deferred datetime object for the given pull request number
164         # or None.
165         d = self._getStateObjectId()
166 
167         @d.addCallback
168         def oid_callback(oid):
169             current = self.master.db.state.getState(
170                 oid, 'pull_request%d' % pr_id, None)
171 
172             @current.addCallback
173             def result_callback(result):
174                 return result
175             return current
176         return d
177 
178     def _setCurrentRev(self, pr_id, rev):
179         # Set the datetime entry for a specifed pull request.
180         d = self._getStateObjectId()
181 
182         @d.addCallback
183         def oid_callback(oid):
184             return self.master.db.state.setState(oid, 'pull_request%d' % pr_id, rev)
185 
186         return d
187 
188     def _getStateObjectId(self):
189         # Return a deferred for object id in state db.
190         return self.master.db.state.getObjectId(
191             '%s/%s#%s' % (self.owner, self.slug, self.branch), self.db_class_name)
192
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/master/buildbot/changes/bitbucket.py b/master/buildbot/changes/bitbucket.py
--- a/master/buildbot/changes/bitbucket.py
+++ b/master/buildbot/changes/bitbucket.py
@@ -102,7 +102,8 @@
             if not self.branch or branch in self.branch:
                 current = yield self._getCurrentRev(nr)
 
-                if not current or current != revision:
+                # compare _short_ hashes to check if the PR has been updated
+                if not current or current[0:12] != revision[0:12]:
                     # parse pull request api page (required for the filter)
                     page = yield client.getPage(str(pr['links']['self']['href']))
                     pr_json = json.loads(page, encoding=self.encoding)

{"golden_diff": "diff --git a/master/buildbot/changes/bitbucket.py b/master/buildbot/changes/bitbucket.py\n--- a/master/buildbot/changes/bitbucket.py\n+++ b/master/buildbot/changes/bitbucket.py\n@@ -102,7 +102,8 @@\n             if not self.branch or branch in self.branch:\n                 current = yield self._getCurrentRev(nr)\n \n-                if not current or current != revision:\n+                # compare _short_ hashes to check if the PR has been updated\n+                if not current or current[0:12] != revision[0:12]:\n                     # parse pull request api page (required for the filter)\n                     page = yield client.getPage(str(pr['links']['self']['href']))\n                     pr_json = json.loads(page, encoding=self.encoding)\n", "issue": "Log scroll bug\n\r\n* [x] I have created a file in the `master/buildbot/newsfragment` directory (and read the `README.txt` in that directory)\r\n\n", "before_files": [{"content": "# This file is part of Buildbot.  Buildbot is free software: you can\n# redistribute it and/or modify it under the terms of the GNU General Public\n# License as published by the Free Software Foundation, version 2.\n#\n# This program is distributed in the hope that it will be useful, but WITHOUT\n# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\n# details.\n#\n# You should have received a copy of the GNU General Public License along with\n# this program; if not, write to the Free Software Foundation, Inc., 51\n# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n#\n# Copyright Buildbot Team Members\nimport time\nfrom datetime import datetime\n\nfrom twisted.internet import defer\nfrom twisted.internet import reactor\nfrom twisted.python import log\nfrom twisted.web import client\n\nfrom buildbot.changes import base\nfrom buildbot.util import ascii2unicode\nfrom buildbot.util import datetime2epoch\nfrom buildbot.util import deferredLocked\nfrom buildbot.util import epoch2datetime\nfrom buildbot.util import json\n\n\nclass BitbucketPullrequestPoller(base.PollingChangeSource):\n\n    compare_attrs = (\"owner\", \"slug\", \"branch\",\n                     \"pollInterval\", \"useTimestamps\",\n                     \"category\", \"project\", \"pollAtLaunch\")\n\n    db_class_name = 'BitbucketPullrequestPoller'\n\n    def __init__(self, owner, slug,\n                 branch=None,\n                 pollInterval=10 * 60,\n                 useTimestamps=True,\n                 category=None,\n                 project='',\n                 pullrequest_filter=True,\n                 encoding='utf-8',\n                 pollAtLaunch=False\n                 ):\n\n        self.owner = owner\n        self.slug = slug\n        self.branch = branch\n        base.PollingChangeSource.__init__(\n            self, name='/'.join([owner, slug]), pollInterval=pollInterval, pollAtLaunch=pollAtLaunch)\n        self.encoding = encoding\n\n        if hasattr(pullrequest_filter, '__call__'):\n            self.pullrequest_filter = pullrequest_filter\n        else:\n            self.pullrequest_filter = (lambda _: pullrequest_filter)\n\n        self.lastChange = time.time()\n        self.lastPoll = time.time()\n        self.useTimestamps = useTimestamps\n        self.category = category if callable(\n            category) else ascii2unicode(category)\n        self.project = ascii2unicode(project)\n        self.initLock = defer.DeferredLock()\n\n    def describe(self):\n        return \"BitbucketPullrequestPoller watching the \"\\\n            \"Bitbucket repository %s/%s, branch: %s\" % (\n                self.owner, self.slug, self.branch)\n\n    @deferredLocked('initLock')\n    def poll(self):\n        d = self._getChanges()\n        d.addCallback(self._processChanges)\n        d.addErrback(self._processChangesFailure)\n        return d\n\n    def _getChanges(self):\n        self.lastPoll = time.time()\n        log.msg(\"BitbucketPullrequestPoller: polling \"\n                \"Bitbucket repository %s/%s, branch: %s\" % (self.owner, self.slug, self.branch))\n        url = \"https://bitbucket.org/api/2.0/repositories/%s/%s/pullrequests\" % (\n            self.owner, self.slug)\n        return client.getPage(url, timeout=self.pollInterval)\n\n    @defer.inlineCallbacks\n    def _processChanges(self, page):\n        result = json.loads(page, encoding=self.encoding)\n        for pr in result['values']:\n            branch = pr['source']['branch']['name']\n            nr = int(pr['id'])\n            # Note that this is a short hash. The full length hash can be accessed via the\n            # commit api resource but we want to avoid requesting multiple pages as long as\n            # we are not sure that the pull request is new or updated.\n            revision = pr['source']['commit']['hash']\n\n            # check branch\n            if not self.branch or branch in self.branch:\n                current = yield self._getCurrentRev(nr)\n\n                if not current or current != revision:\n                    # parse pull request api page (required for the filter)\n                    page = yield client.getPage(str(pr['links']['self']['href']))\n                    pr_json = json.loads(page, encoding=self.encoding)\n\n                    # filter pull requests by user function\n                    if not self.pullrequest_filter(pr_json):\n                        log.msg('pull request does not match filter')\n                        continue\n\n                    # access additional information\n                    author = pr['author']['display_name']\n                    prlink = pr['links']['html']['href']\n                    # Get time updated time. Note that the timezone offset is\n                    # ignored.\n                    if self.useTimestamps:\n                        updated = datetime.strptime(\n                            pr['updated_on'].split('.')[0],\n                            '%Y-%m-%dT%H:%M:%S')\n                    else:\n                        updated = epoch2datetime(reactor.seconds())\n                    title = pr['title']\n                    # parse commit api page\n                    page = yield client.getPage(str(pr['source']['commit']['links']['self']['href']))\n                    commit_json = json.loads(page, encoding=self.encoding)\n                    # use the full-length hash from now on\n                    revision = commit_json['hash']\n                    revlink = commit_json['links']['html']['href']\n                    # parse repo api page\n                    page = yield client.getPage(str(pr['source']['repository']['links']['self']['href']))\n                    repo_json = json.loads(page, encoding=self.encoding)\n                    repo = repo_json['links']['html']['href']\n\n                    # update database\n                    yield self._setCurrentRev(nr, revision)\n                    # emit the change\n                    yield self.master.data.updates.addChange(\n                        author=ascii2unicode(author),\n                        revision=ascii2unicode(revision),\n                        revlink=ascii2unicode(revlink),\n                        comments=u'pull-request #%d: %s\\n%s' % (\n                            nr, title, prlink),\n                        when_timestamp=datetime2epoch(updated),\n                        branch=self.branch,\n                        category=self.category,\n                        project=self.project,\n                        repository=ascii2unicode(repo),\n                        src=u'bitbucket',\n                    )\n\n    def _processChangesFailure(self, f):\n        log.msg('BitbucketPullrequestPoller: json api poll failed')\n        log.err(f)\n        # eat the failure to continue along the defered chain - we still want\n        # to catch up\n        return None\n\n    def _getCurrentRev(self, pr_id):\n        # Return a deferred datetime object for the given pull request number\n        # or None.\n        d = self._getStateObjectId()\n\n        @d.addCallback\n        def oid_callback(oid):\n            current = self.master.db.state.getState(\n                oid, 'pull_request%d' % pr_id, None)\n\n            @current.addCallback\n            def result_callback(result):\n                return result\n            return current\n        return d\n\n    def _setCurrentRev(self, pr_id, rev):\n        # Set the datetime entry for a specifed pull request.\n        d = self._getStateObjectId()\n\n        @d.addCallback\n        def oid_callback(oid):\n            return self.master.db.state.setState(oid, 'pull_request%d' % pr_id, rev)\n\n        return d\n\n    def _getStateObjectId(self):\n        # Return a deferred for object id in state db.\n        return self.master.db.state.getObjectId(\n            '%s/%s#%s' % (self.owner, self.slug, self.branch), self.db_class_name)\n", "path": "master/buildbot/changes/bitbucket.py"}], "after_files": [{"content": "# This file is part of Buildbot.  Buildbot is free software: you can\n# redistribute it and/or modify it under the terms of the GNU General Public\n# License as published by the Free Software Foundation, version 2.\n#\n# This program is distributed in the hope that it will be useful, but WITHOUT\n# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\n# details.\n#\n# You should have received a copy of the GNU General Public License along with\n# this program; if not, write to the Free Software Foundation, Inc., 51\n# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n#\n# Copyright Buildbot Team Members\nimport time\nfrom datetime import datetime\n\nfrom twisted.internet import defer\nfrom twisted.internet import reactor\nfrom twisted.python import log\nfrom twisted.web import client\n\nfrom buildbot.changes import base\nfrom buildbot.util import ascii2unicode\nfrom buildbot.util import datetime2epoch\nfrom buildbot.util import deferredLocked\nfrom buildbot.util import epoch2datetime\nfrom buildbot.util import json\n\n\nclass BitbucketPullrequestPoller(base.PollingChangeSource):\n\n    compare_attrs = (\"owner\", \"slug\", \"branch\",\n                     \"pollInterval\", \"useTimestamps\",\n                     \"category\", \"project\", \"pollAtLaunch\")\n\n    db_class_name = 'BitbucketPullrequestPoller'\n\n    def __init__(self, owner, slug,\n                 branch=None,\n                 pollInterval=10 * 60,\n                 useTimestamps=True,\n                 category=None,\n                 project='',\n                 pullrequest_filter=True,\n                 encoding='utf-8',\n                 pollAtLaunch=False\n                 ):\n\n        self.owner = owner\n        self.slug = slug\n        self.branch = branch\n        base.PollingChangeSource.__init__(\n            self, name='/'.join([owner, slug]), pollInterval=pollInterval, pollAtLaunch=pollAtLaunch)\n        self.encoding = encoding\n\n        if hasattr(pullrequest_filter, '__call__'):\n            self.pullrequest_filter = pullrequest_filter\n        else:\n            self.pullrequest_filter = (lambda _: pullrequest_filter)\n\n        self.lastChange = time.time()\n        self.lastPoll = time.time()\n        self.useTimestamps = useTimestamps\n        self.category = category if callable(\n            category) else ascii2unicode(category)\n        self.project = ascii2unicode(project)\n        self.initLock = defer.DeferredLock()\n\n    def describe(self):\n        return \"BitbucketPullrequestPoller watching the \"\\\n            \"Bitbucket repository %s/%s, branch: %s\" % (\n                self.owner, self.slug, self.branch)\n\n    @deferredLocked('initLock')\n    def poll(self):\n        d = self._getChanges()\n        d.addCallback(self._processChanges)\n        d.addErrback(self._processChangesFailure)\n        return d\n\n    def _getChanges(self):\n        self.lastPoll = time.time()\n        log.msg(\"BitbucketPullrequestPoller: polling \"\n                \"Bitbucket repository %s/%s, branch: %s\" % (self.owner, self.slug, self.branch))\n        url = \"https://bitbucket.org/api/2.0/repositories/%s/%s/pullrequests\" % (\n            self.owner, self.slug)\n        return client.getPage(url, timeout=self.pollInterval)\n\n    @defer.inlineCallbacks\n    def _processChanges(self, page):\n        result = json.loads(page, encoding=self.encoding)\n        for pr in result['values']:\n            branch = pr['source']['branch']['name']\n            nr = int(pr['id'])\n            # Note that this is a short hash. The full length hash can be accessed via the\n            # commit api resource but we want to avoid requesting multiple pages as long as\n            # we are not sure that the pull request is new or updated.\n            revision = pr['source']['commit']['hash']\n\n            # check branch\n            if not self.branch or branch in self.branch:\n                current = yield self._getCurrentRev(nr)\n\n                # compare _short_ hashes to check if the PR has been updated\n                if not current or current[0:12] != revision[0:12]:\n                    # parse pull request api page (required for the filter)\n                    page = yield client.getPage(str(pr['links']['self']['href']))\n                    pr_json = json.loads(page, encoding=self.encoding)\n\n                    # filter pull requests by user function\n                    if not self.pullrequest_filter(pr_json):\n                        log.msg('pull request does not match filter')\n                        continue\n\n                    # access additional information\n                    author = pr['author']['display_name']\n                    prlink = pr['links']['html']['href']\n                    # Get time updated time. Note that the timezone offset is\n                    # ignored.\n                    if self.useTimestamps:\n                        updated = datetime.strptime(\n                            pr['updated_on'].split('.')[0],\n                            '%Y-%m-%dT%H:%M:%S')\n                    else:\n                        updated = epoch2datetime(reactor.seconds())\n                    title = pr['title']\n                    # parse commit api page\n                    page = yield client.getPage(str(pr['source']['commit']['links']['self']['href']))\n                    commit_json = json.loads(page, encoding=self.encoding)\n                    # use the full-length hash from now on\n                    revision = commit_json['hash']\n                    revlink = commit_json['links']['html']['href']\n                    # parse repo api page\n                    page = yield client.getPage(str(pr['source']['repository']['links']['self']['href']))\n                    repo_json = json.loads(page, encoding=self.encoding)\n                    repo = repo_json['links']['html']['href']\n\n                    # update database\n                    yield self._setCurrentRev(nr, revision)\n                    # emit the change\n                    yield self.master.data.updates.addChange(\n                        author=ascii2unicode(author),\n                        revision=ascii2unicode(revision),\n                        revlink=ascii2unicode(revlink),\n                        comments=u'pull-request #%d: %s\\n%s' % (\n                            nr, title, prlink),\n                        when_timestamp=datetime2epoch(updated),\n                        branch=self.branch,\n                        category=self.category,\n                        project=self.project,\n                        repository=ascii2unicode(repo),\n                        src=u'bitbucket',\n                    )\n\n    def _processChangesFailure(self, f):\n        log.msg('BitbucketPullrequestPoller: json api poll failed')\n        log.err(f)\n        # eat the failure to continue along the defered chain - we still want\n        # to catch up\n        return None\n\n    def _getCurrentRev(self, pr_id):\n        # Return a deferred datetime object for the given pull request number\n        # or None.\n        d = self._getStateObjectId()\n\n        @d.addCallback\n        def oid_callback(oid):\n            current = self.master.db.state.getState(\n                oid, 'pull_request%d' % pr_id, None)\n\n            @current.addCallback\n            def result_callback(result):\n                return result\n            return current\n        return d\n\n    def _setCurrentRev(self, pr_id, rev):\n        # Set the datetime entry for a specifed pull request.\n        d = self._getStateObjectId()\n\n        @d.addCallback\n        def oid_callback(oid):\n            return self.master.db.state.setState(oid, 'pull_request%d' % pr_id, rev)\n\n        return d\n\n    def _getStateObjectId(self):\n        # Return a deferred for object id in state db.\n        return self.master.db.state.getObjectId(\n            '%s/%s#%s' % (self.owner, self.slug, self.branch), self.db_class_name)\n", "path": "master/buildbot/changes/bitbucket.py"}]}