[#7808] ticket:703 Improve GH wiki presence check
diff --git a/ForgeImporters/forgeimporters/github/tests/test_wiki.py b/ForgeImporters/forgeimporters/github/tests/test_wiki.py
index 50e7278..6523de6 100644
--- a/ForgeImporters/forgeimporters/github/tests/test_wiki.py
+++ b/ForgeImporters/forgeimporters/github/tests/test_wiki.py
@@ -21,6 +21,7 @@
 from nose.tools import assert_equal
 from mock import Mock, patch, call
 from ming.odm import ThreadLocalORMSession
+import git
 
 from IPython.testing.decorators import module_not_available, skipif
 from allura import model as M
@@ -50,7 +51,9 @@
     @patch('forgeimporters.github.wiki.g')
     @patch('forgeimporters.github.wiki.GitHubProjectExtractor')
     def test_import_tool_happy_path(self, ghpe, g, tlorms, M):
-        with patch('forgeimporters.github.wiki.GitHubWikiImporter.import_pages'), patch('forgeimporters.github.wiki.c'):
+        with patch('forgeimporters.github.wiki.GitHubWikiImporter.import_pages'),\
+             patch('forgeimporters.github.wiki.GitHubWikiImporter.has_wiki_repo', return_value=True),\
+             patch('forgeimporters.github.wiki.c'):
             ghpe.return_value.has_wiki.return_value = True
             p = self._make_project(gh_proj_name='myproject')
             u = Mock(name='c.user')
@@ -522,6 +525,22 @@
         result = u'<p><strong>[[this checklist|Troubleshooting]]</strong></p>'
         assert_equal(f(source, 't.textile').strip(), result)
 
+    @patch('forgeimporters.github.wiki.mkdtemp', autospec=True)
+    @patch('forgeimporters.github.wiki.rmtree', autospec=True)
+    @patch('forgeimporters.github.wiki.git.Repo', autospec=True)
+    def test_has_wiki_repo(self, repo, rmtree, mkdtemp):
+        mkdtemp.return_value = 'fake path'
+        i = GitHubWikiImporter()
+        assert_equal(i.has_wiki_repo('fake url'), True)
+        repo.clone_from.assert_called_once_with(
+            'fake url', to_path='fake path', bare=True)
+        rmtree.assert_called_once_with('fake path')
+
+        def raise_error(*args, **kw):
+            raise git.GitCommandError('bam', 'bam', 'bam')
+        repo.clone_from.side_effect = raise_error
+        assert_equal(i.has_wiki_repo('fake url'), False)
+
 
 class TestGitHubWikiImportController(TestController, TestCase):
 
diff --git a/ForgeImporters/forgeimporters/github/wiki.py b/ForgeImporters/forgeimporters/github/wiki.py
index f2ed923..5b787d9 100644
--- a/ForgeImporters/forgeimporters/github/wiki.py
+++ b/ForgeImporters/forgeimporters/github/wiki.py
@@ -136,7 +136,11 @@
         project_name = "%s/%s" % (user_name, project_name)
         extractor = GitHubProjectExtractor(project_name, user=user)
         wiki_avail = extractor.has_wiki()
-        if not wiki_avail:
+        # has_wiki only indicates that wiki is enabled, but it does not mean
+        # that it has any pages, so we should check if wiki repo actually
+        # exists
+        wiki_url = extractor.get_page_url('wiki_url')
+        if not wiki_avail or not self.has_wiki_repo(wiki_url):
             return
 
         self.github_wiki_url = extractor.get_page_url(
@@ -158,7 +162,6 @@
             M.session.artifact_orm_session._get().skip_mod_date = True
             with h.push_config(c, app=self.app):
                 try:
-                    wiki_url = extractor.get_page_url('wiki_url')
                     self.import_pages(wiki_url, history=with_history)
                 except git.GitCommandError:
                     log.error(
@@ -254,6 +257,15 @@
         """Convert '-' and '/' into spaces in page name to match github behavior"""
         return name.replace('-', ' ').replace('/', ' ')
 
+    def has_wiki_repo(self, wiki_url):
+        wiki_path = mkdtemp()
+        try:
+            wiki = git.Repo.clone_from(wiki_url, to_path=wiki_path, bare=True)
+        except git.GitCommandError:
+            return False
+        rmtree(wiki_path)
+        return True
+
     def import_pages(self, wiki_url, history=None):
         wiki_path = mkdtemp()
         wiki = git.Repo.clone_from(wiki_url, to_path=wiki_path, bare=True)