From 329c269a8ce4f8b8d6c7f99fbdfb4379903ba307 Mon Sep 17 00:00:00 2001 From: Dennis Bartlett Date: Sat, 29 Mar 2025 15:34:02 -0700 Subject: [PATCH] Update Changeset Workflow (#2531) * Update Changeset Workflow * Remove RELEASE related content --- .changeset/.mighty-tools-remain.md | 7 -- .../scripts/overwrite_changeset_changelog.py | 88 +++++-------- .github/workflows/changeset-converter.yml | 91 ++++++++++++++ .github/workflows/check-changeset.yml | 117 ------------------ 4 files changed, 123 insertions(+), 180 deletions(-) delete mode 100644 .changeset/.mighty-tools-remain.md create mode 100644 .github/workflows/changeset-converter.yml delete mode 100644 .github/workflows/check-changeset.yml diff --git a/.changeset/.mighty-tools-remain.md b/.changeset/.mighty-tools-remain.md deleted file mode 100644 index b66bf207..00000000 --- a/.changeset/.mighty-tools-remain.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -"claude-dev": minor ---- -Add Bedrock prompt caching support (optional). - -This feature protected under checkbox because it is not yet rolled out to everyone, and if you will try to send cache headers, and its not enabled for you, you will get error. - diff --git a/.github/scripts/overwrite_changeset_changelog.py b/.github/scripts/overwrite_changeset_changelog.py index 67fdb6a6..fee92719 100644 --- a/.github/scripts/overwrite_changeset_changelog.py +++ b/.github/scripts/overwrite_changeset_changelog.py @@ -22,7 +22,6 @@ Environment Variables: #!/usr/bin/env python3 import os -import sys CHANGELOG_PATH = os.environ.get("CHANGELOG_PATH", "CHANGELOG.md") VERSION = os.environ['VERSION'] @@ -32,72 +31,49 @@ NEW_CONTENT = os.environ.get("NEW_CONTENT", "") def overwrite_changelog_section(changelog_text: str, new_content: str): # Find the section for the specified version version_pattern = f"## {VERSION}\n" - bracketed_version_pattern = f"## [{VERSION}]\n" + unformmatted_prev_version_pattern = f"## {PREV_VERSION}\n" prev_version_pattern = f"## [{PREV_VERSION}]\n" print(f"latest version: {VERSION}") print(f"prev_version: {PREV_VERSION}") - # Try both unbracketed and bracketed version patterns - version_index = changelog_text.find(version_pattern) - if version_index == -1: - version_index = changelog_text.find(bracketed_version_pattern) - if version_index == -1: - # If version not found, add it at the top (after the first line) - first_newline = changelog_text.find('\n') - if first_newline == -1: - # If no newline found, just prepend - return f"## [{VERSION}]\n\n{changelog_text}" - return f"{changelog_text[:first_newline + 1]}## [{VERSION}]\n\n{changelog_text[first_newline + 1:]}" - else: - # Using bracketed version - version_pattern = bracketed_version_pattern - - notes_start_index = version_index + len(version_pattern) - notes_end_index = changelog_text.find(prev_version_pattern, notes_start_index) if PREV_VERSION and prev_version_pattern in changelog_text else len(changelog_text) + notes_start_index = changelog_text.find(version_pattern) + len(version_pattern) + notes_end_index = changelog_text.find(prev_version_pattern, notes_start_index) if PREV_VERSION and (prev_version_pattern in changelog_text or unformmatted_prev_version_pattern in changelog_text) else len(changelog_text) if new_content: return changelog_text[:notes_start_index] + f"{new_content}\n" + changelog_text[notes_end_index:] else: changeset_lines = changelog_text[notes_start_index:notes_end_index].split("\n") - # Ensure we have at least 2 lines before removing them - if len(changeset_lines) < 2: - print("Warning: Changeset content has fewer than 2 lines") - parsed_lines = "\n".join(changeset_lines) - else: - # Remove the first two lines from the regular changeset format, ex: \n### Patch Changes - parsed_lines = "\n".join(changeset_lines[2:]) + filtered_lines = [] + for line in changeset_lines: + # If the previous line is a changeset format + if len(filtered_lines) > 1 and filtered_lines[-1].startswith("### "): + # Remove the last two lines from the filted_lines + filtered_lines.pop() + filtered_lines.pop() + else: + filtered_lines.append(line.strip()) + + # Prepend a new line to the first line of filtered_lines + if filtered_lines: + filtered_lines[0] = "\n" + filtered_lines[0] + + # Print filted_lines wiht a "\n" at the end of each line + for line in filtered_lines: + print(line.strip()) + + parsed_lines = "\n".join(line for line in filtered_lines) updated_changelog = changelog_text[:notes_start_index] + parsed_lines + changelog_text[notes_end_index:] - # Ensure version number is bracketed - updated_changelog = updated_changelog.replace(f"## {VERSION}", f"## [{VERSION}]") return updated_changelog -try: - print(f"Reading changelog from: {CHANGELOG_PATH}") - with open(CHANGELOG_PATH, 'r') as f: - changelog_content = f.read() +with open(CHANGELOG_PATH, 'r') as f: + changelog_content = f.read() - print(f"Changelog content length: {len(changelog_content)} characters") - print("First 200 characters of changelog:") - print(changelog_content[:200]) - print("----------------------------------------------------------------------------------") +new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT) +# print("----------------------------------------------------------------------------------") +# print(new_changelog) +# print("----------------------------------------------------------------------------------") +# Write back to CHANGELOG.md +with open(CHANGELOG_PATH, 'w') as f: + f.write(new_changelog) - new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT) - - print("New changelog content:") - print("----------------------------------------------------------------------------------") - print(new_changelog) - print("----------------------------------------------------------------------------------") - - print(f"Writing updated changelog back to: {CHANGELOG_PATH}") - with open(CHANGELOG_PATH, 'w') as f: - f.write(new_changelog) - - print(f"{CHANGELOG_PATH} updated successfully!") - -except FileNotFoundError: - print(f"Error: Changelog file not found at {CHANGELOG_PATH}") - sys.exit(1) -except Exception as e: - print(f"Error updating changelog: {str(e)}") - print(f"Current working directory: {os.getcwd()}") - sys.exit(1) +print(f"{CHANGELOG_PATH} updated successfully!") diff --git a/.github/workflows/changeset-converter.yml b/.github/workflows/changeset-converter.yml new file mode 100644 index 00000000..cb8a1e7e --- /dev/null +++ b/.github/workflows/changeset-converter.yml @@ -0,0 +1,91 @@ +name: Changeset Converter +run-name: Changeset Conversion + +on: + pull_request: + types: [closed] + +env: + REPO_PATH: ${{ github.repository }} + GIT_REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || 'main' }} + NODE_VERSION: 20.18.1 + +jobs: + # Job 1: Create version bump PR when changesets are merged to main + changeset-pr-version-bump: + if: > + github.event_name == 'pull_request' && + github.event.pull_request.merged == true && + github.event.pull_request.base.ref == 'main' && + github.actor != 'github-actions' + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - name: Git Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ env.GIT_REF }} + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: "npm" + + - name: Install Dependencies + run: npm install changeset + + # Check if there are any new changesets to process + - name: Check for changesets + id: check-changesets + run: | + NEW_CHANGESETS=$(find .changeset -name "*.md" ! -name "README.md" | wc -l | tr -d ' ') + echo "Changesets diff with previous version: $NEW_CHANGESETS" + echo "new_changesets=$NEW_CHANGESETS" >> $GITHUB_OUTPUT + + # Create version bump PR using changesets/action if there are new changesets + - name: Create Changeset Pull Request + if: steps.check-changesets.outputs.new_changesets != '0' + uses: changesets/action@v1 + with: + commit: "changeset version bump" + title: "Changeset version bump" + version: npm run version-packages # This performs the changeset version bump + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # Get current and previous versions to edit changelog entry + - name: Get version + id: get_version + run: | + VERSION=$(git show HEAD:package.json | jq -r '.version') + echo "version=$VERSION" >> $GITHUB_OUTPUT + PREV_VERSION=$(git show origin/main:package.json | jq -r '.version') + echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT + echo "version=$VERSION" + echo "prev_version=$PREV_VERSION" + + # Update CHANGELOG.md with proper format + - name: Update Changelog Format + env: + VERSION: ${{ steps.get_version.outputs.version }} + PREV_VERSION: ${{ steps.get_version.outputs.prev_version }} + run: python .github/scripts/overwrite_changeset_changelog.py + + # Commit and push changelog updates + - name: Push Changelog updates to Pull Request + run: | + git config user.name "github-actions" + git config user.email github-actions@github.com + echo "Running git add and commit..." + git add CHANGELOG.md + git commit -m "Updating CHANGELOG.md format" + git status + echo "--------------------------------------------------------------------------------" + echo "Pushing to remote..." + echo "--------------------------------------------------------------------------------" + CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) + git push origin $CURRENT_BRANCH diff --git a/.github/workflows/check-changeset.yml b/.github/workflows/check-changeset.yml deleted file mode 100644 index f220257c..00000000 --- a/.github/workflows/check-changeset.yml +++ /dev/null @@ -1,117 +0,0 @@ -name: Check Changeset -run-name: Check for Changeset in PR - -permissions: - contents: read - pull-requests: write - -on: - pull_request: - branches: - - main - types: [opened, synchronize, reopened, ready_for_review] - -jobs: - check-changeset: - # Skip draft PRs and dependabot PRs - if: github.event.pull_request.draft == false && github.actor != 'dependabot[bot]' - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha }} - - - name: Check for changeset - id: check-changeset - run: | - # Debug info - echo "Current directory: $(pwd)" - echo "PR Base Ref: ${{ github.event.pull_request.base.ref }}" - echo "PR Head Ref: ${{ github.event.pull_request.head.ref }}" - echo "PR Head SHA: ${{ github.event.pull_request.head.sha }}" - echo "Git status:" - git status - - # Get list of changed files - git fetch origin ${{ github.event.pull_request.base.ref }} - CHANGED_FILES=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} HEAD) - echo "Changed files:" - echo "$CHANGED_FILES" - - # Check if any of the changed files are in docs/ or .github/ - echo "Checking if changes are docs-only..." - DOCS_ONLY=true - while IFS= read -r file; do - if [[ ! "$file" =~ ^(docs/|.github/) ]]; then - echo "Found non-docs change: $file" - DOCS_ONLY=false - break - fi - done <<< "$CHANGED_FILES" - - # If changes are docs-only, skip changeset check - if [ "$DOCS_ONLY" = true ]; then - echo "All changes are in docs/ or .github/, skipping changeset check" - exit 0 - else - echo "Changes include non-docs files, checking for changeset..." - fi - - # Check if any changeset files are in the changed files - echo "Checking for changeset files in changed files..." - CHANGESET_IN_PR=false - while IFS= read -r file; do - if [[ "$file" =~ ^\.changeset/.*\.md$ && "$file" != ".changeset/README.md" && "$file" != ".changeset/config.json" ]]; then - echo "Found changeset file in PR: $file" - CHANGESET_IN_PR=true - break - fi - done <<< "$CHANGED_FILES" - - if [ "$CHANGESET_IN_PR" = false ]; then - echo "No changeset files found in changed files. Changed files in .changeset/:" - echo "$CHANGED_FILES" | grep "^\.changeset/" || true - echo "::error::No changeset file found in PR changes. Please run 'npm run changeset' to create one." - exit 1 - fi - - - name: Comment on PR - if: failure() - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7 - with: - script: | - const message = `This PR requires a changeset since it includes user-facing changes. Please: - - 1. Run \`npm run changeset\` locally - 2. Choose the appropriate version bump: - - \`major\` for breaking changes (1.0.0 → 2.0.0) - - \`minor\` for new features (1.0.0 → 1.1.0) - - \`patch\` for bug fixes (1.0.0 → 1.0.1) - 3. Write a clear description of your changes - 4. Commit the generated changeset file - - Note: Documentation-only changes do not require a changeset.`; - - // Get existing comments - const comments = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number - }); - - // Check if we already commented - const botComment = comments.data.find(comment => - comment.user.login === 'github-actions[bot]' && - comment.body.includes('This PR requires a changeset') - ); - - if (!botComment) { - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - body: message - }); - }