Skip to content

Commit

Permalink
feat: adds metadata versioning for terraform modules (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-awmalik authored Aug 18, 2023
1 parent b8da322 commit 49b33c7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
11 changes: 11 additions & 0 deletions __snapshots__/metadata.js
Original file line number Diff line number Diff line change
@@ -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
`
18 changes: 18 additions & 0 deletions src/strategies/terraform-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
37 changes: 37 additions & 0 deletions src/updaters/terraform/metadata-version.ts
Original file line number Diff line number Diff line change
@@ -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}`
);
}
}
6 changes: 6 additions & 0 deletions test/strategies/terraform-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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);
});
});
});
8 changes: 8 additions & 0 deletions test/updaters/fixtures/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: blueprints.cloud.google.com/v1alpha1
kind: BlueprintMetadata
metadata:
name: foo
spec:
info:
title: bar
version: 2.0.0
38 changes: 38 additions & 0 deletions test/updaters/metadata.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 49b33c7

Please sign in to comment.