From 46072764647bfb23658a6c2245253f05be661474 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 12 Apr 2019 14:27:49 +0300 Subject: [PATCH] feat(bazel): support "container_pull" dependency-type (#3514) --- lib/manager/bazel/extract.js | 35 ++++++++++++++++++- lib/manager/bazel/update.js | 5 +++ .../bazel/__snapshots__/extract.spec.js.snap | 21 +++++++++++ .../bazel/__snapshots__/update.spec.js.snap | 12 +++++++ test/manager/bazel/_fixtures/container_pull | 8 +++++ test/manager/bazel/extract.spec.js | 17 +++++++++ test/manager/bazel/update.spec.js | 34 ++++++++++++++++++ 7 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 test/manager/bazel/_fixtures/container_pull diff --git a/lib/manager/bazel/extract.js b/lib/manager/bazel/extract.js index d42825dc25..0a559937f3 100644 --- a/lib/manager/bazel/extract.js +++ b/lib/manager/bazel/extract.js @@ -63,7 +63,12 @@ function findBalancedParenIndex(longString) { } function parseContent(content) { - return ['http_archive', 'go_repository', 'git_repository'].reduce( + return [ + 'container_pull', + 'http_archive', + 'go_repository', + 'git_repository', + ].reduce( (acc, prefix) => [ ...acc, ...content @@ -99,10 +104,25 @@ function extractPackageFile(content) { let commit; let url; let sha256; + let digest; + let repository; + let registry; let match = def.match(/name\s*=\s*"([^"]+)"/); if (match) { [, depName] = match; } + match = def.match(/digest\s*=\s*"([^"]+)"/); + if (match) { + [, digest] = match; + } + match = def.match(/registry\s*=\s*"([^"]+)"/); + if (match) { + [, registry] = match; + } + match = def.match(/repository\s*=\s*"([^"]+)"/); + if (match) { + [, repository] = match; + } match = def.match(/remote\s*=\s*"([^"]+)"/); if (match) { [, remote] = match; @@ -196,6 +216,19 @@ function extractPackageFile(content) { dep.lookupName = dep.repo; dep.lookupType = 'releases'; deps.push(dep); + } else if ( + depType === 'container_pull' && + currentValue && + digest && + repository && + registry + ) { + dep.currentDigest = digest; + dep.currentValue = currentValue; + dep.depName = depName; + dep.datasource = 'docker'; + dep.lookupName = repository; + deps.push(dep); } else { logger.info( { def }, diff --git a/lib/manager/bazel/update.js b/lib/manager/bazel/update.js index 75341dd1f0..04977c9168 100644 --- a/lib/manager/bazel/update.js +++ b/lib/manager/bazel/update.js @@ -9,6 +9,11 @@ async function updateDependency(fileContent, upgrade) { try { logger.debug(`bazel.updateDependency(): ${upgrade.newValue}`); let newDef; + if (upgrade.depType === 'container_pull') { + newDef = upgrade.def + .replace(/(tag\s*=\s*)"[^"]+"/, `$1"${upgrade.newValue}"`) + .replace(/(digest\s*=\s*)"[^"]+"/, `$1"${upgrade.newDigest}"`); + } if ( upgrade.depType === 'git_repository' || upgrade.depType === 'go_repository' diff --git a/test/manager/bazel/__snapshots__/extract.spec.js.snap b/test/manager/bazel/__snapshots__/extract.spec.js.snap index 03afd9fd98..9464bb05eb 100644 --- a/test/manager/bazel/__snapshots__/extract.spec.js.snap +++ b/test/manager/bazel/__snapshots__/extract.spec.js.snap @@ -1,5 +1,26 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`lib/manager/bazel/extract extractPackageFile() extracts dependencies for container_pull deptype 1`] = ` +Array [ + Object { + "currentDigest": "sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548", + "currentValue": "v1.0.0-alpha31.cli-migrations", + "datasource": "docker", + "def": "container_pull( + name=\\"hasura\\", + registry=\\"index.docker.io\\", + repository=\\"hasura/graphql-engine\\", + # v1.0.0-alpha31.cli-migrations 11/28 + digest=\\"sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548\\", + tag=\\"v1.0.0-alpha31.cli-migrations\\" + )", + "depName": "hasura", + "depType": "container_pull", + "lookupName": "hasura/graphql-engine", + }, +] +`; + exports[`lib/manager/bazel/extract extractPackageFile() extracts dependencies from *.bzl files 1`] = ` Array [ Object { diff --git a/test/manager/bazel/__snapshots__/update.spec.js.snap b/test/manager/bazel/__snapshots__/update.spec.js.snap index f0f420c6cb..63f50b9489 100644 --- a/test/manager/bazel/__snapshots__/update.spec.js.snap +++ b/test/manager/bazel/__snapshots__/update.spec.js.snap @@ -115,3 +115,15 @@ go_rules_dependencies() go_register_toolchains() " `; + +exports[`manager/bazel/update updateDependency updates container_pull deptype and prserves comment 1`] = ` +"container_pull( + name=\\"hasura\\", + registry=\\"index.docker.io\\", + repository=\\"hasura/graphql-engine\\", + # v1.0.0-alpha31.cli-migrations 11/28 + digest=\\"sha256:2c29ba015faef92a3f55b37632fc373a7fbc2c9fddd31e317bf07113391c640b\\", + tag=\\"v1.0.0-alpha42.cli-migrations\\" + ) +" +`; diff --git a/test/manager/bazel/_fixtures/container_pull b/test/manager/bazel/_fixtures/container_pull new file mode 100644 index 0000000000..3340d8b79f --- /dev/null +++ b/test/manager/bazel/_fixtures/container_pull @@ -0,0 +1,8 @@ +container_pull( + name="hasura", + registry="index.docker.io", + repository="hasura/graphql-engine", + # v1.0.0-alpha31.cli-migrations 11/28 + digest="sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548", + tag="v1.0.0-alpha31.cli-migrations" +) diff --git a/test/manager/bazel/extract.spec.js b/test/manager/bazel/extract.spec.js index a6bcb7da24..9cc45b860e 100644 --- a/test/manager/bazel/extract.spec.js +++ b/test/manager/bazel/extract.spec.js @@ -33,6 +33,23 @@ describe('lib/manager/bazel/extract', () => { const res = extractPackageFile(fileWithBzlExtension, config); expect(res.deps).toMatchSnapshot(); }); + + it('extracts dependencies for container_pull deptype', () => { + const res = extractPackageFile( + ` + container_pull( + name="hasura", + registry="index.docker.io", + repository="hasura/graphql-engine", + # v1.0.0-alpha31.cli-migrations 11/28 + digest="sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548", + tag="v1.0.0-alpha31.cli-migrations" + )`, + config + ); + expect(res.deps).toMatchSnapshot(); + }); + it('check remote option in go_repository', () => { const successStory = extractPackageFile( ` diff --git a/test/manager/bazel/update.spec.js b/test/manager/bazel/update.spec.js index 3cf5c50d80..076444d10f 100644 --- a/test/manager/bazel/update.spec.js +++ b/test/manager/bazel/update.spec.js @@ -10,6 +10,11 @@ const content = fs.readFileSync( 'utf8' ); +const contentContainerPull = fs.readFileSync( + path.resolve('test/manager/bazel/_fixtures/container_pull'), + 'utf8' +); + const fileWithBzlExtension = fs.readFileSync( 'test/manager/bazel/_fixtures/repositories.bzl', 'utf8' @@ -36,6 +41,35 @@ describe('manager/bazel/update', () => { const res = await bazelfile.updateDependency(content, upgrade); expect(res).not.toEqual(content); }); + + it('updates container_pull deptype and prserves comment', async () => { + const upgrade = { + depName: 'hasura', + depType: 'container_pull', + def: `container_pull( + name="hasura", + registry="index.docker.io", + repository="hasura/graphql-engine", + # v1.0.0-alpha31.cli-migrations 11/28 + digest="sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548", + tag="v1.0.0-alpha31.cli-migrations" + )`, + currentValue: 'v1.0.0-alpha31.cli-migrations', + currentDigest: + 'sha256:a4e8d8c444ca04fe706649e82263c9f4c2a4229bc30d2a64561b5e1d20cc8548', + newDigest: + 'sha256:2c29ba015faef92a3f55b37632fc373a7fbc2c9fddd31e317bf07113391c640b', + newValue: 'v1.0.0-alpha42.cli-migrations', + }; + const res = await bazelfile.updateDependency( + contentContainerPull, + upgrade + ); + expect(res).toMatchSnapshot(); + expect(res).not.toEqual(contentContainerPull); + expect(res.includes('# v1.0.0-alpha31.cli-migrations 11/28')).toBe(true); + }); + it('updates commit to tag', async () => { const upgrade = { depName: 'com_github_google_uuid',