Class: Glub

Inherits:
Sif::Loader
  • Object
show all
Defined in:
lib/glub.rb

Instance Method Summary collapse

Instance Method Details

#add_user_to_group(group_id, user_id, permission = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/glub.rb', line 106

def add_user_to_group(group_id, user_id, permission=nil)
  if permission.nil?
    access_level = 30
  else
    access_level = determine_access_level(permission)
  end

  group_response = RestClient.get(
    "#{@api_endpoint}/groups/#{group_id}?private_token=#{@api_key}"
  )

  group_response = JSON.parse group_response.body

  group_name = group_response['name']


  puts "Adding user to group #{group_name}"
  command = {
    id: group_id,
    user_id: user_id,
    access_level: access_level
  }

  response = RestClient.post(
     "#{@api_endpoint}/groups/#{group_id}/members?private_token=#{@api_key}",
     command.to_json,
     content_type: 'application/json'
  )

  response = JSON.parse response.body

  puts "User added to group #{group_name}"
end

#cloneObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/glub.rb', line 173

def clone

  response = RestClient.get(
     "#{@api_endpoint}/projects?private_token=#{@api_key}"
  )

  response = JSON.parse response.body

  projects = []
  response.each_with_index do |project, idx|
    idx+=1
    projects << { id: idx, name: project['name'], ssh_repo: project['ssh_url_to_repo'] }
  end
  puts "Projects: "
  projects.each { |project| puts " #{project[:id]} Name:#{project[:name]}"}
  # "#{projects}"
  puts 'Which project do you want to clone? (enter the number)'
  clone_response = $stdin.gets.chomp.to_i
  if !clone_response.nil?
    project = projects.find{ |p| p[:id] == clone_response }
    system "git clone #{project[:ssh_repo]}"
  end
end

#create(project_name, namespace_id = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/glub.rb', line 18

def create(project_name, namespace_id=nil)

  puts "Creating Gitlab project #{project_name}"
  command = {
      :name => project_name,
      :description  => 'this is a new project',
      :default_branch => 'master',
      :issues_enabled => 'true',
      :wiki_ebabled  => 'true',
      :wall_enabled  => 'true',
      :merge_requests_enabled => 'true'
  }

  unless namespace_id.nil?
    command[:namespace_id] = namespace_id
  end


  response = RestClient.post(
     "#{@api_endpoint}/projects?private_token=#{@api_key}",
     command.to_json,
     :content_type => 'application/json'
  )

  response = JSON.parse response.body

  puts "Repository #{project_name} created. Add it as a remote using: "
  setup_repo = "git remote add origin git@#{@gitlab_host}:#{response['path_with_namespace']}.git"
  puts "  #{setup_repo}"
  setup_repo
end

#create_group(group_name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/glub.rb', line 84

def create_group(group_name)

  puts "Creating Gitlab group #{group_name}"
  command = {
    name: group_name,
    path: group_name.downcase.gsub(' ', '-')
  }

  response = RestClient.post(
     "#{@api_endpoint}/groups?private_token=#{@api_key}",
     command.to_json,
     content_type: 'application/json'
  )

  response = JSON.parse response.body

  puts "Group #{group_name} created."
  # add root user by default
  add_user_to_group(response['id'], 1, 'owner')
end

#listObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/glub.rb', line 51

def list

  response = RestClient.get(
     "#{@api_endpoint}/projects?private_token=#{@api_key}"
  )

  response = JSON.parse response.body

  projects = []
  response.each { |project| projects << "Name:#{project['name']} / ID:#{project['id']}" }
  puts "Projects: "
  projects.each { |project| puts "  #{project}" }
  "#{projects}"

end

#list_groupsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/glub.rb', line 68

def list_groups

  response = RestClient.get(
     "#{@api_endpoint}/groups?private_token=#{@api_key}"
  )

  response = JSON.parse response.body

  groups = []
  response.each { |group| groups << "Name:#{group['name']} / ID:#{group['id']}" }
  puts "Groups: "
  groups.each { |group| puts "  #{group}" }
  "#{groups}"
end

#move_project_to_group(group_id, project_id) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/glub.rb', line 141

def move_project_to_group(group_id, project_id)

  group_response = RestClient.get(
    "#{@api_endpoint}/groups/#{group_id}?private_token=#{@api_key}"
  )
  group_response = JSON.parse group_response.body
  group_name = group_response['name']

  project_response = RestClient.get(
    "#{@api_endpoint}/projects/#{project_id}?private_token=#{@api_key}"
  )
  project_response = JSON.parse project_response.body
  project_name = project_response['name']

  puts "Moving project #{project_name} to group #{group_name}"
  command = {
    id: group_id,
    project_id: project_id
  }

  response = RestClient.post(
     "#{@api_endpoint}/groups/#{group_id}/projects/#{project_id}?private_token=#{@api_key}",
     command.to_json,
     content_type: 'application/json'
  )

  response = JSON.parse response.body

  puts "Project #{project_name} moved to group #{group_name}"
end