Skip to content

Commit ce8521a

Browse files
committedJan 23, 2025·
feat: add policy bot configuration
This allows us to have different configurations for the workflows and at the same time have a single configuration for what's required to pass in order to merge a PR. Signed-off-by: Marcelo E. Magallon <[email protected]>
1 parent 6947a51 commit ce8521a

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed
 

Diff for: ‎.github/workflows/validate-policy-bot-config.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Validate Policy Bot Config
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- ".policy.yml"
7+
8+
push:
9+
branches:
10+
- main
11+
paths:
12+
- ".policy.yml"
13+
14+
jobs:
15+
validate:
16+
name: Validate policy bot config
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
23+
with:
24+
persists-credentials: false
25+
26+
- name: Validate policy bot config
27+
run: |
28+
curl \
29+
--silent \
30+
--fail-with-body \
31+
--request PUT \
32+
--upload-file .policy.yml \
33+
https://github-policy-bot.grafana-ops.net/api/validate

Diff for: ‎.policy.yml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This file is generated by generate-policy-bot-config.
2+
# Do not edit directly. Run "make .policy.yml" to update.
3+
4+
# The contents of ".policy.yml.tmpl" were merged with the generated parts of this config.
5+
# To add additional policies to the config, update this file and then run "make .policy.yml".
6+
7+
policy:
8+
approval:
9+
- or:
10+
- and:
11+
- Workflow .github/workflows/build.yml succeeded or skipped
12+
- Workflow .github/workflows/validate-policy-bot-config.yml succeeded or skipped
13+
- default to approval
14+
- override policies
15+
- policy bot config is valid when modified
16+
approval_rules:
17+
- name: Workflow .github/workflows/build.yml succeeded or skipped
18+
if:
19+
targets_branch:
20+
pattern: (^(?:[^/]*)$)
21+
requires:
22+
conditions:
23+
has_workflow_result:
24+
conclusions:
25+
- skipped
26+
- success
27+
workflows:
28+
- .github/workflows/build.yml
29+
- name: Workflow .github/workflows/validate-policy-bot-config.yml succeeded or skipped
30+
if:
31+
changed_files:
32+
paths:
33+
- ^\.policy\.yml$
34+
requires:
35+
conditions:
36+
has_workflow_result:
37+
conclusions:
38+
- skipped
39+
- success
40+
workflows:
41+
- .github/workflows/validate-policy-bot-config.yml
42+
- name: default to approval
43+
- name: policy bot config is valid when modified
44+
if:
45+
changed_files:
46+
paths:
47+
- ^\.policy\.yml
48+
requires:
49+
conditions:
50+
has_successful_status:
51+
- Validate policy bot config
52+
- name: override policies
53+
options:
54+
methods:
55+
comments:
56+
- 'policy bot: approve'
57+
- 'policy-bot: approve'
58+
github_review: false
59+
requires:
60+
count: 1
61+
permissions:
62+
- write

Diff for: ‎.policy.yml.tmpl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Require some statuses to pass only if certain files are modified. This is a
2+
# template file. You can edit it and the generated contents (to enforce that
3+
# conditional workflows pass when they are triggered) will be inserted where
4+
# `MERGE_WITH_GENERATED` is.
5+
6+
policy:
7+
approval:
8+
- or:
9+
- MERGE_WITH_GENERATED
10+
- override policies
11+
- policy bot config is valid when modified
12+
13+
approval_rules:
14+
- name: policy bot config is valid when modified
15+
if:
16+
changed_files:
17+
paths:
18+
- ^\.policy\.yml
19+
requires:
20+
conditions:
21+
has_successful_status:
22+
- Validate policy bot config
23+
24+
- name: override policies
25+
requires:
26+
count: 1
27+
permissions:
28+
- write
29+
options:
30+
methods:
31+
comments:
32+
- "policy bot: approve"
33+
- "policy-bot: approve"
34+
github_review: false

Diff for: ‎scripts/gen-policy-bot-config

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
IMAGE=ghcr.io/grafana/generate-policy-bot-config:latest
7+
8+
targetdir=${1:-}
9+
mergearg=
10+
11+
if test -z "${targetdir}" ; then
12+
targetdir=$(git rev-parse --show-toplevel)
13+
fi
14+
15+
if test -f "${targetdir}/.policy.yml.tmpl" ; then
16+
mergearg="--merge-with=.policy.yml.tmpl"
17+
fi
18+
19+
docker run --rm \
20+
--volume "${targetdir}:/work" \
21+
--user "$(id -u):$(id -g)" \
22+
--workdir /work \
23+
"${IMAGE}" \
24+
--output /work/.policy.yml \
25+
"${mergearg}" \
26+
.

Diff for: ‎scripts/make/620_generate_policy_bot_config.mk

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.PHONY: generate-policy-bot-config
2+
generate-policy-bot-config: ## Generate policy bot config.
3+
$(S) echo 'Generating policy bot configuration...'
4+
$(V) $(ROOTDIR)/scripts/gen-policy-bot-config "$(ROOTDIR)"
5+
$(S) echo 'Done.'

0 commit comments

Comments
 (0)
Please sign in to comment.