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

feat: provide config extends feature #2222

Closed
Closed
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: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@octokit/request": "^6.0.0",
"@octokit/request-error": "^3.0.0",
"@octokit/rest": "^19.0.0",
"@topoconfig/extends": "^0.12.1",
"@types/npm-package-arg": "^6.1.0",
"@xmldom/xmldom": "^0.8.4",
"chalk": "^4.0.0",
Expand Down
5 changes: 5 additions & 0 deletions schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@
"type": "string",
"format": "uri-reference"
},
"extends": {
"description": "Path to the config preset to base from",
"type": "string"
},
"packages": {
"description": "Per-path component configuration.",
"type": "object",
Expand Down Expand Up @@ -407,6 +411,7 @@
],
"properties": {
"$schema": true,
"extends": true,
"packages": true,
"bootstrap-sha": true,
"last-release-sha": true,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/release-please.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ const bootstrapCommand: yargs.CommandModule<{}, BootstrapArgs> = {
versionFile: argv.versionFile,
};
if (argv.dryRun) {
const pullRequest = await await bootstrapper.buildPullRequest(
const pullRequest = await bootstrapper.buildPullRequest(
path,
releaserConfig
);
Expand Down
4 changes: 3 additions & 1 deletion src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
} from './util/pull-request-overflow-handler';
import {signoffCommitMessage} from './util/signoff-commit-message';
import {CommitExclude} from './util/commit-exclude';
import {applyExtends} from './util/apply-extends';

type ExtraJsonFile = {
type: 'json';
Expand Down Expand Up @@ -1372,7 +1373,8 @@ async function parseConfig(
onlyPath?: string,
releaseAs?: string
): Promise<{config: RepositoryConfig; options: ManifestOptions}> {
const config = await fetchManifestConfig(github, configFile, branch);
const rawConfig = await fetchManifestConfig(github, configFile, branch);
const config = await applyExtends(rawConfig, github, branch);
const defaultConfig = extractReleaserConfig(config);
const repositoryConfig: RepositoryConfig = {};
for (const path in config.packages) {
Expand Down
1 change: 1 addition & 0 deletions src/updaters/release-please-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const SCHEMA_URL =

interface ManifestConfigFile extends ManifestConfig {
$schema?: string;
extends?: string;
}

export class ReleasePleaseConfig implements Updater {
Expand Down
99 changes: 99 additions & 0 deletions src/util/apply-extends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {populate} from '@topoconfig/extends';
import {type ManifestConfig, DEFAULT_RELEASE_PLEASE_CONFIG} from '../manifest';
import type {GitHub} from '../github';
import {http} from './http-api';
import * as path from 'path';

type GhResource = {
repo: string;
owner: string;
branch: string;
file: string;
};

export const applyExtends = async (
config: ManifestConfig,
github: GitHub,
branch: string
): Promise<ManifestConfig> => {
return populate(config, {
resolve({id}) {
return resolveRef(id, branch);
},
async load({resolved, cwd}) {
if (cwd.startsWith('https:')) {
return http.getJson<ManifestConfig>(`${cwd}/${resolved}`);
}
return github.getFileJson<ManifestConfig>(resolved, branch);
},
parse({contents}) {
return contents;
},
});
};

export const resolveRef = (id: string, branch: string): string => {
if (id.startsWith('./')) {
return id.slice(2);
}

const ref = parseGithubRef(id, branch);
if (!ref) {
throw new Error(`config: unsupported 'extends' argument: ${id}`);
}

const {owner, repo, branch: _branch, file} = ref;
return `https://raw.githubusercontent.com/${owner}/${repo}/${_branch}/${file}`;
};

// https://github.com/googleapis/release-please/pull/2222#discussion_r1486658546
// owner/repo/path/to/file.json@branchOrSha
// owner/repo:path/to/file.json@branchOrSha
const githubRefPattern =
/^([\w-]+)\/(\.?[\w-]+)(?:[/:]([\w./-]+))?(?:@([\w.-]+))?$/i;

// https://docs.renovatebot.com/config-presets/#github
const renovateLikePattern =
/^github>([\w-]+)\/(\.?[\w-]+)(?::([\w./-]+))?(?:#([\w.-]+))?$/i;

export const parseGithubRef = (
input: string,
branch: string
): GhResource | undefined => {
const [
_,
owner,
repo,
_file = DEFAULT_RELEASE_PLEASE_CONFIG,
_branch = branch,
] = githubRefPattern.exec(input) || renovateLikePattern.exec(input) || [];

if (!_) {
return;
}

const file = _file.endsWith('/')
? _file + DEFAULT_RELEASE_PLEASE_CONFIG
: _file + (path.extname(_file) ? '' : '.json');

return {
owner,
repo,
file,
branch: _branch,
};
};
20 changes: 20 additions & 0 deletions src/util/http-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// We need this wrapper just to apply mocks in tests.
export const http = {
async getJson<T = any>(url: string): Promise<T> {
return (await fetch(url)).json();
},
};
12 changes: 12 additions & 0 deletions test/fixtures/manifest/config/config-remote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
"release-type": "node",
"prerelease": true,
"include-component-in-tag": false,
"packages": {
"packages/bot-config-utils": {
},
"packages/cron-utils": {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "https://example.com",
"packages": {
"packages/bot-config-utils": {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./config.json",
"packages": {
"packages/bot-config-utils": {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "owner/repo@main",
"packages": {
"packages/bot-config-utils": {
}
}
}
111 changes: 111 additions & 0 deletions test/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import * as pluginFactory from '../src/factories/plugin-factory';
import {SentenceCase} from '../src/plugins/sentence-case';
import {NodeWorkspace} from '../src/plugins/node-workspace';
import {CargoWorkspace} from '../src/plugins/cargo-workspace';
import {http} from '../src/util/http-api';
import {PullRequestTitle} from '../src/util/pull-request-title';
import {PullRequestBody} from '../src/util/pull-request-body';
import {RawContent} from '../src/updaters/raw-content';
Expand Down Expand Up @@ -190,6 +191,116 @@ describe('Manifest', () => {
expect(Object.keys(manifest.repositoryConfig)).lengthOf(8);
expect(Object.keys(manifest.releasedVersions)).lengthOf(8);
});

describe('should handle `extends`', () => {
it('processes local (same repo) references', async () => {
const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('config.json', 'main')
.resolves(
buildGitHubFileContent(fixturesPath, 'manifest/config/config.json')
)
.withArgs('release-please-config.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/config/config-with-local-extends.json'
)
)
.withArgs('.release-please-manifest.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/versions/versions.json'
)
);
const manifest = await Manifest.fromManifest(
github,
github.repository.defaultBranch
);
expect(
manifest.repositoryConfig['packages/bot-config-utils'].prerelease
).to.be.undefined;
expect(Object.keys(manifest.repositoryConfig)).lengthOf(1);
});
it('processes remote github-hosted references', async () => {
const getJsonStub = sandbox.stub(http, 'getJson');
getJsonStub
.withArgs(
'https://raw.githubusercontent.com/owner/repo/main/release-please-config.json'
)
.resolves(
JSON.parse(
readFileSync(
resolve(fixturesPath, 'manifest/config/config-remote.json'),
'utf8'
)
)
);

const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('release-please-config.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/config/config-with-remote-extends.json'
)
)
.withArgs('.release-please-manifest.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/versions/versions.json'
)
);
const manifest = await Manifest.fromManifest(
github,
github.repository.defaultBranch
);
expect(
manifest.repositoryConfig['packages/bot-config-utils'].prerelease
).to.be.true;
expect(Object.keys(manifest.repositoryConfig)).lengthOf(1);
});
it('throws error otherwise', async () => {
const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('release-please-config.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/config/config-with-broken-extends.json'
)
)
.withArgs('.release-please-manifest.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/versions/versions.json'
)
);

try {
await Manifest.fromManifest(github, github.repository.defaultBranch);
expect(true).to.be.false;
} catch (e: any) {
expect(e.message).to.eql(
"config: unsupported 'extends' argument: https://example.com"
);
}
});
});

it('should limit manifest loading to the given path', async () => {
const getFileContentsStub = sandbox.stub(
github,
Expand Down
1 change: 1 addition & 0 deletions test/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('schemas', () => {
it(`validates ${manifestFile}`, () => {
const config = require(resolve(fixturesPath, 'config', manifestFile));
const result = configValidator(config);
console.log('!!!error', result, 'errors=', configValidator.errors);
expect(result).to.be.true;
expect(configValidator.errors).to.be.null;
});
Expand Down
Loading