Class: SchwarmCli::RepositoryResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/schwarm_cli/repository_resolver.rb

Overview

Turns a user-provided ‘–repo` reference into a schwarm repository ID.

Accepted inputs:

- a schwarm repository ID (returned as-is)
- "owner/repo"
- "https://github.com/owner/repo[.git]"
- "git@github.com:owner/repo[.git]"

Defined Under Namespace

Classes: ResolutionError

Constant Summary collapse

SLUG_PATTERNS =
[
  %r{\Ahttps?://github\.com/([^/\s]+/[^/\s]+?)(?:\.git)?/?\z},
  %r{\Agit@github\.com:([^/\s:]+/[^/\s:]+?)(?:\.git)?\z},
  %r{\A([^/\s:]+/[^/\s:]+?)(?:\.git)?\z}
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ RepositoryResolver

Returns a new instance of RepositoryResolver.



20
21
22
# File 'lib/schwarm_cli/repository_resolver.rb', line 20

def initialize(client:)
  @client = client
end

Instance Method Details

#resolve(ref) ⇒ Object



24
25
26
27
28
29
# File 'lib/schwarm_cli/repository_resolver.rb', line 24

def resolve(ref)
  return nil if blank?(ref)

  record = resolve_record(ref)
  record ? record["id"] : ref
end

#resolve_record(ref) ⇒ Object

Like ‘resolve`, but returns the full repo record when the ref is looked up via slug/URL. Returns nil when `ref` is blank or already a bare ID (caller should `find` by id directly in that case).



34
35
36
37
38
39
# File 'lib/schwarm_cli/repository_resolver.rb', line 34

def resolve_record(ref)
  return nil if blank?(ref)

  slug = extract_slug(ref.strip)
  slug ? lookup_by_slug(slug, ref) : nil
end