Skip to content

Commit

Permalink
use correct reference when using strip_component
Browse files Browse the repository at this point in the history
The current implmnetation sets the environment name to the "striped"
version of the name, this is then later used as the git reference when
syncing environments.

With the following configuration
```yaml
sources:
  b4ldr:
    strip_component: "wmcs/"
    ignore_branch_prefixes:
    - production
    - ops
    - sandbox
    - pontoon
    remote: https://github.com/b4ldr/puppet-1
```

where upstream has a branch with wmcs/production then we see the
following error

  Unable to sync repo to unresolvable ref 'production' at
    /etc/puppet/code/environments/production/.git

If upstream also has a production branch then the above configuration
ends up syncing  /etc/puppet/code/environments/production/ with the
remote production branch.  even thought it should be striped via
`ignore_branch_prefixes`

This PR updates the name object to keep hold of the original name and
store it as the ref
  • Loading branch information
b4ldr committed Nov 19, 2021
1 parent 853f9a8 commit 64c8c87
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/r10k/environment/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ module Environment
# @api private
class Name

# @!attribute [r] ref
# @return [String] The unmodified reference of the upstream repository
# @!attribute [r] name
# @return [String] The unmodified name of the environment
attr_reader :name
# @return [String] The name of the environment (with component striped)
attr_reader :name, :ref
attr_reader :name, :ref

INVALID_CHARACTERS = %r[\W]

Expand All @@ -16,6 +19,7 @@ def initialize(name, opts)
@prefix = opts[:prefix]
@invalid = opts[:invalid]

@ref = name
@name = derive_name(name, opts[:strip_component])
@opts = opts

Expand Down
4 changes: 2 additions & 2 deletions lib/r10k/source/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def generate_environments
@basedir,
bn.dirname,
{remote: remote,
ref: bn.name,
ref: bn.ref,
puppetfile_name: puppetfile_name,
overrides: @options[:overrides]})
elsif bn.correct?
Expand All @@ -115,7 +115,7 @@ def generate_environments
@basedir,
bn.dirname,
{remote: remote,
ref: bn.name,
ref: bn.ref,
puppetfile_name: puppetfile_name,
overrides: @options[:overrides]})
elsif bn.validate?
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/environment/name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
it "does not modify the given name when no strip_component is given" do
bn = described_class.new('myenv', source: 'source', prefix: false)
expect(bn.dirname).to eq 'myenv'
expect(bn.name).to eq 'myenv'
expect(bn.ref).to eq 'myenv'
end

it "removes the first occurance of a regex match when a regex is given" do
bn = described_class.new('myenv', source: 'source', prefix: false, strip_component: '/env/')
expect(bn.dirname).to eq 'my'
expect(bn.name).to eq 'my'
expect(bn.ref).to eq 'myenv'
end

it "does not modify the given name when there is no regex match" do
bn = described_class.new('myenv', source: 'source', prefix: false, strip_component: '/bar/')
expect(bn.dirname).to eq 'myenv'
expect(bn.name).to eq 'myenv'
expect(bn.ref).to eq 'myenv'
end

it "removes the given name's prefix when it matches strip_component" do
bn = described_class.new('env/prod', source: 'source', prefix: false, strip_component: 'env/')
expect(bn.dirname).to eq 'prod'
expect(bn.name).to eq 'prod'
expect(bn.ref).to eq 'env/prod'
end

it "raises an error when given an integer" do
Expand All @@ -34,21 +42,29 @@
it "uses the branch name as the dirname when prefixing is off" do
bn = described_class.new('mybranch', :source => 'source', :prefix => false)
expect(bn.dirname).to eq 'mybranch'
expect(bn.name).to eq 'mybranch'
expect(bn.ref).to eq 'mybranch'
end

it "prepends the source name when prefixing is on" do
bn = described_class.new('mybranch', :source => 'source', :prefix => true)
expect(bn.dirname).to eq 'source_mybranch'
expect(bn.name).to eq 'mybranch'
expect(bn.ref).to eq 'mybranch'
end

it "prepends the prefix name when prefixing is overridden" do
bn = described_class.new('mybranch', {:prefix => "bar", :sourcename => 'foo'})
expect(bn.dirname).to eq 'bar_mybranch'
expect(bn.name).to eq 'mybranch'
expect(bn.ref).to eq 'mybranch'
end

it "uses the branch name as the dirname when prefixing is nil" do
bn = described_class.new('mybranch', {:prefix => nil, :sourcename => 'foo'})
expect(bn.dirname).to eq 'mybranch'
expect(bn.name).to eq 'mybranch'
expect(bn.ref).to eq 'mybranch'
end
end

Expand Down Expand Up @@ -149,6 +165,8 @@
it "replaces invalid characters in #{branch} with underscores" do
bn = described_class.new(branch.dup, {:correct => true})
expect(bn.dirname).to eq branch.gsub(/\W/, '_')
expect(bn.name).to eq branch
expect(bn.ref).to eq branch
end
end

Expand Down

0 comments on commit 64c8c87

Please sign in to comment.