104 lines
4.7 KiB
YAML
104 lines
4.7 KiB
YAML
# This workflow lets us test a PR from a fork, while still having secrets available
|
|
# * We cannot use the 'pull_request' event in 'general.yml', since GitHub unconditionally blocks
|
|
# secrets when this event is triggered by a PR from a fork.
|
|
# * We cannot use the 'pull_request_target' event in 'general.yml', since it runs unconditionally (!!),
|
|
# regardless of the repository approval settings:
|
|
# https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#controlling-changes-from-forks-to-workflows-in-public-repositories
|
|
# As a result, using this event to build the PR would allow someone to instantly steal our secrets just by opening a PR.
|
|
name: Run all checks for PR from a fork (by pushing to a branch on tensorzero/tensorzero)
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
repository_dispatch:
|
|
types: [check-fork-command]
|
|
|
|
permissions: {}
|
|
|
|
env:
|
|
TARGET_BRANCH: external-contributor/pr-${{ github.event.client_payload.pull_request.number }}
|
|
|
|
concurrency:
|
|
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref || github.run_id }}
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
check-fork:
|
|
name: check-fork
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
if: github.repository == 'tensorzero/tensorzero'
|
|
permissions:
|
|
pull-requests: write # Required to update PR comments, labels, or branches.
|
|
issues: write # Required to comment on and react to PR issue comments.
|
|
contents: write # Required to push generated branch changes.
|
|
actions: write # Required to approve, cancel, rerun, or dispatch workflow runs.
|
|
steps:
|
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
|
with:
|
|
persist-credentials: false
|
|
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
|
|
ref: ${{ github.event.client_payload.pull_request.head.ref }}
|
|
# We need to use a PAT so that GitHub will let us modify workflow files when pushing to a branch
|
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Add initial reaction
|
|
env:
|
|
COMMENT_ID: ${{ github.event.client_payload.github.payload.comment.id }}
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
REPOSITORY: ${{ github.event.client_payload.github.payload.repository.full_name }}
|
|
run: |
|
|
gh api --method POST "repos/$REPOSITORY/issues/comments/$COMMENT_ID/reactions" \
|
|
-H "Accept: application/vnd.github+json" \
|
|
-f content=eyes
|
|
|
|
- name: Force-push to a branch named ${{ env.TARGET_BRANCH }}
|
|
env:
|
|
UPSTREAM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
run: |
|
|
git remote add upstream "https://x-access-token:${UPSTREAM_TOKEN}@github.com/tensorzero/tensorzero.git"
|
|
git checkout -b "$TARGET_BRANCH"
|
|
git push --force upstream "$TARGET_BRANCH"
|
|
- name: Trigger 'General Checks' workflow on ${{ env.TARGET_BRANCH }}
|
|
id: trigger
|
|
run: |
|
|
set -e
|
|
gh workflow run general.yml --ref "$TARGET_BRANCH"
|
|
|
|
# Wait 30s for the workflow to be created
|
|
sleep 30
|
|
|
|
# Get the most recent workflow run for this branch
|
|
RUN_ID=$(gh run list --workflow=general.yml --branch="$TARGET_BRANCH" --limit=1 --json databaseId --jq '.[0].databaseId')
|
|
|
|
if [ -n "$RUN_ID" ]; then
|
|
echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT
|
|
echo "run_url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$RUN_ID" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "Failed to get run ID"
|
|
echo "run_url=https://github.com/$GITHUB_REPOSITORY/actions" >> $GITHUB_OUTPUT
|
|
fi
|
|
env:
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Comment with workflow link
|
|
env:
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
ISSUE_NUMBER: ${{ github.event.client_payload.pull_request.number }}
|
|
REPOSITORY: ${{ github.event.client_payload.github.payload.repository.full_name }}
|
|
RUN_URL: ${{ steps.trigger.outputs.run_url }}
|
|
run: |
|
|
gh issue comment "$ISSUE_NUMBER" --repo "$REPOSITORY" --body "馃殌 General Checks workflow triggered on new branch $TARGET_BRANCH!
|
|
|
|
View the run: $RUN_URL"
|
|
|
|
- name: Add success reaction
|
|
if: success()
|
|
env:
|
|
COMMENT_ID: ${{ github.event.client_payload.github.payload.comment.id }}
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
REPOSITORY: ${{ github.event.client_payload.github.payload.repository.full_name }}
|
|
run: |
|
|
gh api --method POST "repos/$REPOSITORY/issues/comments/$COMMENT_ID/reactions" \
|
|
-H "Accept: application/vnd.github+json" \
|
|
-f content=hooray
|