Skip to content

Commit

Permalink
feat(ci): validate and label pull requests separately (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwrau authored Nov 24, 2023
1 parent c87eed0 commit 1af0e17
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 65 deletions.
41 changes: 0 additions & 41 deletions .github/scripts/validate-and-label-pullrequest.sh

This file was deleted.

20 changes: 20 additions & 0 deletions .github/scripts/validate-pullrequest.sh
Original file line number Diff line number Diff line change
@@ -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
39 changes: 16 additions & 23 deletions .github/workflows/get-changed-chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -25,26 +20,24 @@ jobs:
fetch-depth: 0

- uses: helm/[email protected]
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"
32 changes: 32 additions & 0 deletions .github/workflows/label-pullrequest.yaml
Original file line number Diff line number Diff line change
@@ -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"'"]}'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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 }}

0 comments on commit 1af0e17

Please sign in to comment.