| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# Copyright The OpenTelemetry Authors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
# |
| 6 |
|
| 7 |
set -euo pipefail |
| 8 |
|
| 9 |
if [[ -z "${REPO:-}" || -z "${AUTHOR:-}" || -z "${COMPONENT:-}" || -z "${PR:-}" ]]; then |
| 10 |
echo "At least one of REPO, AUTHOR, COMPONENT, or PR has not been set, please ensure each is set." |
| 11 |
exit 0 |
| 12 |
fi |
| 13 |
|
| 14 |
CUR_DIRECTORY=$(dirname "$0") |
| 15 |
|
| 16 |
main() { |
| 17 |
OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh") |
| 18 |
REVIEWERS="" |
| 19 |
|
| 20 |
if [[ -z "${OWNERS}" ]]; then |
| 21 |
exit 0 |
| 22 |
fi |
| 23 |
|
| 24 |
for OWNER in ${OWNERS}; do |
| 25 |
if [[ "${OWNER}" = "@${AUTHOR}" ]]; then |
| 26 |
continue |
| 27 |
fi |
| 28 |
|
| 29 |
if [[ -n "${REVIEWERS}" ]]; then |
| 30 |
REVIEWERS+="," |
| 31 |
fi |
| 32 |
REVIEWERS+=$(echo "${OWNER}" | sed -E 's/@(.+)/"\1"/') |
| 33 |
done |
| 34 |
|
| 35 |
# We have to use the GitHub API directly due to an issue with how the CLI |
| 36 |
# handles PR updates that causes it require access to organization teams, |
| 37 |
# and the GitHub token doesn't provide that permission. |
| 38 |
# For more: https://github.com/cli/cli/issues/4844 |
| 39 |
# |
| 40 |
# The GitHub API validates that authors are not requested to review, but |
| 41 |
# accepts duplicate logins and logins that are already reviewers. |
| 42 |
echo "Requesting review from code owners: ${REVIEWERS}" |
| 43 |
curl \ |
| 44 |
-X POST \ |
| 45 |
-H "Accept: application/vnd.github+json" \ |
| 46 |
-H "Authorization: Bearer ${GITHUB_TOKEN}" \ |
| 47 |
"https://api.github.com/repos/${REPO}/pulls/${PR}/requested_reviewers" \ |
| 48 |
-d "{\"reviewers\":[${REVIEWERS}]}" \ |
| 49 |
| jq ".message" \ |
| 50 |
|| echo "jq was unable to parse GitHub's response" |
| 51 |
} |
| 52 |
|
| 53 |
# We don't want this workflow to ever fail and block a PR, |
| 54 |
# so ensure all errors are caught. |
| 55 |
main || echo "Failed to run $0" |