Skip to content

Commit

Permalink
feat(ci): adjust chart detection (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwrau authored Aug 30, 2024
1 parent 7095f16 commit 0944c05
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 68 deletions.
63 changes: 32 additions & 31 deletions .github/workflows/get-changed-chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 16 additions & 13 deletions .github/workflows/get-changed-charts.yaml
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 8 additions & 7 deletions .github/workflows/label-pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"'}'
2 changes: 2 additions & 0 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pr-comment-diff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/release-update-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 19 additions & 16 deletions .github/workflows/validate-pullrequest.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Validate and label Pull Request
name: Validate Pull Request

on:
pull_request_target:
Expand All @@ -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
Expand All @@ -24,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 }}

0 comments on commit 0944c05

Please sign in to comment.