|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# Adds a "has:owner-approval" label to a PR if a reviewer who approved it is a component owner. |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +if [[ -z "${REPO:-}" || -z "${PR:-}" ]]; then |
| 22 | + echo "One or more of REPO and PR have not been set. Please ensure each is set." |
| 23 | + exit 0 |
| 24 | +fi |
| 25 | + |
| 26 | +main () { |
| 27 | + CUR_DIRECTORY=$(dirname "$0") |
| 28 | + |
| 29 | + # The latestReviews key returns the latest review for each reviewer cutting out any other reviews. |
| 30 | + JSON=$(gh pr view "${PR}" --json "files,author,latestReviews" | tr -dc '[:print:]' | sed -E 's/\\[a-z]//g') |
| 31 | + FILES=$(echo -n "${JSON}"| jq -r '.files[].path') |
| 32 | + LATEST_REVIEWS=$(echo -n "${JSON}" | jq -c '.latestReviews') |
| 33 | + |
| 34 | + # Fetch components |
| 35 | + COMPONENTS=$(bash "${CUR_DIRECTORY}/get-components.sh" | tac) # Reversed so we visit subdirectories first |
| 36 | + |
| 37 | + declare -A PROCESSED_COMPONENTS |
| 38 | + |
| 39 | + for COMPONENT in ${COMPONENTS}; do |
| 40 | + COMPONENT_OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh") |
| 41 | + |
| 42 | + for FILE in ${FILES}; do |
| 43 | + MATCH=$(echo -n "${FILE}" | grep -E "^${COMPONENT}" || true) |
| 44 | + |
| 45 | + if [[ -z "${MATCH}" ]]; then |
| 46 | + continue |
| 47 | + fi |
| 48 | + |
| 49 | + # If we match a file with a component, skip further processing for this file |
| 50 | + if [[ -v PROCESSED_COMPONENTS["${COMPONENT}"] ]]; then |
| 51 | + continue |
| 52 | + fi |
| 53 | + PROCESSED_COMPONENTS["${COMPONENT}"]=true |
| 54 | + |
| 55 | + # Check if updated file is owned by one of the reviewers" |
| 56 | + echo "${LATEST_REVIEWS}" | jq -c '.[]' | while IFS= read -r REVIEW; do |
| 57 | + REVIEW_AUTHOR=$(echo -n "${REVIEW}"| jq -r '.author.login') |
| 58 | + REVIEW_STATE=$(echo -n "${REVIEW}"| jq -r '.state') |
| 59 | + if [[ "${REVIEW_STATE}" == "APPROVED" ]]; then |
| 60 | + # Review is approved. Checking if reviewer is a component owner |
| 61 | + for OWNER in ${COMPONENT_OWNERS}; do |
| 62 | + if [[ "${REVIEW_AUTHOR}" == "${OWNER}" ]]; then |
| 63 | + echo "Reviewer $REVIEW_AUTHOR is a component owner. Adding 'has:owner-approval' label." |
| 64 | + gh pr edit "${PR}" --repo "${REPO}" --add-label "has:owner-approval" |
| 65 | + exit 0 |
| 66 | + fi |
| 67 | + done |
| 68 | + fi |
| 69 | + done |
| 70 | + done |
| 71 | + done |
| 72 | +} |
| 73 | + |
| 74 | +# Ensure the script does not block a PR even if it fails |
| 75 | +main || echo "Failed to run $0" |
0 commit comments