diff --git a/.github/linters/.yamllint.yml b/.github/linters/.yamllint.yml new file mode 100644 index 0000000..4a33dd6 --- /dev/null +++ b/.github/linters/.yamllint.yml @@ -0,0 +1,11 @@ +--- +extends: default + +rules: + line-length: disable + new-lines: + type: unix + new-line-at-end-of-file: + level: warning + trailing-spaces: + level: warning diff --git a/.github/workflows/lint-go-workflow.yml b/.github/workflows/lint-go-workflow.yml deleted file mode 100644 index 4c2a783..0000000 --- a/.github/workflows/lint-go-workflow.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Reusable Lint Workflow - -on: - workflow_call: - inputs: - go-version: - description: 'Go version' - required: true - default: '1.22' - type: string - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - - - name: Set up Go - uses: actions/setup-go@0c52d54 # v5.0.0 - with: - go-version: ${{ inputs.go-version }} - - - name: Run Linter - run: | - echo "Running golangci-lint" - go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest - golangci-lint run diff --git a/.github/workflows/lint-validation.yml b/.github/workflows/lint-validation.yml new file mode 100644 index 0000000..08bde52 --- /dev/null +++ b/.github/workflows/lint-validation.yml @@ -0,0 +1,77 @@ +name: Workflow Linting and Validation + +on: + pull_request: + paths: + - '.github/workflows/**' + push: + branches: + - main + - master + paths: + - '.github/workflows/**' + +jobs: + lint-and-validate: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Linting workflow files + uses: ibiqlik/action-yamllint@2576378a8e339169678f9939646ee3ee325e845c # v3.1.1 + with: + config_file: .github/linters/.yamllint.yml + + - name: Validate GitHub Actions workflows + id: validate + # uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + uses: actions/github-script@v6 + with: + script: | + const core = require('@actions/core'); + const github = require('@actions/github'); + const fs = require('fs'); + const path = require('path'); + + async function run() { + try { + const token = core.getInput('token'); + const octokit = github.getOctokit(token); + + const workflowsDir = '.github/workflows'; + let isValid = true; + let errorMessage = ''; + + const files = fs.readdirSync(workflowsDir); + for (const file of files) { + const fullPath = path.join(workflowsDir, file); + console.log(`Validating ${fullPath}...`); + + const fileContents = fs.readFileSync(fullPath, 'utf8'); + if (!fileContents.includes('name')) { + isValid = false; + errorMessage += `Validation failed for ${file}: Workflow must have a name\n`; + } + } + + if (!isValid) { + const context = github.context; + const issue_number = context.issue.number || context.payload.pull_request?.number; + if (issue_number) { + await octokit.rest.issues.createComment({ + ...context.repo, + issue_number, + body: `🚨 Workflow Validation Error:\n\`\`\`\n${errorMessage}\n\`\`\``, + }); + } + core.setFailed('One or more workflow validations failed.'); + } + } catch (error) { + core.setFailed(error.message); + } + } + + run(); + env: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/reusable-lint-go-workflow.yml b/.github/workflows/reusable-lint-go-workflow.yml new file mode 100644 index 0000000..c26a943 --- /dev/null +++ b/.github/workflows/reusable-lint-go-workflow.yml @@ -0,0 +1,28 @@ +name: Reusable Lint Workflow + +on: + workflow_call: + inputs: + go-version: + description: 'Go version' + required: true + default: '1.22' + type: string + +jobs: + lint: + runs-on: ubuntu-latest + steps: + + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Set up Go + uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 + with: + go-version: ${{ inputs.go-version }} + + - name: Run Linter + run: | + echo "Running golangci-lint" + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + golangci-lint run