Skip to content

Commit

Permalink
feat(bazel-module): add support for oci.pull (#32453)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <[email protected]>
  • Loading branch information
gzm0 and rarkins authored Dec 5, 2024
1 parent 19033d5 commit 22d356b
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 1 deletion.
55 changes: 55 additions & 0 deletions lib/modules/manager/bazel-module/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Fixtures } from '../../../../test/fixtures';
import { GlobalConfig } from '../../../config/global';
import type { RepoGlobalConfig } from '../../../config/types';
import { BazelDatasource } from '../../datasource/bazel';
import { DockerDatasource } from '../../datasource/docker';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { MavenDatasource } from '../../datasource/maven';
import * as parser from './parser';
Expand Down Expand Up @@ -290,6 +291,60 @@ describe('modules/manager/bazel-module/extract', () => {
]);
});

it('returns oci.pull dependencies', async () => {
const input = codeBlock`
oci.pull(
name = "nginx_image",
digest = "sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720",
image = "index.docker.io/library/nginx",
platforms = ["linux/amd64"],
tag = "1.27.1",
)
`;

const result = await extractPackageFile(input, 'MODULE.bazel');
if (!result) {
throw new Error('Expected a result.');
}
expect(result.deps).toEqual([
{
datasource: DockerDatasource.id,
depType: 'oci_pull',
depName: 'nginx_image',
packageName: 'index.docker.io/library/nginx',
currentValue: '1.27.1',
currentDigest:
'sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720',
},
]);
});

it('returns oci.pull dependencies without tags', async () => {
const input = codeBlock`
oci.pull(
name = "nginx_image",
digest = "sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720",
image = "index.docker.io/library/nginx",
platforms = ["linux/amd64"],
)
`;

const result = await extractPackageFile(input, 'MODULE.bazel');
if (!result) {
throw new Error('Expected a result.');
}
expect(result.deps).toEqual([
{
datasource: DockerDatasource.id,
depType: 'oci_pull',
depName: 'nginx_image',
packageName: 'index.docker.io/library/nginx',
currentDigest:
'sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720',
},
]);
});

it('returns maven.install and bazel_dep dependencies together', async () => {
const input = codeBlock`
bazel_dep(name = "bazel_jar_jar", version = "0.1.0")
Expand Down
6 changes: 6 additions & 0 deletions lib/modules/manager/bazel-module/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as bazelrc from './bazelrc';
import type { RecordFragment } from './fragments';
import { parse } from './parser';
import { RuleToMavenPackageDep, fillRegistryUrls } from './parser/maven';
import { RuleToDockerPackageDep } from './parser/oci';
import { RuleToBazelModulePackageDep } from './rules';
import * as rules from './rules';

Expand All @@ -18,11 +19,16 @@ export async function extractPackageFile(
const records = parse(content);
const pfc = await extractBazelPfc(records, packageFile);
const mavenDeps = extractMavenDeps(records);
const dockerDeps = LooseArray(RuleToDockerPackageDep).parse(records);

if (mavenDeps.length) {
pfc.deps.push(...mavenDeps);
}

if (dockerDeps.length) {
pfc.deps.push(...dockerDeps);
}

return pfc.deps.length ? pfc : null;
} catch (err) {
logger.debug({ err, packageFile }, 'Failed to parse bazel module file.');
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/manager/bazel-module/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Category } from '../../../constants';
import { BazelDatasource } from '../../datasource/bazel';
import { DockerDatasource } from '../../datasource/docker';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { MavenDatasource } from '../../datasource/maven';
import { extractPackageFile } from './extract';
Expand All @@ -15,6 +16,7 @@ export const defaultConfig = {

export const supportedDatasources = [
BazelDatasource.id,
DockerDatasource.id,
GithubTagsDatasource.id,
MavenDatasource.id,
];
29 changes: 29 additions & 0 deletions lib/modules/manager/bazel-module/parser/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,34 @@ describe('modules/manager/bazel-module/parser/index', () => {
),
]);
});

it('finds oci.pull', () => {
const input = codeBlock`
oci.pull(
name = "nginx_image",
digest = "sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720",
image = "index.docker.io/library/nginx",
platforms = ["linux/amd64"],
tag = "1.27.1",
)
`;

const res = parse(input);
expect(res).toEqual([
fragments.record(
{
rule: fragments.string('oci_pull'),
name: fragments.string('nginx_image'),
digest: fragments.string(
'sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720',
),
image: fragments.string('index.docker.io/library/nginx'),
platforms: fragments.array([fragments.string('linux/amd64')], true),
tag: fragments.string('1.27.1'),
},
true,
),
]);
});
});
});
3 changes: 2 additions & 1 deletion lib/modules/manager/bazel-module/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Ctx } from '../context';
import type { RecordFragment } from '../fragments';
import { mavenRules } from './maven';
import { moduleRules } from './module';
import { ociRules } from './oci';

const rule = q.alt<Ctx>(moduleRules, mavenRules);
const rule = q.alt<Ctx>(moduleRules, mavenRules, ociRules);

const query = q.tree<Ctx>({
type: 'root-tree',
Expand Down
41 changes: 41 additions & 0 deletions lib/modules/manager/bazel-module/parser/oci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { query as q } from 'good-enough-parser';
import { z } from 'zod';
import { DockerDatasource } from '../../../datasource/docker';
import type { PackageDependency } from '../../types';
import type { Ctx } from '../context';
import { RecordFragmentSchema, StringFragmentSchema } from '../fragments';
import { kvParams } from './common';

export const RuleToDockerPackageDep = RecordFragmentSchema.extend({
children: z.object({
rule: StringFragmentSchema.extend({
value: z.literal('oci_pull'),
}),
name: StringFragmentSchema,
image: StringFragmentSchema,
tag: StringFragmentSchema.optional(),
digest: StringFragmentSchema.optional(),
}),
}).transform(
({ children: { rule, name, image, tag, digest } }): PackageDependency => ({
datasource: DockerDatasource.id,
depType: rule.value,
depName: name.value,
packageName: image.value,
currentValue: tag?.value,
currentDigest: digest?.value,
}),
);

export const ociRules = q
.sym<Ctx>('oci')
.op('.')
.sym('pull', (ctx, token) => ctx.startRule('oci_pull'))
.join(
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
search: kvParams,
postHandler: (ctx) => ctx.endRule(),
}),
);
20 changes: 20 additions & 0 deletions lib/modules/manager/bazel-module/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
The `bazel-module` manager can update [Bazel module (bzlmod)](https://bazel.build/external/module) enabled workspaces.

### Maven

It also takes care about maven artifacts initalized with [bzlmod](https://github.com/bazelbuild/rules_jvm_external/blob/master/docs/bzlmod.md). For simplicity the name of extension variable is limited to `maven*`. E.g.:

```
Expand All @@ -26,3 +28,21 @@ maven.artifact(
version = "1.11.1",
)
```

### Docker

Similarly, it updates Docker / OCI images pulled with [oci_pull](https://github.com/bazel-contrib/rules_oci/blob/main/docs/pull.md).

Note that the extension must be called `oci`:

```
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
name = "nginx_image",
digest = "sha256:287ff321f9e3cde74b600cc26197424404157a72043226cbbf07ee8304a2c720",
image = "index.docker.io/library/nginx",
platforms = ["linux/amd64"],
tag = "1.27.1",
)
```

0 comments on commit 22d356b

Please sign in to comment.