From be231ed2a51bf5667d2531589c831ccc3586e718 Mon Sep 17 00:00:00 2001 From: GZ Date: Mon, 3 Feb 2025 13:40:01 -0800 Subject: [PATCH] chore: enable GitHub workflow for Metadata updater (#33262) ### Issue # (if applicable) N/A ### Reason for this change Added a weekly triggered workflow to update metadata and constructors. ### Description of changes Build a GitHub action file that will create a PR automatically ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Workflow runs successfully. ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../workflows/analytics-metadata-updater.yml | 54 +++++++++++++++++++ .../lib/metadata-updater.ts | 16 ++++-- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/analytics-metadata-updater.yml diff --git a/.github/workflows/analytics-metadata-updater.yml b/.github/workflows/analytics-metadata-updater.yml new file mode 100644 index 0000000000000..171b30becf778 --- /dev/null +++ b/.github/workflows/analytics-metadata-updater.yml @@ -0,0 +1,54 @@ +name: CDK Analytics Metadata Updater +on: + workflow_dispatch: + pull_request: + branches: + - yuanhaoz/metadata_workflow # TODO, remove this + - v2-release + +jobs: + update-analytics-metadata: + if: github.repository == 'aws/aws-cdk' + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: "*" + env: + NODE_OPTIONS: "--max-old-space-size=8196 --experimental-worker ${NODE_OPTIONS:-}" + + - name: Install dependencies + run: yarn install --frozen-lockfile && cd tools/@aws-cdk/construct-metadata-updater && yarn build+test + + - name: Invoke Analytics Metadata Updater + run: | + cd tools/@aws-cdk/construct-metadata-updater + ./bin/update-construct-metadata + + - name: Check for changes + id: git-check + run: | + if [[ -n "$(git status --porcelain)" ]]; then + echo "changes=true" >> $GITHUB_OUTPUT + else + echo "changes=false" >> $GITHUB_OUTPUT + fi + + - name: Commit & Push changes + if: steps.git-check.outputs.changes == 'true' + run: | + git config --global user.name 'aws-cdk-automation' + git config --global user.email 'aws-cdk-automation@users.noreply.github.com' + git add . + git commit -m "chore: update analytics metadata blueprints" + git push origin ${{ github.event.pull_request.head.ref }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/tools/@aws-cdk/construct-metadata-updater/lib/metadata-updater.ts b/tools/@aws-cdk/construct-metadata-updater/lib/metadata-updater.ts index f2dd5bf064169..23f7647eef02c 100644 --- a/tools/@aws-cdk/construct-metadata-updater/lib/metadata-updater.ts +++ b/tools/@aws-cdk/construct-metadata-updater/lib/metadata-updater.ts @@ -160,12 +160,22 @@ export class ConstructsUpdater extends MetadataUpdater { return; } - // Create the new import statement - sourceFile.addImportDeclaration({ + // Find the correct insertion point (after the last import before the new one) + const importDeclarations = sourceFile.getImportDeclarations(); + let insertIndex = importDeclarations.length; + for (let i = 0; i < importDeclarations.length; i++) { + const existingImport = importDeclarations[i].getModuleSpecifier().getText(); + if (existingImport.localeCompare(relativePath) > 0) { + insertIndex = i; + break; + } + } + + // Insert the new import at the appropriate position + sourceFile.insertImportDeclaration(insertIndex, { moduleSpecifier: relativePath, namedImports: [{ name: "addConstructMetadata" }], }); - console.log(`Added import for MetadataType in file: ${filePath} with relative path: ${relativePath}`); // Write the updated file back to disk