From fe46fc5f43ff3e5f1dc8cb3b2116b69f975551d9 Mon Sep 17 00:00:00 2001 From: Chris Werner Rau Date: Tue, 6 Aug 2024 11:06:23 +0200 Subject: [PATCH 1/2] feat(ci): adjust chart detection # Conflicts: # .github/workflows/get-changed-chart.yaml --- .github/workflows/get-changed-chart.yaml | 63 ++++++++++--------- .github/workflows/get-changed-charts.yaml | 29 +++++---- .github/workflows/label-pullrequest.yaml | 15 ++--- .github/workflows/linter.yaml | 2 + .github/workflows/pr-comment-diff.yaml | 2 + .../workflows/release-update-metadata.yaml | 12 +++- .github/workflows/validate-pullrequest.yaml | 2 + 7 files changed, 73 insertions(+), 52 deletions(-) diff --git a/.github/workflows/get-changed-chart.yaml b/.github/workflows/get-changed-chart.yaml index fcc6d1434..363f1d54d 100644 --- a/.github/workflows/get-changed-chart.yaml +++ b/.github/workflows/get-changed-chart.yaml @@ -2,48 +2,49 @@ 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 }} + found: ${{ needs.getChangedCharts.outputs.count == 1 }} 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 - - if ((num_changed < 1)); then - echo "This PR has seemingly no changes to any charts?" - 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 "This PR has seemingly no changes to any charts?" + exit 1 + ;; + 1) + ( + echo chart="$(<<<"$changed" jq -r first)" + ) | 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 291d9f020..3ba23cd93 100644 --- a/.github/workflows/get-changed-charts.yaml +++ b/.github/workflows/get-changed-charts.yaml @@ -1,33 +1,36 @@ -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 }} + count: ${{ steps.getCharts.outputs.count }} + 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 e498934b7..e663eef00 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 a16c84a31..7c8478b9e 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 030bfb88a..e9ef3296b 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 67885736c..e16536c7a 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 bcd11f597..62f110872 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 From ef6a7897babfb028a93bd9bb4b727a25ce649ad6 Mon Sep 17 00:00:00 2001 From: Chris Werner Rau Date: Tue, 6 Aug 2024 11:14:51 +0200 Subject: [PATCH 2/2] feat(ci): split check by chart found and always --- .github/workflows/validate-pullrequest.yaml | 33 +++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/.github/workflows/validate-pullrequest.yaml b/.github/workflows/validate-pullrequest.yaml index 62f110872..1d76c0e3e 100644 --- a/.github/workflows/validate-pullrequest.yaml +++ b/.github/workflows/validate-pullrequest.yaml @@ -1,4 +1,4 @@ -name: Validate and label Pull Request +name: Validate Pull Request on: pull_request_target: @@ -26,35 +26,36 @@ jobs: - name: Conventional commit check uses: cocogitto/cocogitto-action@5ae166018d8265bb2df85c1eb521e86a17b61085 # v3 - validateAndLabelPR: + validateTitle: if: ${{ !startsWith(github.head_ref, 'release-please--') }} - name: Validate and label PR + name: Validate Title runs-on: ubuntu-latest needs: getChangedChart + env: + PR_TITLE: ${{ github.event.pull_request.title }} steps: - name: Conventional commit check uses: cocogitto/cocogitto-action@5ae166018d8265bb2df85c1eb521e86a17b61085 # v3 with: check: false - - run: | - set -u + - name: Verify that PR title is a conventional commit message + run: | + set -e set -o pipefail - : "${PR_TITLE:?Environment variable must be set}" - - changed="${CHANGED_CHART?Environment variable must be set}" - if ! cog verify "$PR_TITLE"; then echo "PR title must be a conventional commit message" >&2 exit 1 fi + - name: Verify correct scope of PR title + if: ${{ needs.getChangedChart.outputs.found == 'true' }} + env: + CHANGED_CHART: ${{ needs.getChangedChart.outputs.chart }} + run: | + set -e + set -o pipefail - if [[ -n "$changed" ]] && ! cog verify "$PR_TITLE" 2>&1 | grep -Eq "^\s+Scope: $changed(/.+|)\$"; then - echo "PR title must have scope '$changed/\$subscope'" >&2 + if ! cog verify "$PR_TITLE" 2>&1 | grep -Eq "^\s+Scope: $CHANGED_CHART(/.+|)\$"; then + echo "PR title must have scope '$CHANGED_CHART/\$subscope'" >&2 exit 1 fi - env: - PR_TITLE: ${{ github.event.pull_request.title }} - GITHUB_TOKEN: ${{ github.token }} - ISSUE_NUMBER: ${{ github.event.number }} - CHANGED_CHART: ${{ needs.getChangedChart.outputs.chart }}