From 9024d1384b068746b3e744c9ddaa2f9d02c2bd83 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 12:59:18 -0800 Subject: [PATCH 01/15] label PRs with change type according to changelog --- .github/workflows/prepare-new-pr.yml | 20 +++++++++++++ .github/workflows/scripts/prepare-new-pr.sh | 32 +++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .github/workflows/prepare-new-pr.yml create mode 100644 .github/workflows/scripts/prepare-new-pr.sh diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml new file mode 100644 index 0000000000..6bb15aa3f9 --- /dev/null +++ b/.github/workflows/prepare-new-pr.yml @@ -0,0 +1,20 @@ +name: 'Prepare new PR' +on: + pull_request: + types: [opened, ready_for_review] + +jobs: + prepare-new-pr: + runs-on: ubuntu-latest + # if: ${{ github.repository_owner == 'open-telemetry' }} + steps: + - uses: actions/checkout@v4 + + - name: Run update permissions + run: chmod +x ./.github/workflows/scripts/prepare-new-pr.sh + + - 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 }} diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh new file mode 100644 index 0000000000..fb542dd2bc --- /dev/null +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +# +# + +if [ -z ${PR:-} ]; then + echo "PR number is required" + exit 1 +fi + +# set -o xtrace + +CHLOG="$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" +echo "Change log file: ${CHLOG}" + +COUNT="$(echo "$CHLOG" | wc -l)" +if [ -z "$CHLOG" ]; then + echo "No changelog found in the PR. Ignoring this change." + exit 0 +fi + +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 changelogs - $CHLOG. Ignoring this change." +fi + +exit 0 From 330ae8a7438d4df41cf374e916045ff141fd0475 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 13:33:59 -0800 Subject: [PATCH 02/15] up --- .github/workflows/prepare-new-pr.yml | 4 +++- .github/workflows/scripts/prepare-new-pr.sh | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 6bb15aa3f9..d5edf5cb17 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -6,7 +6,9 @@ on: jobs: prepare-new-pr: runs-on: ubuntu-latest - # if: ${{ github.repository_owner == 'open-telemetry' }} + permissions: + pull-requests: write + if: ${{ github.repository_owner == 'open-telemetry' }} steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh index fb542dd2bc..aa350cfb55 100644 --- a/.github/workflows/scripts/prepare-new-pr.sh +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -3,15 +3,13 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 # -# +# This script uses chloggen file to get the change_type and add it as a label to the PR. if [ -z ${PR:-} ]; then echo "PR number is required" exit 1 fi -# set -o xtrace - CHLOG="$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" echo "Change log file: ${CHLOG}" @@ -26,7 +24,7 @@ if [ $COUNT -eq 1 ]; then echo $CHANGE_TYPE gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true else - echo "Found multiple changelogs - $CHLOG. Ignoring this change." + echo "Found multiple changelogs= files - $CHLOG. Ignoring this change." fi exit 0 From 9d11eea46f986435ef79f3e1616ee5eb9d50a3af Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 13:45:22 -0800 Subject: [PATCH 03/15] up --- .github/workflows/scripts/prepare-new-pr.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh index aa350cfb55..7fde34cc0c 100644 --- a/.github/workflows/scripts/prepare-new-pr.sh +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -3,7 +3,13 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 # -# This script uses chloggen file to get the change_type and add it as a label to the PR. +# 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" @@ -11,20 +17,20 @@ if [ -z ${PR:-} ]; then fi CHLOG="$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" -echo "Change log file: ${CHLOG}" +echo "Change log file(s): ${CHLOG}" -COUNT="$(echo "$CHLOG" | wc -l)" 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 changelogs= files - $CHLOG. Ignoring this change." + echo "Found multiple changelog files - $CHLOG. Ignoring this change." fi exit 0 From a0350466c24cbc87472b87dcedfc70ad92c0ed10 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 16:40:51 -0800 Subject: [PATCH 04/15] remove unnecessary things --- .github/workflows/prepare-new-issue.yml | 3 --- .github/workflows/prepare-new-pr.yml | 5 ----- .github/workflows/scripts/prepare-new-pr.sh | 0 3 files changed, 8 deletions(-) mode change 100644 => 100755 .github/workflows/scripts/prepare-new-pr.sh 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 index d5edf5cb17..33b4848a5f 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -6,15 +6,10 @@ on: jobs: prepare-new-pr: runs-on: ubuntu-latest - permissions: - pull-requests: write if: ${{ github.repository_owner == 'open-telemetry' }} steps: - uses: actions/checkout@v4 - - name: Run update permissions - run: chmod +x ./.github/workflows/scripts/prepare-new-pr.sh - - name: Run prepare-new-pr.sh run: ./.github/workflows/scripts/prepare-new-pr.sh env: diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh old mode 100644 new mode 100755 From 3cce94c6f1f0e7b32bbc3ea77d65a6e2116e1325 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 17:04:52 -0800 Subject: [PATCH 05/15] pull_request -> pull_request_target --- .github/workflows/prepare-new-pr.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 33b4848a5f..5e569086ee 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -1,7 +1,7 @@ name: 'Prepare new PR' on: - pull_request: - types: [opened, ready_for_review] + pull_request_target: + types: [opened, reopened] jobs: prepare-new-pr: From d68b60d162723278e70cd038a9b0e8b12e922d21 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 17:11:29 -0800 Subject: [PATCH 06/15] only run on main --- .github/workflows/prepare-new-pr.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 5e569086ee..40333c15c0 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -2,6 +2,7 @@ name: 'Prepare new PR' on: pull_request_target: types: [opened, reopened] + branches: [ 'main*' ] jobs: prepare-new-pr: From bd485e0b61bdae773cb258396dd7c50c1b75ef40 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Sun, 19 Jan 2025 17:18:41 -0800 Subject: [PATCH 07/15] don't modify contents --- .github/workflows/prepare-new-pr.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 40333c15c0..315c1c62f2 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -7,6 +7,9 @@ on: jobs: prepare-new-pr: runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write if: ${{ github.repository_owner == 'open-telemetry' }} steps: - uses: actions/checkout@v4 From a158f716f98b7bdfb771432c24086757a1704898 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 14:08:06 -0800 Subject: [PATCH 08/15] Update .github/workflows/prepare-new-pr.yml Co-authored-by: Trask Stalnaker --- .github/workflows/prepare-new-pr.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 315c1c62f2..7dedd9c350 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -13,6 +13,8 @@ jobs: if: ${{ github.repository_owner == 'open-telemetry' }} steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} - name: Run prepare-new-pr.sh run: ./.github/workflows/scripts/prepare-new-pr.sh From f36454a54770393986659435d23bdcd4308a119a Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 14:29:53 -0800 Subject: [PATCH 09/15] break down into two jobs --- .github/workflows/prepare-new-pr.yml | 25 +++++++++++++--- .github/workflows/scripts/get-change-type.sh | 31 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/scripts/get-change-type.sh diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 7dedd9c350..536d4ec8c8 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -1,23 +1,40 @@ name: 'Prepare new PR' on: pull_request_target: - types: [opened, reopened] + types: [opened, synchronize] branches: [ 'main*' ] jobs: - prepare-new-pr: + get-change-details: 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: - uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} + - id: get-change-type + name: Run get-change-type.sh + run: | + change_type=$(./.github/workflows/scripts/get-change-type.sh) + echo "change_type=$change_type" >> $GITHUB_OUTPUT + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR: ${{ github.event.pull_request.number }} + + prepare-new-pr: + runs-on: ubuntu-latest + permissions: + pull-requests: write + if: ${{ github.repository_owner == 'open-telemetry' }} + steps: + - uses: actions/checkout@v4 - 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: ${{ needs.get-change-details.outputs.change_type }} diff --git a/.github/workflows/scripts/get-change-type.sh b/.github/workflows/scripts/get-change-type.sh new file mode 100644 index 0000000000..28b6e740ef --- /dev/null +++ b/.github/workflows/scripts/get-change-type.sh @@ -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 - $CHLOG. Ignoring this change." + exit 1 +fi From ef06314d49ed26f793f6141ca66ee236250e5476 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 16:00:22 -0800 Subject: [PATCH 10/15] debug --- .github/workflows/prepare-new-pr.yml | 21 +++++++++--- .github/workflows/scripts/get-change-type.sh | 4 +-- .github/workflows/scripts/prepare-new-pr.sh | 36 -------------------- 3 files changed, 19 insertions(+), 42 deletions(-) delete mode 100755 .github/workflows/scripts/prepare-new-pr.sh diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 536d4ec8c8..cb9e0cedeb 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -3,6 +3,7 @@ on: pull_request_target: types: [opened, synchronize] branches: [ 'main*' ] + paths: ['.chloggen/*'] jobs: get-change-details: @@ -19,22 +20,34 @@ jobs: - 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: ${{ github.repository_owner == 'open-telemetry' }} + 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 - - name: Run prepare-new-pr.sh - run: ./.github/workflows/scripts/prepare-new-pr.sh + - 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: ${{ needs.get-change-details.outputs.change_type }} + PR: ${{ github.event.pull_request.number }} + CHANGE_TYPE: ${{ needs.get-change-details.outputs.change_type }} diff --git a/.github/workflows/scripts/get-change-type.sh b/.github/workflows/scripts/get-change-type.sh index 28b6e740ef..9c7a075bf6 100644 --- a/.github/workflows/scripts/get-change-type.sh +++ b/.github/workflows/scripts/get-change-type.sh @@ -26,6 +26,6 @@ if [ $COUNT -eq 1 ]; then echo $CHANGE_TYPE exit 0 else - echo "Found multiple changelog files - $CHLOG. Ignoring this change." + echo "Found multiple changelog files. Ignoring this change." exit 1 -fi +fi \ No newline at end of file diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh deleted file mode 100755 index 7fde34cc0c..0000000000 --- a/.github/workflows/scripts/prepare-new-pr.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/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 - -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}' $CHLOG | xargs) - echo $CHANGE_TYPE - gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true -else - echo "Found multiple changelog files - $CHLOG. Ignoring this change." -fi - -exit 0 From 1b7c151c13feee2cf327625f8fd70b2326b11dd3 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 18:25:39 -0800 Subject: [PATCH 11/15] checkout changelog to a separate dir --- .github/workflows/prepare-new-pr.yml | 4 ++++ .github/workflows/scripts/get-change-type.sh | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index cb9e0cedeb..56f5f5e9b3 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -14,9 +14,12 @@ jobs: outputs: change_type: ${{ steps.get-change-type.outputs.change_type }} steps: + - uses: actions/checkout@v4 - uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} + path: prchangelog + sparse-checkout: .chloggen - id: get-change-type name: Run get-change-type.sh run: | @@ -29,6 +32,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR: ${{ github.event.pull_request.number }} + PR_CHANGELOG_PATH: prchangelog # 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. diff --git a/.github/workflows/scripts/get-change-type.sh b/.github/workflows/scripts/get-change-type.sh index 9c7a075bf6..8f10ca883d 100644 --- a/.github/workflows/scripts/get-change-type.sh +++ b/.github/workflows/scripts/get-change-type.sh @@ -12,7 +12,12 @@ if [ -z ${PR:-} ]; then exit 1 fi -CHLOG="$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" +if [ -z ${PR_CHANGELOG_PATH:-} ]; then + echo "PR_CHANGELOG_PATH 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 From 11023300f2986f9a86a26a192ac36a56ab1b802a Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 18:34:24 -0800 Subject: [PATCH 12/15] Undo two jobs --- .github/workflows/prepare-new-pr.yml | 41 ++++--------------- .../{get-change-type.sh => prepare-new-pr.sh} | 30 +++++++------- 2 files changed, 24 insertions(+), 47 deletions(-) rename .github/workflows/scripts/{get-change-type.sh => prepare-new-pr.sh} (50%) mode change 100644 => 100755 diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 56f5f5e9b3..18205b43e4 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -6,52 +6,29 @@ on: paths: ['.chloggen/*'] jobs: - get-change-details: + 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 - - 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 }} - PR_CHANGELOG_PATH: prchangelog - - # 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 - - name: Set label with change type on the PR - run: gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true + # 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 }} - CHANGE_TYPE: ${{ needs.get-change-details.outputs.change_type }} + PR_CHANGELOG_PATH: prchangelog diff --git a/.github/workflows/scripts/get-change-type.sh b/.github/workflows/scripts/prepare-new-pr.sh old mode 100644 new mode 100755 similarity index 50% rename from .github/workflows/scripts/get-change-type.sh rename to .github/workflows/scripts/prepare-new-pr.sh index 8f10ca883d..95f5fb293b --- a/.github/workflows/scripts/get-change-type.sh +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -2,35 +2,35 @@ # # 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. +# +# 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="$PR_CHANGELOG_PATH/$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" -# echo "Change log file(s): ${CHLOG}" +echo "Change log file(s): ${CHLOG}" if [ -z "$CHLOG" ]; then echo "No changelog found in the PR. Ignoring this change." - exit 1 + exit 0 fi COUNT="$(echo "$CHLOG" | wc -l)" if [ $COUNT -eq 1 ]; then - CHANGE_TYPE=$(awk -F': ' '/^change_type:/ {print $2}' $CHLOG | xargs) + CHANGE_TYPE=$(awk -F': ' '/^change_type:/ {print $2}' "$CHLOG" | xargs) echo $CHANGE_TYPE - exit 0 + gh pr edit "${PR}" --add-label "${CHANGE_TYPE}" || true else echo "Found multiple changelog files. Ignoring this change." - exit 1 -fi \ No newline at end of file +fi + +exit 0 \ No newline at end of file From 0f1680ed5371ceffef850680dde5a5a38ac16ae4 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 19:12:39 -0800 Subject: [PATCH 13/15] up --- .github/workflows/scripts/prepare-new-pr.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh index 95f5fb293b..01e8175c53 100755 --- a/.github/workflows/scripts/prepare-new-pr.sh +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -16,6 +16,11 @@ if [ -z ${PR:-} ]; then 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}" From ec8855707a1c372aba92b929c81c7be1bf1332b2 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 20:50:29 -0800 Subject: [PATCH 14/15] Apply suggestions from code review Co-authored-by: Trask Stalnaker --- .github/workflows/prepare-new-pr.yml | 2 -- .github/workflows/scripts/prepare-new-pr.sh | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/prepare-new-pr.yml b/.github/workflows/prepare-new-pr.yml index 18205b43e4..a8a00fe5eb 100644 --- a/.github/workflows/prepare-new-pr.yml +++ b/.github/workflows/prepare-new-pr.yml @@ -12,8 +12,6 @@ jobs: 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 diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh index 01e8175c53..74b8e3e915 100755 --- a/.github/workflows/scripts/prepare-new-pr.sh +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -17,7 +17,7 @@ if [ -z ${PR:-} ]; then fi if [ -z ${PR_CHANGELOG_PATH:-} ]; then - echo "PR number is required" + echo "PR_CHANGELOG_PATH is required" exit 1 fi @@ -37,5 +37,3 @@ if [ $COUNT -eq 1 ]; then else echo "Found multiple changelog files. Ignoring this change." fi - -exit 0 \ No newline at end of file From 07a43b149b1e6ef96a5f0f98953ceaddeb083dee Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 20 Jan 2025 20:56:00 -0800 Subject: [PATCH 15/15] code review --- .github/workflows/scripts/prepare-new-pr.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/scripts/prepare-new-pr.sh b/.github/workflows/scripts/prepare-new-pr.sh index 74b8e3e915..dc369b80b4 100755 --- a/.github/workflows/scripts/prepare-new-pr.sh +++ b/.github/workflows/scripts/prepare-new-pr.sh @@ -21,7 +21,7 @@ if [ -z ${PR_CHANGELOG_PATH:-} ]; then exit 1 fi -CHLOG="$PR_CHANGELOG_PATH/$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" +CHLOG="$(gh pr view $PR --json files --jq '.files.[].path | select (. | startswith(".chloggen/"))')" echo "Change log file(s): ${CHLOG}" if [ -z "$CHLOG" ]; then @@ -31,7 +31,7 @@ fi COUNT="$(echo "$CHLOG" | wc -l)" if [ $COUNT -eq 1 ]; then - CHANGE_TYPE=$(awk -F': ' '/^change_type:/ {print $2}' "$CHLOG" | xargs) + 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