| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# Copyright The OpenTelemetry Authors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
# |
| 6 |
# Adds code owners without write access as reviewers on a PR. Note that |
| 7 |
# the code owners must still be a member of the `open-telemetry` |
| 8 |
# organization. |
| 9 |
# |
| 10 |
# Note that since this script is considered a requirement for PRs, |
| 11 |
# it should never fail. |
| 12 |
|
| 13 |
set -euo pipefail |
| 14 |
|
| 15 |
if [[ -z "${REPO:-}" || -z "${PR:-}" ]]; then |
| 16 |
echo "One or more of REPO and PR have not been set, please ensure each is set." |
| 17 |
exit 0 |
| 18 |
fi |
| 19 |
|
| 20 |
main () { |
| 21 |
CUR_DIRECTORY=$(dirname "$0") |
| 22 |
# Reviews may have comments that need to be cleaned up for jq, |
| 23 |
# so restrict output to only printable characters and ensure escape |
| 24 |
# sequences are removed. |
| 25 |
# The latestReviews key only returns the latest review for each reviewer, |
| 26 |
# cutting out any other reviews. We use that instead of requestedReviews |
| 27 |
# since we need to get the list of users eligible for requesting another |
| 28 |
# review. The GitHub CLI does not offer a list of all reviewers, which |
| 29 |
# is only available through the API. To cut down on API calls to GitHub, |
| 30 |
# we use the latest reviews to determine which users to filter out. |
| 31 |
JSON=$(gh pr view "${PR}" --json "files,author,latestReviews" | LC_ALL=C tr -dc '[:print:]' | sed -E 's/\\[a-z]//g') |
| 32 |
AUTHOR=$(echo -n "${JSON}"| jq -r '.author.login') |
| 33 |
FILES=$(echo -n "${JSON}"| jq -r '.files[].path') |
| 34 |
REVIEW_LOGINS=$(echo -n "${JSON}"| jq -r '.latestReviews[].author.login') |
| 35 |
COMPONENTS=$(bash "${CUR_DIRECTORY}/get-components.sh") |
| 36 |
REVIEWERS="" |
| 37 |
LABELS="" |
| 38 |
declare -A PROCESSED_COMPONENTS |
| 39 |
declare -A REVIEWED |
| 40 |
|
| 41 |
for REVIEWER in ${REVIEW_LOGINS}; do |
| 42 |
# GitHub adds "app/" in front of user logins. The API docs don't make |
| 43 |
# it clear what this means or whether it will always be present. The |
| 44 |
# '/' character isn't a valid character for usernames, so this won't |
| 45 |
# replace characters within a username. |
| 46 |
REVIEWED["@${REVIEWER//app\//}"]=true |
| 47 |
done |
| 48 |
|
| 49 |
if [[ -v REVIEWED[@] ]]; then |
| 50 |
echo "Users that have already reviewed this PR and will not have another review requested:" "${!REVIEWED[@]}" |
| 51 |
else |
| 52 |
echo "This PR has not yet been reviewed, all code owners are eligible for a review request" |
| 53 |
fi |
| 54 |
|
| 55 |
RISKY_REGEX=' |
| 56 |
^.github/workflows/prepare-release.yml$ |
| 57 |
|^.github/workflows/scripts/release-prepare-release.sh$ |
| 58 |
|^Makefile$ |
| 59 |
|^Makefile.Common$ |
| 60 |
' |
| 61 |
RISKY_REGEX="$(echo "$RISKY_REGEX" | tr -d ' \n')" |
| 62 |
RISKY_FILES="$(echo "$FILES" | grep -E "$RISKY_REGEX")" |
| 63 |
if [[ -n "${RISKY_FILES}" ]]; then |
| 64 |
echo "This PR may affect the release process, as it touches the following files:" \ |
| 65 |
"$(echo "$RISKY_FILES" | sed -E 's/\n/, /')" |
| 66 |
LABELS="release:risky-change" |
| 67 |
else |
| 68 |
echo "This PR does not have release-affecting changes." |
| 69 |
fi |
| 70 |
|
| 71 |
for COMPONENT in ${COMPONENTS}; do |
| 72 |
# Files will be in alphabetical order and there are many files to |
| 73 |
# a component, so loop through files in an inner loop. This allows |
| 74 |
# us to remove all files for a component from the list so they |
| 75 |
# won't be checked against the remaining components in the components |
| 76 |
# list. This provides a meaningful speedup in practice. |
| 77 |
for FILE in ${FILES}; do |
| 78 |
MATCH=$(echo -n "${FILE}" | grep -E "^${COMPONENT}" || true) |
| 79 |
|
| 80 |
if [[ -z "${MATCH}" ]]; then |
| 81 |
continue |
| 82 |
fi |
| 83 |
|
| 84 |
# If we match a file with a component we don't need to process the file again. |
| 85 |
FILES=$(echo -n "${FILES}" | grep -v "${FILE}") |
| 86 |
|
| 87 |
if [[ -v PROCESSED_COMPONENTS["${COMPONENT}"] ]]; then |
| 88 |
continue |
| 89 |
fi |
| 90 |
|
| 91 |
PROCESSED_COMPONENTS["${COMPONENT}"]=true |
| 92 |
|
| 93 |
OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh") |
| 94 |
|
| 95 |
for OWNER in ${OWNERS}; do |
| 96 |
# Users that leave reviews are removed from the "requested reviewers" |
| 97 |
# list and are eligible to have another review requested. We only want |
| 98 |
# to request a review once, so remove them from the list. |
| 99 |
if [[ -v REVIEWED["${OWNER}"] || "${OWNER}" = "@${AUTHOR}" ]]; then |
| 100 |
continue |
| 101 |
fi |
| 102 |
|
| 103 |
if [[ -n "${REVIEWERS}" ]]; then |
| 104 |
REVIEWERS+="," |
| 105 |
fi |
| 106 |
REVIEWERS+=$(echo -n "${OWNER}" | sed -E 's/@(.+)/"\1"/') |
| 107 |
done |
| 108 |
|
| 109 |
# Convert the CODEOWNERS entry to a label |
| 110 |
COMPONENT_NAME=$(echo -n "${COMPONENT}" | sed -E 's%^(.+)/(.+)\1%\1/\2%') |
| 111 |
|
| 112 |
if (( "${#COMPONENT_NAME}" > 50 )); then |
| 113 |
echo "'${COMPONENT_NAME}' exceeds GitHub's 50-character limit on labels, skipping adding label" |
| 114 |
continue |
| 115 |
fi |
| 116 |
|
| 117 |
if [[ -n "${LABELS}" ]]; then |
| 118 |
LABELS+="," |
| 119 |
fi |
| 120 |
LABELS+="${COMPONENT_NAME}" |
| 121 |
done |
| 122 |
done |
| 123 |
|
| 124 |
if [[ -n "${LABELS}" ]]; then |
| 125 |
echo "Adding labels: ${LABELS}" |
| 126 |
gh pr edit "${PR}" --add-label "${LABELS}" || echo "Failed to add labels" |
| 127 |
else |
| 128 |
echo "No labels found" |
| 129 |
fi |
| 130 |
|
| 131 |
# Note that adding the labels above will not trigger any other workflows to |
| 132 |
# add code owners, so we have to do it here. |
| 133 |
# |
| 134 |
# We have to use the GitHub API directly due to an issue with how the CLI |
| 135 |
# handles PR updates that causes it require access to organization teams, |
| 136 |
# and the GitHub token doesn't provide that permission. |
| 137 |
# For more: https://github.com/cli/cli/issues/4844 |
| 138 |
# |
| 139 |
# The GitHub API validates that authors are not requested to review, but |
| 140 |
# accepts duplicate logins and logins that are already reviewers. |
| 141 |
if [[ -n "${REVIEWERS}" ]]; then |
| 142 |
echo "Requesting review from ${REVIEWERS}" |
| 143 |
curl \ |
| 144 |
-X POST \ |
| 145 |
-H "Accept: application/vnd.github+json" \ |
| 146 |
-H "Authorization: Bearer ${GITHUB_TOKEN}" \ |
| 147 |
"https://api.github.com/repos/${REPO}/pulls/${PR}/requested_reviewers" \ |
| 148 |
-d "{\"reviewers\":[${REVIEWERS}]}" \ |
| 149 |
| jq ".message" \ |
| 150 |
|| echo "jq was unable to parse GitHub's response" |
| 151 |
else |
| 152 |
echo "No code owners found" |
| 153 |
fi |
| 154 |
} |
| 155 |
|
| 156 |
# We don't want this workflow to ever fail and block a PR, |
| 157 |
# so ensure all errors are caught. |
| 158 |
main || echo "Failed to run $0" |