Skip to content

Commit 583fced

Browse files
committed
feat(actions): add validation and drift-detection actions
1 parent 67c5a39 commit 583fced

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

.github/workflows/drift.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Check for .policy.yml drift
2+
3+
on:
4+
pull_request:
5+
types:
6+
- edited
7+
- opened
8+
- ready_for_review
9+
- synchronize
10+
push:
11+
branches:
12+
- main
13+
14+
jobs:
15+
drift:
16+
name: Check for drift
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Check repository out
20+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
21+
22+
- name: Check for drift
23+
uses: ./actions/check-for-drift
24+
with:
25+
input_file: .policy.yml
26+
merge_with: policy.yml

actions/check-for-drift/action.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Check for Drift
2+
description: Checks if the generated output is different from the input file
3+
4+
inputs:
5+
input_file:
6+
description: The input file to compare
7+
required: true
8+
9+
merge_with:
10+
description: The file to merge with the input file
11+
required: false
12+
13+
runs:
14+
using: composite
15+
16+
steps:
17+
- name: Check repository out
18+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
19+
env:
20+
action_repo: ${{ github.action_repository }}
21+
action_ref: ${{ github.action_ref }}
22+
with:
23+
path: ${{ github.workspace }}/action-checkout
24+
repository: ${{ env.action_repo }}
25+
ref: ${{ env.action_ref }}
26+
27+
- name: Set up Go
28+
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
29+
with:
30+
go-version-file: ${{ github.workspace }}/action-checkout/go.mod
31+
32+
- name: Build the program
33+
shell: sh
34+
run: |
35+
cd "${{ github.workspace }}/action-checkout"
36+
37+
DESTDIR="$(go env GOPATH)/bin"
38+
mkdir -p "${DESTDIR}"
39+
40+
go build \
41+
-o "${DESTDIR}/generate-policy-bot-config" \
42+
github.com/grafana/generate-policy-bot-config/cmd/generate-policy-bot-config
43+
44+
- name: Generate new config
45+
id: new
46+
shell: sh
47+
run: |
48+
echo "config<<EOC" > "${GITHUB_OUTPUT}"
49+
generate-policy-bot-config \
50+
--output - \
51+
--merge-with ${{ inputs.merge_with }} \
52+
. \
53+
| tee -a "${GITHUB_OUTPUT}"
54+
echo "EOC" >> "${GITHUB_OUTPUT}"
55+
56+
- name: Check for drift
57+
shell: bash
58+
run: |
59+
IFS='' read -r -d '' NEW_CONFIG <<'EOC' || true
60+
${{ steps.new.outputs.config }}
61+
EOC
62+
63+
if ! diff -u ${{ inputs.input_file }} - <<< "${NEW_CONFIG}"; then
64+
echo "Drift detected: ${{ inputs.input_file }} is out-of-date. Regenerate it and commit the result."
65+
exit 1
66+
fi
67+
68+
echo "No drift detected: ${{ inputs.input_file }} is up-to-date."

actions/validate/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# validate-policy-bot-config
2+
3+
Validates the `.policy.yml` configuration file for [Policy Bot][policy-bot]. See
4+
[the documentation][policy-bot-docs] for more information on creating rules.
5+
6+
[policy-bot]: https://github.com/palantir/policy-bot
7+
[policy-bot-docs]: https://github.com/palantir/policy-bot?tab=readme-ov-file#configuration
8+
9+
## Inputs
10+
11+
- `policy`: The path to the `.policy.yml` file to validate. Default: `.policy.yml`.
12+
- `validation_endpoint` (required): The endpoint to validate the configuration
13+
against.
14+
15+
Example workflow:
16+
17+
```yaml
18+
name: validate-policy-bot
19+
on:
20+
pull_request:
21+
paths:
22+
- .policy.yml
23+
push:
24+
paths:
25+
- .policy.yml
26+
27+
jobs:
28+
validate-policy-bot:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Validate Policy Bot configuration
32+
uses: grafana/generate-policy-bot-config/actions/validate@main
33+
```

actions/validate/action.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Validate Policy Bot Config
2+
description: Validates the Policy Bot configuration file.
3+
4+
inputs:
5+
policy:
6+
description: |
7+
Path to the Policy Bot configuration file.
8+
default: .policy.yml
9+
10+
validation_endpoint:
11+
description: |
12+
Validation API endpoint.
13+
required: true
14+
15+
runs:
16+
using: composite
17+
steps:
18+
- name: Validate Policy Bot config
19+
shell: bash
20+
run: |
21+
curl \
22+
--silent \
23+
--fail-with-body \
24+
--request PUT \
25+
--upload-file "${{ inputs.policy }}" \
26+
"${{ inputs.validation_endpoint }}"

0 commit comments

Comments
 (0)