Skip to content

Commit ce18819

Browse files
authored
Automate updating create amplify dependencies for dependabot (#2843)
* add update create amplify deps to dependabot update workflow * add linting and unit test
1 parent 2fbfe45 commit ce18819

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

.changeset/cold-bats-live.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

.github/workflows/health_checks.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ jobs:
275275
if: github.event_name == 'pull_request' && github.event.pull_request.user.login == 'dependabot[bot]'
276276
runs-on: ubuntu-latest
277277
needs:
278-
- install
278+
- build
279279
- resolve_inputs
280280
permissions:
281281
# This is required so that this job can add the 'run-e2e' label and push to the pull request
@@ -288,12 +288,15 @@ jobs:
288288
- uses: ./.github/actions/setup_node
289289
with:
290290
node-version: 18
291-
- uses: ./.github/actions/restore_install_cache
291+
- uses: ./.github/actions/restore_build_cache
292292
with:
293293
node-version: 18
294294
cdk-lib-version: ${{ needs.resolve_inputs.outputs.cdk_lib_version }}
295295
- name: Handle Dependabot version update pull request
296-
run: npx tsx scripts/dependabot_handle_version_update.ts "$BASE_SHA"
296+
run: |
297+
npm run update:create-amplify-deps
298+
npm run lint:fix
299+
npx tsx scripts/dependabot_handle_version_update.ts "$BASE_SHA"
297300
env:
298301
BASE_SHA: ${{ github.event.pull_request.base.sha }}
299302
# The dependabot_handler_version_update script needs to add the 'run-e2e' pull request label

scripts/components/create_amplify_dep_updater.test.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'assert';
22
import fsp from 'fs/promises';
33
import { beforeEach, describe, it, mock } from 'node:test';
44
import { createAmplifyDepUpdater } from './create_amplify_dep_updater.js';
5+
import { EOL } from 'os';
56

67
void describe('createAmplifyDepUpdater', () => {
78
const mockedFsReadFile = mock.method(fsp, 'readFile', () =>
@@ -19,14 +20,43 @@ void describe('createAmplifyDepUpdater', () => {
1920
}),
2021
);
2122
const mockedFsWriteFile = mock.method(fsp, 'writeFile', mock.fn());
23+
const ghContextMocked = {
24+
eventName: '',
25+
sha: '',
26+
ref: '',
27+
workflow: '',
28+
action: '',
29+
actor: '',
30+
job: '',
31+
runAttempt: 0,
32+
runNumber: 0,
33+
runId: 0,
34+
apiUrl: '',
35+
serverUrl: '',
36+
graphqlUrl: '',
37+
payload: {},
38+
issue: {
39+
owner: '',
40+
repo: '',
41+
number: 0,
42+
},
43+
repo: {
44+
owner: '',
45+
repo: '',
46+
},
47+
};
2248

2349
beforeEach(() => {
2450
mockedFsReadFile.mock.resetCalls();
2551
mockedFsWriteFile.mock.resetCalls();
2652
});
2753

2854
void it('successfully pins new dev version', async () => {
29-
await createAmplifyDepUpdater([{ name: 'aws-cdk-lib', version: '2.1.0' }]);
55+
await createAmplifyDepUpdater(
56+
[{ name: 'aws-cdk-lib', version: '2.1.0' }],
57+
undefined,
58+
ghContextMocked,
59+
);
3060
assert.strictEqual(mockedFsReadFile.mock.callCount(), 1);
3161
assert.strictEqual(mockedFsWriteFile.mock.callCount(), 1);
3262
assert.deepStrictEqual(
@@ -54,6 +84,7 @@ void describe('createAmplifyDepUpdater', () => {
5484
await createAmplifyDepUpdater(
5585
[{ name: 'test-prod-package', version: '1.1.0' }],
5686
['test-prod-package'],
87+
ghContextMocked,
5788
);
5889
assert.strictEqual(mockedFsReadFile.mock.callCount(), 1);
5990
assert.strictEqual(mockedFsWriteFile.mock.callCount(), 1);
@@ -88,6 +119,7 @@ void describe('createAmplifyDepUpdater', () => {
88119
{ name: 'test-prod-package', version: '1.1.0' },
89120
],
90121
['aws-cdk', 'aws-cdk-lib', 'test-prod-package'],
122+
ghContextMocked,
91123
);
92124
assert.strictEqual(mockedFsReadFile.mock.callCount(), 1);
93125
assert.strictEqual(mockedFsWriteFile.mock.callCount(), 1);
@@ -115,6 +147,54 @@ void describe('createAmplifyDepUpdater', () => {
115147
);
116148
});
117149

150+
void it('creates changeset file for dependabot pull request', async () => {
151+
const dependabotPRContext = {
152+
...ghContextMocked,
153+
payload: {
154+
pull_request: {
155+
number: 1,
156+
body: 'Bumps aws-cdk-lib from 2.0.0 to 2.1.0',
157+
head: {
158+
ref: 'dependabot/test_version_update_branch',
159+
// eslint-disable-next-line spellcheck/spell-checker
160+
sha: 'abcd1234', // used for naming the changeset file
161+
},
162+
},
163+
},
164+
};
165+
const expectedChangesetContent = `---${EOL}'create-amplify': patch${EOL}---${EOL + EOL}bump create amplify dependencies${EOL}`;
166+
await createAmplifyDepUpdater(
167+
[{ name: 'aws-cdk-lib', version: '2.1.0' }],
168+
undefined,
169+
dependabotPRContext,
170+
);
171+
assert.strictEqual(mockedFsReadFile.mock.callCount(), 1);
172+
assert.strictEqual(mockedFsWriteFile.mock.callCount(), 2);
173+
assert.deepStrictEqual(
174+
mockedFsWriteFile.mock.calls[0].arguments[1],
175+
JSON.stringify(
176+
{
177+
defaultDevPackages: [
178+
'@aws-amplify/backend',
179+
'@aws-amplify/backend-cli',
180+
'aws-cdk-lib@2.1.0', // updated
181+
'constructs@^10.0.0',
182+
'typescript@^5.0.0',
183+
'tsx',
184+
'esbuild',
185+
],
186+
defaultProdPackages: ['aws-amplify', 'test-prod-package@1.0.0'],
187+
},
188+
null,
189+
2,
190+
),
191+
);
192+
assert.deepStrictEqual(
193+
mockedFsWriteFile.mock.calls[1].arguments[1],
194+
expectedChangesetContent,
195+
);
196+
});
197+
118198
void it('does nothing if there are no provided dependencies', async () => {
119199
await createAmplifyDepUpdater([]);
120200
assert.strictEqual(mockedFsReadFile.mock.callCount(), 0);

scripts/components/create_amplify_dep_updater.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import fsp from 'fs/promises';
2+
import { context } from '@actions/github';
3+
import { Context } from '@actions/github/lib/context.js';
24
import path from 'path';
35
import { Dependency } from './get_dependencies_from_package_lock.js';
6+
import {
7+
BumpType,
8+
ChangesetFrontMatterContent,
9+
createChangesetFile,
10+
} from './create_changeset_file.js';
411

512
/**
613
* Modifies target dependencies used for create amplify
714
*/
815
export const createAmplifyDepUpdater = async (
916
dependencies: Dependency[],
1017
createAmplifyDepsToFilter: string[] = ['aws-cdk-lib'],
18+
ghContext: Context = context,
1119
) => {
1220
const targetDependencies: Dependency[] = dependencies.filter((dependency) =>
1321
createAmplifyDepsToFilter.includes(dependency.name),
@@ -72,5 +80,24 @@ export const createAmplifyDepUpdater = async (
7280
2,
7381
),
7482
);
83+
// create changeset if event is a dependabot pull request
84+
if (
85+
ghContext.payload.pull_request &&
86+
ghContext.payload.pull_request.head.ref.startsWith('dependabot/')
87+
) {
88+
const fileName = path.join(
89+
process.cwd(),
90+
`.changeset/dependabot-create-amplify-${ghContext.payload.pull_request.head.sha}.md`,
91+
);
92+
const frontMatterContent: ChangesetFrontMatterContent = {
93+
packageName: 'create-amplify',
94+
bumpType: BumpType.PATCH,
95+
};
96+
await createChangesetFile(
97+
fileName,
98+
[frontMatterContent],
99+
'bump create amplify dependencies',
100+
);
101+
}
75102
}
76103
};

0 commit comments

Comments
 (0)