Skip to content

Commit

Permalink
Merge pull request #273 from travis-ci/bfsy-304-am
Browse files Browse the repository at this point in the history
[BFSY-304] Allow to use commit_message as a template for import source
  • Loading branch information
murtaza-swati authored Apr 29, 2024
2 parents dca14e6 + 65d90f1 commit 550f73b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
11 changes: 10 additions & 1 deletion lib/travis/yml/configs/config/file.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'cgi'
require 'travis/yml/configs/content'
require 'travis/yml/configs/config/base'

Expand All @@ -10,6 +11,8 @@ class File < Obj.new(:ctx, :parent, :vcs_id, :provider, :defn)

attr_reader :path, :ref, :raw

PERMITTED_KEYS = %w(commit_message).freeze

def initialize(ctx, parent, vcs_id, provider, defn)
defn = stringify(defn)
@vcs_id = vcs_id
Expand Down Expand Up @@ -101,10 +104,16 @@ def expand(source)
end

def fetch
Content.new(repo, "#{path_suffix}#{path}", ref).content
Content.new(repo, interpolated_path, ref).content
rescue FileNotFound => e
required? ? raise : nil
end

def interpolated_path
new_path = path.gsub(/%{(#{PERMITTED_KEYS.join('|')}|.*)}/) { CGI.escape(ctx.data[$1.to_sym]) }

"#{path_suffix}#{new_path}"
end
end
end
end
Expand Down
15 changes: 14 additions & 1 deletion lib/travis/yml/schema/def/imports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,26 @@ def define
normal
prefix :source

map :source, to: :str, summary: 'The source to import build config from', eg: './import.yml@v1'
map :source, to: :import_template, summary: 'The source to import build config from', eg: './import.yml@v1'
map :mode, to: :str, values: ['merge', 'deep_merge', 'deep_merge_append', 'deep_merge_prepend'], summary: 'How to merge the imported config into the target config (defaults to deep_merge_append)'
map :if, to: :condition

export
end
end

class ImportTemplate < Type::Str
registry :import
register :import_template

def define
vars *%w(
commit_message
)

export
end
end
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@
"type": "object",
"properties": {
"source": {
"type": "string",
"$ref": "#/definitions/import/import_template",
"summary": "The source to import build config from",
"example": "./import.yml@v1"
},
Expand All @@ -836,7 +836,7 @@
}
},
{
"type": "string",
"$ref": "#/definitions/import/import_template",
"example": "./import.yml@v1"
}
],
Expand Down Expand Up @@ -2451,6 +2451,16 @@
"normal": true
}
},
"import": {
"import_template": {
"$id": "import_template",
"title": "Import Template",
"type": "string",
"vars": [
"commit_message"
]
}
},
"addon": {
"apt": {
"$id": "apt",
Expand Down
22 changes: 22 additions & 0 deletions spec/travis/yml/configs/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,28 @@ def self.imports(sources)
it { expect(subject.map(&:merge_modes)).to eq [{ lft: :deep_merge_append }, { lft: 'merge' }, { lft: 'deep_merge' }] }
end

describe 'imports with template path' do
let(:data) { { commit_message: 'one' } }

let(:travis_yml) do
<<~yml
import:
- source: "%{commit_message}/one.yml"
mode: merge
yml
end

let(:one_yml) do
<<~yml
import:
- source: "%{commit_message}/two.yml"
mode: deep_merge
yml
end

it { expect(subject.map(&:merge_modes)).to eq [{ lft: :deep_merge_append }, { lft: 'merge' }, { lft: 'deep_merge' }] }
end

describe 'conditional imports' do
let(:travis_yml) do
<<~yml
Expand Down
8 changes: 4 additions & 4 deletions spec/travis/yml/schema/def/imports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
type: :object,
properties: {
source: {
type: :string,
summary: 'The source to import build config from',
example: './import.yml@v1'
'$ref': '#/definitions/import/import_template',
example:'./import.yml@v1',
summary: 'The source to import build config from'
},
mode: {
type: :string,
Expand All @@ -60,7 +60,7 @@
},
},
{
type: :string,
'$ref': '#/definitions/import/import_template',
example: './import.yml@v1'
}
]
Expand Down

0 comments on commit 550f73b

Please sign in to comment.