Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,67 @@ name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
checks:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: pnpm/action-setup@v4
with:
version: 9.14.2

- uses: actions/setup-node@v4
with:
node-version: 20
node-version-file: .nvmrc
cache: pnpm

- run: pnpm install --frozen-lockfile
- run: pnpm run test

- run: pnpm run check-types

- name: Run tests for changed codemods
shell: bash
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
EVENT_NAME: ${{ github.event_name }}
HEAD_SHA: ${{ github.sha }}
run: |
if [ "$EVENT_NAME" != "pull_request" ]; then
pnpm run test
exit 0
fi
Comment thread
alexbit-codemod marked this conversation as resolved.

changed_paths=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")

if echo "$changed_paths" | grep -qE '^(package\.json|pnpm-lock\.yaml|pnpm-workspace\.yaml|\.github/workflows/ci\.yml)$'; then
pnpm run test
exit 0
fi

filters=$(
echo "$changed_paths" \
| grep '^codemods/' \
| cut -d/ -f1-2 \
| sort -u \
| sed 's|^|--filter ./|' || true
)

if [ -z "$filters" ]; then
echo "No codemod packages affected; skipping tests."
exit 0
fi

# shellcheck disable=SC2086
pnpm $filters test
99 changes: 99 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Publish

on:
workflow_run:
workflows:
- CI
types:
- completed
workflow_dispatch:
inputs:
codemod_name:
description: Codemod directory name under codemods/
required: true
type: string

concurrency: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch || github.ref }}

permissions:
id-token: write
contents: write

jobs:
tag:
name: Tag released versions
if: >-
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'main'
runs-on: ubuntu-latest
outputs:
changed_dirs: ${{ steps.tag.outputs.changed_dirs }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0

- uses: pnpm/action-setup@f520eceda224fe1a4aed5a2a27a194379a409996 # v6

- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: pnpm

- run: pnpm install --frozen-lockfile

- name: Detect pending changesets
id: pending
run: |
count="$(find .changeset -maxdepth 1 -type f -name '*.md' ! -name 'README.md' | wc -l | tr -d ' ')"
echo "count=$count" >> "$GITHUB_OUTPUT"

- name: Tag released versions
if: steps.pending.outputs.count == '0'
id: tag
run: node scripts/tag-and-publish.mjs

publish:
name: Publish ${{ matrix.dir }}
needs: tag
if: >-
github.event_name == 'workflow_run' &&
needs.tag.outputs.changed_dirs != '' &&
needs.tag.outputs.changed_dirs != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
dir: ${{ fromJson(needs.tag.outputs.changed_dirs) }}
Comment thread
Copilot marked this conversation as resolved.
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}

- uses: pnpm/action-setup@f520eceda224fe1a4aed5a2a27a194379a409996 # v6

- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
cache: pnpm

- run: pnpm install --frozen-lockfile

- name: Publish codemod
uses: codemod/publish-action@dd6c8dbc5ceb1a6146feba41481d88b43da50024 # v1
with:
path: ${{ matrix.dir }}

publish-manual:
name: Publish codemod (manual)
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Publish codemod
uses: codemod/publish-action@dd6c8dbc5ceb1a6146feba41481d88b43da50024 # v1
with:
path: codemods/${{ inputs.codemod_name }}
54 changes: 5 additions & 49 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ on:
types:
- completed

concurrency: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch || github.ref }}
concurrency: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }}

permissions:
id-token: write
contents: write
pull-requests: write

Expand All @@ -22,66 +21,23 @@ jobs:
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'main'
runs-on: ubuntu-latest
outputs:
published: ${{ steps.changesets.outputs.published }}
changed_dirs: ${{ steps.tag.outputs.changed_dirs }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0
fetch-tags: true

- uses: pnpm/action-setup@v4
with:
version: 9.14.2
- uses: pnpm/action-setup@f520eceda224fe1a4aed5a2a27a194379a409996 # v6

- uses: actions/setup-node@v4
with:
node-version: 20
node-version-file: .nvmrc
cache: pnpm

- run: pnpm install --frozen-lockfile

- name: Create Release PR or Publish
id: changesets
uses: changesets/action@v1
- name: Create Release Pull Request
uses: changesets/action@a45c4d594aa4e2c509dc14a9f2b3b67ba3780d0d # v1.9.0
with:
version: pnpm version-packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Tag released versions
if: steps.changesets.outputs.hasChangesets == 'false'
id: tag
run: node scripts/tag-and-publish.mjs

publish:
name: Publish ${{ matrix.dir }}
needs: release
if: needs.release.result == 'success' && needs.release.outputs.changed_dirs != '[]' && needs.release.outputs.changed_dirs != ''
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
dir: ${{ fromJson(needs.release.outputs.changed_dirs) }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}

- uses: pnpm/action-setup@v4
with:
version: 9.14.2

- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- run: pnpm install --frozen-lockfile

- name: Publish codemod
uses: codemod/publish-action@v1
with:
path: ${{ matrix.dir }}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "pnpm -r --filter \"react-*\" test",
"check-types": "pnpm -r --filter \"react-*\" check-types",
"changeset": "changeset",
"version-packages": "changeset version && bash scripts/sync-codemod-versions.sh",
"version-packages": "changeset version && node scripts/sync-codemod-yaml-versions.mjs",
"ci": "pnpm run test && pnpm run check-types"
},
"keywords": [
Expand Down
20 changes: 0 additions & 20 deletions scripts/sync-codemod-versions.sh

This file was deleted.

36 changes: 36 additions & 0 deletions scripts/sync-codemod-yaml-versions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node
import { readdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join, relative } from 'node:path'
import { fileURLToPath } from 'node:url'

const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const versionPattern = /^version:\s*(['"]?)([^'"\n]+)\1\s*$/m

function findCodemodYamlFiles(directory) {
const paths = []

for (const entry of readdirSync(directory, { withFileTypes: true })) {
if (entry.name === 'node_modules') continue

const path = join(directory, entry.name)
if (entry.isDirectory()) {
paths.push(...findCodemodYamlFiles(path))
} else if (entry.isFile() && entry.name === 'codemod.yaml') {
paths.push(path)
}
}

return paths
}

for (const yamlPath of findCodemodYamlFiles(join(root, 'codemods'))) {
const dir = dirname(yamlPath)
const { version } = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'))
const content = readFileSync(yamlPath, 'utf8')
const updated = content.replace(versionPattern, `version: "${version}"`)

if (updated !== content) {
writeFileSync(yamlPath, updated)
console.log(`${relative(root, yamlPath)} -> ${version}`)
}
}
Loading