labclient 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/lib/labclient.rb +23 -4
  3. data/lib/labclient/access_levels.rb +24 -30
  4. data/lib/labclient/branches/branch.rb +25 -0
  5. data/lib/labclient/branches/create.rb +33 -0
  6. data/lib/labclient/client.rb +48 -7
  7. data/lib/labclient/docs.rb +9 -5
  8. data/lib/labclient/epics/epic.rb +13 -0
  9. data/lib/labclient/error.rb +1 -0
  10. data/lib/labclient/files/create.rb +14 -8
  11. data/lib/labclient/generator/generator.rb +70 -0
  12. data/lib/labclient/generator/names.rb +68 -0
  13. data/lib/labclient/generator/template_helper.rb +81 -0
  14. data/lib/labclient/generator/templates/environments.rb +98 -0
  15. data/lib/labclient/generator/templates/pages.rb +67 -0
  16. data/lib/labclient/generator/templates/pipeline_trigger.rb +82 -0
  17. data/lib/labclient/generator/wizard.rb +142 -0
  18. data/lib/labclient/groups/group.rb +9 -1
  19. data/lib/labclient/http.rb +3 -2
  20. data/lib/labclient/issues/create.rb +2 -0
  21. data/lib/labclient/issues/issue.rb +31 -1
  22. data/lib/labclient/issues/update.rb +20 -2
  23. data/lib/labclient/jobs/delete.rb +1 -1
  24. data/lib/labclient/jobs/keep.rb +1 -1
  25. data/lib/labclient/jobs/play.rb +1 -1
  26. data/lib/labclient/jobs/trace.rb +2 -2
  27. data/lib/labclient/klass.rb +34 -4
  28. data/lib/labclient/lab_struct.rb +17 -0
  29. data/lib/labclient/license/list.rb +2 -2
  30. data/lib/labclient/members/member.rb +1 -0
  31. data/lib/labclient/merge_requests/accept.rb +15 -6
  32. data/lib/labclient/merge_requests/create.rb +12 -0
  33. data/lib/labclient/merge_requests/merge_request.rb +49 -4
  34. data/lib/labclient/notes/epics/create.rb +12 -4
  35. data/lib/labclient/notes/epics/delete.rb +3 -3
  36. data/lib/labclient/notes/epics/list.rb +21 -4
  37. data/lib/labclient/notes/epics/show.rb +4 -4
  38. data/lib/labclient/notes/epics/update.rb +4 -4
  39. data/lib/labclient/notes/issues/create.rb +11 -1
  40. data/lib/labclient/notes/issues/list.rb +18 -3
  41. data/lib/labclient/notes/issues/show.rb +1 -1
  42. data/lib/labclient/notes/merge_requests/create.rb +8 -0
  43. data/lib/labclient/notes/merge_requests/list.rb +20 -2
  44. data/lib/labclient/notes/snippets/create.rb +1 -1
  45. data/lib/labclient/notes/snippets/list.rb +20 -3
  46. data/lib/labclient/notes/snippets/show.rb +1 -1
  47. data/lib/labclient/notifications/update.rb +1 -1
  48. data/lib/labclient/overview.rb +110 -11
  49. data/lib/labclient/paginated_response.rb +8 -1
  50. data/lib/labclient/pipelines/pipeline.rb +41 -0
  51. data/lib/labclient/projects/methods.rb +71 -2
  52. data/lib/labclient/projects/reference.rb +14 -0
  53. data/lib/labclient/projects/snippets/project_snippet.rb +12 -0
  54. data/lib/labclient/protected_branches/protect.rb +6 -5
  55. data/lib/labclient/protected_environments/list.rb +29 -0
  56. data/lib/labclient/protected_environments/protect.rb +53 -0
  57. data/lib/labclient/protected_environments/protected_environment.rb +22 -0
  58. data/lib/labclient/protected_environments/show.rb +24 -0
  59. data/lib/labclient/protected_environments/unprotect.rb +31 -0
  60. data/lib/labclient/snippets/snippet.rb +2 -2
  61. data/lib/labclient/users/membership.rb +62 -0
  62. data/lib/labclient/users/memberships.rb +8 -3
  63. data/lib/labclient/users/user.rb +13 -1
  64. data/lib/labclient/version.rb +1 -1
  65. metadata +37 -9
  66. data/lib/labclient/open_struct.rb +0 -14
