Class: ReleaseManager::VCSManager::GitlabAdapter

Inherits:
VcsAdapter
  • Object
show all
Defined in:
lib/release_manager/vcs_manager/gitlab_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#color, #log_level, #logger

Constructor Details

#initializeGitlabAdapter

Returns a new instance of GitlabAdapter.



8
9
10
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 8

def initialize
  @client = Gitlab.client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 6

def client
  @client
end

Class Method Details

.createObject

creates an instance of the gitlab adapter



13
14
15
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 13

def self.create
  new
end

Instance Method Details

#add_permission(username, project_id, access_level = 20) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 36

def add_permission(username, project_id, access_level = 20)
  begin
    project_name = client.project(project_id).path_with_namespace
    user = client.user_search(username).find{|u| u.username == username}
    unless user
      logger.warn("No user found for #{username}")
      return
    end
    unless check_access?(username, project_id, access_level)
      logger.info("Adding member #{username} to project #{project_name}")
      client.add_team_member(project_id, user.id, access_level)
    end
  rescue Gitlab::Error::BadRequest => e
    # if the key is already added no need to do anything else
    logger.warn(e.message)
  end
end

#add_permissions(project_id, user_ids = [], access_level = 20) ⇒ Object



59
60
61
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 59

def add_permissions(project_id, user_ids = [], access_level = 20)
  user_ids.map {|id| add_permission(id, project_id, access_level)}
end

#add_ssh_key(public_key_filename = nil) ⇒ Object

adds the ssh key to the user of the token

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 18

def add_ssh_key(public_key_filename = nil)
  public_key_filename ||= File.expand_path(File.join(ENV['HOME'], '.ssh', 'id_rsa.pub'))
  title = "#{ENV['USER']}@#{ENV['HOST']}"
  raise InvalidSshkey.new("Ssh key does not exist #{public_key_filename}") unless File.exist?(public_key_filename)
  begin
    client.create_ssh_key(title, File.read(public_key_filename))
    logger.info("Adding ssh key #{public_key_filename} to gitlab")
  rescue Gitlab::Error::BadRequest => e
    # if the key is already added no need to do anything else
    return unless e.response_status == 400
  end
end

#check_access?(username, project_id, access_level) ⇒ Boolean

Returns:



54
55
56
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 54

def check_access?(username, project_id, access_level)
  client.team_members(project_id).find { |user| user.username == username && user.access_level == access_level}
end

#clone_repo(branch_name, mod_name, url, repos_dir) ⇒ Object



68
69
70
71
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 68

def clone_repo(branch_name, mod_name, url, repos_dir)
  path = File.join(repos_dir, mod_name)
  Rugged::Repository.clone_at(url, path, checkout_branch: branch_name)
end

#create_repo_branch(repo_id, branch_name) ⇒ Object

Returns String - the branch name that was created.

Returns:

  • String - the branch name that was created



64
65
66
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 64

def create_repo_branch(repo_id, branch_name)
  Gitlab.repo_create_branch(repo_id, branch_name)
end

#project_name(project_id) ⇒ Object



31
32
33
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 31

def project_name(project_id)
  client.projects(project_id)
end

#swap_namespace(url, namespace = nil) ⇒ Object

TODO: extract this out to an adapter replaces namespace from the url with the supplied or default namespace



75
76
77
# File 'lib/release_manager/vcs_manager/gitlab_adapter.rb', line 75

def swap_namespace(url, namespace = nil)
  url.gsub(/\:([\w-]+)\//, ":#{namespace || Gitlab.user.username}/")
end