Skip to content

Commit 93c0aba

Browse files
committed
Expose gitSource programmatically in the App class
1 parent 88fbb1d commit 93c0aba

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

packages/aws-cdk-lib/core/lib/app.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Construct, IConstruct } from 'constructs';
22
import * as fs from 'fs-extra';
3+
import { getGitSource } from './private/git-source';
34
import { PRIVATE_CONTEXT_DEFAULT_STACK_SYNTHESIZER } from './private/private-context';
45
import type { ICustomSynthesis } from './private/synthesis';
56
import { addCustomSynthesis } from './private/synthesis';
@@ -178,6 +179,14 @@ export class App extends Stage {
178179
*/
179180
public readonly _treeMetadata: boolean;
180181

182+
/**
183+
* Git source information for the repository containing this CDK app.
184+
* Contains the repository URL and commit hash, or `undefined` if
185+
* git information is not available or has been disabled via
186+
* `CDK_DISABLE_GIT_SOURCE`.
187+
*/
188+
public readonly gitSource: { readonly repository: string; readonly commit: string } | undefined;
189+
181190
/**
182191
* Initializes a CDK application.
183192
* @param props initialization properties
@@ -219,6 +228,7 @@ export class App extends Stage {
219228
}
220229

221230
this._treeMetadata = props.treeMetadata ?? true;
231+
this.gitSource = getGitSource();
222232
}
223233

224234
private loadContext(defaults: { [key: string]: string } = { }, final: { [key: string]: string } = {}) {

packages/aws-cdk-lib/core/lib/stack.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,8 @@ export class Stack extends Construct implements ITaggable {
14301430
}
14311431

14321432
const metadata: { [key: string]: any } = {};
1433-
const gitSource = getGitSource();
1433+
const app = App.isApp(this.node.root) ? this.node.root as App : undefined;
1434+
const gitSource = app?.gitSource ?? getGitSource();
14341435
if (gitSource) {
14351436
metadata['AWS::CloudFormation::Source'] = {
14361437
Repository: gitSource.repository,

packages/aws-cdk-lib/core/test/app.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,42 @@ describe('app', () => {
382382
const stack = new Stack(stage, 'TestStack');
383383
expect(App.of(stack)).toBe(app);
384384
});
385+
386+
test('gitSource is populated by default and propagated to stacks', () => {
387+
const savedEnv = process.env.CDK_DISABLE_GIT_SOURCE;
388+
process.env.CDK_DISABLE_GIT_SOURCE = '';
389+
try {
390+
const app = new App();
391+
const stack = new Stack(app, 'Stack');
392+
new CfnResource(stack, 'Resource', { type: 'MyResource' });
393+
394+
expect(app.gitSource).toBeDefined();
395+
expect(typeof app.gitSource!.repository).toBe('string');
396+
expect(typeof app.gitSource!.commit).toBe('string');
397+
expect(app.gitSource!.commit).toMatch(/^[a-f0-9]{40}$/);
398+
399+
const assembly = app.synth();
400+
const template = assembly.getStackByName(stack.stackName).template;
401+
const source = template?.Metadata?.['AWS::CloudFormation::Source'];
402+
expect(source).toEqual({
403+
Repository: app.gitSource!.repository,
404+
Commit: app.gitSource!.commit,
405+
});
406+
} finally {
407+
process.env.CDK_DISABLE_GIT_SOURCE = savedEnv;
408+
}
409+
});
410+
411+
test('gitSource is undefined when CDK_DISABLE_GIT_SOURCE is set', () => {
412+
const savedEnv = process.env.CDK_DISABLE_GIT_SOURCE;
413+
process.env.CDK_DISABLE_GIT_SOURCE = '1';
414+
try {
415+
const app = new App();
416+
expect(app.gitSource).toBeUndefined();
417+
} finally {
418+
process.env.CDK_DISABLE_GIT_SOURCE = savedEnv;
419+
}
420+
});
385421
});
386422

387423
class MyConstruct extends Construct {

0 commit comments

Comments
 (0)