Repository navigation
feat(sandbox): stop idle agent sandboxes from OSC 7501 program status #47063
Workflow file for this run
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: Required CI Gates | ||
|
Check warning on line 1 in .github/workflows/required-ci-gates.yml
|
||
| # Branch protection requires the `OpenShell / ...` statuses this workflow | ||
| # posts. It runs from the base repository, so it can post them for fork PRs, | ||
| # whose own pull_request runs only get a read-only token. | ||
| on: | ||
| merge_group: | ||
| types: [checks_requested] | ||
| pull_request_target: | ||
| types: [opened, synchronize, reopened, ready_for_review, labeled, unlabeled] | ||
| workflow_run: | ||
| workflows: | ||
| - Branch Checks | ||
| - Branch E2E Checks | ||
| - Helm Lint | ||
| types: [completed] | ||
| permissions: | ||
| actions: read | ||
| contents: read | ||
| pull-requests: read | ||
| statuses: write | ||
| # Group by head SHA so a pull_request_target evaluation and a workflow_run | ||
| # evaluation of the same commit cannot race and leave a stale status behind. | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.head.sha || github.event.workflow_run.head_sha || github.sha || github.run_id }} | ||
| cancel-in-progress: true | ||
| jobs: | ||
| publish: | ||
| name: Publish required CI gate statuses | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Evaluate required CI gates | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| GH_REPO: ${{ github.repository }} | ||
| EVENT_NAME: ${{ github.event_name }} | ||
| PR_NUMBER_FROM_EVENT: ${{ github.event.pull_request.number }} | ||
| PR_HEAD_SHA_FROM_EVENT: ${{ github.event.pull_request.head.sha }} | ||
| PR_LABELS_FROM_EVENT: ${{ toJSON(github.event.pull_request.labels.*.name) }} | ||
| GITHUB_SHA_FROM_CONTEXT: ${{ github.sha }} | ||
| GITHUB_REF_NAME_FROM_CONTEXT: ${{ github.ref_name }} | ||
| GITHUB_RUN_ID_FROM_CONTEXT: ${{ github.run_id }} | ||
| WORKFLOW_RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | ||
| WORKFLOW_RUN_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} | ||
| WORKFLOW_RUN_EVENT: ${{ github.event.workflow_run.event }} | ||
| WORKFLOW_RUN_HTML_URL: ${{ github.event.workflow_run.html_url }} | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| post_status() { | ||
| local context="$1" | ||
| local state="$2" | ||
| local description="$3" | ||
| local target_url="${4:-}" | ||
| args=( | ||
| --method POST | ||
| "repos/$GH_REPO/statuses/$HEAD_SHA" | ||
| -f "state=$state" | ||
| -f "context=$context" | ||
| -f "description=$description" | ||
| ) | ||
| if [ -n "$target_url" ]; then | ||
| args+=(-f "target_url=$target_url") | ||
| fi | ||
| echo "$context: $state - $description" | ||
| gh api "${args[@]}" >/dev/null | ||
| } | ||
| has_label() { | ||
| local label="$1" | ||
| jq -e --arg label "$label" 'index($label) != null' <<< "$LABELS_JSON" >/dev/null | ||
| } | ||
| resolve_pull_request_event() { | ||
| CONTEXT_KIND="pull_request" | ||
| PR_NUMBER="$PR_NUMBER_FROM_EVENT" | ||
| HEAD_SHA="$PR_HEAD_SHA_FROM_EVENT" | ||
| LABELS_JSON=$(jq -c . <<< "$PR_LABELS_FROM_EVENT") | ||
| STATUS_TARGET_URL="https://github.com/$GH_REPO/pull/$PR_NUMBER" | ||
| } | ||
| load_pr_context() { | ||
| CONTEXT_KIND="pull_request" | ||
| PR_NUMBER="$1" | ||
| local pr state | ||
| pr=$(gh api "repos/$GH_REPO/pulls/$PR_NUMBER") | ||
| state=$(jq -r '.state' <<< "$pr") | ||
| if [ "$state" != "open" ]; then | ||
| echo "PR #$PR_NUMBER is $state; nothing to publish." | ||
| exit 0 | ||
| fi | ||
| HEAD_SHA=$(jq -r '.head.sha' <<< "$pr") | ||
| LABELS_JSON=$(jq -c '[.labels[].name]' <<< "$pr") | ||
| STATUS_TARGET_URL="https://github.com/$GH_REPO/pull/$PR_NUMBER" | ||
| } | ||
| # Fork PR runs carry no pull_requests list, so match open PRs by head SHA. | ||
| load_pr_context_for_sha() { | ||
| local sha="$1" number | ||
| if [[ ! "$sha" =~ ^[0-9a-f]{40}$ ]]; then | ||
| echo "Ignoring run for '$sha': not a commit SHA." | ||
| exit 0 | ||
| fi | ||
| number=$(gh api --paginate "repos/$GH_REPO/pulls?state=open&per_page=100" \ | ||
| --jq ".[] | select(.head.sha == \"$sha\") | .number" | head -n 1) | ||
| if [ -z "$number" ]; then | ||
| echo "No open PR has head $sha; nothing to publish." | ||
| exit 0 | ||
| fi | ||
| load_pr_context "$number" | ||
| } | ||
| resolve_merge_group_event() { | ||
| CONTEXT_KIND="merge_group" | ||
| HEAD_SHA="$GITHUB_SHA_FROM_CONTEXT" | ||
| LABELS_JSON="[]" | ||
| EXPECTED_HEAD_BRANCH="$GITHUB_REF_NAME_FROM_CONTEXT" | ||
| STATUS_TARGET_URL="https://github.com/$GH_REPO/actions/runs/$GITHUB_RUN_ID_FROM_CONTEXT" | ||
| } | ||
| resolve_merge_group_workflow_run_event() { | ||
| CONTEXT_KIND="merge_group" | ||
| HEAD_SHA="$WORKFLOW_RUN_HEAD_SHA" | ||
| LABELS_JSON="[]" | ||
| EXPECTED_HEAD_BRANCH="$WORKFLOW_RUN_HEAD_BRANCH" | ||
| STATUS_TARGET_URL="${WORKFLOW_RUN_HTML_URL:-https://github.com/$GH_REPO/actions}" | ||
| } | ||
| resolve_workflow_run_event() { | ||
| case "$WORKFLOW_RUN_EVENT" in | ||
| merge_group) | ||
| resolve_merge_group_workflow_run_event | ||
| ;; | ||
| pull_request) | ||
| load_pr_context_for_sha "$WORKFLOW_RUN_HEAD_SHA" | ||
| ;; | ||
| *) | ||
| echo "Ignoring workflow_run from event '$WORKFLOW_RUN_EVENT'." | ||
| exit 0 | ||
| ;; | ||
| esac | ||
| } | ||
| resolve_context() { | ||
| EXPECTED_HEAD_BRANCH="" | ||
| if [ "$EVENT_NAME" = "pull_request_target" ]; then | ||
| resolve_pull_request_event | ||
| elif [ "$EVENT_NAME" = "merge_group" ]; then | ||
| resolve_merge_group_event | ||
| elif [ "$EVENT_NAME" = "workflow_run" ]; then | ||
| resolve_workflow_run_event | ||
| else | ||
| echo "Unsupported event '$EVENT_NAME'." | ||
| exit 1 | ||
| fi | ||
| STATUS_TARGET_URL="${STATUS_TARGET_URL:-https://github.com/$GH_REPO/actions}" | ||
| } | ||
| # Runs for HEAD_SHA, newest first. | ||
| list_runs() { | ||
| local workflow_file="$1" | ||
| if [ "$CONTEXT_KIND" = "merge_group" ]; then | ||
| gh api "repos/$GH_REPO/actions/workflows/$workflow_file/runs?head_sha=$HEAD_SHA&event=merge_group" \ | ||
| --jq '.workflow_runs' \ | ||
| | jq -c --arg branch "$EXPECTED_HEAD_BRANCH" '[.[] | select(.head_branch == $branch)] | sort_by(.created_at) | reverse' | ||
| return | ||
| fi | ||
| gh api "repos/$GH_REPO/actions/workflows/$workflow_file/runs?head_sha=$HEAD_SHA&event=pull_request&per_page=100" \ | ||
| --jq '.workflow_runs' | jq -c 'sort_by(.created_at) | reverse' | ||
| } | ||
| run_jobs() { | ||
| gh api --paginate "repos/$GH_REPO/actions/runs/$1/jobs?per_page=30" | jq -s '[.[].jobs[]]' | ||
| } | ||
| # A labeled event for an unrelated label finishes without running | ||
| # the required job. Skip those and use the newest run | ||
| # where the required job ran. | ||
| evaluate_required_job() { | ||
| local context="$1" workflow_name="$2" required_job_name="$3" runs="$4" | ||
| local run run_id status run_url job job_conclusion | ||
| while read -r run; do | ||
| [ -n "$run" ] || continue | ||
| run_id=$(jq -r '.id' <<< "$run") | ||
| status=$(jq -r '.status' <<< "$run") | ||
| run_url=$(jq -r '.html_url' <<< "$run") | ||
| if [ "$status" != "completed" ]; then | ||
| post_status "$context" pending "$workflow_name is $status" "$run_url" | ||
| return 0 | ||
| fi | ||
| job=$(run_jobs "$run_id" | jq -c --arg name "$required_job_name" '[.[] | select(.name == $name)] | .[0] // empty') | ||
| job_conclusion=$(jq -r '.conclusion // empty' <<< "$job") | ||
| if [ -z "$job" ] || [ "$job_conclusion" = "skipped" ]; then | ||
| continue | ||
| fi | ||
| if [ "$job_conclusion" = "success" ]; then | ||
| post_status "$context" success "$required_job_name passed" "$run_url" | ||
| else | ||
| post_status "$context" failure "$required_job_name concluded $job_conclusion" "$run_url" | ||
| fi | ||
| return 0 | ||
| done < <(jq -c '.[]' <<< "$runs") | ||
| post_status "$context" pending "Waiting for $required_job_name" "$STATUS_TARGET_URL" | ||
| } | ||
| evaluate_workflow() { | ||
| local context="$1" | ||
| local workflow_file="$2" | ||
| local workflow_name="$3" | ||
| local required_label="${4:-}" | ||
| local required_job_name="${5:-}" | ||
| local workflow_url="https://github.com/$GH_REPO/actions/workflows/$workflow_file" | ||
| if [ "$CONTEXT_KIND" = "pull_request" ] && [ -n "$required_label" ] && ! has_label "$required_label"; then | ||
| post_status "$context" success "$required_label not applied" "$STATUS_TARGET_URL" | ||
| return 0 | ||
| fi | ||
| local runs latest run_id status conclusion run_url real_success | ||
| runs=$(list_runs "$workflow_file") | ||
| if [ -n "$required_job_name" ]; then | ||
| evaluate_required_job "$context" "$workflow_name" "$required_job_name" "$runs" | ||
| return 0 | ||
| fi | ||
| latest=$(jq -c '.[0] // empty' <<< "$runs") | ||
| if [ -z "$latest" ]; then | ||
| post_status "$context" pending "Waiting for $workflow_name" "$workflow_url" | ||
| return 0 | ||
| fi | ||
| run_id=$(jq -r '.id' <<< "$latest") | ||
| status=$(jq -r '.status' <<< "$latest") | ||
| conclusion=$(jq -r '.conclusion' <<< "$latest") | ||
| run_url=$(jq -r '.html_url' <<< "$latest") | ||
| if [ "$status" != "completed" ]; then | ||
| post_status "$context" pending "$workflow_name is $status" "$run_url" | ||
| return 0 | ||
| fi | ||
| if [ "$conclusion" = "action_required" ]; then | ||
| post_status "$context" pending "$workflow_name awaits maintainer approval" "$run_url" | ||
| return 0 | ||
| fi | ||
| if [ "$conclusion" != "success" ]; then | ||
| post_status "$context" failure "$workflow_name concluded $conclusion" "$run_url" | ||
| return 0 | ||
| fi | ||
| real_success=$(run_jobs "$run_id" | jq '[.[] | select(.conclusion == "success")] | length') | ||
| if [ "$real_success" -lt 1 ]; then | ||
| post_status "$context" failure "No real CI jobs ran" "$run_url" | ||
| return 0 | ||
| fi | ||
| post_status "$context" success "$workflow_name passed" "$run_url" | ||
| } | ||
| resolve_context | ||
| evaluate_workflow "OpenShell / Branch Checks" "branch-checks.yml" "Branch Checks" | ||
| # E2E is opt-in on PRs and does not run for merge groups. | ||
| if [ "$CONTEXT_KIND" != "merge_group" ]; then | ||
| evaluate_workflow "OpenShell / E2E" "branch-e2e.yml" "Branch E2E Checks" "test:e2e" "Core E2E result" | ||
| else | ||
| post_status "OpenShell / E2E" success "Not applicable: E2E runs only on PRs" "$STATUS_TARGET_URL" | ||
| fi | ||
| evaluate_workflow "OpenShell / Helm Lint" "helm-lint.yml" "Helm Lint" | ||