Better pattern matching for repo_url

This commit is contained in:
Lucas Wilson-Richter
2024-05-15 17:53:19 +10:00
parent 6b55c0579c
commit a537f4d311

21
fcl
View File

@ -28,21 +28,28 @@ def print_error(message)
system(%Q[gum style --bold --border=double --padding='0 1' --foreground 212 "#{message}"])
end
# Transform SSH URLs for GitHub into HTTPS, so we can do the recommended auth
# things.
#
# Also, make some assumptions when info is missing, so I can provide stuff like
# `buildkite-plugins/ecr-buildkite-plugin` and have this puppy still work.
#
def repo_url(repo_str)
## GitHub HTTPS. We like these, use it as-is.
if repo_str =~ /^(git|https)\:\/\/github\.com\//
case repo_str
# GitHub HTTPS. We like these, use it as-is.
when /^(git|https)\:\/\/github\.com\//
repo_str
## GitHub SSH. Let's convert this to HTTPS.
elsif repo_str =~ /^git@github\.com\:.+\.git$/
# GitHub SSH. Let's convert this to HTTPS.
when /^git@github\.com\:.+\.git$/
repo_str.sub(/^git@github\.com:/, 'https://github.com/')
# Host, user/org and repo. Assume it's HTTPS.
elsif repo_str =~ /^[^\/\s]+\/[^\/\s]+\/[^\/\s]+/
when /^[^\/\s]+\/[^\/\s]+\/[^\/\s]+/
"https://#{repo_str}#{repo_str.end_with?('.git') ? '' : '.git'}"
# Just a user/org name and repo name. Assume it's on GitHub and make an HTTPS URL.
elsif repo_str =~ /^[\w\-_]+\/[\w\-_\.]+$/
when /^[\w\-_]+\/[\w\-_\.]+$/
"https://github.com/#{repo_str}.git"
# Just a repo name. Assume it's a DEFAULT_ORG repo on GitHub and make an HTTPS URL.
elsif repo_str =~ /^[\w\-_\.]+$/
when /^[\w\-_\.]+$/
"https://github.com/#{DEFAULT_ORG}/#{repo_str}.git"
else
raise "I can't make a repo URL out of '#{repo_str}'."