diff --git a/.github/workflows/prepare-new-issue.yml b/.github/workflows/prepare-new-issue.yml index 345b7dcd1e..f17a144fca 100644 --- a/.github/workflows/prepare-new-issue.yml +++ b/.github/workflows/prepare-new-issue.yml @@ -10,9 +10,6 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Run update permissions - run: chmod +x ./.github/workflows/scripts/prepare-new-issue.sh - - name: Run prepare-new-issue.sh run: ./.github/workflows/scripts/prepare-new-issue.sh env: diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml new file mode 100644 index 0000000000..18205b43e4 --- /dev/null +++ b/.github/workflows/prepare-new-pr.yml @@ -0,0 +1,34 @@ +name: 'Prepare new PR' +on: + pull_request_target: + types: [opened, synchronize] + branches: [ 'main*' ] + paths: ['.chloggen/*'] + +jobs: + prepare-new-pr: + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + if: ${{ github.repository_owner == 'open-telemetry' }} + outputs: + change_type: ${{ steps.get-change-type.outputs.change_type }} + steps: + # check out main + - uses: actions/checkout@v4 + # sparse checkout to only get the .chloggen directory + - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + path: prchangelog + sparse-checkout: .chloggen + + # we're going to run prepare-new-pr script from the main branch + # to parse changelog record from the PR branch. + - name: Run prepare-new-pr.sh + run: ./.github/workflows/scripts/prepare-new-pr.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR: ${{ github.event.pull_request.number }} + PR_CHANGELOG_PATH: prchangelog diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh new file mode 100755 index 0000000000..01e8175c53 --- /dev/null +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +# +# This script uses chloggen file to get the change_type and adds it as a label to the PR. +# If there are none or multiple changelog files, it will ignore the PR. +# +# Notes: +# - label should exist in the repository in order to add it to the PR. +# - if label already exist, this is a no-op. +# - if any error happens, the script quietly exits with 0. + +if [ -z ${PR:-} ]; then + echo "PR number is required" + exit 1 +fi + +if [ -z ${PR_CHANGELOG_PATH:-} ]; then + echo "PR number is required" + exit 1 +fi + +CHLOG="$PR_CHANGELOG_PATH/$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" +echo "Change log file(s): ${CHLOG}" + +if [ -z "$CHLOG" ]; then + echo "No changelog found in the PR. Ignoring this change." + exit 0 +fi + +COUNT="$(echo "$CHLOG" | wc -l)" +if [ $COUNT -eq 1 ]; then + CHANGE_TYPE=$(awk -F': ' '/^change_type:/ {print $2}' "$CHLOG" | xargs) + echo $CHANGE_TYPE + gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true +else + echo "Found multiple changelog files. Ignoring this change." +fi + +exit 0 \ No newline at end of file