|
| 1 | +import fs from "fs"; |
| 2 | +import yaml from "js-yaml"; |
| 3 | +import { Octokit } from "@octokit/core"; |
| 4 | + |
| 5 | +// Load backend.yml and parse matrix.include |
| 6 | +const backendYml = yaml.load(fs.readFileSync(".github/workflows/backend.yml", "utf8")); |
| 7 | +const jobs = backendYml.jobs; |
| 8 | +const backendJob = jobs["backend-jobs"]; |
| 9 | +const strategy = backendJob.strategy; |
| 10 | +const matrix = strategy.matrix; |
| 11 | +const includes = matrix.include; |
| 12 | + |
| 13 | +// Set up Octokit for PR changed files |
| 14 | +const token = process.env.GITHUB_TOKEN; |
| 15 | +const octokit = new Octokit({ auth: token }); |
| 16 | + |
| 17 | +const eventPath = process.env.GITHUB_EVENT_PATH; |
| 18 | +const event = JSON.parse(fs.readFileSync(eventPath, "utf8")); |
| 19 | + |
| 20 | +let prNumber, repo, owner; |
| 21 | +if (event.pull_request) { |
| 22 | + prNumber = event.pull_request.number; |
| 23 | + repo = event.repository.name; |
| 24 | + owner = event.repository.owner.login; |
| 25 | +} else { |
| 26 | + throw new Error("This workflow must be triggered by a pull_request event."); |
| 27 | +} |
| 28 | + |
| 29 | +async function getChangedFiles() { |
| 30 | + let files = []; |
| 31 | + let page = 1; |
| 32 | + while (true) { |
| 33 | + const res = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/files', { |
| 34 | + owner, |
| 35 | + repo, |
| 36 | + pull_number: prNumber, |
| 37 | + per_page: 100, |
| 38 | + page |
| 39 | + }); |
| 40 | + files = files.concat(res.data.map(f => f.filename)); |
| 41 | + if (res.data.length < 100) break; |
| 42 | + page++; |
| 43 | + } |
| 44 | + return files; |
| 45 | +} |
| 46 | + |
| 47 | +// Infer backend path |
| 48 | +function inferBackendPath(item) { |
| 49 | + if (item.dockerfile.endsWith("python")) { |
| 50 | + return `backend/python/${item.backend}`; |
| 51 | + } |
| 52 | + if (item.dockerfile.endsWith("golang")) { |
| 53 | + return `backend/go/${item.backend}`; |
| 54 | + } |
| 55 | + if (item.dockerfile.endsWith("llama-cpp")) { |
| 56 | + return `backend/cpp/llama-cpp`; |
| 57 | + } |
| 58 | + return null; |
| 59 | +} |
| 60 | + |
| 61 | +(async () => { |
| 62 | + const changedFiles = await getChangedFiles(); |
| 63 | + |
| 64 | + console.log("Changed files:", changedFiles); |
| 65 | + |
| 66 | + const filtered = includes.filter(item => { |
| 67 | + const backendPath = inferBackendPath(item); |
| 68 | + if (!backendPath) return false; |
| 69 | + return changedFiles.some(file => file.startsWith(backendPath)); |
| 70 | + }); |
| 71 | + |
| 72 | + console.log("Filtered files:", filtered); |
| 73 | + |
| 74 | + const hasBackends = filtered.length > 0 ? 'true' : 'false'; |
| 75 | + console.log("Has backends?:", hasBackends); |
| 76 | + |
| 77 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `has-backends=${hasBackends}\n`); |
| 78 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `matrix=${JSON.stringify({ include: filtered })}\n`); |
| 79 | +})(); |
0 commit comments