-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Label PRs with change type according to changelog #1769
Open
lmolkova
wants to merge
10
commits into
open-telemetry:main
Choose a base branch
from
lmolkova:pr-labels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−3
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9024d13
label PRs with change type according to changelog
lmolkova 330ae8a
up
lmolkova 9d11eea
up
lmolkova a035046
remove unnecessary things
lmolkova 3cce94c
pull_request -> pull_request_target
lmolkova d68b60d
only run on main
lmolkova bd485e0
don't modify contents
lmolkova a158f71
Update .github/workflows/prepare-new-pr.yml
lmolkova f36454a
break down into two jobs
lmolkova ef06314
debug
lmolkova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
name: 'Prepare new PR' | ||
on: | ||
pull_request_target: | ||
types: [opened, synchronize] | ||
branches: [ 'main*' ] | ||
paths: ['.chloggen/*'] | ||
|
||
jobs: | ||
get-change-details: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
if: ${{ github.repository_owner == 'open-telemetry' }} | ||
outputs: | ||
change_type: ${{ steps.get-change-type.outputs.change_type }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.head_ref }} | ||
- id: get-change-type | ||
name: Run get-change-type.sh | ||
run: | | ||
set +e | ||
change_type=$(./.github/workflows/scripts/get-change-type.sh) | ||
if [ $? -ne 0 ]; then | ||
change_type="" | ||
fi | ||
echo "change_type=$change_type" >> $GITHUB_OUTPUT | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
PR: ${{ github.event.pull_request.number }} | ||
|
||
# get-change-details job needs to check out PR code to read changelog, but it only needs read permissions. | ||
# prepare-new-pr job needs write permissions to set labels, but it runs on the main branch. | ||
# this way we don't have write permissions while running PR code. | ||
prepare-new-pr: | ||
runs-on: ubuntu-latest | ||
needs: get-change-details | ||
permissions: | ||
pull-requests: write | ||
if: | | ||
needs.get-change-details.outputs.change_type != '' && | ||
needs.get-change-details.outputs.change_type != null && | ||
github.repository_owner == 'open-telemetry' | ||
steps: | ||
- uses: actions/checkout@v4 | ||
lmolkova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- name: Set label with change type on the PR | ||
run: gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
PR: ${{ github.event.pull_request.number }} | ||
CHANGE_TYPE: ${{ needs.get-change-details.outputs.change_type }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Copyright The OpenTelemetry Authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
# This script gets change type from chloggen file. | ||
# If there are none or multiple changelog files, it will return 1. | ||
|
||
if [ -z ${PR:-} ]; then | ||
echo "PR number 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 1 | ||
fi | ||
|
||
COUNT="$(echo "$CHLOG" | wc -l)" | ||
if [ $COUNT -eq 1 ]; then | ||
CHANGE_TYPE=$(awk -F': ' '/^change_type:/ {print $2}' $CHLOG | xargs) | ||
echo $CHANGE_TYPE | ||
exit 0 | ||
else | ||
echo "Found multiple changelog files. Ignoring this change." | ||
exit 1 | ||
fi |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is in the danger zone because it's running this script from the PR branch under elevated permissions
I'm not sure it's vulnerable b/c this part isn't passing any(noticed it is passingenv
vars, but it's probably an area better to avoidGITHUB_TOKEN
to the script)I think a good way to mitigate this risk would be to checkout main, then checkout the PR branch in a subdir, e.g.
then it's clearer we're only using the files from the prbranch, and avoids temptation to execute any scripts in it
I haven't used it before, but it looks like could even add a
sparse-checkout: .chloggen
to the checkout action to make it even clearer that we're using those files from the PR branchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, didn't see your most recent updates when I left this, looking...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is still giving
GITHUB_TOKEN
to the script in the PR branch