Summary
PrComet::Github::Client#initialize raises undefined method '[]' for nil:NilClass when the git remote URL does not end with .git.
Root cause
lib/pr_comet/github/client.rb:
REPOSITORY_MATCHER = %r{github\.com[:/](?<repository>.+)\.git}.freeze
def initialize(access_token, remote_url)
@client = Octokit::Client.new(access_token: access_token)
@repository = remote_url.match(REPOSITORY_MATCHER)[:repository]
end
The regex requires a literal .git suffix at the end of the URL. However, some repositories have their origin remote configured without the .git suffix, e.g.:
$ git remote get-url origin
https://github.com/ryz310/my_api_client
In that case remote_url.match(REPOSITORY_MATCHER) returns nil, and nil[:repository] raises:
undefined method `[]' for nil:NilClass
Since this happens inside PrComet.new's constructor, gem_comet release (and any other consumer of pr_comet) fails immediately, before GITHUB_ACCESS_TOKEN or any other configuration is even used.
Reproduction
REPOSITORY_MATCHER = %r{github\.com[:/](?<repository>.+)\.git}
remote_url = "https://github.com/ryz310/my_api_client"
remote_url.match(REPOSITORY_MATCHER)[:repository]
# => undefined method `[]' for nil:NilClass (NoMethodError)
Suggested fix
Make the .git suffix optional in the regex, e.g.:
REPOSITORY_MATCHER = %r{github\.com[:/](?<repository>.+?)(\.git)?\z}.freeze
or strip an optional trailing .git before matching. This would make pr_comet robust regardless of whether the origin remote URL includes the .git suffix.
Environment
- pr_comet 0.5.1 (via gem_comet 0.8.0)
- Reproduced with a public GitHub repository whose
origin remote lacks the .git suffix
Summary
PrComet::Github::Client#initializeraisesundefined method '[]' for nil:NilClasswhen the git remote URL does not end with.git.Root cause
lib/pr_comet/github/client.rb:The regex requires a literal
.gitsuffix at the end of the URL. However, some repositories have theiroriginremote configured without the.gitsuffix, e.g.:In that case
remote_url.match(REPOSITORY_MATCHER)returnsnil, andnil[:repository]raises:Since this happens inside
PrComet.new's constructor,gem_comet release(and any other consumer ofpr_comet) fails immediately, beforeGITHUB_ACCESS_TOKENor any other configuration is even used.Reproduction
Suggested fix
Make the
.gitsuffix optional in the regex, e.g.:or strip an optional trailing
.gitbefore matching. This would makepr_cometrobust regardless of whether theoriginremote URL includes the.gitsuffix.Environment
originremote lacks the.gitsuffix