# Path: gg/main.py
# class Config:
#     def __init__(self):
#         self.verbose = False  # default
#         self.configfile = DEFAULT_CONFIGFILE
#         try:
#             self.repo = get_repo()
#         except git.InvalidGitRepositoryError as exception:
#             error_out(f"{exception.args[0]} is not a git repository")
# 
# Path: gg/builtins/start/gg_start.py
# @cli.command()
# @click.argument("bugnumber", default="")
# @pass_config
# def start(config, bugnumber=""):
#     """Create a new topic branch."""
#     repo = config.repo
# 
#     if bugnumber:
#         summary, bugnumber, url = get_summary(config, bugnumber)
#     else:
#         url = None
#         summary = None
# 
#     if summary:
#         summary = input('Summary ["{}"]: '.format(summary)).strip() or summary
#     else:
#         summary = input("Summary: ").strip()
# 
#     branch_name = ""
#     if bugnumber:
#         if is_github({"bugnumber": bugnumber, "url": url}):
#             branch_name = "{}-".format(bugnumber)
#         else:
#             branch_name = "{}-".format(bugnumber)
# 
#     def clean_branch_name(string):
#         string = re.sub(r"\s+", " ", string)
#         for each in " |":
#             string = string.replace(each, "-")
#         for each in ("->", "=>"):
#             string = string.replace(each, "-")
#         for each in "@%^&:'\"/(),[]{}!.?`$<>#*;=":
#             string = string.replace(each, "")
#         string = re.sub("-+", "-", string)
#         string = string.strip("-")
#         return string.lower().strip()
# 
#     branch_name += clean_branch_name(summary)
# 
#     if not branch_name:
#         error_out("Must provide a branch name")
# 
#     # Check that the branch doesn't already exist
#     found = list(find(repo, branch_name, exact=True))
#     if found:
#         error_out("There is already a branch called {!r}".format(found[0].name))
# 
#     new_branch = repo.create_head(branch_name)
#     new_branch.checkout()
#     if config.verbose:
#         click.echo("Checkout out new branch: {}".format(branch_name))
# 
#     save(config.configfile, summary, branch_name, bugnumber=bugnumber, url=url)
# 
# def parse_remote_url(url):
#     """return a tuple of (org, repo) from the remote git URL"""
#     # The URL will either be git@github.com:org/repo.git or
#     # https://github.com/org/repo.git and in both cases
#     # it's not guarantee that the domain is github.com.
#     # FIXME: Make it work non-github.com domains
#     if url.startswith("git@"):
#         path = url.split(":", 1)[1]
#     else:
#         parsed = urllib.parse.urlparse(url)
#         path = parsed.path[1:]
# 
#     assert path.endswith(".git"), path
#     path = path[:-4]
#     return path.split("/")


import json
import os
import tempfile
import shutil
import pytest
import requests_mock
from click.testing import CliRunner
from gg.main import Config
from gg.builtins.start.gg_start import start, parse_remote_url
    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    result = runner.invoke(
        start,
        ["https://bugzilla.mozilla.org/show_bug.cgi?id=123456"],
        input='foo "bar"\n',
        obj=config,
    )
    if result.exception:
        raise result.exception
    assert result.exit_code == 0
    assert not result.exception

    mocked_git().create_head.assert_called_with("123456-foo-bar")
    mocked_git().create_head().checkout.assert_called_with()

    with open(temp_configfile) as f:
        saved = json.load(f)

        key = "gg-start-test:123456-foo-bar"
        assert key in saved
        assert saved[key]["description"] == 'foo "bar"'
        assert saved[key]["date"]


def test_parse_remote_url():
    org, repo = parse_remote_url("git@github.com:org/repo.git")
    assert org == "org"
    assert repo == "repo"

# Path: gg/main.py
# class Config:
#     def __init__(self):
#         self.verbose = False  # default
#         self.configfile = DEFAULT_CONFIGFILE
#         try:
#             self.repo = get_repo()
#         except git.InvalidGitRepositoryError as exception:
#             error_out(f"{exception.args[0]} is not a git repository")
# 
# Path: gg/builtins/start/gg_start.py
# @cli.command()
# @click.argument("bugnumber", default="")
# @pass_config
# def start(config, bugnumber=""):
#     """Create a new topic branch."""
#     repo = config.repo
# 
#     if bugnumber:
#         summary, bugnumber, url = get_summary(config, bugnumber)
#     else:
#         url = None
#         summary = None
# 
#     if summary:
#         summary = input('Summary ["{}"]: '.format(summary)).strip() or summary
#     else:
#         summary = input("Summary: ").strip()
# 
#     branch_name = ""
#     if bugnumber:
#         if is_github({"bugnumber": bugnumber, "url": url}):
#             branch_name = "{}-".format(bugnumber)
#         else:
#             branch_name = "{}-".format(bugnumber)
# 
#     def clean_branch_name(string):
#         string = re.sub(r"\s+", " ", string)
#         for each in " |":
#             string = string.replace(each, "-")
#         for each in ("->", "=>"):
#             string = string.replace(each, "-")
#         for each in "@%^&:'\"/(),[]{}!.?`$<>#*;=":
#             string = string.replace(each, "")
#         string = re.sub("-+", "-", string)
#         string = string.strip("-")
#         return string.lower().strip()
# 
#     branch_name += clean_branch_name(summary)
# 
#     if not branch_name:
#         error_out("Must provide a branch name")
# 
#     # Check that the branch doesn't already exist
#     found = list(find(repo, branch_name, exact=True))
#     if found:
#         error_out("There is already a branch called {!r}".format(found[0].name))
# 
#     new_branch = repo.create_head(branch_name)
#     new_branch.checkout()
#     if config.verbose:
#         click.echo("Checkout out new branch: {}".format(branch_name))
# 
#     save(config.configfile, summary, branch_name, bugnumber=bugnumber, url=url)
# 
# def parse_remote_url(url):
#     """return a tuple of (org, repo) from the remote git URL"""
#     # The URL will either be git@github.com:org/repo.git or
#     # https://github.com/org/repo.git and in both cases
#     # it's not guarantee that the domain is github.com.
#     # FIXME: Make it work non-github.com domains
#     if url.startswith("git@"):
#         path = url.split(":", 1)[1]
#     else:
#         parsed = urllib.parse.urlparse(url)
#         path = parsed.path[1:]
# 
#     assert path.endswith(".git"), path
#     path = path[:-4]
#     return path.split("/")


# Path: gg/builtins/start/tests/test_gg_start.py
import json
import os
import tempfile
import shutil
import pytest
import requests_mock
from click.testing import CliRunner
from gg.main import Config
from gg.builtins.start.gg_start import start, parse_remote_url

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    result = runner.invoke(
        start,
        ["https://bugzilla.mozilla.org/show_bug.cgi?id=123456"],
        input='foo "bar"\n',
        obj=config,
    )
    if result.exception:
        raise result.exception
    assert result.exit_code == 0
    assert not result.exception

    mocked_git().create_head.assert_called_with("123456-foo-bar")
    mocked_git().create_head().checkout.assert_called_with()

    with open(temp_configfile) as f:
        saved = json.load(f)

        key = "gg-start-test:123456-foo-bar"
        assert key in saved
        assert saved[key]["description"] == 'foo "bar"'
        assert saved[key]["date"]


def test_parse_remote_url():
    org, repo = parse_remote_url("git@github.com:org/repo.git")
    assert org == "org"
    assert repo == "repo"

    org, repo = parse_remote_url("https://github.com/org/repo.git")