Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds filter for failed to replace env in config erros #10237

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions common/lib/dependabot/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ def self.updater_error_details(error)
{
"error-type": "missing_environment_variable",
"error-detail": {
"environment-variable": error.environment_variable
"environment-variable": error.environment_variable,
"error-message": error.message
}
}
when Dependabot::GoModulePathMismatch
Expand Down Expand Up @@ -550,10 +551,15 @@ class MissingEnvironmentVariable < DependabotError
sig { returns(String) }
attr_reader :environment_variable

sig { params(environment_variable: String).void }
def initialize(environment_variable)
sig { returns(String) }
attr_reader :message

sig { params(environment_variable: String, message: String).void }
def initialize(environment_variable, message = "")
@environment_variable = environment_variable
super("Missing environment variable #{@environment_variable}")
@message = message

super("Missing environment variable #{@environment_variable}. #{@message}")
end
end

Expand Down
19 changes: 19 additions & 0 deletions npm_and_yarn/lib/dependabot/npm_and_yarn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
# Used to identify if error message is related to yarn workspaces
DEPENDENCY_FILE_NOT_RESOLVABLE = "conflicts with direct dependency"

ENV_VAR_NOT_RESOLVABLE = /Failed to replace env in config: \$\{(?<var>.*)\}/

class Utils
extend T::Sig

Expand All @@ -87,6 +89,14 @@
required_version: match_data[:required_version]
}
end

sig { params(error_message: String).returns(String) }
def self.extract_var(error_message)
match_data = T.must(error_message.match(ENV_VAR_NOT_RESOLVABLE)).named_captures["var"]
Dismissed Show dismissed Hide dismissed
return "" unless match_data

match_data
end
end

YARN_CODE_REGEX = /(YN\d{4})/
Expand Down Expand Up @@ -191,6 +201,15 @@
new_error: ->(_error, message) { DependencyFileNotResolvable.new(message) },
in_usage: false,
matchfn: nil
},
{
patterns: [ENV_VAR_NOT_RESOLVABLE],
new_error: lambda { |_error, message|
var = Utils.extract_var(message)
Dependabot::MissingEnvironmentVariable.new(var, message)
},
in_usage: false,
matchfn: nil
}
].freeze, T::Array[{
patterns: T::Array[T.any(String, Regexp)],
Expand Down
58 changes: 58 additions & 0 deletions npm_and_yarn/spec/dependabot/npm_and_yarn/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3281,6 +3281,28 @@
end
end

context "when the npm registry access token var is missing its env var" do
let(:files) { project_dependency_files("yarn/npm_global_registry_env_var_missing") }
let(:credentials) do
[Dependabot::Credential.new({
"type" => "npm_registry",
"registry" => "https://registry.npmjs.org",
"token" => "${NPM_TOKEN}"
})]
end

let(:source) do
{ type: "registry", url: "https://registry.npmjs.org" }
end

it "keeps the preference for the npm registry" do
expect { updated_yarn_lock }.to raise_error do |error|
expect(error).to be_a(Dependabot::MissingEnvironmentVariable)
expect(error.message).to include("Failed to replace env in config: ${NPM_TOKEN}")
end
end
end

context "when there's a duplicate indirect dependency" do
let(:files) { project_dependency_files("yarn/duplicate_indirect_dependency") }

Expand Down Expand Up @@ -3327,6 +3349,42 @@
end
end

context "when the private registry access #1 token var is missing its env var" do
let(:files) { project_dependency_files("yarn/npm_global_registry_env_var_missing") }
let(:credentials) do
[Dependabot::Credential.new({
"type" => "npm_registry",
"registry" => "https://packagecloud.io/",
"token" => "${PACKAGECLOUD_TOKEN}"
})]
end

it "keeps the preference for the npm registry" do
expect { updated_yarn_lock }.to raise_error do |error|
expect(error).to be_a(Dependabot::MissingEnvironmentVariable)
expect(error.message).to include("Failed to replace env in config: ${PACKAGECLOUD_TOKEN}")
end
end
end

context "when the private registry access #2 token var is missing its env var" do
let(:files) { project_dependency_files("yarn/npm_global_registry_env_var_missing") }
let(:credentials) do
[Dependabot::Credential.new({
"type" => "npm_registry",
"registry" => "https://npm.fontawesome.com/",
"token" => "${FONTAWESOME_NPM_AUTH_TOKEN}"
})]
end

it "keeps the preference for the npm registry" do
expect { updated_yarn_lock }.to raise_error do |error|
expect(error).to be_a(Dependabot::MissingEnvironmentVariable)
expect(error.message).to include("Failed to replace env in config: ${FONTAWESOME_NPM_AUTH_TOKEN}")
end
end
end

context "with resolutions" do
let(:files) { project_dependency_files("yarn/resolution_specified") }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
registry "https://registry.npmjs.org"
"//registry.npmjs.org/:_authToken" ${NPM_TOKEN}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "{{ name }}",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no\\ test\\ specified\" && exit 1",
"prettify": "prettier --write \"{{packages/*/src,examples,cypress,scripts}/**/,}*.{js,jsx,ts,tsx,css,md}\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/waltfy/PROTO_TEST.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/waltfy/PROTO_TEST/issues"
},
"homepage": "https://github.com/waltfy/PROTO_TEST#readme",
"dependencies": {
"fetch-factory": "^0.0.1"
},
"devDependencies": {
"etag" : "^1.0.0"
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


encoding@^0.1.11:
version "0.1.12"
resolved "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
dependencies:
iconv-lite "~0.4.13"

es6-promise@^3.0.2:
version "3.3.1"
resolved "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613"

etag@^1.0.0:
version "1.7.0"
resolved "https://registry.npmjs.org/etag/-/etag-1.7.0.tgz#03d30b5f67dd6e632d2945d30d6652731a34d5d8"

fetch-factory@^0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/fetch-factory/-/fetch-factory-0.0.1.tgz#e0076059bdb31e3147c75b3b8c04133ba8c7e071"
dependencies:
es6-promise "^3.0.2"
isomorphic-fetch "^2.1.1"
lodash "^3.10.1"

iconv-lite@~0.4.13:
version "0.4.15"
resolved "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.15.tgz#fe265a218ac6a57cfe854927e9d04c19825eddeb"

is-stream@^1.0.1:
version "1.1.0"
resolved "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"

isomorphic-fetch@^2.1.1:
version "2.2.1"
resolved "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9"
dependencies:
node-fetch "^1.0.1"
whatwg-fetch ">=0.10.0"

lodash@^3.10.1:
version "3.10.1"
resolved "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

node-fetch@^1.0.1:
version "1.6.3"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-1.6.3.tgz#dc234edd6489982d58e8f0db4f695029abcd8c04"
dependencies:
encoding "^0.1.11"
is-stream "^1.0.1"

whatwg-fetch@>=0.10.0:
version "2.0.2"
resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.2.tgz#fe294d1d89e36c5be8b3195057f2e4bc74fc980e"

Loading