Backend: Use `include:remote` that points a YAML contains `include:template:rules` raises a system error
Problem
Discovered this bug while testing the new feature for refactoring Auto DevOps yamls.
For example, I included the modified Auto-DevOps.gitlab-ci.yml
in a test project. With the change:
include:
- remote: https://gitlab.com/gitlab-org/gitlab/-/raw/afe7f6fbd2a19f7633348d89d5e7fa673a553df7/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml
This results in 500 errors on viewing the file. This is because the system runs the static linter on the blob viewer and the linter encountered an exception. For example, you can reproduce this bug in the following commands in rails-console:
project = Project.find_by_full_path('root/rules-include-test')
user = User.first
content = <<~EOS
include:
- remote: https://gitlab.com/gitlab-org/gitlab/-/raw/afe7f6fbd2a19f7633348d89d5e7fa673a553df7/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml
EOS
Gitlab::Ci::Lint.new(project: project, current_user: user).validate(content)
Debugging with pry:
From: /devkitkat/services/rails/src/lib/gitlab/ci/pipeline/expression/statement.rb:13 Gitlab::Ci::Pipeline::Expression::Statement#initialize:
10: def initialize(statement, variables = nil)
11: @lexer = Expression::Lexer.new(statement)
12: binding.pry
=> 13: @variables = variables&.to_hash
14: end
[1] pry(#<Gitlab::Ci::Pipeline::Expression::Statement>)> variables&.to_hash
NoMethodError: undefined method `to_hash' for []:Array
Did you mean? to_h
from (pry):17:in `initialize'
This is because the context.mutate
wipes out the variables
in File::Template
:
From: /devkitkat/services/rails/src/lib/gitlab/ci/config/external/file/base.rb:102 Gitlab::Ci::Config::External::File::Base#expand_includes:
100: def expand_includes(hash)
101: binding.pry
=> 102: External::Processor.new(hash, context.mutate(expand_context_attrs)).perform
103: binding.pry
104: end
Sentry
Original title: NoMethodError: undefined method `to_hash' for []:Array
https://sentry.gitlab.net/gitlab/gitlabcom/issues/2813714/?referrer=gitlab_plugin
NoMethodError: undefined method `to_hash' for []:Array
Did you mean? to_h
lib/gitlab/ci/pipeline/expression/statement.rb:12:in `initialize'
@variables = variables&.to_hash
lib/gitlab/ci/build/rules/rule/clause/if.rb:12:in `new'
::Gitlab::Ci::Pipeline::Expression::Statement.new(
lib/gitlab/ci/build/rules/rule/clause/if.rb:12:in `satisfied_by?'
::Gitlab::Ci::Pipeline::Expression::Statement.new(
lib/gitlab/ci/build/rules/rule.rb:27:in `block in matches?'
@clauses.all? { |clause| clause.satisfied_by?(pipeline, context) }
lib/gitlab/ci/build/rules/rule.rb:27:in `all?'
@clauses.all? { |clause| clause.satisfied_by?(pipeline, context) }
...
(159 additional frame(s) were not displayed)
Edited by Mark Nuzzo