feat: further improve ergonomics by accepting null or undefined whene… #41
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: ci | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| jobs: | |
| build_and_test: | |
| services: | |
| postgres: | |
| image: postgres | |
| env: | |
| POSTGRES_USER: test | |
| POSTGRES_PASSWORD: test | |
| POSTGRES_DB: test | |
| options: >- | |
| --health-cmd pg_isready | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: | |
| - 5439:5432 | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: sqlc-dev/setup-sqlc@v4 | |
| with: | |
| sqlc-version: '1.27.0' | |
| - uses: oven-sh/setup-bun@v2 | |
| - run: wget https://github.com/bytecodealliance/javy/releases/download/v4.0.0/javy-x86_64-linux-v4.0.0.gz | |
| - run: gzip -d javy-x86_64-linux-v4.0.0.gz -c > ./javy | |
| - run: chmod +x ./javy | |
| - run: bun install | |
| - name: Run tests | |
| run: bun run test | |
| - id: calculate_sha256 | |
| run: | | |
| sha256sum plugin.wasm > plugin.wasm.sha256 | |
| SHA256=$(cat plugin.wasm.sha256 | awk '{ print $1 }') | |
| echo "sha256=$SHA256" >> $GITHUB_OUTPUT | |
| - name: Set outputs | |
| id: vars | |
| run: echo "tag_name=v$(date +'%Y%m%d.%H%M%S')_$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| - name: Create GitHub Release | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| id: create_release | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: ${{ steps.vars.outputs.tag_name }} | |
| release_name: Nightly Release | |
| draft: false | |
| prerelease: false | |
| - name: Upload Plugin WASM to Release | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: plugin.wasm | |
| asset_name: plugin.wasm | |
| asset_content_type: application/wasm | |
| - name: Upload Plugin WASM SHA256 to Release | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| upload_url: ${{ steps.create_release.outputs.upload_url }} | |
| asset_path: plugin.wasm.sha256 | |
| asset_name: plugin.wasm.sha256 | |
| asset_content_type: text/plain | |
| - name: Update Release Description with SHA256 and Commits | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| uses: actions/github-script@v4 | |
| env: | |
| tag: ${{ steps.vars.outputs.tag_name }} | |
| sha256: ${{ steps.calculate_sha256.outputs.sha256 }} | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| script: | | |
| const { tag, sha256 } = process.env; | |
| // Retrieve all releases to identify current and previous releases | |
| const { data: releases } = await github.repos.listReleases({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| }); | |
| // Find the current release | |
| const currentRelease = releases.find(r => r.tag_name === tag); | |
| if (!currentRelease) { | |
| throw new Error(`Cannot find release for tag ${tag}`); | |
| } | |
| // Sort releases by creation date, descending | |
| const sortedReleases = releases.sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); | |
| // Find the current release index, then take the next release as the previous | |
| const currentIndex = sortedReleases.findIndex(r => r.id === currentRelease.id); | |
| const previousRelease = sortedReleases[currentIndex + 1]; | |
| let commitsDescription = ""; | |
| // If theres a previous release, compare commits; otherwise note that no previous release was found | |
| if (previousRelease) { | |
| const { data: compare } = await github.repos.compareCommits({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| base: previousRelease.tag_name, | |
| head: tag | |
| }); | |
| // Build a simple list of commit messages and short SHAs | |
| commitsDescription = compare.commits | |
| .map(commit => `- ${commit.commit.message} (${commit.sha.substring(0, 7)})`) | |
| .join("\n"); | |
| } else { | |
| commitsDescription = "No previous release found."; | |
| } | |
| // Retrieve the current release details by its tag | |
| const { data: release } = await github.repos.getReleaseByTag({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| tag: tag | |
| }); | |
| // Construct the new body with plugin info and commit list | |
| const newBody = `\`\`\`yml | |
| plugins: | |
| - name: ts | |
| wasm: | |
| url: https://github.com/eEQK/sqlc-gen-typescript/releases/download/${tag}/plugin.wasm | |
| sha256: ${sha256} | |
| \`\`\` | |
| Commits since last release: | |
| ${commitsDescription} | |
| `; | |
| // Update the release description | |
| await github.repos.updateRelease({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| release_id: release.id, | |
| body: newBody | |
| }); | |
| - name: Update README with new plugin URL and SHA256 | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| uses: actions/github-script@v4 | |
| env: | |
| tag: ${{ steps.vars.outputs.tag_name }} | |
| sha256: ${{ steps.calculate_sha256.outputs.sha256 }} | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| script: | | |
| const { tag, sha256 } = process.env; | |
| const fs = require('fs'); | |
| // Read the current README.md | |
| const readmePath = 'README.md'; | |
| let readmeContent = fs.readFileSync(readmePath, 'utf8'); | |
| // Create the new plugin configuration block | |
| const newPluginConfig = `\`\`\`yml | |
| plugins: | |
| - name: ts | |
| wasm: | |
| url: https://github.com/eEQK/sqlc-gen-typescript/releases/download/${tag}/plugin.wasm | |
| sha256: ${sha256} | |
| \`\`\``; | |
| // Replace the existing plugin configuration (lines 13-19) | |
| // Look for the pattern: ```yml followed by plugins: section | |
| const pluginConfigRegex = /```yml\s*\nplugins:\s*\n- name: ts\s*\n\s*wasm:\s*\n\s*url: https:\/\/github\.com\/eEQK\/sqlc-gen-typescript\/releases\/download\/[^\/]+\/[^\s]+\s*\n\s*sha256: [a-f0-9]+\s*\n```/; | |
| if (pluginConfigRegex.test(readmeContent)) { | |
| readmeContent = readmeContent.replace(pluginConfigRegex, newPluginConfig); | |
| } else { | |
| console.log('Could not find plugin configuration pattern in README.md'); | |
| process.exit(1); | |
| } | |
| // Write the updated README.md back | |
| fs.writeFileSync(readmePath, readmeContent, 'utf8'); | |
| // Commit and push the changes | |
| const { execSync } = require('child_process'); | |
| execSync('git config --local user.email "action@github.com"'); | |
| execSync('git config --local user.name "GitHub Action"'); | |
| execSync('git add README.md'); | |
| // Check if there are changes to commit | |
| try { | |
| execSync('git diff --staged --exit-code', { stdio: 'ignore' }); | |
| console.log('No changes to README.md'); | |
| } catch (error) { | |
| // There are changes, so commit them | |
| execSync(`git commit -m "Update README.md with new plugin URL and SHA256 for ${tag}"`); | |
| execSync('git push origin main'); | |
| console.log('README.md updated and pushed to main branch'); | |
| } |