Skip to content

Commit

Permalink
Label PRs that contain breaking changes based on the changelog (#1769)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
lmolkova and trask authored Jan 22, 2025
1 parent 68b0afa commit e2f8527
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/prepare-new-issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/prepare-new-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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' }}
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
39 changes: 39 additions & 0 deletions .github/workflows/scripts/prepare-new-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/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_CHANGELOG_PATH is required"
exit 1
fi

CHLOG="$(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}' "$PR_CHANGELOG_PATH/$CHLOG" | xargs)
echo $CHANGE_TYPE
gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true
else
echo "Found multiple changelog files. Ignoring this change."
fi

0 comments on commit e2f8527

Please sign in to comment.