From c1682f21abdf213af40a4f15dcb65f0123e3f799 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 22 Apr 2025 17:14:15 -0700 Subject: [PATCH 1/5] Add Github action to add as:owner-approval label --- .github/workflows/pr-review.yml | 26 ++++++++ .../scripts/add-labels-to-reviewed-pr.sh | 66 +++++++++++++++++++ .github/workflows/scripts/get-codeowners.sh | 39 +++++++++++ .github/workflows/scripts/get-components.sh | 20 ++++++ 4 files changed, 151 insertions(+) create mode 100644 .github/workflows/pr-review.yml create mode 100644 .github/workflows/scripts/add-labels-to-reviewed-pr.sh create mode 100644 .github/workflows/scripts/get-codeowners.sh create mode 100644 .github/workflows/scripts/get-components.sh diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml new file mode 100644 index 0000000000..29e0d58f78 --- /dev/null +++ b/.github/workflows/pr-review.yml @@ -0,0 +1,26 @@ +name: PR review +# 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. + +on: + pull_request_target: + types: + - submitted + +jobs: + approved: + if: github.event.review.state == 'approved' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Run update permissions + run: chmod +x ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh + + - name: Run add-labels-to-reviewed-pr.sh + run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + ISSUE: ${{ github.event.issue.number }} + COMMENT: ${{ github.event.comment.body }} + SENDER: ${{ github.event.sender.login }} \ No newline at end of file diff --git a/.github/workflows/scripts/add-labels-to-reviewed-pr.sh b/.github/workflows/scripts/add-labels-to-reviewed-pr.sh new file mode 100644 index 0000000000..9fab7ab071 --- /dev/null +++ b/.github/workflows/scripts/add-labels-to-reviewed-pr.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +# +# Adds a "has:owner-approval" label to a PR if a reviewer who approved it is a component owner. + +set -euo pipefail + +if [[ -z "${REPO:-}" || -z "${PR:-}" ]]; then + echo "One or more of REPO and PR have not been set. Please ensure each is set." + exit 0 +fi + +main () { + CUR_DIRECTORY=$(dirname "$0") + + # The latestReviews key returns the latest review for each reviewer cutting out any other reviews. + JSON=$(gh pr view "${PR}" --json "files,author,latestReviews" | tr -dc '[:print:]' | sed -E 's/\\[a-z]//g') + AUTHOR=$(echo -n "${JSON}"| jq -r '.author.login') + FILES=$(echo -n "${JSON}"| jq -r '.files[].path') + LATEST_REVIEWS=$(echo -n "${JSON}" | jq -c '.latestReviews') + + # Fetch components + COMPONENTS=$(bash "${CUR_DIRECTORY}/get-components.sh" | tac) # Reversed so we visit subdirectories first + + declare -A PROCESSED_COMPONENTS + + for COMPONENT in ${COMPONENTS}; do + COMPONENT_OWNERS=$(COMPONENT="${COMPONENT}" bash "${CUR_DIRECTORY}/get-codeowners.sh") + + for FILE in ${FILES}; do + MATCH=$(echo -n "${FILE}" | grep -E "^${COMPONENT}" || true) + + if [[ -z "${MATCH}" ]]; then + continue + fi + + # If we match a file with a component, skip further processing for this file + if [[ -v PROCESSED_COMPONENTS["${COMPONENT}"] ]]; then + continue + fi + + PROCESSED_COMPONENTS["${COMPONENT}"]=true + + # Check if updated file is owned by one of the reviewers" + echo "${LATEST_REVIEWS}" | jq -c '.[]' | while IFS= read -r REVIEW; do + REVIEW_AUTHOR=$(echo -n "${REVIEW}"| jq -r '.author.login') + REVIEW_STATE=$(echo -n "${REVIEW}"| jq -r '.state') + if [[ "${REVIEW_STATE}" == "APPROVED" ]]; then + # Review is approved. Checking if reviewer is a component owner + for OWNER in ${COMPONENT_OWNERS}; do + if [[ "${REVIEW_AUTHOR}" == "${OWNER}" ]]; then + echo "Reviewer $REVIEW_AUTHOR is a component owner. Adding 'has:owner-approval' label." + gh pr edit "${PR}" --repo "${REPO}" --add-label "has:owner-approval" + exit 0 + fi + done + fi + done + done + done +} + +# Ensure the script does not block a PR even if it fails +main || echo "Failed to run $0" \ No newline at end of file diff --git a/.github/workflows/scripts/get-codeowners.sh b/.github/workflows/scripts/get-codeowners.sh new file mode 100644 index 0000000000..f35c4a9fb3 --- /dev/null +++ b/.github/workflows/scripts/get-codeowners.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +# +# Gets the owners for a given component from the component_owners.yml file. + +# Define the file path +YML_FILE=".github/component_owners.yml" + +if [[ -z "${COMPONENT:-}" ]]; then + echo "COMPONENT has not been set, please ensure it is set." + exit 1 +fi + +FOUND=0 + +# Parse the YAML file and extract owners for the given component +while IFS= read -r line; do + # Check if the line matches the given component + if [[ "$line" =~ ^[[:space:]]*${COMPONENT}:[[:space:]]*$ ]]; then + FOUND=1 + continue + fi + + # If the component is found, extract owners + if [[ $FOUND -eq 1 ]]; then + # Stop if we encounter another component or an empty line + if [[ "$line" =~ ^[[:space:]]*[^#]+: || -z "$line" ]]; then + break + fi + + # Extract the owner (remove leading spaces and '- ') + if [[ "$line" =~ ^[[:space:]]*-[[:space:]]*[^#]+ ]]; then + OWNER=$(echo "$line" | sed -E 's/^[[:space:]]*-[[:space:]]*([^#]+).*/\1/') + echo "$OWNER" + fi + fi +done < "$YML_FILE" diff --git a/.github/workflows/scripts/get-components.sh b/.github/workflows/scripts/get-components.sh new file mode 100644 index 0000000000..583473ee75 --- /dev/null +++ b/.github/workflows/scripts/get-components.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +# +# Gets the components from the component_owners.yml file. + + +# Define the file path +YML_FILE=".github/component_owners.yml" + +# Parse the YAML file and extract components and their owners +while IFS= read -r line; do + # Check if the line contains a component (ends with ':') + if [[ "$line" =~ ^[[:space:]]*[^#]+: ]]; then + # Extract the component name (remove leading spaces and trailing ':') + COMPONENT=$(echo "$line" | sed -E 's/^[[:space:]]*([^:]+):.*/\1/') + echo "$COMPONENT" + fi +done < "$YML_FILE" \ No newline at end of file From d2eaa31fb15121ec23f4a53894875a0e84783d14 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Wed, 23 Apr 2025 14:03:28 -0700 Subject: [PATCH 2/5] Format --- .github/workflows/pr-review.yml | 10 ++++++---- .../scripts/add-labels-to-reviewed-pr.sh | 17 +++++++++++++---- .github/workflows/scripts/get-codeowners.sh | 13 ++++++++++++- .github/workflows/scripts/get-components.sh | 15 +++++++++++++-- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index 29e0d58f78..0fcc830490 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -6,8 +6,10 @@ on: types: - submitted +permissions: read-all + jobs: - approved: + add-owner-approved-label: if: github.event.review.state == 'approved' runs-on: ubuntu-latest steps: @@ -21,6 +23,6 @@ jobs: run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - ISSUE: ${{ github.event.issue.number }} - COMMENT: ${{ github.event.comment.body }} - SENDER: ${{ github.event.sender.login }} \ No newline at end of file + REPO: ${{ github.repository }} + PR: ${{ github.event.number }} + \ No newline at end of file diff --git a/.github/workflows/scripts/add-labels-to-reviewed-pr.sh b/.github/workflows/scripts/add-labels-to-reviewed-pr.sh index 9fab7ab071..c06607fe6f 100644 --- a/.github/workflows/scripts/add-labels-to-reviewed-pr.sh +++ b/.github/workflows/scripts/add-labels-to-reviewed-pr.sh @@ -1,7 +1,18 @@ #!/usr/bin/env bash # # Copyright The OpenTelemetry Authors -# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. # # Adds a "has:owner-approval" label to a PR if a reviewer who approved it is a component owner. @@ -17,7 +28,6 @@ main () { # The latestReviews key returns the latest review for each reviewer cutting out any other reviews. JSON=$(gh pr view "${PR}" --json "files,author,latestReviews" | tr -dc '[:print:]' | sed -E 's/\\[a-z]//g') - AUTHOR=$(echo -n "${JSON}"| jq -r '.author.login') FILES=$(echo -n "${JSON}"| jq -r '.files[].path') LATEST_REVIEWS=$(echo -n "${JSON}" | jq -c '.latestReviews') @@ -40,7 +50,6 @@ main () { if [[ -v PROCESSED_COMPONENTS["${COMPONENT}"] ]]; then continue fi - PROCESSED_COMPONENTS["${COMPONENT}"]=true # Check if updated file is owned by one of the reviewers" @@ -63,4 +72,4 @@ main () { } # Ensure the script does not block a PR even if it fails -main || echo "Failed to run $0" \ No newline at end of file +main || echo "Failed to run $0" diff --git a/.github/workflows/scripts/get-codeowners.sh b/.github/workflows/scripts/get-codeowners.sh index f35c4a9fb3..03ce00bc91 100644 --- a/.github/workflows/scripts/get-codeowners.sh +++ b/.github/workflows/scripts/get-codeowners.sh @@ -1,7 +1,18 @@ #!/usr/bin/env bash # # Copyright The OpenTelemetry Authors -# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. # # Gets the owners for a given component from the component_owners.yml file. diff --git a/.github/workflows/scripts/get-components.sh b/.github/workflows/scripts/get-components.sh index 583473ee75..57eda9eb0b 100644 --- a/.github/workflows/scripts/get-components.sh +++ b/.github/workflows/scripts/get-components.sh @@ -1,7 +1,18 @@ #!/usr/bin/env bash # # Copyright The OpenTelemetry Authors -# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. # # Gets the components from the component_owners.yml file. @@ -17,4 +28,4 @@ while IFS= read -r line; do COMPONENT=$(echo "$line" | sed -E 's/^[[:space:]]*([^:]+):.*/\1/') echo "$COMPONENT" fi -done < "$YML_FILE" \ No newline at end of file +done < "$YML_FILE" From 6a65a905b9cd0a5e17cbbec266bcfeaaaf068560 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Wed, 23 Apr 2025 15:07:40 -0700 Subject: [PATCH 3/5] Test --- .github/workflows/pr-review copy.yml | 24 +++++++++++++++++++ .github/workflows/pr-review.yml | 7 +----- .../src/index.ts | 4 +++- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/pr-review copy.yml diff --git a/.github/workflows/pr-review copy.yml b/.github/workflows/pr-review copy.yml new file mode 100644 index 0000000000..7b65c638dc --- /dev/null +++ b/.github/workflows/pr-review copy.yml @@ -0,0 +1,24 @@ +name: PR review Copy for test +# 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. + +on: + pull_request_target: + types: + - opened + - edited + - synchronize + +jobs: + add-owner-approved-label: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Run add-labels-to-reviewed-pr.sh + run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + PR: ${{ github.event.number }} + \ No newline at end of file diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index 0fcc830490..ab18432cf7 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -2,12 +2,10 @@ name: PR review # 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. on: - pull_request_target: + pull_request_review: types: - submitted -permissions: read-all - jobs: add-owner-approved-label: if: github.event.review.state == 'approved' @@ -16,9 +14,6 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: Run update permissions - run: chmod +x ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh - - name: Run add-labels-to-reviewed-pr.sh run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh env: diff --git a/detectors/node/opentelemetry-resource-detector-azure/src/index.ts b/detectors/node/opentelemetry-resource-detector-azure/src/index.ts index c81d93e0a2..0b3f1e7537 100644 --- a/detectors/node/opentelemetry-resource-detector-azure/src/index.ts +++ b/detectors/node/opentelemetry-resource-detector-azure/src/index.ts @@ -11,9 +11,11 @@ * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and - * limitations under the License. + * limitations under the License. */ +// UNDO, Added for testing purposes + export { azureAppServiceDetector, azureFunctionsDetector, From 896a41c66e18001489700b4fd7d573bc0c4824ee Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Wed, 23 Apr 2025 15:10:57 -0700 Subject: [PATCH 4/5] Test --- .github/workflows/pr-review copy.yml | 24 ------------------------ .github/workflows/pr-title.yml | 8 +++++++- 2 files changed, 7 insertions(+), 25 deletions(-) delete mode 100644 .github/workflows/pr-review copy.yml diff --git a/.github/workflows/pr-review copy.yml b/.github/workflows/pr-review copy.yml deleted file mode 100644 index 7b65c638dc..0000000000 --- a/.github/workflows/pr-review copy.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: PR review Copy for test -# 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. - -on: - pull_request_target: - types: - - opened - - edited - - synchronize - -jobs: - add-owner-approved-label: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Run add-labels-to-reviewed-pr.sh - run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REPO: ${{ github.repository }} - PR: ${{ github.event.number }} - \ No newline at end of file diff --git a/.github/workflows/pr-title.yml b/.github/workflows/pr-title.yml index 4298282b9c..c8f40d8a02 100644 --- a/.github/workflows/pr-title.yml +++ b/.github/workflows/pr-title.yml @@ -12,6 +12,12 @@ jobs: name: Validate PR title runs-on: ubuntu-latest steps: - - uses: amannn/action-semantic-pull-request@v5 + - name: Checkout + uses: actions/checkout@v4 + + - name: Run add-labels-to-reviewed-pr.sh + run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + PR: ${{ github.event.number }} From 5fa39baf59a0dd3ac1a6179f75227b3a7d977c65 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Wed, 23 Apr 2025 15:14:18 -0700 Subject: [PATCH 5/5] Undo change in azure detector --- .github/workflows/pr-review.yml | 1 - .github/workflows/pr-title.yml | 8 +------- .../opentelemetry-resource-detector-azure/src/index.ts | 4 +--- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index ab18432cf7..0293ce197c 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -1,6 +1,5 @@ name: PR review # 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. - on: pull_request_review: types: diff --git a/.github/workflows/pr-title.yml b/.github/workflows/pr-title.yml index c8f40d8a02..4298282b9c 100644 --- a/.github/workflows/pr-title.yml +++ b/.github/workflows/pr-title.yml @@ -12,12 +12,6 @@ jobs: name: Validate PR title runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Run add-labels-to-reviewed-pr.sh - run: ./.github/workflows/scripts/add-labels-to-reviewed-pr.sh + - uses: amannn/action-semantic-pull-request@v5 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REPO: ${{ github.repository }} - PR: ${{ github.event.number }} diff --git a/detectors/node/opentelemetry-resource-detector-azure/src/index.ts b/detectors/node/opentelemetry-resource-detector-azure/src/index.ts index 0b3f1e7537..c81d93e0a2 100644 --- a/detectors/node/opentelemetry-resource-detector-azure/src/index.ts +++ b/detectors/node/opentelemetry-resource-detector-azure/src/index.ts @@ -11,11 +11,9 @@ * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and - * limitations under the License. + * limitations under the License. */ -// UNDO, Added for testing purposes - export { azureAppServiceDetector, azureFunctionsDetector,