Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check PR for mistakes in ecosystem page change #4164

Merged
merged 1 commit into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/policy/files.rego
@@ -0,0 +1,46 @@
# Expects policy input as provided by:
# https://api.github.com/repos/open-policy-agent/opa/pulls/${PR_ID}/files
#
# Note that the "filename" here refers to the full path of the file, like
# docs/website/data/integrations.yaml - since that's how it's named in the
# input we'll use the same convention here.

package files

import future.keywords.in

filenames := [f | f := input[_].filename]

changes := {filename: attributes |
c := input[_]
filename := c.filename
attributes := object.remove(c, ["filename"])
}

deny["Logo must be placed in docs/website/static/img/logos/integrations"] {
"docs/website/data/integrations.yaml" in filenames

some filename in filenames
endswith(filename, ".png")
changes[filename].status == "added"
directory := substring(filename, 0, last_indexof(filename, "/"))
directory != "docs/website/static/img/logos/integrations"
}

deny["Logo must be a .png file"] {
"docs/website/data/integrations.yaml" in filenames

some filename in filenames
changes[filename].status == "added"
directory := substring(filename, 0, last_indexof(filename, "/"))
directory == "docs/website/static/img/logos/integrations"
not endswith(filename, ".png")
}

last_indexof(string, search) = i {
all := [i | chars := split(string, ""); chars[i] == search]
count(all) > 0
i := all[count(all) - 1]
} else = -1 {
true
}
44 changes: 44 additions & 0 deletions .github/policy/files_test.rego
@@ -0,0 +1,44 @@
package files_test

import data.files.deny

test_deny_logo_if_added_in_wrong_directory {
expected := "Logo must be placed in docs/website/static/img/logos/integrations"
deny[expected] with input as [
{
"filename": "docs/website/data/integrations.yaml",
"status": "modified",
},
{
"filename": "docs/website/static/img/logos/example.png",
"status": "added",
},
]
}

test_allow_logo_if_added_in_correct_directory {
count(deny) == 0 with input as [
{
"filename": "docs/website/data/integrations.yaml",
"status": "modified",
},
{
"filename": "docs/website/static/img/logos/integrations/example.png",
"status": "added",
},
]
}

test_deny_logo_if_not_png_file {
expected := "Logo must be a .png file"
deny[expected] with input as [
{
"filename": "docs/website/data/integrations.yaml",
"status": "modified",
},
{
"filename": "docs/website/static/img/logos/integrations/example.jpg",
"status": "added",
},
]
}
21 changes: 21 additions & 0 deletions .github/workflows/pull-request.yaml
Expand Up @@ -274,3 +274,24 @@ jobs:
- name: Build
run: make ci-go-ci-build-linux GOVERSION=${{ matrix.version }}
timeout-minutes: 30

# Run PR metadata against Rego policies
rego-check-pr:
name: Rego PR checks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Download OPA
uses: infracost/setup-opa@v1
anderseknert marked this conversation as resolved.
Show resolved Hide resolved

- name: Test policies
run: opa test .github/policy

- name: Run policy checks on changed files
run: |
curl --silent --fail --header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files \
| opa eval --data .github/policy/files.rego --format values --stdin-input --fail-defined 'data.files.deny[message]'