@@ -0,0 +1,70 @@
1
+ # The glorious gitlab labclient of labs
2
+ module LabClient
3
+ # Generator Namespace
4
+ module Generator
5
+ # Docs for the wizard
6
+ class GeneratorDocs
7
+ @group_name = 'Wizard'
8
+ extend Docs
9
+
10
+ doc 'Wizard' do
11
+ markdown <<~DOC
12
+ <h6 style="color:red"><b>Experimental</b></h6>
13
+
14
+ This is experimental and will be likely changing frequently and dramatically. This will create (*potentially many*) and manipulate objects within GitLab. **This is not recommended for use in production environments**
15
+ DOC
16
+ end
17
+
18
+ doc 'Wizard' do
19
+ title 'About'
20
+ markdown <<~DOC
21
+ The Wizard is designed to help setup test instances, and provide easy mechanisms for populating and testing GitLab features.
22
+
23
+ Requires 'faker' to function, which will need to be manually/added installed, as it is not a depdenency of the gem.
24
+ DOC
25
+
26
+ example 'gem install faker'
27
+ end
28
+
29
+ doc 'Wizard' do
30
+ title 'Client'
31
+ desc <<-DOC
32
+ The wizard client will self-populate with defaults and parameters from the main client object. But each can be manually configured.
33
+ DOC
34
+
35
+ example <<~DOC
36
+ wizard = client.wizard
37
+ => #<Wizard count={:users=>20, :projects=>5, :groups=>5, :issues=>2}, random=true, domain=labclient>
38
+
39
+ # Disable Random
40
+ wizard.random = false
41
+ DOC
42
+ end
43
+
44
+ doc 'Wizard' do
45
+ markdown <<~DOC
46
+ | Setting | Type | Description |
47
+ | ----------------- | ------- | ------------------------------------------------------ |
48
+ | random | Boolean | Run Populate. Setup random Users, Projects, and Groups |
49
+ | count | Hash | Settings for random. User, Project, Group count |
50
+ | password | String | Default password for generated Users |
51
+ | domain | String | Defaults to client url. Default email domain for users |
52
+ | skip_confirmation | Boolean | Enable/Disable skip_confirmation on new users |
53
+ DOC
54
+ end
55
+
56
+ doc 'Wizard' do
57
+ title 'Run one templates'
58
+ desc <<-DOC
59
+ Outside of the wizard at large, you can also select and manually run specific templates.
60
+ DOC
61
+
62
+ example <<~DOC
63
+ pages = client.wizard.template(:pages)
64
+ => #<LabClient::Template Pages>
65
+ pages.run! # Run pages template
66
+ DOC
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,68 @@
1
+ # Name Generator
2
+ module LabClient
3
+ # Helper to Generate Data / Populate GitLab
4
+ module Generator
5
+ # Name Helper
6
+ module Names
7
+ def create_file(content, branch = :master, message = nil)
8
+ {
9
+ branch: branch,
10
+ commit_message: message || gen_description,
11
+ content: content
12
+ }
13
+ end
14
+
15
+ def gen_groups
16
+ [
17
+ Faker::Games::LeagueOfLegends.location,
18
+ Faker::Movies::LordOfTheRings.location,
19
+ Faker::TvShows::RickAndMorty.location,
20
+ Faker::TvShows::StarTrek.location,
21
+ Faker::Games::ElderScrolls.region,
22
+ Faker::Games::ElderScrolls.city,
23
+ Faker::Games::Zelda.location,
24
+ Faker::Games::SuperSmashBros.stage
25
+ ].map { |x| x.gsub(/[^0-9A-Za-z]/, '') }
26
+ end
27
+
28
+ def gen_projects
29
+ [
30
+ Faker::Games::ElderScrolls.creature,
31
+ Faker::Movies::LordOfTheRings.location,
32
+ Faker::Games::LeagueOfLegends.summoner_spell,
33
+ Faker::Games::Pokemon.move,
34
+ Faker::TvShows::RickAndMorty.location,
35
+ Faker::Games::LeagueOfLegends.masteries,
36
+ Faker::Superhero.power
37
+ ].map { |x| x.gsub(/[^0-9A-Za-z]/, '') }
38
+ end
39
+
40
+ def gen_people
41
+ [
42
+ Faker::Movies::LordOfTheRings.character,
43
+ Faker::Games::WorldOfWarcraft.hero,
44
+ Faker::TvShows::StarTrek.character,
45
+ Faker::Games::LeagueOfLegends.champion,
46
+ Faker::Movies::PrincessBride.character
47
+ ]
48
+ end
49
+
50
+ def gen_description
51
+ [
52
+ Faker::Hacker.say_something_smart,
53
+ Faker::Games::LeagueOfLegends.quote,
54
+ Faker::Company.bs,
55
+ Faker::Movies::PrincessBride.quote
56
+ ].sample
57
+ end
58
+
59
+ # rubocop:disable Metrics/AbcSize
60
+ def generate_names
61
+ @user_names = Array.new(count[:users]) { gen_people }.flatten.uniq.sample(count[:users])
62
+ @group_names = Array.new(count[:groups]) { gen_groups }.flatten.uniq.sample(count[:groups])
63
+ @project_names = Array.new(count[:projects]) { gen_projects }.flatten.uniq.sample(count[:projects])
64
+ end
65
+ # rubocop:enable Metrics/AbcSize
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,81 @@
1
+ module LabClient
2
+ module Generator
3
+ # Public Method Helpers
4
+ module TemplateMethods
5
+ extend ActiveSupport::Concern
6
+
7
+ @group_prefix = 'Environment'
8
+
9
+ class_methods do
10
+ def template_name
11
+ name.demodulize.underscore.to_sym
12
+ end
13
+ end
14
+ end
15
+
16
+ # Common Helper Class
17
+ class TemplateHelper
18
+ include TemplateMethods
19
+
20
+ attr_reader :client
21
+ attr_accessor :opts
22
+
23
+ def inspect
24
+ "#<LabClient::Template #{self.class.to_s.demodulize}>"
25
+ end
26
+
27
+ def initialize(client, opts = {})
28
+ @client = client
29
+ self.opts = opts
30
+ setup
31
+ end
32
+
33
+ def setup
34
+ # Blank Place Holder
35
+ end
36
+ end
37
+
38
+ # ====================================================
39
+ # Helper for Common Group/Project Templates
40
+ # - Create Group, @group
41
+ # - Execute any `setup_` methods
42
+ # - Print all @projects, and @group
43
+ class GroupTemplateHelper < TemplateHelper
44
+ @group_prefix = 'Environment'
45
+ include Names
46
+ attr_accessor :group_name, :group_path, :group_suffix, :project_name
47
+
48
+ def run!
49
+ puts "Running: #{group_suffix}"
50
+ generate_group
51
+
52
+ # Run `setup_` prefixed classes
53
+ self.class.instance_methods.grep(/setup_/).each { |x| send(x) }
54
+
55
+ # Print Created Groups/Project
56
+ puts "#{@group.name} - #{@group.web_url}"
57
+ @projects.each do |project|
58
+ puts " - #{project.name} - #{project.web_url}"
59
+ end
60
+
61
+ {
62
+ group: @group,
63
+ projects: @projects
64
+ }
65
+ end
66
+
67
+ def setup
68
+ self.group_suffix = self.class.name.demodulize
69
+ self.group_name = opts[:group_name] || "#{gen_groups.sample} #{group_suffix}"
70
+ self.group_path = opts[:group_path] || group_name.downcase.gsub(' ', '-')
71
+
72
+ @projects = []
73
+ end
74
+
75
+ def generate_group
76
+ @group = client.groups.create(name: group_name, path: group_path)
77
+ raise 'Unable to Create Group' unless @group.success?
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,98 @@
1
+ module LabClient
2
+ module Generator
3
+ # Docs for the wizard
4
+ class GeneratorDocs
5
+ doc 'Templates' do
6
+ title 'Environments'
7
+ markdown <<~DOC
8
+ - On Stop: Create environment on new branch. Stop environment on merge
9
+
10
+ | Setting | Default | Type | Description |
11
+ | ---------- | --------- | ------- | -------------------------------------- |
12
+ | group_name | Generated | String | Parent Group name for pages projects |
13
+ | group_path | Generated | String | Parent Group path |
14
+ DOC
15
+ end
16
+ end
17
+
18
+ # Child and other Trigger Examples
19
+ # https://docs.gitlab.com/ee/ci/yaml/#trigger
20
+ class Environments < GroupTemplateHelper
21
+ def on_stop_yaml
22
+ <<~YAML
23
+ image: busybox:latest
24
+
25
+ build:
26
+ stage: build
27
+ script:
28
+ - echo "Build on both master and branches"
29
+
30
+ branch-review:
31
+ stage: deploy
32
+ environment:
33
+ name: ${CI_COMMIT_REF_NAME}
34
+ on_stop: review-teardown
35
+ script:
36
+ - echo "branch-review"
37
+ only:
38
+ - branches
39
+ except:
40
+ - master
41
+
42
+ review-teardown:
43
+ stage: deploy
44
+ when: manual
45
+ variables:
46
+ GIT_STRATEGY: none
47
+ environment:
48
+ name: ${CI_COMMIT_REF_NAME}
49
+ action: stop
50
+ script:
51
+ - echo "review-teardown"
52
+ only:
53
+ - branches
54
+ except:
55
+ - master
56
+ YAML
57
+ end
58
+
59
+ # rubocop:disable Metrics/MethodLength
60
+ def setup_master_branch_environment_on_stop
61
+ project = @group.project_create(
62
+ name: 'On Stop',
63
+ description: 'Environments created on non-master branches, stopped on merge',
64
+ auto_devops_enabled: false,
65
+ only_allow_merge_if_pipeline_succeeds: true
66
+ )
67
+
68
+ # Create Parent
69
+ project.file_create('README.md', create_file("# #{project.name}"))
70
+
71
+ # # Create Branch
72
+ project.branch_create(branch: :branch, ref: :master)
73
+
74
+ # Create Branch Files
75
+ project.file_create('.gitlab-ci.yml', create_file(on_stop_yaml, :branch))
76
+
77
+ # Create Merge Request
78
+ merge_request = project.merge_request_create(
79
+ title: 'Merge Test Branch!',
80
+ source_branch: :branch,
81
+ target_branch: :master
82
+ )
83
+
84
+ # Wait for Merge
85
+ merge_request.wait_for_merge_status
86
+
87
+ # Merge
88
+ merge_request.accept(
89
+ should_remove_source_branch: true,
90
+ merge_when_pipeline_succeeds: true
91
+ )
92
+
93
+ @projects.push project
94
+ end
95
+ # rubocop:enable Metrics/MethodLength
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,67 @@
1
+ module LabClient
2
+ module Generator
3
+ # Docs for the wizard
4
+ class GeneratorDocs
5
+ doc 'Templates' do
6
+ title 'Pages'
7
+ markdown <<~DOC
8
+ Create GitLab Pages group and test projects. Projects imported from Gitlab's [demo pages projects](https://gitlab.com/pages)
9
+
10
+ | Setting | Default | Type | Description |
11
+ | ---------- | --------- | ------- | -------------------------------------- |
12
+ | group_name | Generated | String | Parent Group name for pages projects |
13
+ | group_path | Generated | String | Parent Group path |
14
+ | count | 5 | Integer | Number of projects to import (max: 28) |
15
+ DOC
16
+ end
17
+ end
18
+
19
+ # Page Import Creation
20
+ class Pages < GroupTemplateHelper
21
+ attr_accessor :count
22
+
23
+ # All Available Pages Projects
24
+ def list
25
+ %w[
26
+ brunch doxygen emacs-reveal frozen-flask gatsby
27
+ gitbook hakyll harp hexo hugo hyde ikiwiki
28
+ jekyll jigsaw lektor metalsmith
29
+ middleman mkdocs nanoc nikola nuxt octopress
30
+ org-mode pelican plain-html sphinx vuepress zim
31
+ ]
32
+ end
33
+
34
+ def setup
35
+ super
36
+ self.count = opts[:count] || 5
37
+ end
38
+
39
+ def generate_project(name)
40
+ @group.project_create(
41
+ name: name,
42
+ description: "#{name} Pages",
43
+ import_url: "https://gitlab.com/pages/#{name}.git"
44
+ )
45
+ end
46
+
47
+ def generate_pipelines
48
+ @projects.each do |project|
49
+ project.pipeline_create(ref: :master)
50
+ end
51
+ end
52
+
53
+ def wait_for_import
54
+ @projects.each(&:wait_for_import)
55
+ end
56
+
57
+ def setup_projects
58
+ @projects = list.sample(count).map do |name|
59
+ generate_project(name)
60
+ end
61
+
62
+ wait_for_import
63
+ generate_pipelines
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,82 @@
1
+ module LabClient
2
+ module Generator
3
+ # Docs for the wizard
4
+ class GeneratorDocs
5
+ doc 'Templates' do
6
+ title 'Pipeline Triggers'
7
+ markdown <<~DOC
8
+ Create GitLab Pipeline Triggers group and test projects.
9
+
10
+ - Child Pipeline: Parent pipeline with artifact, that is then executed as a child pipeline
11
+
12
+ | Setting | Default | Type | Description |
13
+ | ---------- | --------- | ------- | -------------------------------------- |
14
+ | group_name | Generated | String | Parent Group name for pages projects |
15
+ | group_path | Generated | String | Parent Group path |
16
+ DOC
17
+ end
18
+ end
19
+
20
+ # Child and other Trigger Examples
21
+ # https://docs.gitlab.com/ee/ci/yaml/#trigger
22
+ class PipelineTrigger < GroupTemplateHelper
23
+ def setup_projects
24
+ create_child_pipeline
25
+ end
26
+
27
+ def trigger_child_pipeline_yaml
28
+ <<~YAML
29
+ image: busybox:latest
30
+
31
+ build_child:
32
+ stage: build
33
+ script:
34
+ - cp child_pipeline.yml artifact.yml
35
+ artifacts:
36
+ paths:
37
+ - artifact.yml
38
+
39
+ trigger_child:
40
+ stage: deploy
41
+ trigger:
42
+ include:
43
+ - artifact: artifact.yml
44
+ job: build_child
45
+
46
+ YAML
47
+ end
48
+
49
+ def child_pipeline_yaml
50
+ <<~YAML
51
+ image: busybox:latest
52
+
53
+ child:
54
+ script:
55
+ - echo "Do your build here"
56
+ YAML
57
+ end
58
+
59
+ def create_child_pipeline
60
+ project = @group.project_create(
61
+ name: 'Child Pipeline',
62
+ description: 'Child Pipeline',
63
+ auto_devops_enabled: false
64
+ )
65
+
66
+ # Create Child
67
+ project.file_create(
68
+ 'child_pipeline.yml',
69
+ create_file(child_pipeline_yaml)
70
+ )
71
+
72
+ # Create Parent
73
+ project.file_create(
74
+ '.gitlab-ci.yml',
75
+ create_file(trigger_child_pipeline_yaml)
76
+ )
77
+
78
+ @projects.push project
79
+ end
80
+ end
81
+ end
82
+ end