diff options
| author | Can Eldem <celdem@gitlab.com> | 2020-06-08 12:16:35 +0000 |
|---|---|---|
| committer | Can Eldem <celdem@gitlab.com> | 2020-06-08 12:16:35 +0000 |
| commit | 5eef2e4c1a5c28f4e2b8e48e4701dae56264a979 (patch) | |
| tree | f853dc1c8601fb4222908f2ce8d4e03880367db3 | |
| parent | 12ef778891c4bf9fae19ffdc9d566fc7ab309957 (diff) | |
| parent | 55d3e5288826e21286054ed7cbaaec15e037f1dd (diff) | |
Merge branch 'asdf-java-version' into 'master'v3.11.2
Load the configured version of Java/Python
See merge request gitlab-org/security-products/license-management!163
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | config/.bashrc | 15 | ||||
| -rw-r--r-- | lib/license/finder/ext/maven.rb | 6 | ||||
| -rw-r--r-- | lib/license/management/version.rb | 2 | ||||
| -rwxr-xr-x | run.sh | 30 | ||||
| -rw-r--r-- | spec/integration/java/maven_spec.rb | 42 | ||||
| -rw-r--r-- | spec/integration/python/pip_spec.rb | 42 | ||||
| -rw-r--r-- | spec/support/integration_test_helper.rb | 20 |
9 files changed, 142 insertions, 21 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dbd565..6fae87c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # GitLab License management changelog +## v3.11.2 + +- Ensure `LM_*_VERSION` variables take precedence over `ASDF_*_VERSION` variables. (!163) + ## v3.11.1 - Fix dependency scanning in golang projects. (!160) diff --git a/Gemfile.lock b/Gemfile.lock index a3b02e0..9ea4a44 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - license-management (3.11.1) + license-management (3.11.2) license_finder (~> 6.0.0) GEM diff --git a/config/.bashrc b/config/.bashrc index 9c4dd9a..5ab592a 100644 --- a/config/.bashrc +++ b/config/.bashrc @@ -20,16 +20,23 @@ update_java_home() { fi } +function switch_to_exact() { + local tool=$1 + local version=$2 + + asdf shell "$tool" "$version" + if [[ "$tool" = "java" ]]; then + update_java_home + fi +} + function switch_to() { local tool=$1 local major_version=$2 local version version="$(grep "$tool" "$HOME/.tool-versions"| tr ' ' '\n' | grep "^$major_version")" - asdf shell "$tool" "$version" - if [[ "$tool" = "java" ]]; then - update_java_home - fi + switch_to_exact "$tool" "$version" } function major_version_from() { diff --git a/lib/license/finder/ext/maven.rb b/lib/license/finder/ext/maven.rb index d6a08dc..1a3dea8 100644 --- a/lib/license/finder/ext/maven.rb +++ b/lib/license/finder/ext/maven.rb @@ -12,7 +12,9 @@ module LicenseFinder def current_packages Dir.chdir(project_path) do - LicenseFinder::SharedHelpers::Cmd.run(detect_licenses_command) + _stdout, _stderr, status = shell.execute(detect_licenses_command) + return [] unless status.success? + resource_files.flat_map { |file| map_from(file.read) }.uniq end end @@ -27,7 +29,7 @@ module LicenseFinder "-Dlicense.excludedScopes=#{@ignored_groups.to_a.join(',')}", "-Dorg.slf4j.simpleLogger.log.org.codehaus.mojo.license=debug", ENV.fetch('MAVEN_CLI_OPTS', '-DskipTests') - ].join(' ') + ] end def resource_files diff --git a/lib/license/management/version.rb b/lib/license/management/version.rb index bf992e9..5570468 100644 --- a/lib/license/management/version.rb +++ b/lib/license/management/version.rb @@ -2,6 +2,6 @@ module License module Management - VERSION = '3.11.1' + VERSION = '3.11.2' end end @@ -4,6 +4,8 @@ set -e [[ -z ${SETUP_CMD:-} ]] && set -uo pipefail BUNDLE_JOBS="$(nproc)" +export ASDF_JAVA_VERSION="${ASDF_JAVA_VERSION:-}" +export ASDF_PYTHON_VERSION="${ASDF_PYTHON_VERSION:-}" export BUNDLE_JOBS export BUNDLE_WITHOUT="development:test" export CI_API_V4_URL="${CI_API_V4_URL:-https://gitlab.com/api/v4}" @@ -15,6 +17,8 @@ export HISTFILESIZE=0 export HISTSIZE=0 export LANG=C.UTF-8 export LICENSE_FINDER_CLI_OPTS=${LICENSE_FINDER_CLI_OPTS:=--no-debug} +export LM_JAVA_VERSION=${LM_JAVA_VERSION:-} +export LM_PYTHON_VERSION=${LM_PYTHON_VERSION:-} export LM_REPORT_FILE=${LM_REPORT_FILE:-'gl-license-management-report.json'} export MAVEN_CLI_OPTS="${MAVEN_CLI_OPTS:--DskipTests}" export NO_UPDATE_NOTIFIER=true @@ -61,6 +65,26 @@ function prepare_dotnet() { [[ $(ls ./*.sln 2> /dev/null) ]] && RECURSIVE="--recursive" } +function prepare_java() { + if [ -n "$LM_JAVA_VERSION" ]; then + switch_to java "adopt-openjdk-${LM_JAVA_VERSION}" + elif [ -n "$ASDF_JAVA_VERSION" ]; then + switch_to_exact java "$ASDF_JAVA_VERSION" + else + switch_to java "adopt-openjdk-8" + fi +} + +function prepare_python() { + if [ -n "$LM_PYTHON_VERSION" ]; then + switch_to python "$(major_version_from "$LM_PYTHON_VERSION")" + elif [ -n "$ASDF_PYTHON_VERSION" ]; then + switch_to_exact python "$ASDF_PYTHON_VERSION" + else + switch_to python "3" + fi +} + function prepare_tools() { if ! asdf current 2> >(grep -q 'is not installed'); then echo "Installing missing tools…" @@ -69,6 +93,9 @@ function prepare_tools() { } function prepare_project() { + prepare_java + prepare_python + if [[ -z ${SETUP_CMD:-} ]]; then prepare_tools || true prepare_dotnet || true @@ -80,9 +107,6 @@ function prepare_project() { fi } -switch_to python "$(major_version_from "${LM_PYTHON_VERSION:-3}")" -switch_to java "adopt-openjdk-${LM_JAVA_VERSION:-8}" - prepare_project [[ $CI_DEBUG_TRACE == 'true' ]] && debug_env diff --git a/spec/integration/java/maven_spec.rb b/spec/integration/java/maven_spec.rb index 85637b2..9230a9f 100644 --- a/spec/integration/java/maven_spec.rb +++ b/spec/integration/java/maven_spec.rb @@ -100,4 +100,46 @@ RSpec.describe "maven" do specify { expect(report.dependency_names).to match_array(['jackson-core']) } specify { expect(report.licenses_for('jackson-core')).to match_array(['Apache-2.0']) } end + + context "when specifying the version of java using environment variables" do + let(:output_file) { Pathname.new(runner.project_path.join('output.txt')) } + + before do + runner.add_file('custom.sh') do + <<~SCRIPT + #!/bin/bash -l + + java -version &> '#{output_file}' + SCRIPT + end + end + + it 'prioritizes `LM_JAVA_VERSION` over the `ASDF_JAVA_VERSION`' do + runner.scan(env: { + 'ASDF_JAVA_VERSION' => 'adopt-openjdk-11.0.7+10', + 'LM_JAVA_VERSION' => '8', + 'SETUP_CMD' => 'bash custom.sh' + }) + + expect(output_file).to exist + expect(output_file.read).to include('openjdk version "1.8.0_242"') + end + + it 'reads the ASDF_JAVA_VERSION' do + runner.scan(env: { + 'ASDF_JAVA_VERSION' => 'adopt-openjdk-11.0.7+10', + 'SETUP_CMD' => 'bash custom.sh' + }) + + expect(output_file).to exist + expect(output_file.read).to include('openjdk version "11.0.7"') + end + + it 'defaults to java 8' do + runner.scan(env: { 'SETUP_CMD' => 'bash custom.sh' }) + + expect(output_file).to exist + expect(output_file.read).to include('openjdk version "1.8.0_242"') + end + end end diff --git a/spec/integration/python/pip_spec.rb b/spec/integration/python/pip_spec.rb index 0ba3d0e..e808188 100644 --- a/spec/integration/python/pip_spec.rb +++ b/spec/integration/python/pip_spec.rb @@ -157,4 +157,46 @@ setup( expect(report.licenses_for('requests')).to match_array(['Apache-2.0']) end end + + context "when specifying the version of Python using environment variables" do + let(:output_file) { Pathname.new(runner.project_path.join('output.txt')) } + + before do + runner.add_file('custom.sh') do + <<~SCRIPT + #!/bin/bash -l + + python --version &> '#{output_file}' + SCRIPT + end + end + + it 'prioritizes `LM_PYTHON_VERSION` over the `ASDF_PYTHON_VERSION`' do + runner.scan(env: { + 'ASDF_PYTHON_VERSION' => '3.8.2', + 'LM_PYTHON_VERSION' => '2.7.18', + 'SETUP_CMD' => 'bash custom.sh' + }) + + expect(output_file).to exist + expect(output_file.read).to include('2.7.18') + end + + it 'reads the ASDF_PYTHON_VERSION' do + runner.scan(env: { + 'ASDF_PYTHON_VERSION' => '3.8.2', + 'SETUP_CMD' => 'bash custom.sh' + }) + + expect(output_file).to exist + expect(output_file.read).to include('3.8.2') + end + + it 'defaults to Python 3' do + runner.scan(env: { 'SETUP_CMD' => 'bash custom.sh' }) + + expect(output_file).to exist + expect(output_file.read).to include('3.8.2') + end + end end diff --git a/spec/support/integration_test_helper.rb b/spec/support/integration_test_helper.rb index aa3f0c0..f474265 100644 --- a/spec/support/integration_test_helper.rb +++ b/spec/support/integration_test_helper.rb @@ -40,13 +40,13 @@ module IntegrationTestHelper class IntegrationTestRunner attr_reader :project_path - def initialize(project_path = File.join(Dir.pwd, 'tmp', SecureRandom.uuid)) + def initialize(project_path = Pathname.pwd.join('tmp').join(SecureRandom.uuid)) FileUtils.mkdir_p(project_path) - @project_path = project_path + @project_path = Pathname(project_path) end def add_file(name, content = nil) - full_path = Pathname.new(File.join(project_path, name)) + full_path = project_path.join(name) FileUtils.mkdir_p(full_path.dirname) IO.write(full_path, block_given? ? yield : content) end @@ -57,22 +57,22 @@ module IntegrationTestHelper def clone(repo, branch: 'master') if branch.match?(/\b[0-9a-f]{5,40}\b/) - execute({}, 'git', 'clone', '--quiet', repo, project_path) + execute({}, 'git', 'clone', '--quiet', repo, project_path.to_s) Dir.chdir project_path do execute({}, 'git', 'checkout', branch) end else - execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, project_path) + execute({}, 'git', 'clone', '--quiet', '--depth=1', '--single-branch', '--branch', branch, repo, project_path.to_s) end end def scan(env: {}) - return {} unless execute(env, './bin/docker-test', project_path) + return {} unless execute(env, './bin/docker-test', project_path.to_s) - report_path = "#{project_path}/gl-license-management-report.json" - return {} unless File.exist?(report_path) + report_path = project_path.join('gl-license-management-report.json') + return {} unless report_path.exist? - Report.new(IO.read(report_path)) + Report.new(report_path.read) end def execute(env = {}, *args) @@ -82,7 +82,7 @@ module IntegrationTestHelper end def cleanup - FileUtils.rm_rf(project_path) if Dir.exist?(project_path) + FileUtils.rm_rf(project_path) if project_path.exist? end end |
