Skip to content

Commit c769177

Browse files
committed
WIP: CLI: New command to push commits
1 parent 64170aa commit c769177

5 files changed

Lines changed: 100 additions & 3 deletions

File tree

.rubocop_todo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-10-18 17:37:39 UTC using RuboCop version 1.22.1.
3+
# on 2021-10-18 17:37:42 UTC using RuboCop version 1.22.1.
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
@@ -14,14 +14,14 @@ Metrics/AbcSize:
1414
# Offense count: 2
1515
# Configuration parameters: CountComments, CountAsOne.
1616
Metrics/ClassLength:
17-
Max: 154
17+
Max: 174
1818

1919
# Offense count: 4
2020
# Configuration parameters: IgnoredMethods.
2121
Metrics/CyclomaticComplexity:
2222
Max: 14
2323

24-
# Offense count: 16
24+
# Offense count: 17
2525
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
2626
Metrics/MethodLength:
2727
Max: 34

features/push.feature

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Feature: push
2+
Push commits to remote
3+
4+
Scenario: Push available commits to remote
5+
Given a mocked git configuration
6+
And a puppet module "puppet-test" from "awesome"
7+
And a file named "managed_modules.yml" with:
8+
"""
9+
---
10+
puppet-test:
11+
namespace: awesome
12+
"""
13+
And a file named "modulesync.yml" with:
14+
"""
15+
---
16+
branch: modulesync
17+
"""
18+
And a git_base option appended to "modulesync.yml" for local tests
19+
And I successfully run `msync reset`
20+
And I cd to "modules/awesome/puppet-test"
21+
And I run `touch hello`
22+
And I run `git add hello`
23+
And I run `git commit -m'Hello!'`
24+
And I cd to "~"
25+
Then the puppet module "puppet-test" from "awesome" should have no commits made by "Aruba"
26+
When I successfully run `msync push --verbose`
27+
Then the puppet module "puppet-test" from "awesome" should have 1 commit made by "Aruba" in branch "modulesync"
28+
29+
@announce-output
30+
Scenario: Push command without a branch sets
31+
Given a basic setup with a puppet module "puppet-test" from "awesome"
32+
When I run `msync push --verbose`
33+
Then the exit status should be 1
34+
And the stderr should contain:
35+
"""
36+
Error: 'branch' option is missing, please set it in configuration or in command line.
37+
"""
38+
39+
Scenario: Report the need to clone repositories if sourcecode was not cloned before
40+
Given a basic setup with a puppet module "puppet-test" from "awesome"
41+
And the global option "branch" sets to "modulesync"
42+
When I run `msync push --verbose`
43+
Then the exit status should be 1
44+
And the stderr should contain:
45+
"""
46+
puppet-test: Repository must be locally available before trying to push
47+
"""

lib/modulesync.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,21 @@ def self.reset(cli_options)
223223
)
224224
end
225225
end
226+
227+
def self.push(cli_options)
228+
@options = config_defaults.merge(cli_options)
229+
230+
if @options[:branch].nil?
231+
raise Thor::Error,
232+
"Error: 'branch' option is missing, please set it in configuration or in command line."
233+
end
234+
235+
puts "XXX: #{@options[:branch]}"
236+
237+
managed_modules.each do |puppet_module|
238+
puppet_module.repository.push branch: @options[:branch], remote_branch: @options[:remote_branch]
239+
rescue ModuleSync::Error => e
240+
raise Thor::Error, "#{puppet_module.given_name}: #{e.message}"
241+
end
242+
end
226243
end

lib/modulesync/cli.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,28 @@ def reset
195195
ModuleSync.reset(config)
196196
end
197197

198+
desc 'push', 'Push all available commits from branch to remote'
199+
option :configs,
200+
:aliases => '-c',
201+
:desc => 'The local directory or remote repository to define the list of managed modules,' \
202+
' the file templates, and the default values for template variables.'
203+
option :managed_modules_conf,
204+
:desc => 'The file name to define the list of managed modules'
205+
option :branch,
206+
:aliases => '-b',
207+
:desc => 'Branch name to push',
208+
:default => CLI.defaults[:branch]
209+
option :remote_branch,
210+
:desc => 'Remote branch to push to (e.g. maintenance)'
211+
212+
def push
213+
config = {
214+
:command => 'push',
215+
}.merge(options)
216+
config = Util.symbolize_keys(config)
217+
ModuleSync.push(config)
218+
end
219+
198220
desc 'hook', 'Activate or deactivate a git hook.'
199221
subcommand 'hook', ModuleSync::CLI::Hook
200222
end

lib/modulesync/repository.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ def submit_changes(files, options)
161161
true
162162
end
163163

164+
def push(branch:, remote_branch:)
165+
raise ModuleSync::Error, 'Repository must be locally available before trying to push' unless cloned?
166+
167+
remote = 'origin'
168+
remote_url = git.remote(remote).url
169+
remote_branch ||= branch
170+
puts "Push branch '#{branch}' to '#{remote_url}' (#{remote}/#{remote_branch})"
171+
172+
git.push(remote, "#{branch}:#{remote_branch}", force: true)
173+
end
174+
164175
# Needed because of a bug in the git gem that lists ignored files as
165176
# untracked under some circumstances
166177
# https://github.com/schacon/ruby-git/issues/130

0 commit comments

Comments
 (0)