Skip to content

Commit 98fe295

Browse files
authored
Merge pull request #219 from opus-codium/rework-prmr-feature
Rework PR/MR feature
2 parents f173ac3 + 57ebbb4 commit 98fe295

20 files changed

Lines changed: 869 additions & 265 deletions

.rubocop_todo.yml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-04-22 16:30:35 +0200 using RuboCop version 0.50.0.
3+
# on 2021-04-23 16:47:47 +0200 using RuboCop version 0.50.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
9+
# Offense count: 3
1010
Lint/UselessAssignment:
1111
Exclude:
1212
- 'lib/modulesync.rb'
1313

1414
# Offense count: 10
1515
Metrics/AbcSize:
16-
Max: 67
16+
Max: 59
1717

1818
# Offense count: 2
1919
# Configuration parameters: CountComments.
2020
Metrics/ClassLength:
21-
Max: 128
21+
Max: 125
2222

23-
# Offense count: 3
23+
# Offense count: 4
2424
Metrics/CyclomaticComplexity:
25-
Max: 12
25+
Max: 13
26+
27+
# Offense count: 3
28+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
29+
# URISchemes: http, https
30+
Metrics/LineLength:
31+
Max: 186
2632

27-
# Offense count: 13
33+
# Offense count: 17
2834
# Configuration parameters: CountComments.
2935
Metrics/MethodLength:
30-
Max: 36
36+
Max: 32
3137

3238
# Offense count: 3
3339
Metrics/PerceivedComplexity:
34-
Max: 13
40+
Max: 14
3541

3642
# Offense count: 8
3743
Style/Documentation:
@@ -49,3 +55,12 @@ Style/Documentation:
4955
Style/EachWithObject:
5056
Exclude:
5157
- 'lib/modulesync/util.rb'
58+
59+
# Offense count: 2
60+
# Cop supports --auto-correct.
61+
# Configuration parameters: EnforcedStyleForMultiline, SupportedStylesForMultiline.
62+
# SupportedStylesForMultiline: comma, consistent_comma, no_comma
63+
Style/TrailingCommaInArguments:
64+
Exclude:
65+
- 'lib/modulesync/git_service/base.rb'
66+
- 'lib/modulesync/source_code.rb'

features/update.feature

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -713,28 +713,6 @@ Feature: update
713713
And the puppet module "puppet-test" from "fakenamespace" should have only 1 commit made by "Aruba"
714714
And the puppet module "puppet-test" from "fakenamespace" should have 1 commit made by "Aruba" in branch "test"
715715

716-
Scenario: Creating a GitHub PR with an update
717-
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
718-
And a directory named "moduleroot"
719-
And I set the environment variables to:
720-
| variable | value |
721-
| GITHUB_TOKEN | foobar |
722-
When I run `msync update --noop --branch managed_update --pr`
723-
Then the output should contain "Would submit PR "
724-
And the exit status should be 0
725-
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
726-
727-
Scenario: Creating a GitLab MR with an update
728-
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
729-
And a directory named "moduleroot"
730-
And I set the environment variables to:
731-
| variable | value |
732-
| GITLAB_TOKEN | foobar |
733-
When I run `msync update --noop --branch managed_update --pr`
734-
Then the output should contain "Would submit MR "
735-
And the exit status should be 0
736-
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
737-
738716
Scenario: Repository with a default branch other than master
739717
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
740718
And the puppet module "puppet-test" from "fakenamespace" has the default branch named "develop"
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
Feature: Create a pull-request/merge-request after update
2+
3+
Scenario: Run update in no-op mode and ask for GitHub PR
4+
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
5+
And a file named "managed_modules.yml" with:
6+
"""
7+
---
8+
puppet-test:
9+
github: {}
10+
"""
11+
And I set the environment variables to:
12+
| variable | value |
13+
| GITHUB_TOKEN | foobar |
14+
| GITHUB_BASE_URL | https://github.example.com |
15+
And a file named "config_defaults.yml" with:
16+
"""
17+
---
18+
test:
19+
name: aruba
20+
"""
21+
And a file named "moduleroot/test.erb" with:
22+
"""
23+
<%= @configs['name'] %>
24+
"""
25+
When I run `msync update --noop --branch managed_update --pr`
26+
Then the output should contain "Would submit PR "
27+
And the exit status should be 0
28+
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
29+
30+
Scenario: Run update in no-op mode and ask for GitLab MR
31+
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
32+
And a file named "managed_modules.yml" with:
33+
"""
34+
---
35+
puppet-test:
36+
gitlab: {
37+
base_url: 'https://gitlab.example.com'
38+
}
39+
"""
40+
And I set the environment variables to:
41+
| variable | value |
42+
| GITLAB_TOKEN | foobar |
43+
And a file named "config_defaults.yml" with:
44+
"""
45+
---
46+
test:
47+
name: aruba
48+
"""
49+
And a file named "moduleroot/test.erb" with:
50+
"""
51+
<%= @configs['name'] %>
52+
"""
53+
When I run `msync update --noop --branch managed_update --pr`
54+
Then the output should contain "Would submit MR "
55+
And the exit status should be 0
56+
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
57+
58+
Scenario: Run update without changes in no-op mode and ask for GitLab MR
59+
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
60+
And a directory named "moduleroot"
61+
And a file named "managed_modules.yml" with:
62+
"""
63+
---
64+
puppet-test:
65+
gitlab: {
66+
base_url: 'https://gitlab.example.com'
67+
}
68+
"""
69+
And I set the environment variables to:
70+
| variable | value |
71+
| GITLAB_TOKEN | foobar |
72+
When I run `msync update --noop --branch managed_update --pr`
73+
Then the output should not contain "Would submit MR "
74+
And the exit status should be 0
75+
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
76+
77+
Scenario: Ask for PR without credentials
78+
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
79+
And a file named "managed_modules.yml" with:
80+
"""
81+
---
82+
puppet-test:
83+
gitlab: {
84+
base_url: https://gitlab.example.com
85+
}
86+
"""
87+
And a file named "config_defaults.yml" with:
88+
"""
89+
---
90+
test:
91+
name: aruba
92+
"""
93+
And a file named "moduleroot/test.erb" with:
94+
"""
95+
<%= @configs['name'] %>
96+
"""
97+
When I run `msync update --noop --pr`
98+
Then the stderr should contain "A token is required to use services from gitlab"
99+
And the exit status should be 1
100+
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
101+
102+
Scenario: Ask for PR/MR with modules from GitHub and from GitLab
103+
Given a basic setup with a puppet module "puppet-github" from "fakenamespace"
104+
And a basic setup with a puppet module "puppet-gitlab" from "fakenamespace"
105+
And a file named "managed_modules.yml" with:
106+
"""
107+
---
108+
puppet-github:
109+
github:
110+
base_url: https://github.example.com
111+
token: 'secret'
112+
puppet-gitlab:
113+
gitlab:
114+
base_url: https://gitlab.example.com
115+
token: 'secret'
116+
"""
117+
And a file named "config_defaults.yml" with:
118+
"""
119+
---
120+
test:
121+
name: aruba
122+
"""
123+
And a file named "moduleroot/test.erb" with:
124+
"""
125+
<%= @configs['name'] %>
126+
"""
127+
When I run `msync update --noop --branch managed_update --pr`
128+
Then the exit status should be 0
129+
And the output should contain "Would submit PR "
130+
And the output should contain "Would submit MR "
131+
And the puppet module "puppet-github" from "fakenamespace" should have no commits made by "Aruba"
132+
And the puppet module "puppet-gitlab" from "fakenamespace" should have no commits made by "Aruba"
133+
134+
Scenario: Ask for PR with same source and target branch
135+
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
136+
And a file named "managed_modules.yml" with:
137+
"""
138+
---
139+
puppet-test:
140+
gitlab:
141+
token: 'secret'
142+
base_url: 'https://gitlab.example.com'
143+
"""
144+
And a file named "config_defaults.yml" with:
145+
"""
146+
---
147+
test:
148+
name: aruba
149+
"""
150+
And a file named "moduleroot/test.erb" with:
151+
"""
152+
<%= @configs['name'] %>
153+
"""
154+
When I run `msync update --noop --branch managed_update --pr --pr-target-branch managed_update`
155+
Then the stderr should contain "Unable to open a pull request with the same source and target branch: 'managed_update'"
156+
And the exit status should be 1
157+
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"
158+
159+
Scenario: Ask for PR with the default branch as source and target
160+
Given a basic setup with a puppet module "puppet-test" from "fakenamespace"
161+
And the puppet module "puppet-test" from "fakenamespace" has the default branch named "custom_default_branch"
162+
And a file named "managed_modules.yml" with:
163+
"""
164+
---
165+
puppet-test:
166+
github:
167+
token: 'secret'
168+
base_url: 'https://gitlab.example.com'
169+
"""
170+
And a file named "config_defaults.yml" with:
171+
"""
172+
---
173+
test:
174+
name: aruba
175+
"""
176+
And a file named "moduleroot/test.erb" with:
177+
"""
178+
<%= @configs['name'] %>
179+
"""
180+
And a directory named "moduleroot"
181+
When I run `msync update --noop --pr`
182+
Then the stderr should contain "Unable to open a pull request with the same source and target branch: 'custom_default_branch'"
183+
And the exit status should be 1
184+
And the puppet module "puppet-test" from "fakenamespace" should have no commits made by "Aruba"

