Merge pull request #114 from peter-evans/dev
Add support for ssh protocol
This commit is contained in:
commit
b6a458d96a
6 changed files with 56 additions and 28 deletions
20
dist/src/common.py
vendored
20
dist/src/common.py
vendored
|
@ -9,16 +9,20 @@ def get_random_string(length=7, chars=string.ascii_lowercase + string.digits):
|
||||||
|
|
||||||
|
|
||||||
def parse_github_repository(url):
|
def parse_github_repository(url):
|
||||||
# Parse the github repository from a URL
|
# Parse the protocol and github repository from a URL
|
||||||
# e.g. peter-evans/create-pull-request
|
# e.g. HTTPS, peter-evans/create-pull-request
|
||||||
pattern = re.compile(r"^https://github.com/(.+/.+)$")
|
https_pattern = re.compile(r"^https://github.com/(.+/.+)$")
|
||||||
|
ssh_pattern = re.compile(r"^git@github.com:(.+/.+).git$")
|
||||||
|
|
||||||
# Check we have a match
|
match = https_pattern.match(url)
|
||||||
match = pattern.match(url)
|
if match is not None:
|
||||||
if match is None:
|
return "HTTPS", match.group(1)
|
||||||
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")
|
|
||||||
|
|
||||||
return match.group(1)
|
match = ssh_pattern.match(url)
|
||||||
|
if match is not None:
|
||||||
|
return "SSH", match.group(1)
|
||||||
|
|
||||||
|
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")
|
||||||
|
|
||||||
|
|
||||||
def parse_display_name_email(display_name_email):
|
def parse_display_name_email(display_name_email):
|
||||||
|
|
13
dist/src/create_pull_request.py
vendored
13
dist/src/create_pull_request.py
vendored
|
@ -31,11 +31,12 @@ def get_git_config_value(repo, name):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_github_repository():
|
def get_repository_detail():
|
||||||
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
|
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
|
||||||
if remote_origin_url is None:
|
if remote_origin_url is None:
|
||||||
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
|
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
|
||||||
return cmn.parse_github_repository(remote_origin_url)
|
protocol, github_repository = cmn.parse_github_repository(remote_origin_url)
|
||||||
|
return remote_origin_url, protocol, github_repository
|
||||||
|
|
||||||
|
|
||||||
def git_user_config_is_set(repo):
|
def git_user_config_is_set(repo):
|
||||||
|
@ -115,7 +116,7 @@ repo = Repo(path)
|
||||||
|
|
||||||
# Determine the GitHub repository from git config
|
# Determine the GitHub repository from git config
|
||||||
# This will be the target repository for the pull request
|
# This will be the target repository for the pull request
|
||||||
github_repository = get_github_repository()
|
repo_url, protocol, github_repository = get_repository_detail()
|
||||||
print(f"Target repository set to {github_repository}")
|
print(f"Target repository set to {github_repository}")
|
||||||
|
|
||||||
# Determine if the checked out ref is a valid base for a pull request
|
# Determine if the checked out ref is a valid base for a pull request
|
||||||
|
@ -173,8 +174,10 @@ except ValueError as e:
|
||||||
print(f"::error::{e} " + "Unable to continue. Exiting.")
|
print(f"::error::{e} " + "Unable to continue. Exiting.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Set the repository URL
|
# Set the auth token in the repo URL
|
||||||
repo_url = f"https://x-access-token:{github_token}@github.com/{github_repository}"
|
# This supports checkout@v1. From v2 the auth token is saved for further use.
|
||||||
|
if protocol == "HTTPS":
|
||||||
|
repo_url = f"https://x-access-token:{github_token}@github.com/{github_repository}"
|
||||||
|
|
||||||
# Create or update the pull request branch
|
# Create or update the pull request branch
|
||||||
result = coub.create_or_update_branch(repo, repo_url, commit_message, base, branch)
|
result = coub.create_or_update_branch(repo, repo_url, commit_message, base, branch)
|
||||||
|
|
9
dist/src/test_common.py
vendored
9
dist/src/test_common.py
vendored
|
@ -10,9 +10,16 @@ def test_get_random_string():
|
||||||
|
|
||||||
|
|
||||||
def test_parse_github_repository_success():
|
def test_parse_github_repository_success():
|
||||||
repository = cmn.parse_github_repository(
|
protocol, repository = cmn.parse_github_repository(
|
||||||
"https://github.com/peter-evans/create-pull-request"
|
"https://github.com/peter-evans/create-pull-request"
|
||||||
)
|
)
|
||||||
|
assert protocol == "HTTPS"
|
||||||
|
assert repository == "peter-evans/create-pull-request"
|
||||||
|
|
||||||
|
protocol, repository = cmn.parse_github_repository(
|
||||||
|
"git@github.com:peter-evans/create-pull-request.git"
|
||||||
|
)
|
||||||
|
assert protocol == "SSH"
|
||||||
assert repository == "peter-evans/create-pull-request"
|
assert repository == "peter-evans/create-pull-request"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,16 +9,20 @@ def get_random_string(length=7, chars=string.ascii_lowercase + string.digits):
|
||||||
|
|
||||||
|
|
||||||
def parse_github_repository(url):
|
def parse_github_repository(url):
|
||||||
# Parse the github repository from a URL
|
# Parse the protocol and github repository from a URL
|
||||||
# e.g. peter-evans/create-pull-request
|
# e.g. HTTPS, peter-evans/create-pull-request
|
||||||
pattern = re.compile(r"^https://github.com/(.+/.+)$")
|
https_pattern = re.compile(r"^https://github.com/(.+/.+)$")
|
||||||
|
ssh_pattern = re.compile(r"^git@github.com:(.+/.+).git$")
|
||||||
|
|
||||||
# Check we have a match
|
match = https_pattern.match(url)
|
||||||
match = pattern.match(url)
|
if match is not None:
|
||||||
if match is None:
|
return "HTTPS", match.group(1)
|
||||||
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")
|
|
||||||
|
|
||||||
return match.group(1)
|
match = ssh_pattern.match(url)
|
||||||
|
if match is not None:
|
||||||
|
return "SSH", match.group(1)
|
||||||
|
|
||||||
|
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")
|
||||||
|
|
||||||
|
|
||||||
def parse_display_name_email(display_name_email):
|
def parse_display_name_email(display_name_email):
|
||||||
|
|
|
@ -31,11 +31,12 @@ def get_git_config_value(repo, name):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_github_repository():
|
def get_repository_detail():
|
||||||
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
|
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
|
||||||
if remote_origin_url is None:
|
if remote_origin_url is None:
|
||||||
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
|
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
|
||||||
return cmn.parse_github_repository(remote_origin_url)
|
protocol, github_repository = cmn.parse_github_repository(remote_origin_url)
|
||||||
|
return remote_origin_url, protocol, github_repository
|
||||||
|
|
||||||
|
|
||||||
def git_user_config_is_set(repo):
|
def git_user_config_is_set(repo):
|
||||||
|
@ -115,7 +116,7 @@ repo = Repo(path)
|
||||||
|
|
||||||
# Determine the GitHub repository from git config
|
# Determine the GitHub repository from git config
|
||||||
# This will be the target repository for the pull request
|
# This will be the target repository for the pull request
|
||||||
github_repository = get_github_repository()
|
repo_url, protocol, github_repository = get_repository_detail()
|
||||||
print(f"Target repository set to {github_repository}")
|
print(f"Target repository set to {github_repository}")
|
||||||
|
|
||||||
# Determine if the checked out ref is a valid base for a pull request
|
# Determine if the checked out ref is a valid base for a pull request
|
||||||
|
@ -173,8 +174,10 @@ except ValueError as e:
|
||||||
print(f"::error::{e} " + "Unable to continue. Exiting.")
|
print(f"::error::{e} " + "Unable to continue. Exiting.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Set the repository URL
|
# Set the auth token in the repo URL
|
||||||
repo_url = f"https://x-access-token:{github_token}@github.com/{github_repository}"
|
# This supports checkout@v1. From v2 the auth token is saved for further use.
|
||||||
|
if protocol == "HTTPS":
|
||||||
|
repo_url = f"https://x-access-token:{github_token}@github.com/{github_repository}"
|
||||||
|
|
||||||
# Create or update the pull request branch
|
# Create or update the pull request branch
|
||||||
result = coub.create_or_update_branch(repo, repo_url, commit_message, base, branch)
|
result = coub.create_or_update_branch(repo, repo_url, commit_message, base, branch)
|
||||||
|
|
|
@ -10,9 +10,16 @@ def test_get_random_string():
|
||||||
|
|
||||||
|
|
||||||
def test_parse_github_repository_success():
|
def test_parse_github_repository_success():
|
||||||
repository = cmn.parse_github_repository(
|
protocol, repository = cmn.parse_github_repository(
|
||||||
"https://github.com/peter-evans/create-pull-request"
|
"https://github.com/peter-evans/create-pull-request"
|
||||||
)
|
)
|
||||||
|
assert protocol == "HTTPS"
|
||||||
|
assert repository == "peter-evans/create-pull-request"
|
||||||
|
|
||||||
|
protocol, repository = cmn.parse_github_repository(
|
||||||
|
"git@github.com:peter-evans/create-pull-request.git"
|
||||||
|
)
|
||||||
|
assert protocol == "SSH"
|
||||||
assert repository == "peter-evans/create-pull-request"
|
assert repository == "peter-evans/create-pull-request"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue