Skip to content

REPOSITORY_MATCHER fails when remote URL has no ".git" suffix #502

Description

@ryz310

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions