labclient 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/labclient.rb +7 -4
  3. data/lib/labclient/branches/branch.rb +25 -0
  4. data/lib/labclient/branches/create.rb +33 -0
  5. data/lib/labclient/client.rb +2 -2
  6. data/lib/labclient/epics/epic.rb +13 -0
  7. data/lib/labclient/error.rb +1 -0
  8. data/lib/labclient/files/create.rb +14 -8
  9. data/lib/labclient/generator/generator.rb +2 -14
  10. data/lib/labclient/generator/names.rb +17 -3
  11. data/lib/labclient/generator/template_helper.rb +82 -0
  12. data/lib/labclient/generator/templates/environments.rb +98 -0
  13. data/lib/labclient/generator/templates/pages.rb +24 -30
  14. data/lib/labclient/generator/templates/pipeline_trigger.rb +82 -0
  15. data/lib/labclient/generator/wizard.rb +23 -7
  16. data/lib/labclient/http.rb +2 -1
  17. data/lib/labclient/issues/issue.rb +6 -1
  18. data/lib/labclient/klass.rb +3 -2
  19. data/lib/labclient/lab_struct.rb +17 -0
  20. data/lib/labclient/license/list.rb +2 -2
  21. data/lib/labclient/merge_requests/accept.rb +15 -6
  22. data/lib/labclient/merge_requests/create.rb +12 -0
  23. data/lib/labclient/merge_requests/merge_request.rb +49 -4
  24. data/lib/labclient/notes/epics/create.rb +4 -4
  25. data/lib/labclient/notes/epics/delete.rb +3 -3
  26. data/lib/labclient/notes/epics/list.rb +21 -4
  27. data/lib/labclient/notes/epics/show.rb +4 -4
  28. data/lib/labclient/notes/epics/update.rb +4 -4
  29. data/lib/labclient/notes/issues/create.rb +3 -1
  30. data/lib/labclient/notes/issues/list.rb +18 -3
  31. data/lib/labclient/notes/issues/show.rb +1 -1
  32. data/lib/labclient/notes/merge_requests/list.rb +20 -2
  33. data/lib/labclient/notes/snippets/create.rb +1 -1
  34. data/lib/labclient/notes/snippets/list.rb +20 -3
  35. data/lib/labclient/notes/snippets/show.rb +1 -1
  36. data/lib/labclient/notifications/update.rb +1 -1
  37. data/lib/labclient/paginated_response.rb +1 -1
  38. data/lib/labclient/pipelines/pipeline.rb +41 -0
  39. data/lib/labclient/projects/methods.rb +30 -2
  40. data/lib/labclient/projects/reference.rb +5 -0
  41. data/lib/labclient/projects/snippets/project_snippet.rb +12 -0
  42. data/lib/labclient/snippets/snippet.rb +2 -2
  43. data/lib/labclient/users/user.rb +6 -0
  44. data/lib/labclient/version.rb +1 -1
  45. metadata +10 -7
  46. data/lib/labclient/generator/templates/template.rb +0 -23
  47. data/lib/labclient/open_struct.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ea1ef7fca86883f565358293520dd1b39484ca9c190e543408d91eba2d2e0e5
4
- data.tar.gz: 4d963d0f5b7740d9e514ab3e5bc170576a628e7f8642d59cf6777463cd0c3141
3
+ metadata.gz: ab2d7a4a0dcf9e876c42f3d92537ad03f6cc9d00391bf574188de36fa5dde3a6
4
+ data.tar.gz: 9bba10386c15059b97c79a0e6f0ed322af7614736b78e55fd968f362e767dd8e
5
5
  SHA512:
6
- metadata.gz: b46b5af89a9b7beafd0b7287e7c6047200e748cfedcd72021231823d75885b1551158d4d80227b1a6ba6c09b6024c27a02f63cb0aaefa8ea399c13a669fcaa94
7
- data.tar.gz: 5bf022a157f8f3cccdd0776a5e851bfe6a30ad189efcee4da7c3c43bc27cc5eb8ca185d857d165024f83edb17ec992657b4f33cc3a9065a19f2ab15655d9fc67
6
+ metadata.gz: 8998430e5b8d07584da6fe906d465238fd7725fa0773b9bb11a7b47dc755ce5fcb80d0cbb22055b16327665d70cbd034a24a63d310fabd11bd560c0ebd994f95
7
+ data.tar.gz: 3e61e5c568a93cfad5955198f6524349a89a06d729ead2d8f98c1d05c78e885edfb9fbb069fee2226731b26a1ab8c0404b857883f745d99c7564280e8f855b4d
@@ -3,7 +3,7 @@ require 'oj'
3
3
  require 'pry'
4
4
  require 'typhoeus'
5
5
  require 'active_support/all'
6
- require 'awesome_print'
6
+ require 'amazing_print'
7
7
  require 'gitlab_chronic_duration'
8
8
  require 'ostruct'
9
9
 
@@ -13,8 +13,8 @@ require 'labclient/http'
13
13
  require 'labclient/access_levels'
14
14
  require 'labclient/common'
15
15
  require 'labclient/curl'
16
+ require 'labclient/lab_struct'
16
17
  require 'labclient/klass'
17
- require 'labclient/open_struct'
18
18
  require 'labclient/class_helpers'
19
19
  require 'labclient/paginated_response'
20
20
  require 'labclient/error'
@@ -768,6 +768,7 @@ require 'labclient/files/delete'
768
768
  # Branches
769
769
  require 'labclient/branches/list'
770
770
  require 'labclient/branches/show'
771
+ require 'labclient/branches/create'
771
772
  require 'labclient/branches/delete'
772
773
  require 'labclient/branches/delete_merged'
773
774
  require 'labclient/branches/branch'
@@ -844,12 +845,14 @@ require 'labclient/resource_labels/resource_label'
844
845
 
845
846
  # Generators
846
847
  require 'labclient/generator/names'
847
- require 'labclient/generator/templates/template' # Helper
848
- require 'labclient/generator/templates/pages'
848
+ require 'labclient/generator/template_helper' # Default Include Template
849
849
 
850
850
  # Wizard
851
851
  require 'labclient/generator/generator'
852
852
  require 'labclient/generator/wizard'
853
853
 
854
+ # Dynamically Require Templates (Simplify new template creation)
855
+ Dir[File.dirname(__FILE__) + '/labclient/generator/templates/*.rb'].sort.each { |file| require file }
856
+
854
857
  # I am Very Last
855
858
  require 'labclient/client'
@@ -11,5 +11,30 @@ module LabClient
11
11
  def commit
12
12
  Commit.new(@table[:commit], response, client)
13
13
  end
14
+
15
+ def pipelines
16
+ project_id = collect_project_id
17
+ client.pipelines.list(project_id, ref: name)
18
+ end
19
+
20
+ # Wait for Import / Set a Hard Limit
21
+ def wait_for_pipelines(total_time = 300, sleep_time = 15)
22
+ Timeout.timeout(total_time) do
23
+ loop do
24
+ reload
25
+ puts 'Waiting for Pipelines'
26
+ break unless pipelines.empty?
27
+
28
+ sleep sleep_time
29
+ end
30
+ end
31
+ end
32
+
33
+ help do
34
+ subtitle 'Branch'
35
+ option 'commit', 'Show Commit information for branch'
36
+ option 'pipelines', 'Show Pipelines for this Branch/Ref'
37
+ option 'wait_for_pipelines', 'Looping, on newly created branches, wait for pipelines to populate (not finish) [Timeout, Interval]'
38
+ end
14
39
  end
15
40
  end
@@ -0,0 +1,33 @@
1
+ # Top namespace
2
+ module LabClient
3
+ # Specifics
4
+ class Branches < Common
5
+ doc 'Create' do
6
+ desc 'Get a list of repository branches from a project, sorted by name alphabetically. [Project ID, String]'
7
+ example 'client.branches.create(264, branch: :branch, ref: :master)'
8
+ result '[#<Branch name: feature>, #<Branch name: master>]'
9
+
10
+ markdown <<~DOC
11
+ | Attribute | Type | Required | Description |
12
+ |----------|--------|---------|-------------------------------------------------------------------------------------------------------------|
13
+ | id | integer | yes | ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user. |
14
+ | branch | string | yes | Name of the branch. |
15
+ | ref | string | yes | Branch name or commit SHA to create branch from. |
16
+ DOC
17
+ end
18
+
19
+ doc 'Create' do
20
+ desc 'via Project'
21
+ example <<~DOC
22
+ project = client.projects.show(264)
23
+ project.branch_create(branch: :branch, ref: :master)
24
+ DOC
25
+ end
26
+
27
+ def create(project_id, query)
28
+ project_id = format_id(project_id)
29
+
30
+ client.request(:post, "projects/#{project_id}/repository/branches", Branch, query)
31
+ end
32
+ end
33
+ end
@@ -139,10 +139,10 @@ module LabClient
139
139
  resp
140
140
  end
141
141
 
142
- # Assume we want OpenStruct if @klass is ever nil
142
+ # Assume we want LabStruct if @klass is ever nil
143
143
  def process(resp)
144
144
  case resp.data
145
- when OpenStruct
145
+ when LabStruct
146
146
  klass ? klass.new(resp.data, resp, self) : resp.data
147
147
  when Array
148
148
  if @klass.nil?
@@ -51,6 +51,15 @@ module LabClient
51
51
  client.resource_labels.epics.show(group_id, id, resource_event_id)
52
52
  end
53
53
 
54
+ # Notes
55
+ def notes
56
+ client.notes.epics.list(group_id, iid)
57
+ end
58
+
59
+ def note_create(query)
60
+ client.notes.epics.create(group_id, iid, query)
61
+ end
62
+
54
63
  date_time_attrs %i[created_at closed_at updated_at]
55
64
 
56
65
  help do
@@ -64,6 +73,10 @@ module LabClient
64
73
  option 'issue_remove', 'Remove issue from Epic [Epic Issue ID]'
65
74
  option 'issue_update', 'Update issue association [Epic Issue ID, Hash]'
66
75
 
76
+ # Notes
77
+ option 'notes', 'List notes/comments. [Hash]'
78
+ option 'note_create', 'Creates a new note. [Hash]'
79
+
67
80
  # Resource Labels
68
81
  option 'resource_labels', 'List of all label events'
69
82
  option 'resource_label', 'Show single label event [Resource Event ID]'
@@ -3,6 +3,7 @@ module LabClient
3
3
  # Class Shim
4
4
  class Error < StandardError
5
5
  attr_reader :resp
6
+
6
7
  def initialize(resp)
7
8
  super
8
9
  @resp = resp
@@ -16,15 +16,18 @@ module LabClient
16
16
  result '{:file_path=>"new.md", :branch=>"master"}'
17
17
 
18
18
  markdown <<~DOC
19
- **Parameters**:
19
+ Parameters
20
+
21
+ | **Attribute** | **Required** | **Description** |
22
+ | -------------- | ------------ | ----------------------------------------------- |
23
+ | branch | yes | Name of the branch |
24
+ | content | yes | File content |
25
+ | start_branch | no | Name of the branch to start the new commit from |
26
+ | encoding | no | Change encoding to ‘base64’. Default is text. |
27
+ | author_email | no | Specify the commit author’s email address |
28
+ | author_name | no | Specify the commit author’s name |
29
+ | commit_message | no | Commit message |
20
30
 
21
- * branch (required) - Name of the branch
22
- * start_branch (optional) - Name of the branch to start the new commit from
23
- * encoding (optional) - Change encoding to ‘base64’. Default is text.
24
- * author_email (optional) - Specify the commit author’s email address
25
- * author_name (optional) - Specify the commit author’s name
26
- * content (required) - File content
27
- * commit_message (required) - Commit message
28
31
 
29
32
  DOC
30
33
  end
@@ -43,6 +46,9 @@ module LabClient
43
46
  def create(project_id, file_path, query)
44
47
  project_id = format_id(project_id)
45
48
 
49
+ # Path Name Encoding
50
+ file_path = CGI.escape(file_path)
51
+
46
52
  client.request(:post, "projects/#{project_id}/repository/files/#{file_path}", nil, query)
47
53
  end
48
54
  end
@@ -54,29 +54,17 @@ module LabClient
54
54
  end
55
55
 
56
56
  doc 'Wizard' do
57
- title 'Templates'
57
+ title 'Run one templates'
58
58
  desc <<-DOC
59
59
  Outside of the wizard at large, you can also select and manually run specific templates.
60
60
  DOC
61
61
 
62
62
  example <<~DOC
63
- pages = client.wizard.pages
63
+ pages = client.wizard.template(:pages)
64
64
  => #<LabClient::Template Pages>
65
65
  pages.run! # Run pages template
66
66
  DOC
67
67
  end
68
-
69
- doc 'Pages' do
70
- markdown <<~DOC
71
- Create GitLab Pages group and test projects. Projects imported from Gitlab's [demo pages projects](https://gitlab.com/pages)
72
-
73
- | Setting | Default | Type | Description |
74
- | ---------- | --------- | ------- | -------------------------------------- |
75
- | group_name | Generated | String | Parent Group name for pages projects |
76
- | group_path | Generated | String | Parent Group path |
77
- | count | 5 | Integer | Number of projects to import (max: 28) |
78
- DOC
79
- end
80
68
  end
81
69
  end
82
70
  end
@@ -4,20 +4,34 @@ module LabClient
4
4
  module Generator
5
5
  # Name Helper
6
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
+
7
15
  def gen_groups
8
16
  [
9
17
  Faker::Games::LeagueOfLegends.location,
10
18
  Faker::Movies::LordOfTheRings.location,
11
19
  Faker::TvShows::RickAndMorty.location,
12
20
  Faker::TvShows::StarTrek.location,
13
- Faker::Games::ElderScrolls.region
21
+ Faker::Games::ElderScrolls.region,
22
+ Faker::Games::ElderScrolls.city,
23
+ Faker::Games::Zelda.location,
24
+ Faker::Games::SuperSmashBros.stage
14
25
  ].map { |x| x.gsub(/[^0-9A-Za-z]/, '') }
15
26
  end
16
27
 
17
28
  def gen_projects
18
29
  [
19
30
  Faker::Games::ElderScrolls.creature,
31
+ Faker::Movies::LordOfTheRings.location,
20
32
  Faker::Games::LeagueOfLegends.summoner_spell,
33
+ Faker::Games::Pokemon.move,
34
+ Faker::TvShows::RickAndMorty.location,
21
35
  Faker::Games::LeagueOfLegends.masteries,
22
36
  Faker::Superhero.power
23
37
  ].map { |x| x.gsub(/[^0-9A-Za-z]/, '') }
@@ -45,8 +59,8 @@ module LabClient
45
59
  # rubocop:disable Metrics/AbcSize
46
60
  def generate_names
47
61
  @user_names = Array.new(count[:users]) { gen_people }.flatten.uniq.sample(count[:users])
48
- @group_names = Array.new(count[:groups]) { gen_groups }.flatten.uniq
49
- @project_names = Array.new(rand(count[:projects])) { gen_projects.sample }.uniq
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])
50
64
  end
51
65
  # rubocop:enable Metrics/AbcSize
52
66
  end
@@ -0,0 +1,82 @@
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
+ # puts "#{@group.name} - #{@group.web_url}"
78
+ raise 'Unable to Create Group' unless @group.success?
79
+ end
80
+ end
81
+ end
82
+ 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