Skip to content

Commit

Permalink
feat: add policy bot configuration (#1047)
Browse files Browse the repository at this point in the history
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 <marcelo.magallon@grafana.com>
  • Loading branch information
mem authored Jan 28, 2025
1 parent df9c5d9 commit 406851a
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/validate-policy-bot-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Validate Policy Bot Config

on:
pull_request:
paths:
- ".policy.yml"

push:
branches:
- main
paths:
- ".policy.yml"

jobs:
validate:
name: Validate policy bot config

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
persists-credentials: false

- name: Validate policy bot config
run: |
curl \
--silent \
--fail-with-body \
--request PUT \
--upload-file .policy.yml \
https://github-policy-bot.grafana-ops.net/api/validate
72 changes: 72 additions & 0 deletions .policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This file is generated by generate-policy-bot-config.
# Do not edit directly. Run "make .policy.yml" to update.

# The contents of ".policy.yml.tmpl" were merged with the generated parts of this config.
# To add additional policies to the config, update this file and then run "make .policy.yml".

policy:
approval:
- or:
- and:
- Workflow .github/workflows/ci.yml succeeded or skipped
- Workflow .github/workflows/is-compatible.yml succeeded or skipped
- Workflow .github/workflows/validate-policy-bot-config.yml succeeded or skipped
- default to approval
- override policies
- policy bot config is valid when modified
approval_rules:
- name: Workflow .github/workflows/ci.yml succeeded or skipped
if:
targets_branch:
pattern: (^main$)
requires:
conditions:
has_workflow_result:
conclusions:
- skipped
- success
workflows:
- .github/workflows/ci.yml
- name: Workflow .github/workflows/is-compatible.yml succeeded or skipped
requires:
conditions:
has_workflow_result:
conclusions:
- skipped
- success
workflows:
- .github/workflows/is-compatible.yml
- name: Workflow .github/workflows/validate-policy-bot-config.yml succeeded or skipped
if:
changed_files:
paths:
- ^\.policy\.yml$
requires:
conditions:
has_workflow_result:
conclusions:
- skipped
- success
workflows:
- .github/workflows/validate-policy-bot-config.yml
- name: default to approval
- name: policy bot config is valid when modified
if:
changed_files:
paths:
- ^\.policy\.yml
requires:
conditions:
has_successful_status:
- Validate policy bot config
- name: override policies
options:
methods:
comments:
- 'policy bot: approve'
- 'policy-bot: approve'
github_review: false
requires:
count: 1
permissions:
- write
34 changes: 34 additions & 0 deletions .policy.yml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Require some statuses to pass only if certain files are modified. This is a
# template file. You can edit it and the generated contents (to enforce that
# conditional workflows pass when they are triggered) will be inserted where
# `MERGE_WITH_GENERATED` is.

policy:
approval:
- or:
- MERGE_WITH_GENERATED
- override policies
- policy bot config is valid when modified

approval_rules:
- name: policy bot config is valid when modified
if:
changed_files:
paths:
- ^\.policy\.yml
requires:
conditions:
has_successful_status:
- Validate policy bot config

- name: override policies
requires:
count: 1
permissions:
- write
options:
methods:
comments:
- "policy bot: approve"
- "policy-bot: approve"
github_review: false
26 changes: 26 additions & 0 deletions scripts/gen-policy-bot-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

set -e
set -u

IMAGE=ghcr.io/grafana/generate-policy-bot-config:latest

targetdir=${1:-}
mergearg=

if test -z "${targetdir}" ; then
targetdir=$(git rev-parse --show-toplevel)
fi

if test -f "${targetdir}/.policy.yml.tmpl" ; then
mergearg="--merge-with=.policy.yml.tmpl"
fi

docker run --rm \
--volume "${targetdir}:/work" \
--user "$(id -u):$(id -g)" \
--workdir /work \
"${IMAGE}" \
--output /work/.policy.yml \
"${mergearg}" \
.
5 changes: 5 additions & 0 deletions scripts/make/620_generate_policy_bot_config.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: generate-policy-bot-config
generate-policy-bot-config: ## Generate policy bot config.
$(S) echo 'Generating policy bot configuration...'
$(V) $(ROOTDIR)/scripts/gen-policy-bot-config "$(ROOTDIR)"
$(S) echo 'Done.'

0 comments on commit 406851a

Please sign in to comment.