|
| 1 | +// Generate descriptive PR title based on changed files |
| 2 | +// Usage: Called from GitHub Actions with context |
| 3 | + |
| 4 | +module.exports = async ({ github, context, core, version }) => { |
| 5 | + const pr = context.payload.pull_request; |
| 6 | + if (!pr) { |
| 7 | + core.setFailed("No PR context found."); |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + const base = pr.base.ref; |
| 12 | + const head = pr.head.ref; |
| 13 | + |
| 14 | + // Define semantic-release type and scope |
| 15 | + const typeMap = { |
| 16 | + test: "feat", |
| 17 | + main: "release", |
| 18 | + }; |
| 19 | + const type = typeMap[base] || "chore"; |
| 20 | + |
| 21 | + // Get changed files to make title more descriptive |
| 22 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 23 | + owner: context.repo.owner, |
| 24 | + repo: context.repo.repo, |
| 25 | + pull_number: pr.number, |
| 26 | + }); |
| 27 | + |
| 28 | + // Count only .qmd documentation changes (ignore media files) |
| 29 | + const docsFiles = files.filter((f) => f.filename.startsWith("DOCS/") && f.filename.endsWith(".qmd")); |
| 30 | + |
| 31 | + // Group by category (first directory after DOCS/) |
| 32 | + const categories = {}; |
| 33 | + docsFiles.forEach((f) => { |
| 34 | + const match = f.filename.match(/^DOCS\/([^/]+)\//); |
| 35 | + if (match) { |
| 36 | + const category = match[1]; |
| 37 | + categories[category] = (categories[category] || 0) + 1; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + // Build descriptive content from categories |
| 42 | + const categoryList = Object.entries(categories) |
| 43 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 44 | + .map(([cat, count]) => { |
| 45 | + const formatted = cat.replace(/_/g, " "); |
| 46 | + return count > 1 ? `${count} ${formatted}` : formatted; |
| 47 | + }); |
| 48 | + |
| 49 | + // Construct title |
| 50 | + let title; |
| 51 | + if (categoryList.length > 0) { |
| 52 | + const what = categoryList.slice(0, 3).join(", "); // Max 3 categories in title |
| 53 | + const more = categoryList.length > 3 ? ` +${categoryList.length - 3} more` : ""; |
| 54 | + title = `${type}(docs): update ${what}${more}`; |
| 55 | + } else { |
| 56 | + // Fallback to branch-based title |
| 57 | + title = `${type}(merge): promote ${head} to ${base}`; |
| 58 | + } |
| 59 | + |
| 60 | + // Truncate to 100 characters safely |
| 61 | + if (title.length > 100) { |
| 62 | + title = title.slice(0, 97) + "..."; |
| 63 | + } |
| 64 | + |
| 65 | + console.log(`Setting PR title to: ${title}`); |
| 66 | + |
| 67 | + await github.rest.pulls.update({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + pull_number: pr.number, |
| 71 | + title: title, |
| 72 | + }); |
| 73 | +}; |
0 commit comments