Skip to content

Commit c1682f2

Browse files
committed
Add Github action to add as:owner-approval label
1 parent 41d0d4b commit c1682f2

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

.github/workflows/pr-review.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: PR review
2+
# This workflow is triggered when a PR review is submitted. It checks if the review is approved and runs a script to add labels to the PR based on the review.
3+
4+
on:
5+
pull_request_target:
6+
types:
7+
- submitted
8+
9+
jobs:
10+
approved:
11+
if: github.event.review.state == 'approved'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Run update permissions
18+
run: chmod +x ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh
19+
20+
- name: Run add-labels-to-reviewed-pr.sh
21+
run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
ISSUE: ${{ github.event.issue.number }}
25+
COMMENT: ${{ github.event.comment.body }}
26+
SENDER: ${{ github.event.sender.login }}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright The OpenTelemetry Authors
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Adds a "has:owner-approval" label to a PR if a reviewer who approved it is a component owner.
7+
8+
set -euo pipefail
9+
10+
if [[ -z "${REPO:-}" || -z "${PR:-}" ]]; then
11+
echo "One or more of REPO and PR have not been set. Please ensure each is set."
12+
exit 0
13+
fi
14+
15+
main () {
16+
CUR_DIRECTORY=$(dirname "$0")
17+
18+
# The latestReviews key returns the latest review for each reviewer cutting out any other reviews.
19+
JSON=$(gh pr view "${PR}" --json "files,author,latestReviews" | tr -dc '[:print:]' | sed -E 's/\\[a-z]//g')
20+
AUTHOR=$(echo -n "${JSON}"| jq -r '.author.login')
21+
FILES=$(echo -n "${JSON}"| jq -r '.files[].path')
22+
LATEST_REVIEWS=$(echo -n "${JSON}" | jq -c '.latestReviews')
23+
24+
# Fetch components
25+
COMPONENTS=$(bash "${CUR_DIRECTORY}/get-components.sh" | tac) # Reversed so we visit subdirectories first
26+
27+
declare -A PROCESSED_COMPONENTS
28+
29+
for COMPONENT in ${COMPONENTS}; do
30+
COMPONENT_OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh")
31+
32+
for FILE in ${FILES}; do
33+
MATCH=$(echo -n "${FILE}" | grep -E "^${COMPONENT}" || true)
34+
35+
if [[ -z "${MATCH}" ]]; then
36+
continue
37+
fi
38+
39+
# If we match a file with a component, skip further processing for this file
40+
if [[ -v PROCESSED_COMPONENTS["${COMPONENT}"] ]]; then
41+
continue
42+
fi
43+
44+
PROCESSED_COMPONENTS["${COMPONENT}"]=true
45+
46+
# Check if updated file is owned by one of the reviewers"
47+
echo "${LATEST_REVIEWS}" | jq -c '.[]' | while IFS= read -r REVIEW; do
48+
REVIEW_AUTHOR=$(echo -n "${REVIEW}"| jq -r '.author.login')
49+
REVIEW_STATE=$(echo -n "${REVIEW}"| jq -r '.state')
50+
if [[ "${REVIEW_STATE}" == "APPROVED" ]]; then
51+
# Review is approved. Checking if reviewer is a component owner
52+
for OWNER in ${COMPONENT_OWNERS}; do
53+
if [[ "${REVIEW_AUTHOR}" == "${OWNER}" ]]; then
54+
echo "Reviewer $REVIEW_AUTHOR is a component owner. Adding 'has:owner-approval' label."
55+
gh pr edit "${PR}" --repo "${REPO}" --add-label "has:owner-approval"
56+
exit 0
57+
fi
58+
done
59+
fi
60+
done
61+
done
62+
done
63+
}
64+
65+
# Ensure the script does not block a PR even if it fails
66+
main || echo "Failed to run $0"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright The OpenTelemetry Authors
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Gets the owners for a given component from the component_owners.yml file.
7+
8+
# Define the file path
9+
YML_FILE=".github/component_owners.yml"
10+
11+
if [[ -z "${COMPONENT:-}" ]]; then
12+
echo "COMPONENT has not been set, please ensure it is set."
13+
exit 1
14+
fi
15+
16+
FOUND=0
17+
18+
# Parse the YAML file and extract owners for the given component
19+
while IFS= read -r line; do
20+
# Check if the line matches the given component
21+
if [[ "$line" =~ ^[[:space:]]*${COMPONENT}:[[:space:]]*$ ]]; then
22+
FOUND=1
23+
continue
24+
fi
25+
26+
# If the component is found, extract owners
27+
if [[ $FOUND -eq 1 ]]; then
28+
# Stop if we encounter another component or an empty line
29+
if [[ "$line" =~ ^[[:space:]]*[^#]+: || -z "$line" ]]; then
30+
break
31+
fi
32+
33+
# Extract the owner (remove leading spaces and '- ')
34+
if [[ "$line" =~ ^[[:space:]]*-[[:space:]]*[^#]+ ]]; then
35+
OWNER=$(echo "$line" | sed -E 's/^[[:space:]]*-[[:space:]]*([^#]+).*/\1/')
36+
echo "$OWNER"
37+
fi
38+
fi
39+
done < "$YML_FILE"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright The OpenTelemetry Authors
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Gets the components from the component_owners.yml file.
7+
8+
9+
# Define the file path
10+
YML_FILE=".github/component_owners.yml"
11+
12+
# Parse the YAML file and extract components and their owners
13+
while IFS= read -r line; do
14+
# Check if the line contains a component (ends with ':')
15+
if [[ "$line" =~ ^[[:space:]]*[^#]+: ]]; then
16+
# Extract the component name (remove leading spaces and trailing ':')
17+
COMPONENT=$(echo "$line" | sed -E 's/^[[:space:]]*([^:]+):.*/\1/')
18+
echo "$COMPONENT"
19+
fi
20+
done < "$YML_FILE"

0 commit comments

Comments
 (0)