class ModuleSync::GitService::Base

Generic class for git services

Public Class Methods

extract_hostname(url) click to toggle source

This method extracts hostname from URL like:

Returns nil if

  • /path/to/repo.git/

  • file:///path/to/repo.git/

  • any invalid URL

# File lib/modulesync/git_service/base.rb, line 43
def self.extract_hostname(url)
  return nil if url.start_with?('/', 'file://') # local path (e.g. file:///path/to/repo)

  unless url.start_with?(%r{[a-z]+://}) # SSH notation does not contain protocol (e.g. user@server:path/to/repo/)
    pattern = /^(?<user>.*@)?(?<hostname>[\w|.]*):(?<repo>.*)$/ # SSH path (e.g. user@server:repo)
    return url.match(pattern)[:hostname] if url.match?(pattern)
  end

  URI.parse(url).host
rescue URI::InvalidURIError
  nil
end
guess_endpoint_from(remote:) click to toggle source

This method attempts to guess the git service endpoint based on remote

# File lib/modulesync/git_service/base.rb, line 24
def self.guess_endpoint_from(remote:)
  hostname = extract_hostname(remote)
  return nil if hostname.nil?

  "https://#{hostname}"
end

Public Instance Methods

open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:) click to toggle source
# File lib/modulesync/git_service/base.rb, line 5
def open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:)
  unless source_branch != target_branch
    raise ModuleSync::Error,
          "Unable to open a pull request with the same source and target branch: '#{source_branch}'"
  end

  _open_pull_request(
    repo_path: repo_path,
    namespace: namespace,
    title: title,
    message: message,
    source_branch: source_branch,
    target_branch: target_branch,
    labels: labels,
    noop: noop,
  )
end

Protected Instance Methods

_open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:) click to toggle source
# File lib/modulesync/git_service/base.rb, line 58
def _open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:)
  raise NotImplementedError
end