From 1af0e17d8ec4146ec554a7b3372a9f0ebd81bbb2 Mon Sep 17 00:00:00 2001 From: Chris Werner Rau Date: Fri, 24 Nov 2023 13:12:00 +0100 Subject: [PATCH] feat(ci): validate and label pull requests separately (#643) --- .../scripts/validate-and-label-pullrequest.sh | 41 ------------------- .github/scripts/validate-pullrequest.sh | 20 +++++++++ .github/workflows/get-changed-chart.yaml | 39 ++++++++---------- .github/workflows/label-pullrequest.yaml | 32 +++++++++++++++ ...request.yaml => validate-pullrequest.yaml} | 7 +++- 5 files changed, 74 insertions(+), 65 deletions(-) delete mode 100755 .github/scripts/validate-and-label-pullrequest.sh create mode 100755 .github/scripts/validate-pullrequest.sh create mode 100644 .github/workflows/label-pullrequest.yaml rename .github/workflows/{validate-and-label-pullrequest.yaml => validate-pullrequest.yaml} (80%) diff --git a/.github/scripts/validate-and-label-pullrequest.sh b/.github/scripts/validate-and-label-pullrequest.sh deleted file mode 100755 index 171d27e1c..000000000 --- a/.github/scripts/validate-and-label-pullrequest.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env bash - -[[ "$RUNNER_DEBUG" == 1 ]] && set -x - -set -eu -set -o pipefail - -: "${PR_TITLE:?Environment variable must be set}" - -changed=$(ct list-changed) - -if [[ -z "$changed" ]]; then - 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 - -# Strip charts directory -changed="${changed##*/}" - -if ! cog verify "$PR_TITLE"; then - echo "PR title must be a conventional commit message" >&2 - exit 1 -fi - -if ! cog verify "$PR_TITLE" 2>&1 | grep -Eq "^\s+Scope: $changed(/.+|)\$"; then - echo "PR title must have scope '$changed/\$subscope'" >&2 - exit 1 -fi - -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"'"]}' diff --git a/.github/scripts/validate-pullrequest.sh b/.github/scripts/validate-pullrequest.sh new file mode 100755 index 000000000..7e1005c78 --- /dev/null +++ b/.github/scripts/validate-pullrequest.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +[[ "$RUNNER_DEBUG" == 1 ]] && set -x + +set -eu +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 + +if [[ -n "$changed" ]] && ! cog verify "$PR_TITLE" 2>&1 | grep -Eq "^\s+Scope: $changed(/.+|)\$"; then + echo "PR title must have scope '$changed/\$subscope'" >&2 + exit 1 +fi diff --git a/.github/workflows/get-changed-chart.yaml b/.github/workflows/get-changed-chart.yaml index 3f4004ba9..30d2d51ab 100644 --- a/.github/workflows/get-changed-chart.yaml +++ b/.github/workflows/get-changed-chart.yaml @@ -2,11 +2,6 @@ name: Get single changed chart in last commit on: workflow_call: - inputs: - chart: - type: string - required: false - description: The name of the chart to run against outputs: chart: description: The name of the changed cart in the last commit @@ -16,7 +11,7 @@ jobs: getChangedChart: runs-on: ubuntu-latest outputs: - chart: ${{ steps.getChartName.outputs.chart }} + chart: ${{ steps.getChangedChart.outputs.chart }} env: CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }} steps: @@ -25,26 +20,24 @@ jobs: fetch-depth: 0 - uses: helm/chart-testing-action@v2.6.1 - if: ${{ !inputs.chart }} - id: getChangedChart - if: ${{ !inputs.chart }} name: Get changed chart in this commit run: | set -x set -o pipefail - ( - echo -n chart= - ct list-changed --since "HEAD~" | cut -d / -f 2 - ) | tee "$GITHUB_OUTPUT" - - id: getChartName - name: Get name for changed chart - run: | - set -x - set -o pipefail - chart="${{ inputs.chart }}" - chart="${chart:-${{ steps.getChangedChart.outputs.chart }}}" - chart=${chart:?chart variable is empty} - ( - echo "chart=${chart}" - ) | tee "$GITHUB_OUTPUT" + changed="$(ct list-changed --since "HEAD~" | cut -d / -f 2)" + + if [[ -z "$changed" ]]; then + echo chart= | tee "$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 "$GITHUB_OUTPUT" diff --git a/.github/workflows/label-pullrequest.yaml b/.github/workflows/label-pullrequest.yaml new file mode 100644 index 000000000..7f37ba933 --- /dev/null +++ b/.github/workflows/label-pullrequest.yaml @@ -0,0 +1,32 @@ +name: Label Chart Pull Request + +on: + pull_request: + paths: + - charts/** + types: + - opened + - edited + - reopened + - synchronize + +jobs: + getChangedChart: + uses: ./.github/workflows/get-changed-chart.yaml + labelPullRequest: + name: Validate and label PR + runs-on: ubuntu-latest + needs: getChangedChart + steps: + - env: + PR_TITLE: ${{ github.event.pull_request.title }} + GITHUB_TOKEN: ${{ github.token }} + ISSUE_NUMBER: ${{ github.event.number }} + CHANGED_CHART: ${{ needs.getChangedChart.outputs.chart }} + 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"'"]}' diff --git a/.github/workflows/validate-and-label-pullrequest.yaml b/.github/workflows/validate-pullrequest.yaml similarity index 80% rename from .github/workflows/validate-and-label-pullrequest.yaml rename to .github/workflows/validate-pullrequest.yaml index 1501d1ad3..118cc8663 100644 --- a/.github/workflows/validate-and-label-pullrequest.yaml +++ b/.github/workflows/validate-pullrequest.yaml @@ -9,6 +9,9 @@ on: - synchronize jobs: + getChangedChart: + if: ${{ !startsWith(github.head_ref, 'release-please--') }} + uses: ./.github/workflows/get-changed-chart.yaml validateCommits: if: ${{ !startsWith(github.head_ref, 'release-please--') }} name: Validate commits @@ -25,6 +28,7 @@ jobs: if: ${{ !startsWith(github.head_ref, 'release-please--') }} name: Validate and label PR runs-on: ubuntu-latest + needs: getChangedChart env: CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }} steps: @@ -38,8 +42,9 @@ jobs: uses: cocogitto/cocogitto-action@v3 with: check: false - - run: .github/scripts/validate-and-label-pullrequest.sh + - run: .github/scripts/validate-pullrequest.sh env: PR_TITLE: ${{ github.event.pull_request.title }} GITHUB_TOKEN: ${{ github.token }} ISSUE_NUMBER: ${{ github.event.number }} + CHANGED_CHART: ${{ needs.getChangedChart.outputs.chart }}