From 49b33c73b2571699ad8362f8b6b934154a6ce375 Mon Sep 17 00:00:00 2001 From: Awais Malik Date: Fri, 18 Aug 2023 08:57:13 -0700 Subject: [PATCH] feat: adds metadata versioning for terraform modules (#2041) --- __snapshots__/metadata.js | 11 +++++++ src/strategies/terraform-module.ts | 18 ++++++++++ src/updaters/terraform/metadata-version.ts | 37 +++++++++++++++++++++ test/strategies/terraform-module.ts | 6 ++++ test/updaters/fixtures/metadata.yaml | 8 +++++ test/updaters/metadata.ts | 38 ++++++++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 __snapshots__/metadata.js create mode 100644 src/updaters/terraform/metadata-version.ts create mode 100644 test/updaters/fixtures/metadata.yaml create mode 100644 test/updaters/metadata.ts diff --git a/__snapshots__/metadata.js b/__snapshots__/metadata.js new file mode 100644 index 000000000..96a6e215a --- /dev/null +++ b/__snapshots__/metadata.js @@ -0,0 +1,11 @@ +exports['metadata.yaml updateContent updates version in metadata.yaml 1'] = ` +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintMetadata +metadata: + name: foo +spec: + info: + title: bar + version: 2.1.0 + +` diff --git a/src/strategies/terraform-module.ts b/src/strategies/terraform-module.ts index e6d5be4b3..71287fcd2 100644 --- a/src/strategies/terraform-module.ts +++ b/src/strategies/terraform-module.ts @@ -17,6 +17,7 @@ import {Changelog} from '../updaters/changelog'; // Terraform specific. import {ReadMe} from '../updaters/terraform/readme'; import {ModuleVersion} from '../updaters/terraform/module-version'; +import {MetadataVersion} from '../updaters/terraform/metadata-version'; import {BaseStrategy, BuildUpdatesOptions} from './base'; import {Update} from '../update'; import {Version} from '../version'; @@ -88,6 +89,23 @@ export class TerraformModule extends BaseStrategy { }), }); }); + + // Update metadata.yaml to current candidate version. + const metadataFiles = await this.github.findFilesByFilenameAndRef( + 'metadata.yaml', + this.targetBranch, + this.path + ); + + metadataFiles.forEach(path => { + updates.push({ + path: this.addPath(path), + createIfMissing: false, + updater: new MetadataVersion({ + version, + }), + }); + }); return updates; } diff --git a/src/updaters/terraform/metadata-version.ts b/src/updaters/terraform/metadata-version.ts new file mode 100644 index 000000000..b75740bab --- /dev/null +++ b/src/updaters/terraform/metadata-version.ts @@ -0,0 +1,37 @@ +// Copyright 2023 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 {logger as defaultLogger, Logger} from '../../util/logger'; +import {DefaultUpdater} from '../default'; + +/** + * Updates a Terraform metadata.yaml or metadata.display.yaml file(s). + */ +export class MetadataVersion extends DefaultUpdater { + /** + * Given initial file contents, return updated contents. + * @param {string} content The initial content + * @returns {string} The updated content + */ + updateContent(content: string, logger: Logger = defaultLogger): string { + const oldVersion = content.match(/version: [0-9]+\.[0-9]+\.[0-9]+(-\w+)?/); + if (oldVersion) { + logger.info(`updating from ${oldVersion} to v${this.version}`); + } + return content.replace( + /version: [0-9]+\.[0-9]+\.[0-9]+(-\w+)?/g, + `version: ${this.version}` + ); + } +} diff --git a/test/strategies/terraform-module.ts b/test/strategies/terraform-module.ts index f2b095296..e120d4bb9 100644 --- a/test/strategies/terraform-module.ts +++ b/test/strategies/terraform-module.ts @@ -23,6 +23,7 @@ import {Version} from '../../src/version'; import {Changelog} from '../../src/updaters/changelog'; import {ReadMe} from '../../src/updaters/terraform/readme'; import {ModuleVersion} from '../../src/updaters/terraform/module-version'; +import {MetadataVersion} from '../../src/updaters/terraform/metadata-version'; const sandbox = sinon.createSandbox(); @@ -120,6 +121,9 @@ describe('TerraformModule', () => { findFilesStub .withArgs('versions.tf.tmpl', 'main', '.') .resolves(['path1/versions.tf.tmpl', 'path2/versions.tf.tmpl']); + findFilesStub + .withArgs('metadata.yaml', 'main', '.') + .resolves(['path1/metadata.yaml', 'path2/metadata.yaml']); const latestRelease = undefined; const release = await strategy.buildReleasePullRequest( COMMITS, @@ -134,6 +138,8 @@ describe('TerraformModule', () => { assertHasUpdate(updates, 'path2/versions.tf', ModuleVersion); assertHasUpdate(updates, 'path1/versions.tf.tmpl', ModuleVersion); assertHasUpdate(updates, 'path2/versions.tf.tmpl', ModuleVersion); + assertHasUpdate(updates, 'path1/metadata.yaml', MetadataVersion); + assertHasUpdate(updates, 'path2/metadata.yaml', MetadataVersion); }); }); }); diff --git a/test/updaters/fixtures/metadata.yaml b/test/updaters/fixtures/metadata.yaml new file mode 100644 index 000000000..dccd8a5a3 --- /dev/null +++ b/test/updaters/fixtures/metadata.yaml @@ -0,0 +1,8 @@ +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintMetadata +metadata: + name: foo +spec: + info: + title: bar + version: 2.0.0 diff --git a/test/updaters/metadata.ts b/test/updaters/metadata.ts new file mode 100644 index 000000000..f462844d8 --- /dev/null +++ b/test/updaters/metadata.ts @@ -0,0 +1,38 @@ +// Copyright 2023 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 {readFileSync} from 'fs'; +import {resolve} from 'path'; +import * as snapshot from 'snap-shot-it'; +import {describe, it} from 'mocha'; +import {MetadataVersion} from '../../src/updaters/terraform/metadata-version'; +import {Version} from '../../src/version'; + +const fixturesPath = './test/updaters/fixtures'; + +describe('metadata.yaml', () => { + describe('updateContent', () => { + it('updates version in metadata.yaml', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './metadata.yaml'), + 'utf8' + ).replace(/\r\n/g, '\n'); + const version = new MetadataVersion({ + version: Version.parse('2.1.0'), + }); + const newContent = version.updateContent(oldContent); + snapshot(newContent); + }); + }); +});