Skip to content

Commit

Permalink
Merge pull request #2 from storyprotocol/feat/add_workflow_linting_va…
Browse files Browse the repository at this point in the history
…lidation

[feat] add workflow to lint and validate reusable workflows
  • Loading branch information
AndyBoWu committed Apr 3, 2024
2 parents d03108d + 5df2639 commit f88ea2b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: ".github/workflows"
schedule:
interval: "daily"
11 changes: 11 additions & 0 deletions .github/linters/.yamllint.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 0 additions & 28 deletions .github/workflows/lint-go-workflow.yml

This file was deleted.

65 changes: 65 additions & 0 deletions .github/workflows/lint-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
with:
script: |
const fs = require('fs');
const path = require('path');
const workflowsDir = '.github/workflows';
let isValid = true;
let errorMessage = '';
fs.readdirSync(workflowsDir).forEach(file => {
const fullPath = path.join(workflowsDir, file);
console.log(`Validating ${fullPath}...`);
try {
const fileContents = fs.readFileSync(fullPath, 'utf8');
if (!fileContents.includes('name')) {
throw new Error('Workflow must have a name');
}
} catch (error) {
isValid = false;
errorMessage += `Validation failed for ${file}: ${error.message}\n`;
}
});
if (!isValid) {
const issue_number = github.context.issue.number || github.context.payload.pull_request?.number;
if (issue_number) {
await github.rest.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: issue_number,
body: `🚨 Workflow Validation Error:\n\`\`\`\n${errorMessage}\n\`\`\``,
});
}
core.setFailed('One or more workflow validations failed.');
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 28 additions & 0 deletions .github/workflows/reusable-lint-go-workflow.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f88ea2b

Please sign in to comment.