Sha256: fc795bf51459929aed4df6650e589b78a781af27616de16b9bc4cbcc1592b532

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Codestatus
  module PackageResolvers
    class Base
      GITHUB_REPOSITORY_REGEXP = %r{(https?|git)://github.com/(?<owner>[^/]*)/(?<repo>[^/]*)(\.git)?/?.*}.freeze
      BITBUCKET_REPOSITORY_REGEXP = %r{(https?|git)://bitbucket.org/(?<owner>[^/]*)/(?<repo>[^/]*)(\.git)?/?.*}.freeze

      def self.resolve(package)
        self.new(package).resolve
      end

      def self.resolve!(package)
        self.new(package).resolve!
      end

      def initialize(package)
        @package = package
      end

      attr_reader :package

      def resolve
        detect_repository
      rescue PackageNotFoundError, RepositoryNotFoundError
        # noop
      end

      def resolve!
        detect_repository
      end

      private

      def detect_repository
        raise PackageNotFoundError unless found?
        repository = github_repository || bitbucket_repository
        raise RepositoryNotFoundError unless repository
        repository
      end

      def found?
        raise NotImplementedError
      end

      def github_repository
        urls.map { |url|
          matched = GITHUB_REPOSITORY_REGEXP.match(url)
          next unless matched

          repo = [matched[:owner], matched[:repo]].join('/')
          Repositories::GitHubRepository.new(repo)
        }.compact.first
      end

      def bitbucket_repository
        urls.map { |url|
          matched = BITBUCKET_REPOSITORY_REGEXP.match(url)
          next unless matched

          repo = [matched[:owner], matched[:repo]].join('/')
          Repositories::BitbucketRepository.new(repo)
        }.compact.first
      end

      def urls
        raise NotImplementedError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codestatus-0.1.3 lib/codestatus/package_resolvers/base.rb