From 52d40a42c3ab99dc7b999302a8539f031f490736 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 02 2017 12:00:04 +0000
Subject: github: Allow --path for giving path to local git repo


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/pagure_importer/commands/github.py b/pagure_importer/commands/github.py
index 070f130..1564859 100644
--- a/pagure_importer/commands/github.py
+++ b/pagure_importer/commands/github.py
@@ -3,7 +3,7 @@ import pagure_importer
 from pagure_importer.app import app, REPO_PATH
 from pagure_importer.utils.importer_github import GithubImporter
 from pagure_importer.utils import (
-    gh_get_contributors, gh_get_issue_users, gh_assemble_users,
+    gh_get_contributors, gh_get_issue_users, gh_assemble_users, get_repo_name,
 )
 
 import pagure_importer.utils.git as gitutils
@@ -21,7 +21,8 @@ from pagure_importer.utils.exceptions import (
               help="Github project like pypingou/pagure")
 @click.option('--nopush', is_flag=True,
               help="Do not push the result of pagure-importer back")
-def github(username, password, project, nopush):
+@click.option('--path', help="Optional, Give path of local git repo (bare)")
+def github(username, path, project, nopush):
     gen_json = click.confirm(
         "Do you want to generate jsons for project's contributers"
         " and issue commentors?")
@@ -29,41 +30,32 @@ def github(username, password, project, nopush):
         gh_get_contributors(username, password, project)
         gh_get_issue_users(username, password, project)
         gh_assemble_users()
-    else:
-        repos = pagure_importer.utils.display_repo()
-        if repos:
-            repo_index = click.prompt(
-                'Choose the import destination repo', default=1)
-            repo_name = repos[int(repo_index)-1]
-
-            newpath, new_repo = gitutils.clone_repo(repo_name, REPO_PATH)
-
-            with GithubImporter(username=username,
-                                password=password,
-                                project=project,
-                                repo_name=repo_name,
-                                repo_folder=REPO_PATH,
-                                nopush=nopush) as github_importer:
-
-                repo = github_importer.github.get_repo(
-                    github_importer.github_project_name)
-                try:
-                    repo_name = repo.name
-                except:
-                    raise GithubRepoNotFound(
-                            'Repo not found, project name wrong')
-                github_importer.import_issues(repo)
-
-                # update the local git repo
-                new_repo = gitutils.update_git(
-                    new_repo,
-                    commit_message='Imported issues from the github project: %s' % repo_name)
-
-                if not nopush:
-                    gitutils.push_repo(new_repo)
-        else:
-            click.echo(
-                'No ticket repository found. Use pgimport clone command')
-            return
-
-    return
+        return
+
+    # get repo's name and path of it's parent directory
+    repo_name, repo_path = get_repo_name(path)
+    newpath, new_repo = gitutils.clone_repo(repo_name, repo_path)
+
+    with GithubImporter(username=username,
+                        password=password,
+                        project=project,
+                        repo_name=repo_name,
+                        repo_folder=repo_path,
+                        nopush=nopush) as github_importer:
+
+        repo = github_importer.github.get_repo(
+            github_importer.github_project_name)
+        try:
+            repo_name = repo.name
+        except:
+            raise GithubRepoNotFound('Repo not found, project name wrong')
+        github_importer.import_issues(repo)
+
+        # update the local git repo
+        new_repo = gitutils.update_git(
+            new_repo,
+            commit_message='Imported issues from the github project: %s' % repo_name)
+
+        if not nopush:
+            gitutils.push_repo(new_repo)
+        return
diff --git a/pagure_importer/utils/__init__.py b/pagure_importer/utils/__init__.py
index 2e11f18..c8d9a56 100644
--- a/pagure_importer/utils/__init__.py
+++ b/pagure_importer/utils/__init__.py
@@ -291,3 +291,36 @@ def get_secure_filename(attachment, filename):
     filename = '%s-%s' % (hashlib.sha256(attachment).hexdigest(),
                           werkzeug.secure_filename(str(filename)))
     return filename
+
+
+def get_repo_name(path):
+    ''' Return the repo name and repo's parent full path, based on the path
+    (if user used --path) and REPO_PATH '''
+
+    if path:
+        path = path.strip().rstrip('/')
+        if not path.startswith('/'):
+            full_path = os.path.join(
+                os.path.abspath(os.path.dirname(__file__)),
+                '../..',
+                path
+            )
+        else:
+            full_path = path
+            path = full_path.rsplit('/')[-1]
+
+        if not os.path.exists(full_path) or not full_path.endswith('.git'):
+            raise FileNotFound(
+                'The path specified of the git repo is either not a bare'
+                ' git repo or it doesn\'t exist'
+            )
+        return (path, os.path.dirname(full_path))
+    repos = display_repo()
+    if repos:
+        repo_index = click.prompt(
+            'Choose the import destination repo', default=1)
+        repo_name = repos[int(repo_index)-1]
+        return (repo_name, REPO_PATH)
+    raise FileNotFound(
+        'No git repo found, please use pgimport clone command first'
+    )

