|
| 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 | +# Only published workspace members (those without `publish = false`) are |
| 25 | +# considered. |
| 26 | +# |
| 27 | +# semver-check <base_ref> <packages...> |
| 28 | +# Run cargo-semver-checks for the given packages against base_ref. |
| 29 | +# Output and exit code are passed through unchanged; the caller is |
| 30 | +# responsible for capturing/formatting them. |
| 31 | +# |
| 32 | +# comment <repo> <pr_number> <check_result> [logs] |
| 33 | +# Upsert or delete a sticky PR comment based on check_result. |
| 34 | +# check_result: "success" deletes any existing comment, |
| 35 | +# anything else upserts the comment with the provided logs. |
| 36 | +# Requires GH_TOKEN to be set. |
| 37 | + |
| 38 | +set -euo pipefail |
| 39 | + |
| 40 | +MARKER="<!-- semver-check-comment -->" |
| 41 | + |
| 42 | +# ── changed-crates ────────────────────────────────────────────────── |
| 43 | +cmd_changed_crates() { |
| 44 | + local base_ref="${1:?Usage: changed_crates.sh changed-crates <base_ref>}" |
| 45 | + |
| 46 | + # 1. Files changed between the PR and the base branch. |
| 47 | + local changed_files |
| 48 | + changed_files=$(git diff --name-only "${base_ref}...HEAD") |
| 49 | + |
| 50 | + # 2. Every publishable workspace member, one per line as |
| 51 | + # "<crate-name> <crate-dir>". `publish = false` in Cargo.toml shows |
| 52 | + # up as `"publish": []` in cargo metadata, so filtering on that |
| 53 | + # excludes internal crates without a manual exclusion list. |
| 54 | + local crates |
| 55 | + crates=$(cargo metadata --no-deps --format-version 1 | jq -r ' |
| 56 | + (.workspace_root + "/") as $root |
| 57 | + | .packages[] |
| 58 | + | select(.publish != []) |
| 59 | + | "\(.name) \(.manifest_path | ltrimstr($root) | rtrimstr("/Cargo.toml"))" |
| 60 | + ') |
| 61 | + |
| 62 | + # 3. Keep crates whose directory contains a changed file. |
| 63 | + while read -r name dir; do |
| 64 | + if grep -q "^${dir}/" <<<"$changed_files"; then |
| 65 | + echo "$name" |
| 66 | + fi |
| 67 | + done <<<"$crates" | xargs |
| 68 | +} |
| 69 | + |
| 70 | +# ── semver-check ──────────────────────────────────────────────────── |
| 71 | +cmd_semver_check() { |
| 72 | + local base_ref="${1:?Usage: changed_crates.sh semver-check <base_ref> <packages...>}" |
| 73 | + shift |
| 74 | + |
| 75 | + local args=() |
| 76 | + for pkg in "$@"; do |
| 77 | + args+=(--package "$pkg") |
| 78 | + done |
| 79 | + |
| 80 | + cargo semver-checks --baseline-rev "$base_ref" "${args[@]}" |
| 81 | +} |
| 82 | + |
| 83 | +# ── comment ───────────────────────────────────────────────────────── |
| 84 | +cmd_comment() { |
| 85 | + local repo="${1:?Usage: changed_crates.sh comment <repo> <pr_number> <check_result> [logs]}" |
| 86 | + local pr_number="${2:?}" |
| 87 | + local check_result="${3:?}" |
| 88 | + local logs="${4:-}" |
| 89 | + |
| 90 | + # Find existing comment with our marker |
| 91 | + local comment_id |
| 92 | + comment_id=$(gh api "repos/${repo}/issues/${pr_number}/comments" \ |
| 93 | + --jq ".[] | select(.body | contains(\"${MARKER}\")) | .id" | head -1) |
| 94 | + |
| 95 | + echo "existing breaking change comment id $comment_id" |
| 96 | + |
| 97 | + if [ "$check_result" = "success" ]; then |
| 98 | + # Delete the comment if one exists |
| 99 | + if [ -n "$comment_id" ]; then |
| 100 | + echo "result is success, so deleting breaking change comment" |
| 101 | + gh api "repos/${repo}/issues/comments/${comment_id}" --method DELETE |
| 102 | + else |
| 103 | + echo "result is success and no previous comment to delete" |
| 104 | + fi |
| 105 | + else |
| 106 | + local body="${MARKER} |
| 107 | +Thank you for opening this pull request! |
| 108 | +
|
| 109 | +Reviewer note: [cargo-semver-checks](https://github.com/obi1kenobi/cargo-semver-checks) reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch). |
| 110 | +
|
| 111 | +<details> |
| 112 | +<summary>Details</summary> |
| 113 | +
|
| 114 | +\`\`\` |
| 115 | +${logs} |
| 116 | +\`\`\` |
| 117 | +
|
| 118 | +</details>" |
| 119 | + |
| 120 | + if [ -n "$comment_id" ]; then |
| 121 | + echo "comment already exists, updating content" |
| 122 | + gh api "repos/${repo}/issues/comments/${comment_id}" \ |
| 123 | + --method PATCH --field body="$body" |
| 124 | + else |
| 125 | + echo "no comment with breaking changes, creating a new one" |
| 126 | + gh api "repos/${repo}/issues/${pr_number}/comments" \ |
| 127 | + --method POST --field body="$body" |
| 128 | + fi |
| 129 | + fi |
| 130 | +} |
| 131 | + |
| 132 | +# ── main ──────────────────────────────────────────────────────────── |
| 133 | +cmd="${1:?Usage: changed_crates.sh <changed-crates|semver-check|comment> [args...]}" |
| 134 | +shift |
| 135 | + |
| 136 | +case "$cmd" in |
| 137 | + changed-crates) cmd_changed_crates "$@" ;; |
| 138 | + semver-check) cmd_semver_check "$@" ;; |
| 139 | + comment) cmd_comment "$@" ;; |
| 140 | + *) echo "Unknown command: $cmd" >&2; exit 1 ;; |
| 141 | +esac |
0 commit comments