|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +# Helper script for the breaking-changes-detector workflow. |
| 20 | +# |
| 21 | +# Subcommands: |
| 22 | +# changed-crates <base_ref> |
| 23 | +# Print space-separated list of crate names whose files changed vs base_ref. |
| 24 | +# |
| 25 | +# semver-check <base_ref> <packages...> |
| 26 | +# Run cargo-semver-checks for the given packages against base_ref. |
| 27 | +# Prints the (ANSI-stripped) log output to stdout. |
| 28 | +# Exit code matches cargo-semver-checks (0 = pass, non-zero = breaking). |
| 29 | +# |
| 30 | +# comment <repo> <pr_number> <check_result> [logs] |
| 31 | +# Upsert or delete a sticky PR comment based on check_result. |
| 32 | +# check_result: "success" deletes any existing comment, |
| 33 | +# anything else upserts the comment with the provided logs. |
| 34 | +# Requires GH_TOKEN to be set. |
| 35 | + |
| 36 | +set -euo pipefail |
| 37 | + |
| 38 | +MARKER="<!-- semver-check-comment -->" |
| 39 | + |
| 40 | +# ── changed-crates ────────────────────────────────────────────────── |
| 41 | +cmd_changed_crates() { |
| 42 | + local base_ref="${1:?Usage: changed_crates.sh changed-crates <base_ref>}" |
| 43 | + |
| 44 | + # Parse workspace members from root Cargo.toml, excluding internal crates |
| 45 | + # that are not published / not part of the public API. |
| 46 | + local members |
| 47 | + members=$(sed -n '/^members = \[/,/\]/p' Cargo.toml | grep '"' | sed 's/.*"\(.*\)".*/\1/' \ |
| 48 | + | grep -v -e '^benchmarks$' -e '^test-utils$' -e '^datafusion/sqllogictest$' -e '^datafusion/doc$') |
| 49 | + |
| 50 | + local changed_files |
| 51 | + changed_files=$(git diff --name-only "${base_ref}...HEAD") |
| 52 | + |
| 53 | + local packages="" |
| 54 | + for member in $members; do |
| 55 | + if echo "$changed_files" | grep -q "^${member}/"; then |
| 56 | + local pkg |
| 57 | + pkg=$(grep '^name\s*=' "$member/Cargo.toml" | head -1 | sed 's/.*=\s*"\(.*\)"/\1/') |
| 58 | + if [ -n "$pkg" ]; then |
| 59 | + packages="$packages $pkg" |
| 60 | + fi |
| 61 | + fi |
| 62 | + done |
| 63 | + |
| 64 | + echo "$packages" | xargs |
| 65 | +} |
| 66 | + |
| 67 | +# ── semver-check ──────────────────────────────────────────────────── |
| 68 | +cmd_semver_check() { |
| 69 | + local base_ref="${1:?Usage: changed_crates.sh semver-check <base_ref> <packages...>}" |
| 70 | + shift |
| 71 | + |
| 72 | + local args="" |
| 73 | + for pkg in "$@"; do |
| 74 | + args="$args --package $pkg" |
| 75 | + done |
| 76 | + |
| 77 | + set +e |
| 78 | + # Compare the PR's code against the base branch to detect breaking changes. |
| 79 | + # Use tee to show output in the Actions log while also capturing it. |
| 80 | + cargo semver-checks --baseline-rev "$base_ref" $args 2>&1 | tee /tmp/semver-output.txt |
| 81 | + local exit_code=${PIPESTATUS[0]} |
| 82 | + set -e |
| 83 | + |
| 84 | + # Strip ANSI escape codes from the captured output for the PR comment. |
| 85 | + sed 's/\x1b\[[0-9;]*m//g' /tmp/semver-output.txt |
| 86 | + return "$exit_code" |
| 87 | +} |
| 88 | + |
| 89 | +# ── comment ───────────────────────────────────────────────────────── |
| 90 | +cmd_comment() { |
| 91 | + local repo="${1:?Usage: changed_crates.sh comment <repo> <pr_number> <check_result> [logs]}" |
| 92 | + local pr_number="${2:?}" |
| 93 | + local check_result="${3:?}" |
| 94 | + local logs="${4:-}" |
| 95 | + |
| 96 | + # Find existing comment with our marker |
| 97 | + local comment_id |
| 98 | + comment_id=$(gh api "repos/${repo}/issues/${pr_number}/comments" \ |
| 99 | + --jq ".[] | select(.body | contains(\"${MARKER}\")) | .id" | head -1) |
| 100 | + |
| 101 | + if [ "$check_result" = "success" ]; then |
| 102 | + # Delete the comment if one exists |
| 103 | + if [ -n "$comment_id" ]; then |
| 104 | + gh api "repos/${repo}/issues/comments/${comment_id}" --method DELETE |
| 105 | + fi |
| 106 | + else |
| 107 | + local body="${MARKER} |
| 108 | +Thank you for opening this pull request! |
| 109 | +
|
| 110 | +Reviewer note: [cargo-semver-checks](https://github.com/obi1kenobi/cargo-semver-checks) reported the current version number is not SemVer-compatible with the changes made since the last release. |
| 111 | +
|
| 112 | +Details: |
| 113 | +
|
| 114 | +\`\`\` |
| 115 | +${logs} |
| 116 | +\`\`\`" |
| 117 | + |
| 118 | + if [ -n "$comment_id" ]; then |
| 119 | + gh api "repos/${repo}/issues/comments/${comment_id}" \ |
| 120 | + --method PATCH --field body="$body" |
| 121 | + else |
| 122 | + gh api "repos/${repo}/issues/${pr_number}/comments" \ |
| 123 | + --method POST --field body="$body" |
| 124 | + fi |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +# ── main ──────────────────────────────────────────────────────────── |
| 129 | +cmd="${1:?Usage: changed_crates.sh <changed-crates|semver-check|comment> [args...]}" |
| 130 | +shift |
| 131 | + |
| 132 | +case "$cmd" in |
| 133 | + changed-crates) cmd_changed_crates "$@" ;; |
| 134 | + semver-check) cmd_semver_check "$@" ;; |
| 135 | + comment) cmd_comment "$@" ;; |
| 136 | + *) echo "Unknown command: $cmd" >&2; exit 1 ;; |
| 137 | +esac |
0 commit comments