lib/modulesync.rb

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,16 @@ def self.manage_module(puppet_module, module_files, defaults)
129129

130130
if options[:noop]
131131
puts "Using no-op. Files in '#{puppet_module.given_name}' may be changed but will not be committed."
132-
puppet_module.repository.show_changes(options)
133-
options[:pr] && \
134-
pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options)
132+
changed = puppet_module.repository.show_changes(options)
133+
changed && options[:pr] && puppet_module.open_pull_request
135134
elsif !options[:offline]
136135
pushed = puppet_module.repository.submit_changes(files_to_manage, options)
137136
# Only bump/tag if pushing didn't fail (i.e. there were changes)
138137
if pushed && options[:bump]
139138
new = puppet_module.bump(options[:message], options[:changelog])
140139
puppet_module.repository.tag(new, options[:tag_pattern]) if options[:tag]
141140
end
142-
pushed && options[:pr] && \
143-
pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options)
141+
pushed && options[:pr] && puppet_module.open_pull_request
144142
end
145143
end
146144

@@ -157,15 +155,6 @@ def self.update(cli_options)
157155
@options = config_defaults.merge(cli_options)
158156
defaults = Util.parse_config(config_path(CONF_FILE, options))
159157

160-
if options[:pr]
161-
unless options[:branch]
162-
$stderr.puts 'A branch must be specified with --branch to use --pr!'
163-
raise
164-
end
165-
166-
@pr = create_pr_manager if options[:pr]
167-
end
168-
169158
local_template_dir = config_path(MODULE_FILES_DIR, options)
170159
local_files = find_template_files(local_template_dir)
171160
module_files = relative_names(local_files, local_template_dir)
@@ -189,40 +178,4 @@ def self.update(cli_options)
189178
end
190179
exit 1 if errors && options[:fail_on_warnings]
191180
end
192-
193-
def self.pr(puppet_module)
194-
module_options = puppet_module.options
195-
github_conf = module_options[:github]
196-
gitlab_conf = module_options[:gitlab]
197-
198-
if !github_conf.nil?
199-
base_url = github_conf[:base_url] || ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com')
200-
require 'modulesync/pr/github'
201-
ModuleSync::PR::GitHub.new(github_conf[:token], base_url)
202-
elsif !gitlab_conf.nil?
203-
base_url = gitlab_conf[:base_url] || ENV.fetch('GITLAB_BASE_URL', 'https://gitlab.com/api/v4')
204-
require 'modulesync/pr/gitlab'
205-
ModuleSync::PR::GitLab.new(gitlab_conf[:token], base_url)
206-
elsif @pr.nil?
207-
$stderr.puts 'No GitHub or GitLab token specified for --pr!'
208-
raise
209-
else
210-
@pr
211-
end
212-
end
213-
214-
def self.create_pr_manager
215-
github_token = ENV.fetch('GITHUB_TOKEN', '')
216-
gitlab_token = ENV.fetch('GITLAB_TOKEN', '')
217-
218-
if !github_token.empty?
219-
require 'modulesync/pr/github'
220-
ModuleSync::PR::GitHub.new(github_token, ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com'))
221-
elsif !gitlab_token.empty?
222-
require 'modulesync/pr/gitlab'
223-
ModuleSync::PR::GitLab.new(gitlab_token, ENV.fetch('GITLAB_BASE_URL', 'https://gitlab.com/api/v4'))
224-
else
225-
warn '--pr specified without environment variables GITHUB_TOKEN or GITLAB_TOKEN'
226-
end
227-
end
228181
end

lib/modulesync/cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Base < Thor
104104
:default => CLI.defaults[:pr_labels] || []
105105
option :pr_target_branch,
106106
:desc => 'Target branch for the pull/merge request',
107-
:default => CLI.defaults[:pr_target_branch] || 'master'
107+
:default => CLI.defaults[:pr_target_branch]
108108
option :offline,
109109
:type => :boolean,
110110
:desc => 'Do not run any Git commands. Allows the user to manage Git outside of ModuleSync.',

0 commit comments

Comments
 (0)