1
0
Fork 0
mirror of https://github.com/karancode/yamllint-github-action.git synced 2024-08-16 10:19:48 +02:00

Add testing framework bats and fix #4 (#5)

* Fix mdl issues

* Add bats as testing framework

* Fix #4

* Add GitHub codeowner file

* Add Dependabot config

* Add github workflow for testing

* don't comment if yamllint is successful (fixes #2)

* remove GITHUB_EVENT.json
This commit is contained in:
Richard Steinbrück 2020-04-28 10:39:44 +02:00 committed by GitHub
parent 8d8558d725
commit dd59165b84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 385 additions and 16 deletions

9
.dependabot/config.yml Normal file
View file

@ -0,0 +1,9 @@
version: 1
update_configs:
- package_manager: "docker"
directory: "/"
update_schedule: "weekly"
automerged_updates:
- match:
dependency_type: "all"
update_type: "all"

1
.github/CODEOWNERS vendored Normal file
View file

@ -0,0 +1 @@
* @karancode

37
.github/workflows/test.yml vendored Normal file
View file

@ -0,0 +1,37 @@
name: "Continuous Integration"
on:
push:
branches-ignore:
- 'dependabot/*'
schedule:
- cron: '22 22 * * 5'
jobs:
lint:
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout@v2
- name: Markdown Lint
uses: actionshub/markdownlint@v1.0.0
- name: Dockerfile Lint
uses: burdzwastaken/hadolint-action@1.6.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HADOLINT_ACTION_DOCKERFILE_FOLDER: .
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install bats
run: sudo apt-get -y install bats > /dev/null
- name: Run CI script
run: ./tests/run.bats
- name: Run action
uses: ./
with:
yamllint_file_or_dir: "./tests/data/single_files/file2.yml"

1
.mdlrc Normal file
View file

@ -0,0 +1 @@
rules "~MD013,~MD024,~MD033"

View file

@ -1,7 +1,9 @@
FROM python:3.7-alpine FROM python:3.7-alpine
# hadolint ignore=DL3018
RUN apk add --update --no-cache bash ca-certificates curl git jq openssh RUN apk add --update --no-cache bash ca-certificates curl git jq openssh
# hadolint ignore=DL3013
RUN pip install yamllint RUN pip install yamllint
RUN ["bin/sh", "-c", "mkdir -p /src"] RUN ["bin/sh", "-c", "mkdir -p /src"]

View file

