|
| 1 | +name: PR Assistant |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + push: |
| 7 | + branches: [main, master] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + pull-requests: write |
| 13 | + checks: write |
| 14 | + issues: write |
| 15 | + |
| 16 | +env: |
| 17 | + REQUIRED_WORKFLOW_SCHEMA_VERSION: "2.0.0" |
| 18 | + |
| 19 | +jobs: |
| 20 | + # ================================================================================= |
| 21 | + # JOB 1: PR Validation & Feedback |
| 22 | + # Validates example workflow definitions |
| 23 | + # ================================================================================= |
| 24 | + pr-validation: |
| 25 | + if: github.event_name == 'pull_request' && github.event.action != 'closed' |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Checkout Code |
| 29 | + uses: actions/checkout@v6 |
| 30 | + |
| 31 | + - name: Set up Python |
| 32 | + uses: actions/setup-python@v6 |
| 33 | + with: |
| 34 | + python-version: '3.12' |
| 35 | + |
| 36 | + - name: Install Dependencies |
| 37 | + run: | |
| 38 | + pip install pyyaml |
| 39 | +
|
| 40 | + # --- 1. Validate workflows.json (optional for examples) --- |
| 41 | + - name: 📋 Validate workflows.json |
| 42 | + run: | |
| 43 | + python << 'EOF' |
| 44 | + import json |
| 45 | + from pathlib import Path |
| 46 | +
|
| 47 | + workflows_json = Path("workflows.json") |
| 48 | + if workflows_json.exists(): |
| 49 | + with open(workflows_json) as f: |
| 50 | + data = json.load(f) |
| 51 | +
|
| 52 | + if "workflows" not in data or not isinstance(data["workflows"], list): |
| 53 | + print("❌ workflows.json must contain 'workflows' array") |
| 54 | + exit(1) |
| 55 | +
|
| 56 | + print(f"✅ workflows.json valid ({len(data['workflows'])} examples)") |
| 57 | + else: |
| 58 | + print("ℹ️ workflows.json not found (optional for examples)") |
| 59 | + EOF |
| 60 | +
|
| 61 | + # --- 2. Validate Example Workflow YAML Files --- |
| 62 | + - name: 📝 Validate Example Workflow YAML Files |
| 63 | + run: | |
| 64 | + python << 'EOF' |
| 65 | + import yaml |
| 66 | + import sys |
| 67 | + from pathlib import Path |
| 68 | +
|
| 69 | + errors = [] |
| 70 | + validated = 0 |
| 71 | +
|
| 72 | + print("Validating example workflow YAML files...") |
| 73 | +
|
| 74 | + # Check all YAML files in examples directory |
| 75 | + for yaml_file in Path(".").rglob("*.yaml"): |
| 76 | + if ".github" in str(yaml_file): |
| 77 | + continue |
| 78 | +
|
| 79 | + try: |
| 80 | + with open(yaml_file) as f: |
| 81 | + workflow_data = yaml.safe_load(f) |
| 82 | +
|
| 83 | + if not workflow_data: |
| 84 | + continue |
| 85 | +
|
| 86 | + # Basic validation (less strict for examples) |
| 87 | + if "id" not in workflow_data: |
| 88 | + errors.append(f"{yaml_file}: missing 'id' field") |
| 89 | +
|
| 90 | + if "name" not in workflow_data: |
| 91 | + errors.append(f"{yaml_file}: missing 'name' field") |
| 92 | +
|
| 93 | + # Trigger is recommended |
| 94 | + if "trigger" not in workflow_data: |
| 95 | + print(f"⚠️ {yaml_file}: missing 'trigger' (recommended)") |
| 96 | +
|
| 97 | + if not any(e.startswith(str(yaml_file)) for e in errors): |
| 98 | + validated += 1 |
| 99 | + print(f"✅ {yaml_file} valid") |
| 100 | +
|
| 101 | + except yaml.YAMLError as e: |
| 102 | + errors.append(f"{yaml_file}: Invalid YAML - {e}") |
| 103 | + except Exception as e: |
| 104 | + errors.append(f"{yaml_file}: {e}") |
| 105 | +
|
| 106 | + if errors: |
| 107 | + print("\n❌ Errors found:") |
| 108 | + for e in errors: |
| 109 | + print(f" - {e}") |
| 110 | + sys.exit(1) |
| 111 | + else: |
| 112 | + print(f"\n✅ All {validated} example workflow(s) valid.") |
| 113 | + EOF |
| 114 | +
|
| 115 | + # --- 3. Validate manifest.json (if exists) --- |
| 116 | + - name: 📄 Validate manifest.json |
| 117 | + run: | |
| 118 | + python << 'EOF' |
| 119 | + import json |
| 120 | + from pathlib import Path |
| 121 | +
|
| 122 | + manifest_path = Path("manifest.json") |
| 123 | + if manifest_path.exists(): |
| 124 | + try: |
| 125 | + with open(manifest_path) as f: |
| 126 | + manifest = json.load(f) |
| 127 | +
|
| 128 | + required_fields = ["schema_version", "domain", "name", "version"] |
| 129 | + for field in required_fields: |
| 130 | + if field not in manifest: |
| 131 | + print(f"⚠️ manifest.json missing field '{field}'") |
| 132 | + else: |
| 133 | + print(f"✅ manifest.json valid") |
| 134 | + except Exception as e: |
| 135 | + print(f"⚠️ Error reading manifest.json: {e}") |
| 136 | + else: |
| 137 | + print("ℹ️ manifest.json not found (optional)") |
| 138 | + EOF |
| 139 | +
|
| 140 | + # --- 4. Feedback Comment --- |
| 141 | + - name: Feedback Comment |
| 142 | + if: always() |
| 143 | + uses: actions/github-script@v6 |
| 144 | + with: |
| 145 | + script: | |
| 146 | + const outcome = '${{ job.status }}'; |
| 147 | + if (outcome === 'failure') { |
| 148 | + github.rest.issues.createComment({ |
| 149 | + issue_number: context.issue.number, |
| 150 | + owner: context.repo.owner, |
| 151 | + repo: context.repo.repo, |
| 152 | + body: "❌ **PR Validation Failed**\n\nPlease check:\n- Workflow YAML files are valid\n- Required fields (id, name) are present" |
| 153 | + }); |
| 154 | + } else if (outcome === 'success') { |
| 155 | + github.rest.issues.createComment({ |
| 156 | + issue_number: context.issue.number, |
| 157 | + owner: context.repo.owner, |
| 158 | + repo: context.repo.repo, |
| 159 | + body: "✅ **PR Validation Passed**\n\nAll example workflow checks passed!" |
| 160 | + }); |
| 161 | + } |
| 162 | +
|
| 163 | + # ================================================================================= |
| 164 | + # JOB 2: Thank Contributor |
| 165 | + # ================================================================================= |
| 166 | + thank-you: |
| 167 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true |
| 168 | + runs-on: ubuntu-latest |
| 169 | + steps: |
| 170 | + - uses: actions/github-script@v6 |
| 171 | + with: |
| 172 | + script: | |
| 173 | + const author = context.payload.pull_request.user.login; |
| 174 | + const admins = ['FaserF', 'fabia', 'github-actions[bot]']; |
| 175 | + if (admins.includes(author)) return; |
| 176 | +
|
| 177 | + github.rest.issues.createComment({ |
| 178 | + issue_number: context.issue.number, |
| 179 | + owner: context.repo.owner, |
| 180 | + repo: context.repo.repo, |
| 181 | + body: `🎉 **Thank you @${author}!**\n\nYour contribution to the **faneX-ID Workflow Examples** is valuable! 🚀` |
| 182 | + }); |
0 commit comments