diff --git a/.github/workflows/get-changed-chart.yaml b/.github/workflows/get-changed-chart.yaml index cafeb3ffe0..7ac6432bc6 100644 --- a/.github/workflows/get-changed-chart.yaml +++ b/.github/workflows/get-changed-chart.yaml @@ -2,43 +2,51 @@ name: Get single changed chart in last commit on: workflow_call: + inputs: + pr_number: + type: number outputs: chart: description: The name of the changed cart in the last commit value: ${{ jobs.getChangedChart.outputs.chart }} + found: + description: A chart was changed + value: ${{ jobs.getChangedChart.outputs.found == 'true' }} jobs: + getChangedCharts: + uses: ./.github/workflows/get-changed-charts.yaml + with: + pr_number: ${{ inputs.pr_number }} getChangedChart: runs-on: ubuntu-latest + needs: getChangedCharts outputs: chart: ${{ steps.getChangedChart.outputs.chart }} steps: - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha }} - - - uses: helm/chart-testing-action@e6669bcd63d7cb57cb4380c33043eebe5d111992 # v2.6.1 - - id: getChangedChart - name: Get changed chart in this commit - env: - CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }} + name: Get changed chart in PR run: | - set -x + set -e set -o pipefail - changed="$(ct list-changed | cut -d / -f 2)" - - if [[ -z "$changed" ]]; then - echo chart= | tee -a "$GITHUB_OUTPUT" - exit 0 - fi - - num_changed=$(wc -l <<<"$changed") - - if ((num_changed > 1)); then - echo "This PR has changes to multiple charts. Please create individual PRs per chart." >&2 - exit 1 - fi - - echo chart="$changed" | tee -a "$GITHUB_OUTPUT" + changed='${{ needs.getChangedCharts.outputs.charts }}' + num_changed='${{ needs.getChangedCharts.outputs.count }}' + + case "$num_changed" in + 0) + ( + echo chart= + echo found=false + )| tee -a "$GITHUB_OUTPUT" + ;; + 1) + ( + echo chart="$(<<<"$changed" jq -r first)" + echo found=true + ) | tee -a "$GITHUB_OUTPUT" + ;; + *) + echo "This PR has changes to multiple charts. Please create individual PRs per chart." >&2 + exit 1 + ;; + esac diff --git a/.github/workflows/get-changed-charts.yaml b/.github/workflows/get-changed-charts.yaml index 291d9f0205..0b528280a9 100644 --- a/.github/workflows/get-changed-charts.yaml +++ b/.github/workflows/get-changed-charts.yaml @@ -1,33 +1,35 @@ -name: Get changed charts in last commit +name: Get changed charts in PR on: workflow_call: + inputs: + pr_number: + type: number outputs: charts: - description: The names of the changed charts in the last commit + description: The names of the changed charts in the PR value: ${{ jobs.getChangedCharts.outputs.charts }} + count: + value: ${{ jobs.getChangedCharts.outputs.count }} jobs: getChangedCharts: runs-on: ubuntu-latest outputs: charts: ${{ steps.getCharts.outputs.charts }} + permissions: + pull-requests: read steps: - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha }} - - - uses: helm/chart-testing-action@e6669bcd63d7cb57cb4380c33043eebe5d111992 # v2.6.1 - - name: Get all charts id: getCharts env: - CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }} + PULL_REQUEST_NUMBER: ${{ inputs.pr_number }} + GITHUB_TOKEN: ${{ github.token }} run: | - set -ex + set -e set -o pipefail + charts="$(gh api --paginate /repos/${GITHUB_REPOSITORY}/pulls/${PULL_REQUEST_NUMBER}/files | jq -cr 'map(.filename | select(test("charts/[^/]*")) | split("/") | .[1] | select(.)) | unique')" ( - echo -n charts= - ct list-changed | cut -d / -f 2 | jq -c -Rn '[inputs]' + echo charts="$charts" + echo count="$(<<<"$charts" jq -r length)" ) | tee -a "$GITHUB_OUTPUT" diff --git a/.github/workflows/label-pullrequest.yaml b/.github/workflows/label-pullrequest.yaml index e498934b71..e663eef001 100644 --- a/.github/workflows/label-pullrequest.yaml +++ b/.github/workflows/label-pullrequest.yaml @@ -11,22 +11,23 @@ on: - synchronize jobs: - getChangedChart: - uses: ./.github/workflows/get-changed-chart.yaml + getChangedCharts: + uses: ./.github/workflows/get-changed-charts.yaml + with: + pr_number: ${{ github.event.pull_request.number }} labelPullRequest: - name: Validate and label PR + name: Label PR runs-on: ubuntu-latest - needs: getChangedChart + needs: getChangedCharts steps: - env: - PR_TITLE: ${{ github.event.pull_request.title }} GITHUB_TOKEN: ${{ github.token }} ISSUE_NUMBER: ${{ github.event.number }} - CHANGED_CHART: ${{ needs.getChangedChart.outputs.chart }} + CHANGED_CHARTS: ${{ needs.getChangedCharts.outputs.charts }} run: | curl --silent --fail-with-body \ -X POST \ -H 'Accept: application/vnd.github+json' \ -H "Authorization: token ${GITHUB_TOKEN}" \ "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${ISSUE_NUMBER}/labels" \ - -d '{"labels":["'"$CHANGED_CHART"'"]}' + -d '{"labels":'"${CHANGED_CHARTS}"'}' diff --git a/.github/workflows/linter.yaml b/.github/workflows/linter.yaml index 847832a019..80e0056ecf 100644 --- a/.github/workflows/linter.yaml +++ b/.github/workflows/linter.yaml @@ -11,6 +11,8 @@ on: jobs: getChangedCharts: uses: ./.github/workflows/get-changed-charts.yaml + with: + pr_number: ${{ github.event.pull_request.number }} prepare-helm-chart: name: prepare helm chart runs-on: ubuntu-latest diff --git a/.github/workflows/pr-comment-diff.yaml b/.github/workflows/pr-comment-diff.yaml index 030bfb88a8..e9ef3296b2 100644 --- a/.github/workflows/pr-comment-diff.yaml +++ b/.github/workflows/pr-comment-diff.yaml @@ -13,6 +13,8 @@ on: jobs: getChangedChart: uses: ./.github/workflows/get-changed-chart.yaml + with: + pr_number: ${{ github.event.pull_request.number }} postDiffComment: runs-on: ubuntu-latest needs: getChangedChart diff --git a/.github/workflows/release-update-metadata.yaml b/.github/workflows/release-update-metadata.yaml index c91437b005..d10e42c518 100644 --- a/.github/workflows/release-update-metadata.yaml +++ b/.github/workflows/release-update-metadata.yaml @@ -4,13 +4,18 @@ concurrency: cancel-in-progress: true on: - push: + pull_request: + types: + - opened + - synchronize branches: - release-please--branches--main--components-* jobs: getChangedChart: uses: ./.github/workflows/get-changed-chart.yaml + with: + pr_number: ${{ github.event.pull_request.number }} update-metadata-files: runs-on: ubuntu-latest needs: getChangedChart @@ -19,6 +24,11 @@ jobs: CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }} CHART: ${{ needs.getChangedChart.outputs.chart }} steps: + - name: Validate changed chart + if: ${{ needs.getChangedChart.outputs.found == 'true' }} + run: | + echo 'No chart has been changed?' >&2 + exit 1 - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 with: fetch-depth: 0 diff --git a/.github/workflows/validate-pullrequest.yaml b/.github/workflows/validate-pullrequest.yaml index bcd11f597a..62f110872c 100644 --- a/.github/workflows/validate-pullrequest.yaml +++ b/.github/workflows/validate-pullrequest.yaml @@ -12,6 +12,8 @@ jobs: getChangedChart: if: ${{ !startsWith(github.head_ref, 'release-please--') }} uses: ./.github/workflows/get-changed-chart.yaml + with: + pr_number: ${{ github.event.pull_request.number }} validateCommits: if: ${{ !startsWith(github.head_ref, 'release-please--') }} name: Validate commits