@ -1,16 +1,19 @@
# yamllint-github-action # yamllint-github-action
Yamllint GitHub Actions allow you to execute `yamllint` command within GitHub Actions. Yamllint GitHub Actions allow you to execute `yamllint` command within GitHub Actions.
The output of the actions can be viewed from the Actions tab in the main repository view. If the actions are executed on a pull request event, a comment may be posted on the pull request. The output of the actions can be viewed from the Actions tab in the main repository view. If the actions are executed on a pull request event, a comment may be posted on the pull request.
Yamllint GitHub Actions is a single GitHub Action that can be executed on different directories depending on the content of the GitHub Actions YAML file. Yamllint GitHub Actions is a single GitHub Action that can be executed on different directories depending on the content of the GitHub Actions YAML file.
## Success Criteria ## Success Criteria
An exit code of `0` is considered a successful execution. An exit code of `0` is considered a successful execution.
## Usage ## Usage
The most common usage is to run `yamllint` on a file/directory. A comment will be posted to the pull request depending on the output of the Yamllint command being executed. This workflow can be configured by adding the following content to the GitHub Actions workflow YAML file. The most common usage is to run `yamllint` on a file/directory. A comment will be posted to the pull request depending on the output of the Yamllint command being executed. This workflow can be configured by adding the following content to the GitHub Actions workflow YAML file.
```yaml ```yaml
name: 'Yamllint GitHub Actions' name: 'Yamllint GitHub Actions'
on: on:
@ -31,19 +34,21 @@ jobs:
env: env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```
This was a simplified example showing the basic features of this Yamllint GitHub Actions. This was a simplified example showing the basic features of this Yamllint GitHub Actions.
# Inputs ## Inputs
Inputs configure Yamllint GitHub Actions to perform lint action. Inputs configure Yamllint GitHub Actions to perform lint action.
* `yamllint_file_or_dir` - (Optional) The file or directory to run `yamllint` on (assumes that the directory contains *.yaml file). Defaults to `.`. | Parameter | Default | Description |
* `yamllint_strict` - (Optional) Yamllint strict option. Defaults to `false`. |------------------------------|---------|---------------------------------------------------------------------------------------------------------|
* `yamllint_config_filepath` - (Optional) Path to a custom config file. Defaults to default configs. | `yamllint_file_or_dir` | . | (Optional) The file or directory to run `yamllint` on (assumes that the directory contains *.yaml file) |
* `yamllint_config_datapath` - (Optional) Custom configuration (as YAML source). Defaults to default configs. | `yamllint_strict` | `false` | (Optional) Yamllint strict option. |
* `yamllint_format` - (Optional) Format for parsing output. Defaults to `auto`. | `yamllint_config_filepath` | `empty` | (Optional) Path to a custom config file. |
* `yamllint_comment` - (Optional) Whether or not to comment on GitHub pull requests. Defaults to `false`. | `yamllint_config_datapath` | `empty` | (Optional) Custom configuration (as YAML source). |
| `yamllint_format` | `auto` | (Optional) Format for parsing. |
| `yamllint_comment` | `false` | (Optional) Comment on GitHub pull requests, possible are true,false |
## Outputs ## Outputs
@ -56,3 +61,10 @@ Outputs are used to pass information to subsequent GitHub Actions steps.
Secrets are similar to inputs except that they are encrypted and only used by GitHub Actions. It's a convenient way to keep sensitive data out of the GitHub Actions workflow YAML file. Secrets are similar to inputs except that they are encrypted and only used by GitHub Actions. It's a convenient way to keep sensitive data out of the GitHub Actions workflow YAML file.
* `GITHUB_ACCESS_TOKEN` - (Optional) The GitHub API token used to post comments to pull requests. Not required if the `yamllint_comment` input is set to `false`. * `GITHUB_ACCESS_TOKEN` - (Optional) The GitHub API token used to post comments to pull requests. Not required if the `yamllint_comment` input is set to `false`.
## Development
### Testing
For testing the [bats](https://github.com/bats-core/bats-core) testing framework is used.
Tests can be run with ``./tests/run.bats`` but first you need to install [bats](https://github.com/bats-core/bats-core#installation).

View file

@ -29,7 +29,7 @@ inputs:
yamllint_comment: yamllint_comment:
description: 'Comment yamllint output' description: 'Comment yamllint output'
required: false required: false
default: '0' default: 'false'
outputs: outputs:
yamllint_output: yamllint_output:
description: 'Output of yamllint' description: 'Output of yamllint'

View file

@ -28,7 +28,11 @@ function parse_inputs {
fi fi
yamllint_comment=0 yamllint_comment=0
if [ "${INPUT_YAMLLINT_COMMENT}" != "0" ] || [ "${INPUT_YAMLLINT_COMMENT}" != "false" ]; then if [[ "${INPUT_YAMLLINT_COMMENT}" == "0" || "${INPUT_YAMLLINT_COMMENT}" == "false" ]]; then
yamllint_comment="0"
fi
if [[ "${INPUT_YAMLLINT_COMMENT}" == "1" || "${INPUT_YAMLLINT_COMMENT}" == "true" ]]; then
yamllint_comment="1" yamllint_comment="1"
fi fi

View file

@ -23,8 +23,8 @@ function yaml_lint {
echo echo
fi fi
# comment # comment if lint failed
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ] && [ "${yamllint_comment}" == "1" ]; then if [ "${GITHUB_EVENT_NAME}" == "pull_request" ] && [ "${yamllint_comment}" == "1" ] && [ ${lint_exit_code} -ne 0 ]; then
lint_comment_wrapper="#### \`yamllint\` ${lint_comment_status} lint_comment_wrapper="#### \`yamllint\` ${lint_comment_status}
<details><summary>Show Output</summary> <details><summary>Show Output</summary>

View file

@ -0,0 +1,16 @@
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: karma
namespace: monitoring
spec:
jobLabel: karma
select0r:
matchLabels:
app.kubernetes.io/instance: karma
namespaceSelector:
matchNames:
- monitoring
endpoints:
- path: /alerts/metrics

View file

@ -0,0 +1,21 @@
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
creationTimestamp: null
labels:
app: prometheus-operator
release: prometheus-operator
name: redis
namespace: monitoring
spec:
groups:
- name: redis.memory
rules:
- alerts: RedisMemoryAlmostFull
annotations:
messages: 'Redis memory is at {{ $value | humanizePercentage }} of maximum for 2 days in a row'
expr: redis_memory_used_bytes/redis_memory_max_bytes > 0.8
f0r: 2d
labels:
severity: warning

View file

@ -0,0 +1,21 @@
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
creationTimestamp: null
labels:
app: prometheus-operator
release: prometheus-operator
name: redis
namespace: monitoring
spec:
groups:
- name: redis.memory
rules:
- alert: RedisMemoryAlmostFull
annotations:
message: 'Redis memory is at {{ $value | humanizePercentage }} of maximum for 2 days in a row'
expr: redis_memory_used_bytes/redis_memory_max_bytes > 0.8
for: 2d
labels:
severity: warning

View file

@ -0,0 +1,16 @@
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: karma
namespace: monitoring
spec:
jobLabel: karma
selector:
matchLabels:
app.kubernetes.io/instance: karma
namespaceSelector:
matchNames:
- monitoring
endpoints:
- path: /alerts/metrics

229
tests/run.bats Executable file
View file

@ -0,0 +1,229 @@
#!/usr/bin/env bats
# NOTE: environment variable GITHUB_EVENT_PATH
# is required in a pullrequest scenario. If the variable
# is not set the test the test is gonna hang.
# global variables ############################################################
CONTAINER_NAME="yamllint-github-action"
# build container to test the behavior ########################################
@test "build container" {
docker build -t $CONTAINER_NAME . >&2
}
# functions ###################################################################
debug() {
status="$1"
output="$2"
if [[ ! "${status}" -eq "0" ]]; then
echo "status: ${status}"
echo "output: ${output}"
fi
}
###############################################################################
## test cases #################################################################
###############################################################################
function setup() {
unset INPUT_YAMLLINT_FILE_OR_DIR
unset INPUT_YAMLLINT_STRICT
unset INPUT_YAMLLINT_CONFIG_FILEPATH
unset INPUT_YAMLLINT_CONFIG_DATAPATH
unset INPUT_YAMLLINT_FORMAT
unset INPUT_YAMLLINT_COMMENT
}
## INPUT_YAMLLINT_FILE_OR_DIR #################################################
###############################################################################
## File
@test "INPUT_YAMLLINT_FILE_OR_DIR: valid single without warnings or errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file2.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "lint: info: successful yamllint on ${INPUT_YAMLLINT_FILE_OR_DIR}."
[[ "${status}" -eq 0 ]]
}
@test "INPUT_YAMLLINT_FILE_OR_DIR: valid single with one errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "lint: error: failed yamllint on"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
[[ "${status}" -eq 1 ]]
}
## folder
@test "INPUT_YAMLLINT_FILE_OR_DIR: nestet_folder with one errors" {
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/nestet_folder" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "lint: error: failed yamllint on"
echo $output | grep -q "line too long (115 > 80 characters)"
[[ "${status}" -eq 1 ]]
}
## INPUT_YAMLLINT_COMMENT #################################################
###############################################################################
### enabled (1,true)
@test "INPUT_YAMLLINT_COMMENT: set 1 in PR scenario with lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e INPUT_YAMLLINT_COMMENT="1" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -q "lint: info: commenting on the pull request"
[[ "${status}" -eq 1 ]]
}
@test "INPUT_YAMLLINT_COMMENT: set true in PR scenario with lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e INPUT_YAMLLINT_COMMENT="true" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -q "lint: info: commenting on the pull request"
[[ "${status}" -eq 1 ]]
}
@test "INPUT_YAMLLINT_COMMENT: set true in PR scenario without lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file2.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e INPUT_YAMLLINT_COMMENT="true" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -vq "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -vq "lint: info: commenting on the pull request"
[[ "${status}" -eq 0 ]]
}
### disabled (0,false)
@test "INPUT_YAMLLINT_COMMENT: set 0 in PR scenario with lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e INPUT_YAMLLINT_COMMENT="0" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -vq "lint: info: commenting on the pull request"
[[ "${status}" -eq 1 ]]
}
@test "INPUT_YAMLLINT_COMMENT: set false in PR scenario with lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e INPUT_YAMLLINT_COMMENT="false" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -vq "lint: info: commenting on the pull request"
[[ "${status}" -eq 1 ]]
}
### disabled (empty,notset)
@test "INPUT_YAMLLINT_COMMENT: set empty in PR scenario with lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e INPUT_YAMLLINT_COMMENT="" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -vq "lint: info: commenting on the pull request"
[[ "${status}" -eq 1 ]]
}
@test "INPUT_YAMLLINT_COMMENT: not set in PR scenario with lint errors" {
INPUT_YAMLLINT_FILE_OR_DIR="/mnt/tests/data/single_files/file1.yml"
run docker run --rm \
-v "$(pwd):/mnt/" \
-e INPUT_YAMLLINT_FILE_OR_DIR="${INPUT_YAMLLINT_FILE_OR_DIR}" \
-e GITHUB_EVENT_PATH="/tmp/" \
-e GITHUB_EVENT_NAME="pull_request" \
-i $CONTAINER_NAME
debug "${status}" "${output}" "${lines}"
echo $output | grep -q "$INPUT_YAMLLINT_FILE_OR_DIR"
echo $output | grep -q "line too long (114 > 80 characters)"
echo $output | grep -q "::set-output name=yamllint_output::${INPUT_YAMLLINT_FILE_OR_DIR}"
echo $output | grep -vq "lint: info: commenting on the pull request"
[[ "${status}" -eq 1 ]]
}