Sha256: 3567f953e68264919dee804917cd0a2a7b231f0b83c07ca3feb2caa28babda63

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'thor'
require 'gitlab'
require 'tty-spinner'
require 'tty-progressbar'
require 'tty-table'
require 'tty-prompt'

require 'gl/cli'

module Gl
  class Error < StandardError; end
  def self.current_project(project = nil)
    current_remote = project || Gl.remote_url

    if current_remote.empty?
      prompt = TTY::Prompt.new
      prompt.ask('No remote found, please enter a project (like `group/project`)')
    else
      remote_parse[1]
    end
  end

  def self.open_in_browser(url)
    url = "#{Gitlab.endpoint.gsub('api/v4', '')}#{url}"
    `sensible-browser #{url}`
  end

  def self.remote_url
    `git config --get remote.origin.url`.chomp
  end

  def self.remote_parse
    if remote_url.start_with?('git@')
      regexp = %r{(git@|https:\/\/|ssh:\/\/)(.*?)(:|\/)(.*)?(\.git)}
      url = remote_url

      host = url.gsub(regexp, '\2')
      project = url.gsub(regexp, '\4')
    else
      uri = URI(remote_url)
      host = uri.host
      project = uri.path.gsub(%r{\/(.*)\.git$}, '\1')
    end

    if host.nil? || project.nil?
      puts 'Could not discover git remote. Make sure you are in a valid git repository'
      exit(1)
    end

    [host, project]
  end

  def self.remote_base
    remote_parse[0]
  end

  def self.remote_slug
    remote_base.gsub('.', '-')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gl-0.2.0 lib/gl.rb