feat: simplify project structure and improve documentation #31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: PR Auto-publish Detection | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| jobs: | |
| detect-and-comment: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Need full history for diff | |
| - name: Detect codemod changes | |
| id: detect | |
| run: | | |
| # Get the base and head commits | |
| BASE_SHA="${{ github.event.pull_request.base.sha }}" | |
| HEAD_SHA="${{ github.event.pull_request.head.sha }}" | |
| echo "Comparing $BASE_SHA..$HEAD_SHA" | |
| # Find changed codemod directories | |
| CHANGED_CODEMODS=$(git diff --name-only $BASE_SHA..$HEAD_SHA | grep '^recipes/' | cut -d'/' -f1-2 | sort -u | tr '\n' ' ') | |
| if [ -z "$CHANGED_CODEMODS" ]; then | |
| echo "has-changes=false" >> $GITHUB_OUTPUT | |
| echo "No codemod changes detected" | |
| else | |
| echo "has-changes=true" >> $GITHUB_OUTPUT | |
| echo "codemods=$CHANGED_CODEMODS" >> $GITHUB_OUTPUT | |
| echo "Changed codemods: $CHANGED_CODEMODS" | |
| fi | |
| - name: Check API key configuration | |
| id: check-api-key | |
| run: | | |
| if [ -z "${{ secrets.CODEMOD_API_KEY }}" ]; then | |
| echo "api-key-configured=false" >> $GITHUB_OUTPUT | |
| echo "API key not configured" | |
| else | |
| echo "api-key-configured=true" >> $GITHUB_OUTPUT | |
| echo "API key is configured" | |
| fi | |
| - name: Comment on PR | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { data: 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 existingComment = comments.find(comment => | |
| comment.user.type === 'Bot' && | |
| (comment.body.includes('Auto-publish') || comment.body.includes('Codemod Registry')) | |
| ); | |
| if (existingComment) { | |
| console.log('Comment already exists, skipping...'); | |
| return; | |
| } | |
| const hasChanges = '${{ steps.detect.outputs.has-changes }}' === 'true'; | |
| const codemods = '${{ steps.detect.outputs.codemods }}'.split(' ').filter(Boolean); | |
| const apiKeyConfigured = '${{ steps.check-api-key.outputs.api-key-configured }}' === 'true'; | |
| let commentBody; | |
| if (hasChanges && apiKeyConfigured) { | |
| // Codemod changes + API key configured | |
| commentBody = `## π Codemods Ready for Auto-Publish | |
| **Changed codemods:** ${codemods.map(c => `\`${c}\``).join(', ')} | |
| These codemods will be **automatically published** to the Codemod Registry when this PR is merged to \`main\`. | |
| **What happens on merge:** | |
| - β Codemods are automatically published to the registry | |
| - β Version conflicts are auto-resolved (patch version bump) | |
| - β Publishing errors are handled gracefully | |
| --- | |
| *This comment was automatically generated by the auto-publish detection workflow.*`; | |
| } else if (hasChanges && !apiKeyConfigured) { | |
| // Codemod changes + no API key | |
| commentBody = `## π¦ Codemods Detected | |
| **Changed codemods:** ${codemods.map(c => `\`${c}\``).join(', ')} | |
| **Auto-publishing is available!** π | |
| These codemods can be **automatically published** to the Codemod Registry when this PR is merged to \`main\`. | |
| **To enable auto-publishing:** | |
| 1. Get API key from [https://app.codemod.com/api-keys](https://app.codemod.com/api-keys) | |
| 2. Create GitHub Environment: Repository Settings β Environments β New environment (name it \`production\`) | |
| 3. Add secret: \`CODEMOD_API_KEY\` with your API key | |
| **What happens when enabled:** | |
| - β Codemods are automatically published to the registry | |
| - β Version conflicts are auto-resolved (patch version bump) | |
| - β Publishing errors are handled gracefully | |
| --- | |
| *This comment was automatically generated by the auto-publish detection workflow.*`; | |
| } else { | |
| // No codemod changes | |
| commentBody = `## π¦ Codemod Repository | |
| This repository supports **automatic codemod publishing** to the Codemod Registry. | |
| **To enable auto-publishing:** | |
| 1. Get API key from [https://app.codemod.com/api-keys](https://app.codemod.com/api-keys) | |
| 2. Create GitHub Environment: Repository Settings β Environments β New environment (name it \`production\`) | |
| 3. Add secret: \`CODEMOD_API_KEY\` with your API key | |
| **What happens when enabled:** | |
| - β Codemods in \`recipes/\` are automatically published to the registry | |
| - β Version conflicts are auto-resolved (patch version bump) | |
| - β Publishing errors are handled gracefully | |
| --- | |
| *This comment was automatically generated by the auto-publish detection workflow.*`; | |
| } | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: commentBody | |
| }); |