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

feat(workflow): Add reusable YAML schema linting workflow #42

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/lint-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ jobs:
with:
yamllint-args: "--config-file=.yamllint.yaml"

yaml-schema:
uses: ./.github/workflows/reusable-yamlschema.yaml
secrets: inherit
with:
ignore: |
.github/labels/base.yaml
.github/labels/golang.yaml
.github/labeler/base.yaml
.github/labeler/golang.yaml

actionlint:
uses: ./.github/workflows/reusable-actionlint.yaml
secrets: inherit
Expand Down
101 changes: 101 additions & 0 deletions .github/workflows/reusable-yamlschema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Reusable - YAMLLint

on:
workflow_call:
inputs:
debug:
description: Show debug logs.
type: boolean
required: false
default: false
ignore:
description: A newline-separated list of files to ignore.
type: string
required: false
default: ""
working-directory:
description: The directory to lint (recursively).
type: string
required: false
default: "."

jobs:
yamlschemas:
runs-on: ubuntu-latest
steps:
- name: Generate Token
uses: actions/create-github-app-token@3378cda945da322a8db4b193e19d46352ebe2de5 # v1.10.4
id: app-token
with:
app-id: "${{ secrets.BOT_APP_ID }}"
private-key: "${{ secrets.BOT_APP_PRIVATE_KEY }}"

- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
token: "${{ steps.app-token.outputs.token }}"

- name: Check for schema comments
shell: bash
env:
DEBUG: "${{ inputs.debug }}"
YAML_SCHEMA_IGNORE: "${{ inputs.ignore }}"
DIRECTORY: "${{ inputs.working-directory }}"
run: |
# The schema comment to look for
SCHEMA_COMMENT="# yaml-language-server: \$schema="

# Colors for output
RED='\033[0;31m'
GRAY='\033[0;90m'
NC='\033[0m' # No Color

# Split YAML_SCHEMA_IGNORE by comma if it exists
IFS=$'\n' read -r -a IGNORE_FILES <<<"$YAML_SCHEMA_IGNORE"

# Check if the directory exists
if [ ! -d "$DIRECTORY" ]; then
echo "Directory $DIRECTORY does not exist."
exit 1
fi

# Initialize flag to track missing schemas
missing_schema=0

# Function to check if a file is in the ignore list
function is_ignored() {
local file=$1
for ignore in "${IGNORE_FILES[@]}"; do
if [[ "$file" == *"$ignore"* ]]; then
return 0
fi
done
return 1
}

# Use a for loop to avoid subshell issues
for file in $(find "$DIRECTORY" -type f \( -name "*.yaml" -o -name "*.yml" \)); do
# Skip ignored files
if is_ignored "$file"; then
continue
fi

# Check if the schema comment exists in the file
if grep -q "$SCHEMA_COMMENT" "$file"; then
if [[ $DEBUG == true ]]; then
# Print in gray if LOG_LEVEL is debug and schema is found
echo -e "${GRAY}$file: Schema comment found.${NC}"
fi
else
# Print in red if the schema is missing
echo -e "${RED}$file: Schema comment missing!${NC}"
missing_schema=1
fi
done

# Exit with code 1 if any file was missing the schema
if [[ $missing_schema -eq 1 ]]; then
exit 1
fi
1 change: 1 addition & 0 deletions .releaserc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
# yaml-language-server: $schema=https://json.schemastore.org/semantic-release.json
branches:
- main

Expand Down
Loading