Skip to content

Commit 29dbaa3

Browse files
authored
Merge branch 'main' into feat/migrate-slack-to-fanout
2 parents 9ff9b45 + ff79c24 commit 29dbaa3

4 files changed

Lines changed: 214 additions & 43 deletions

File tree

cdk/src/main.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* SOFTWARE.
1818
*/
1919

20-
import { App, Aspects } from 'aws-cdk-lib';
20+
import { App, Aspects, Tags } from 'aws-cdk-lib';
2121
import { AwsSolutionsChecks } from 'cdk-nag';
2222
import { AgentStack } from './stacks/agent';
2323

@@ -33,7 +33,7 @@ Aspects.of(app).add(new AwsSolutionsChecks());
3333

3434
const stackName = app.node.tryGetContext('stackName') ?? 'backgroundagent-dev';
3535

36-
new AgentStack(
36+
const stack = new AgentStack(
3737
app,
3838
stackName,
3939
{
@@ -42,4 +42,21 @@ new AgentStack(
4242
},
4343
);
4444

45+
const githubTagKeys = [
46+
'sha',
47+
'ref',
48+
'ref-type',
49+
'actor',
50+
'head-ref',
51+
'run-id',
52+
'event',
53+
'repository',
54+
'clean',
55+
] as const;
56+
57+
for (const key of githubTagKeys) {
58+
const value = app.node.tryGetContext(`github:${key}`);
59+
Tags.of(stack).add(`github:${key}`, value || 'none');
60+
}
61+
4562
app.synth();
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* MIT No Attribution
3+
*
4+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so.
10+
*
11+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17+
* SOFTWARE.
18+
*/
19+
20+
import { App, Tags } from 'aws-cdk-lib';
21+
import { Template } from 'aws-cdk-lib/assertions';
22+
import { AgentStack } from '../../src/stacks/agent';
23+
24+
const GITHUB_TAG_KEYS = [
25+
'github:sha',
26+
'github:ref',
27+
'github:ref-type',
28+
'github:actor',
29+
'github:head-ref',
30+
'github:run-id',
31+
'github:event',
32+
'github:repository',
33+
'github:clean',
34+
] as const;
35+
36+
function synthWithTags(context: Record<string, string> = {}): Template {
37+
const app = new App({ context });
38+
const stackName = app.node.tryGetContext('stackName') ?? 'backgroundagent-dev';
39+
const stack = new AgentStack(app, stackName, {
40+
env: { account: '123456789012', region: 'us-east-1' },
41+
});
42+
43+
const githubTagKeys = [
44+
'sha', 'ref', 'ref-type', 'actor', 'head-ref',
45+
'run-id', 'event', 'repository', 'clean',
46+
] as const;
47+
48+
for (const key of githubTagKeys) {
49+
const value = app.node.tryGetContext(`github:${key}`);
50+
Tags.of(stack).add(`github:${key}`, value || 'none');
51+
}
52+
53+
return Template.fromStack(stack);
54+
}
55+
56+
describe('github:* resource tags', () => {
57+
let templateWithDefaults: Template;
58+
let templateWithValues: Template;
59+
60+
beforeAll(() => {
61+
templateWithDefaults = synthWithTags();
62+
templateWithValues = synthWithTags({
63+
'github:sha': 'f36d352c5a1bc3a90d3e60e30e2f9d4345426724',
64+
'github:ref': 'main',
65+
'github:ref-type': 'branch',
66+
'github:actor': 'scottschreckengaust',
67+
'github:head-ref': '',
68+
'github:run-id': '12345678',
69+
'github:event': 'push',
70+
'github:repository': 'aws-samples/sample-autonomous-cloud-coding-agents',
71+
'github:clean': 'true',
72+
});
73+
});
74+
75+
test('all 9 github:* tags default to "none" when no context is provided', () => {
76+
const resources = templateWithDefaults.findResources('AWS::DynamoDB::Table');
77+
const firstResource = Object.values(resources)[0];
78+
const tags: Array<{ Key: string; Value: string }> = firstResource?.Properties?.Tags ?? [];
79+
80+
for (const tagKey of GITHUB_TAG_KEYS) {
81+
const tag = tags.find(t => t.Key === tagKey);
82+
expect(tag).toBeDefined();
83+
expect(tag!.Value).toBe('none');
84+
}
85+
});
86+
87+
test('github:* tags reflect context values when provided', () => {
88+
const resources = templateWithValues.findResources('AWS::DynamoDB::Table');
89+
const firstResource = Object.values(resources)[0];
90+
const tags: Array<{ Key: string; Value: string }> = firstResource?.Properties?.Tags ?? [];
91+
92+
expect(tags.find(t => t.Key === 'github:sha')!.Value).toBe('f36d352c5a1bc3a90d3e60e30e2f9d4345426724');
93+
expect(tags.find(t => t.Key === 'github:ref')!.Value).toBe('main');
94+
expect(tags.find(t => t.Key === 'github:ref-type')!.Value).toBe('branch');
95+
expect(tags.find(t => t.Key === 'github:actor')!.Value).toBe('scottschreckengaust');
96+
expect(tags.find(t => t.Key === 'github:head-ref')!.Value).toBe('none');
97+
expect(tags.find(t => t.Key === 'github:run-id')!.Value).toBe('12345678');
98+
expect(tags.find(t => t.Key === 'github:event')!.Value).toBe('push');
99+
expect(tags.find(t => t.Key === 'github:repository')!.Value).toBe('aws-samples/sample-autonomous-cloud-coding-agents');
100+
expect(tags.find(t => t.Key === 'github:clean')!.Value).toBe('true');
101+
});
102+
103+
test('empty string context values resolve to "none"', () => {
104+
const template = synthWithTags({
105+
'github:sha': '',
106+
'github:head-ref': '',
107+
});
108+
const resources = template.findResources('AWS::DynamoDB::Table');
109+
const firstResource = Object.values(resources)[0];
110+
const tags: Array<{ Key: string; Value: string }> = firstResource?.Properties?.Tags ?? [];
111+
112+
expect(tags.find(t => t.Key === 'github:sha')!.Value).toBe('none');
113+
expect(tags.find(t => t.Key === 'github:head-ref')!.Value).toBe('none');
114+
});
115+
});

docs/package.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)