diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 64ecd49b7..b0fde878d 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -23,6 +23,9 @@ workflows: - 'actions/setup-node@v7.1.0' - 'actions/setup-python@v7.1.0' - 'actions/upload-artifact@v7.0.2' + '.github/workflows/sync-app-client.yml': + - 'actions/checkout@v7.0.1' + - 'actions/create-github-app-token@v3.4.0' '.github/workflows/verify-actions-lock.yml': - 'actions/checkout@v7.0.1' dependencies: @@ -36,6 +39,11 @@ dependencies: commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' owner_id: 44036562 repo_id: 197814629 + 'actions/create-github-app-token@v3.4.0': + ref: 'v3.4.0' + commit: 'sha1-f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc' + owner_id: 44036562 + repo_id: 642580244 'actions/download-artifact@v8.0.2': ref: 'v8.0.2' commit: 'sha1-9000827ccba6bdab643e8b6fd33ac0654aef8333' diff --git a/.github/workflows/sync-app-client.yml b/.github/workflows/sync-app-client.yml new file mode 100644 index 000000000..da49df4e6 --- /dev/null +++ b/.github/workflows/sync-app-client.yml @@ -0,0 +1,112 @@ +name: Sync Deepnote app client + +on: + schedule: + - cron: '0 8 * * *' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + +jobs: + sync: + name: Sync the bundled app client + runs-on: ubuntu-latest + timeout-minutes: 10 + env: + BRANCH: automation/sync-app-client + CLIENT_PATH: resources/appPreview/v1.js + CLIENT_URL: https://deepnote.com/static/app-client/v1.js + TITLE: 'chore(app-preview): update the bundled Deepnote app client' + steps: + - name: Checkout + uses: actions/checkout@v7.0.1 + with: + ref: main + persist-credentials: false + + # Pushes and pull requests made with GITHUB_TOKEN don't trigger CI or E2E, and E2E running the new client is the point. + - name: Create GitHub App token + id: app-token + uses: actions/create-github-app-token@v3.4.0 + with: + client-id: ${{ vars.GH_TOKEN_APP_CLIENT_ID }} + private-key: ${{ secrets.GH_TOKEN_APP_PRIVATE_KEY }} + permission-contents: write + permission-pull-requests: write + + - name: Open or update the pull request + env: + APP_SLUG: ${{ steps.app-token.outputs.app-slug }} + GH_TOKEN: ${{ steps.app-token.outputs.token }} + run: | + set -euo pipefail + + new_client="$RUNNER_TEMP/v1.js" + content_type=$(curl --fail --silent --show-error --location --retry 3 --max-time 60 \ + --output "$new_client" --write-out '%{content_type}' "$CLIENT_URL") + case "$content_type" in + text/javascript* | application/javascript*) ;; + *) + echo "::error::$CLIENT_URL answered with '$content_type', not JavaScript" + exit 1 + ;; + esac + + bot_name="${APP_SLUG}[bot]" + bot_email="$(gh api "users/$bot_name" --jq .id)+$bot_name@users.noreply.github.com" + git config user.name "$bot_name" + git config user.email "$bot_email" + gh auth setup-git + + pr=$(gh pr list --base main --head "$BRANCH" --state open \ + --json number,headRefOid,isCrossRepository \ + --jq '.[] | select(.isCrossRepository | not) | "\(.number) \(.headRefOid)"') + pr_number=${pr% *} + pr_head=${pr#* } + + only_bot_commits() { + local emails + emails=$(gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$pr_number/commits" \ + --jq '.[].commit | .author.email, .committer.email') || exit + ! grep --quiet --invert-match --line-regexp --fixed-strings "$bot_email" <<< "$emails" + } + + if cmp --silent "$new_client" "$CLIENT_PATH"; then + echo "main already has the client that $CLIENT_URL serves." + if [[ -n "$pr_number" ]] && only_bot_commits; then + gh pr close "$pr_number" --delete-branch --comment "main already has the client that $CLIENT_URL serves." + fi + exit 0 + fi + + if [[ -n "$pr_number" ]]; then + git fetch --quiet --depth=1 origin "$pr_head" + if git show "$pr_head:$CLIENT_PATH" | cmp --silent - "$new_client"; then + echo "#$pr_number already has the client that $CLIENT_URL serves." + exit 0 + fi + if ! only_bot_commits; then + echo "::error::$CLIENT_URL serves a new client, but #$pr_number has commits from someone other than $bot_name, so it is left as is. Update $CLIENT_PATH there by hand." + exit 1 + fi + fi + + git switch --quiet --create "$BRANCH" + cp "$new_client" "$CLIENT_PATH" + git commit --quiet --all --message "$TITLE" + # An empty lease requires the branch not to exist. + lease=${pr_head:-$(git ls-remote origin "refs/heads/$BRANCH" | cut -f1)} + git push --force-with-lease="refs/heads/$BRANCH:$lease" origin "HEAD:refs/heads/$BRANCH" + + if [[ -z "$pr_number" ]]; then + gh pr create --draft --base main --head "$BRANCH" --title "$TITLE" --body-file - <