|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +report_step() { |
| 5 | + echo "--- $1" |
| 6 | +} |
| 7 | + |
| 8 | +# Ensure we're in the repo root |
| 9 | +cd "${KIBANA_DIR:-$(pwd)}" |
| 10 | + |
| 11 | +# Target branch for the PR (set by Buildkite from the schedule's branch; override via BUILDKITE_BRANCH when triggering manually) |
| 12 | +TARGET_BRANCH="${BUILDKITE_BRANCH:-main}" |
| 13 | +echo "Target branch for PR: $TARGET_BRANCH" |
| 14 | + |
| 15 | +# Capture working tree state before bootstrap so we can detect bootstrap-induced changes |
| 16 | +report_step "Capturing pre-bootstrap state" |
| 17 | +pre_bootstrap_diff=$(git diff --no-ext-diff 2>/dev/null || true) |
| 18 | +pre_bootstrap_untracked=$(git status -s -u 2>/dev/null || true) |
| 19 | + |
| 20 | +report_step "Bootstrap Kibana" |
| 21 | +.buildkite/scripts/bootstrap.sh |
| 22 | + |
| 23 | +# Fail if bootstrap changed anything; only metadata update changes should be committed |
| 24 | +report_step "Verifying bootstrap did not change files" |
| 25 | +post_bootstrap_diff=$(git diff --no-ext-diff 2>/dev/null || true) |
| 26 | +post_bootstrap_untracked=$(git status -s -u 2>/dev/null || true) |
| 27 | +if [[ "$pre_bootstrap_diff" != "$post_bootstrap_diff" || "$pre_bootstrap_untracked" != "$post_bootstrap_untracked" ]]; then |
| 28 | + echo "Bootstrap produced changes to the working tree. Only Scout metadata updates should be committed. Aborting." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +report_step "Update Scout test config manifests" |
| 33 | +node scripts/scout.js update-test-config-manifests |
| 34 | + |
| 35 | +# Check for changes from the metadata update |
| 36 | +if git diff --exit-code --quiet 2>/dev/null && git diff --cached --exit-code --quiet 2>/dev/null; then |
| 37 | + echo "No Scout metadata changes. Nothing to commit." |
| 38 | + exit 0 |
| 39 | +fi |
| 40 | + |
| 41 | +report_step "Scout metadata changed. Creating pull request." |
| 42 | + |
| 43 | +KIBANA_MACHINE_USERNAME="kibanamachine" |
| 44 | +git config --global user.name "$KIBANA_MACHINE_USERNAME" |
| 45 | +git config --global user.email '42973632+kibanamachine@users.noreply.github.com' |
| 46 | + |
| 47 | +PR_TITLE='[Scout] Update test config manifests' |
| 48 | +BRANCH_NAME="scout_metadata_update_$(date +%s)" |
| 49 | + |
| 50 | +# Check if an open PR with the same title targeting this base already exists |
| 51 | +existing_pr_json=$(gh pr list --base "$TARGET_BRANCH" --search "$PR_TITLE" --state open --author "$KIBANA_MACHINE_USERNAME" --limit 1 --json number,headRefName,title 2>/dev/null || true) |
| 52 | +existing_pr_title=$(echo "$existing_pr_json" | jq -r '.[0].title // empty') |
| 53 | +if [[ "$existing_pr_title" == "$PR_TITLE" ]]; then |
| 54 | + existing_branch=$(echo "$existing_pr_json" | jq -r '.[0].headRefName // empty') |
| 55 | + existing_pr_number=$(echo "$existing_pr_json" | jq -r '.[0].number // empty') |
| 56 | + echo "An open PR for Scout metadata update (base $TARGET_BRANCH) already exists. Updating PR #$existing_pr_number (branch: $existing_branch) via force-push." |
| 57 | + git checkout -b "$BRANCH_NAME" |
| 58 | + git add -A |
| 59 | + git commit -m "[Scout] Update test config manifests" |
| 60 | + git push origin "$BRANCH_NAME:$existing_branch" --force |
| 61 | + echo "Updated existing PR: https://github.com/elastic/kibana/pull/$existing_pr_number" |
| 62 | + exit 0 |
| 63 | +fi |
| 64 | + |
| 65 | +git checkout -b "$BRANCH_NAME" |
| 66 | +git add -A |
| 67 | +git commit -m "[Scout] Update test config manifests" |
| 68 | + |
| 69 | +report_step "Pushing branch and creating PR (base: $TARGET_BRANCH)" |
| 70 | +git push origin "$BRANCH_NAME" |
| 71 | + |
| 72 | +pr_url=$(gh pr create --repo elastic/kibana --base "$TARGET_BRANCH" --head "$BRANCH_NAME" \ |
| 73 | + --title "$PR_TITLE" \ |
| 74 | + --body "Generated by $BUILDKITE_BUILD_URL" \ |
| 75 | + --label 'release_note:skip') |
| 76 | +echo "Opened PR: $pr_url" |
| 77 | + |
| 78 | +gh pr merge --repo elastic/kibana --auto --squash "$pr_url" |
0 commit comments