Class: ModuleSync::GitService::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/modulesync/git_service/base.rb

Overview

Generic class for git services

Direct Known Subclasses

GitHub, GitLab

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_hostname(url) ⇒ Object

This method extracts hostname from URL like:

Returns nil if

  • /path/to/repo.git/

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

  • any invalid URL



43
44
45
46
47
48
49
50
51
52
53
54
# 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:) ⇒ Object

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



24
25
26
27
28
29
# 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

Instance Method Details

#open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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