diff --git a/.github/workflows/update-compat-data.yml b/.github/workflows/update-compat-data.yml new file mode 100644 index 000000000000..9e68a4dd3371 --- /dev/null +++ b/.github/workflows/update-compat-data.yml @@ -0,0 +1,78 @@ +name: Update compat data +env: + YARN_ENABLE_SCRIPTS: false # disable post-install scripts +on: + workflow_dispatch: + inputs: {} + schedule: + - cron: "0 0 * * 5" + +jobs: + createPullRequest: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/checkout@v2 + with: + repository: kangax/compat-table + path: packages/babel-compat-data/build/compat-table + - name: Use Node.js latest + uses: actions/setup-node@v2-beta + with: + node-version: "*" + - name: Get latest kangax/compat-data commit + id: lastCommit + run: echo "::set-output name=sha1::$(node ./scripts/update-compat-data/get-last-commit.sh)" + - name: Compare last kangax/compat-data commit with current one + run: echo ${{ steps.lastCommit.outputs.sha1 }} | node ./scripts/update-compat-data/compare-with-current-commit.js + - name: Update compat-data commit + run: echo ${{ steps.lastCommit.outputs.sha1 }} | ./scripts/update-compat-data/bump-data-compat-version.sh + - name: Run update script + run: make build-compat-data + - name: Commit changes + run: | + git config user.name "Babel Bot" + git config user.email "babel-bot@users.noreply.github.com" + git checkout -b update-compat-data + git commit -am "chore: update compat data to ${{ steps.lastCommit.outputs.sha1 }}" + git push --force origin update-compat-data + - name: Create Pull Request + uses: actions/github-script@v4 + with: + github-token: ${{ secrets.BOT_TOKEN }} + script: | + const base = process.env.GITHUB_REF.replace("refs/heads/", ""); + const requestParam = { + owner: context.repo.owner, + repo: context.repo.repo, + head: context.repo.owner + ":update-compat-data", + base: base, + state: "open" + }; + const result = await github.pulls.list(requestParam); + console.log("Open PR request: ", requestParam); + console.log("Open PR response: ", result); + const prs = result.data; + if (prs.length === 0) { + const requestParam = { + owner: context.repo.owner, + repo: context.repo.repo, + head: "update-compat-data", + base: base, + maintainer_can_modify: true, + title: "Update compat data", + body: "Update compat data to [${{ steps.lastCommit.outputs.sha1 }}](https://github.com/kangax/compat-table/commit/${{ steps.lastCommit.outputs.sha1 }}).", + }; + const result = await github.pulls.create(requestParam); + console.log("Create PR request: ", requestParam) + console.log("Create PR response: ", result); + + github.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: result.data.number, + labels: ["area: compat-data", "repo automation :robot:"] + }) + } + + diff --git a/scripts/update-compat-data/bump-data-compat-version.sh b/scripts/update-compat-data/bump-data-compat-version.sh new file mode 100644 index 000000000000..7635f4f11280 --- /dev/null +++ b/scripts/update-compat-data/bump-data-compat-version.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# read given commit SHA1 from stdin, update to COMPAT_TABLE_COMMIT in download-compat-table.sh +# usage: +# node ./scripts/update-compat-data/get-last-commit.js | sh ./scripts/update-compat-data/bump-data-compat-version.sh + +set -e +perl -i -pe 's/^COMPAT_TABLE_COMMIT.+$/COMPAT_TABLE_COMMIT='$(cat)'/' ./packages/babel-compat-data/download-compat-table.sh diff --git a/scripts/update-compat-data/compare-with-current-commit.js b/scripts/update-compat-data/compare-with-current-commit.js new file mode 100644 index 000000000000..6d95512fa7a2 --- /dev/null +++ b/scripts/update-compat-data/compare-with-current-commit.js @@ -0,0 +1,49 @@ +import { readFile } from "fs/promises"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const CURRENT_COMMIT_VARIABLE_NAME = "COMPAT_TABLE_COMMIT"; + +async function getCurrentCommit() { + const target = path.join( + __dirname, + "..", + "..", + "packages", + "babel-compat-data", + "scripts", + "download-compat-table.sh" + ); + const lines = (await readFile(target, { encoding: "utf-8" })).split("\n"); + + for (const line of lines) { + if (line.includes(CURRENT_COMMIT_VARIABLE_NAME)) { + return line.split("=").at(1); + } + } + + return undefined; +} + +async function checkCurrentCommit(lastCommit) { + if (typeof lastCommit !== "string") { + throw new Error(`Last commit should be provided but got ${lastCommit}`); + } + + const currentCommit = await getCurrentCommit(); + if (typeof lastCommit !== "string") { + throw new Error("Not valid current commit found: ${currentCommit}"); + } + + console.log("last commit :", lastCommit); + console.log("current commit :", currentCommit); + + if (lastCommit === currentCommit) { + throw new Error("compat-data doesn't need to be updated"); + } + + return lastCommit; +} + +checkCurrentCommit(process.argv[2]).then(console.log); diff --git a/scripts/update-compat-data/get-last-commit.sh b/scripts/update-compat-data/get-last-commit.sh new file mode 100644 index 000000000000..1aec89303996 --- /dev/null +++ b/scripts/update-compat-data/get-last-commit.sh @@ -0,0 +1,4 @@ + set -e + export GIT_DIR=./packages/babel-compat-data/build/compat-table + git fetch -q origin HEAD + git rev-parse FETCH_HEAD