feat(manager/cargo): support private registry index env var (#20379)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
This commit is contained in:
Tom Fay 2023-04-18 09:49:24 +01:00 committed by GitHub
parent 7d6438b16f
commit 2a72f85cc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 2 deletions

View file

@ -11,7 +11,7 @@ Renovate supports upgrading dependencies in `Cargo.toml` files and their accompa
1. Renovate searches in each repository for any `Cargo.toml` files
1. Renovate extracts existing dependencies from `[dependencies]`, `[dev-dependencies]`, `[build-dependencies]` and `[workspace.dependencies]`
1. Renovate tries to find and parse a `.cargo/config.toml` file to discover index URLs for private registries
1. Renovate looks up Cargo configuration to find index URLs for private registries
1. Renovate resolves the dependency's version using the crates.io API or by cloning the index URL
1. If Renovate finds an update, Renovate will use `cargo update` to update both `Cargo.toml` and `Cargo.lock`
@ -19,6 +19,16 @@ Renovate supports upgrading dependencies in `Cargo.toml` files and their accompa
Renovate updates Rust crates by default.
## Cargo configuration and private registry discovery
Renovate can find private registry URLs in these Cargo configuration files:
- `.cargo/config.toml`
- `.cargo/config` (legacy)
Renovate can also find private registry URLs via a `CARGO_REGISTRIES_<name>_INDEX` environment variable.
Read the [Rust environment variables docs](https://doc.rust-lang.org/cargo/reference/environment-variables.html#configuration-environment-variables) to learn more.
## Private crate registries and private Git dependencies
If any dependencies are hosted in private Git repositories, [Git Authentication for cargo](https://doc.rust-lang.org/cargo/appendix/git-authentication.html) must be set up.

View file

@ -31,6 +31,8 @@ describe('modules/manager/cargo/extract', () => {
};
GlobalConfig.set(adminConfig);
delete process.env.CARGO_REGISTRIES_PRIVATE_CRATES_INDEX;
delete process.env.CARGO_REGISTRIES_MCORBIN_INDEX;
});
afterEach(async () => {
@ -113,6 +115,50 @@ describe('modules/manager/cargo/extract', () => {
expect(res?.deps).toHaveLength(3);
});
it('extracts registry urls from environment', async () => {
process.env.CARGO_REGISTRIES_PRIVATE_CRATES_INDEX =
'https://dl.cloudsmith.io/basic/my-org/my-repo/cargo/index.git';
process.env.CARGO_REGISTRIES_MCORBIN_INDEX =
'https://github.com/mcorbin/testregistry';
const res = await extractPackageFile(cargo6toml, 'Cargo.toml', {
...config,
});
expect(res?.deps).toEqual([
{
currentValue: '0.1.0',
datasource: 'crate',
depName: 'proprietary-crate',
depType: 'dependencies',
managerData: {
nestedVersion: true,
},
registryUrls: [
'https://dl.cloudsmith.io/basic/my-org/my-repo/cargo/index.git',
],
},
{
currentValue: '3.0.0',
datasource: 'crate',
depName: 'mcorbin-test',
depType: 'dependencies',
managerData: {
nestedVersion: true,
},
registryUrls: ['https://github.com/mcorbin/testregistry'],
},
{
currentValue: '0.2',
datasource: 'crate',
depName: 'tokio',
depType: 'dependencies',
managerData: {
nestedVersion: false,
},
},
]);
});
it('extracts workspace dependencies', async () => {
const cargoToml = codeBlock`
[package]

View file

@ -15,6 +15,11 @@ import type {
CargoSection,
} from './types';
function getCargoIndexEnv(registryName: string): string | null {
const registry = registryName.toUpperCase().replaceAll('-', '_');
return process.env[`CARGO_REGISTRIES_${registry}_INDEX`] ?? null;
}
function extractFromSection(
parsedContent: CargoSection,
section: keyof CargoSection,
@ -47,7 +52,9 @@ function extractFromSection(
currentValue = version;
nestedVersion = true;
if (registryName) {
const registryUrl = cargoRegistries[registryName];
const registryUrl =
cargoRegistries[registryName] ?? getCargoIndexEnv(registryName);
if (registryUrl) {
registryUrls = [registryUrl];
} else {