Repository: smarkets/marge-bot

Issue:
unexpected merge state locked
```
marge-bot_1  | 2018-02-05 12:39:47,026 INFO Commit id to merge 'bb052510c9a6357fe733b793f4c1f5b38a32e89e' (into: 'aa5d3b11e3768d34f74bde8de7095a629d76cba1')
marge-bot_1  | 2018-02-05 12:39:53,476 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:40:03,757 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:40:14,265 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:40:24,748 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:40:35,059 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:40:45,633 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:40:55,966 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:41:06,263 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:41:16,685 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:41:27,039 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:41:37,310 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:41:47,509 INFO Giving 10 more secs for !125 to be merged...
marge-bot_1  | 2018-02-05 12:41:57,879 ERROR Unexpected Exception
marge-bot_1  | Traceback (most recent call last):
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/job.py", line 55, in execute
marge-bot_1  |     self.rebase_and_accept(approvals)
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/job.py", line 198, in rebase_and_accept
marge-bot_1  |     self.wait_for_branch_to_be_merged()
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/job.py", line 238, in wait_for_branch_to_be_merged
marge-bot_1  |     assert merge_request.state in ('opened', 'reopened'), merge_request.state
marge-bot_1  | AssertionError: locked
marge-bot_1  | Traceback (most recent call last):
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/bin/.marge.app-wrapped", line 4, in <module>
marge-bot_1  |     marge.app.main()
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/app.py", line 221, in main
marge-bot_1  |     marge_bot.start()
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/bot.py", line 39, in start
marge-bot_1  |     self._run(repo_manager)
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/bot.py", line 108, in _run
marge-bot_1  |     merge_job.execute()
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/job.py", line 55, in execute
marge-bot_1  |     self.rebase_and_accept(approvals)
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/job.py", line 198, in rebase_and_accept
marge-bot_1  |     self.wait_for_branch_to_be_merged()
marge-bot_1  |   File "/nix/store/v4xzqxg6yvhvdqk6w453pig8v7r69v1c-python3.6-marge-0.5.1/lib/python3.6/site-packages/marge/job.py", line 238, in wait_for_branch_to_be_merged
marge-bot_1  |     assert merge_request.state in ('opened', 'reopened'), merge_request.state
marge-bot_1  | AssertionError: locked
```

here's how it looked at gitlab web:

![image](https://user-images.githubusercontent.com/199095/35805166-7aa99aa6-0a83-11e8-8b5b-7ffdd88c4c50.png)


Comments:

Patch:
diff --git a/marge/git.py b/marge/git.py
index fa7c68a..2400001 100644
--- a/marge/git.py
+++ b/marge/git.py
@@ -84,7 +84,7 @@ class Repo(namedtuple('Repo', 'remote_url local_path ssh_key_file timeout')):
     def _fuse_branch(self, strategy, branch, target_branch, source_repo_url=None):
         assert source_repo_url or branch != target_branch, branch
 
-        self.git('fetch', 'origin')
+        self.git('fetch', '--prune', 'origin')
         if source_repo_url:
             # "upsert" remote 'source' and fetch it
             try:
@@ -92,7 +92,7 @@ class Repo(namedtuple('Repo', 'remote_url local_path ssh_key_file timeout')):
             except GitError:
                 pass
             self.git('remote', 'add', 'source', source_repo_url)
-            self.git('fetch', 'source')
+            self.git('fetch', '--prune', 'source')
             self.git('checkout', '-B', branch, 'source/' + branch, '--')
         else:
             self.git('checkout', '-B', branch, 'origin/' + branch, '--')
diff --git a/marge/job.py b/marge/job.py
index 82d80f2..b1022b5 100644
--- a/marge/job.py
+++ b/marge/job.py
@@ -37,7 +37,7 @@ class MergeJob(object):
             return
 
         state = merge_request.state
-        if state not in ('opened', 'reopened'):
+        if state not in ('opened', 'reopened', 'locked'):
             if state in ('merged', 'closed'):
                 log.info('The merge request is already %s!', state)
             else:
@@ -236,7 +236,7 @@ class MergeJob(object):
                 return  # success!
             if merge_request.state == 'closed':
                 raise CannotMerge('someone closed the merge request while merging!')
-            assert merge_request.state in ('opened', 'reopened'), merge_request.state
+            assert merge_request.state in ('opened', 'reopened', 'locked'), merge_request.state
 
             log.info('Giving %s more secs for !%s to be merged...', waiting_time_in_secs, merge_request.iid)
             time.sleep(waiting_time_in_secs)


Test patch:
diff --git a/tests/test_git.py b/tests/test_git.py
index 4569d03..77e5f31 100644
--- a/tests/test_git.py
+++ b/tests/test_git.py
@@ -38,7 +38,7 @@ class TestRepo(object):
         self.repo.rebase('feature_branch', 'master_of_the_universe')
 
         assert get_calls(mocked_run) == [
-            'git -C /tmp/local/path fetch origin',
+            'git -C /tmp/local/path fetch --prune origin',
             'git -C /tmp/local/path checkout -B feature_branch origin/feature_branch --',
             'git -C /tmp/local/path rebase origin/master_of_the_universe',
             'git -C /tmp/local/path rev-parse HEAD'
@@ -48,7 +48,7 @@ class TestRepo(object):
         self.repo.merge('feature_branch', 'master_of_the_universe')
 
         assert get_calls(mocked_run) == [
-            'git -C /tmp/local/path fetch origin',
+            'git -C /tmp/local/path fetch --prune origin',
             'git -C /tmp/local/path checkout -B feature_branch origin/feature_branch --',
             'git -C /tmp/local/path merge origin/master_of_the_universe',
             'git -C /tmp/local/path rev-parse HEAD'