Skip to content

Commit

Permalink
Merge pull request #107 from peter-evans/dev
Browse files Browse the repository at this point in the history
Determine target github repository from git config
  • Loading branch information
peter-evans authored Feb 7, 2020
2 parents b706407 + d870062 commit cc7020a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
13 changes: 13 additions & 0 deletions dist/src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def get_random_string(length=7, chars=string.ascii_lowercase + string.digits):
return "".join(random.choice(chars) for _ in range(length))


def parse_github_repository(url):
# Parse the github repository from a URL
# e.g. peter-evans/create-pull-request
pattern = re.compile(r"^https://github.com/(.+/.+)$")

# Check we have a match
match = pattern.match(url)
if match is None:
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")

return match.group(1)


def parse_display_name_email(display_name_email):
# Parse the name and email address from a string in the following format
# Display Name <email@address.com>
Expand Down
13 changes: 12 additions & 1 deletion dist/src/create_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def get_git_config_value(repo, name):
return None


def get_github_repository():
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
if remote_origin_url is None:
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
return cmn.parse_github_repository(remote_origin_url)


def git_user_config_is_set(repo):
name = get_git_config_value(repo, "user.name")
email = get_git_config_value(repo, "user.email")
Expand Down Expand Up @@ -94,7 +101,6 @@ def set_committer_author(repo, committer, author):

# Get required environment variables
github_token = os.environ["GITHUB_TOKEN"]
github_repository = os.environ["GITHUB_REPOSITORY"]
# Get environment variables with defaults
path = os.getenv("CPR_PATH", os.getcwd())
branch = os.getenv("CPR_BRANCH", DEFAULT_BRANCH)
Expand All @@ -107,6 +113,11 @@ def set_committer_author(repo, committer, author):
# Set the repo path
repo = Repo(path)

# Determine the GitHub repository from git config
# This will be the target repository for the pull request
github_repository = get_github_repository()
print(f"Target repository set to {github_repository}")

# Determine if the checked out ref is a valid base for a pull request
# The action needs the checked out HEAD ref to be a branch
# This check will fail in the following cases:
Expand Down
17 changes: 17 additions & 0 deletions dist/src/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ def test_get_random_string():
assert len(cmn.get_random_string(length=20)) == 20


def test_parse_github_repository_success():
repository = cmn.parse_github_repository(
"https://github.com/peter-evans/create-pull-request"
)
assert repository == "peter-evans/create-pull-request"


def test_parse_github_repository_failure():
url = "https://github.com/peter-evans"
with pytest.raises(ValueError) as e_info:
cmn.parse_github_repository(url)
assert (
e_info.value.args[0]
== f"The format of '{url}' is not a valid GitHub repository URL"
)


def test_parse_display_name_email_success():
name, email = cmn.parse_display_name_email("abc def <abc@def.com>")
assert name == "abc def"
Expand Down
13 changes: 13 additions & 0 deletions src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def get_random_string(length=7, chars=string.ascii_lowercase + string.digits):
return "".join(random.choice(chars) for _ in range(length))


def parse_github_repository(url):
# Parse the github repository from a URL
# e.g. peter-evans/create-pull-request
pattern = re.compile(r"^https://github.com/(.+/.+)$")

# Check we have a match
match = pattern.match(url)
if match is None:
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")

return match.group(1)


def parse_display_name_email(display_name_email):
# Parse the name and email address from a string in the following format
# Display Name <email@address.com>
Expand Down
13 changes: 12 additions & 1 deletion src/create_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def get_git_config_value(repo, name):
return None


def get_github_repository():
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
if remote_origin_url is None:
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
return cmn.parse_github_repository(remote_origin_url)


def git_user_config_is_set(repo):
name = get_git_config_value(repo, "user.name")
email = get_git_config_value(repo, "user.email")
Expand Down Expand Up @@ -94,7 +101,6 @@ def set_committer_author(repo, committer, author):

# Get required environment variables
github_token = os.environ["GITHUB_TOKEN"]
github_repository = os.environ["GITHUB_REPOSITORY"]
# Get environment variables with defaults
path = os.getenv("CPR_PATH", os.getcwd())
branch = os.getenv("CPR_BRANCH", DEFAULT_BRANCH)
Expand All @@ -107,6 +113,11 @@ def set_committer_author(repo, committer, author):
# Set the repo path
repo = Repo(path)

# Determine the GitHub repository from git config
# This will be the target repository for the pull request
github_repository = get_github_repository()
print(f"Target repository set to {github_repository}")

# Determine if the checked out ref is a valid base for a pull request
# The action needs the checked out HEAD ref to be a branch
# This check will fail in the following cases:
Expand Down
17 changes: 17 additions & 0 deletions src/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ def test_get_random_string():
assert len(cmn.get_random_string(length=20)) == 20


def test_parse_github_repository_success():
repository = cmn.parse_github_repository(
"https://github.com/peter-evans/create-pull-request"
)
assert repository == "peter-evans/create-pull-request"


def test_parse_github_repository_failure():
url = "https://github.com/peter-evans"
with pytest.raises(ValueError) as e_info:
cmn.parse_github_repository(url)
assert (
e_info.value.args[0]
== f"The format of '{url}' is not a valid GitHub repository URL"
)


def test_parse_display_name_email_success():
name, email = cmn.parse_display_name_email("abc def <abc@def.com>")
assert name == "abc def"
Expand Down

0 comments on commit cc7020a

Please # to comment.