|
| 1 | +async function fetchRepoLabels(github, owner, repo) { |
| 2 | + const labelMap = new Map(); |
| 3 | + let page = 1; |
| 4 | + |
| 5 | + while (true) { |
| 6 | + const { data: labels } = await github.rest.issues.listLabelsForRepo({ |
| 7 | + owner, |
| 8 | + repo, |
| 9 | + per_page: 100, |
| 10 | + page, |
| 11 | + }); |
| 12 | + |
| 13 | + if (labels.length === 0) break; |
| 14 | + |
| 15 | + for (const label of labels) { |
| 16 | + labelMap.set(label.name.toLowerCase(), label.name); |
| 17 | + } |
| 18 | + |
| 19 | + if (labels.length < 100) break; |
| 20 | + page++; |
| 21 | + } |
| 22 | + |
| 23 | + return labelMap; |
| 24 | +} |
| 25 | + |
| 26 | +async function handleAddLabel({ github, context, labelArgs }) { |
| 27 | + const { owner, repo } = context.repo; |
| 28 | + const issueNumber = context.payload.issue.number; |
| 29 | + const issueState = context.payload.issue.state; |
| 30 | + const commenter = context.payload.comment.user.login; |
| 31 | + |
| 32 | + if (issueState === 'closed') { |
| 33 | + await github.rest.issues.createComment({ |
| 34 | + owner, |
| 35 | + repo, |
| 36 | + issue_number: issueNumber, |
| 37 | + body: `❌ Cannot add labels — this issue is already **closed**.`, |
| 38 | + }); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (!labelArgs || labelArgs.length === 0) { |
| 43 | + await github.rest.issues.createComment({ |
| 44 | + owner, |
| 45 | + repo, |
| 46 | + issue_number: issueNumber, |
| 47 | + body: `❌ @${commenter}, please provide at least one label name.\n\n**Usage:** \`/addlabel label1 label2 ...\`\n\n**Example:** \`/addlabel good-first-issue frontend\``, |
| 48 | + }); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const seen = new Set(); |
| 53 | + const uniqueLabelArgs = []; |
| 54 | + for (const label of labelArgs) { |
| 55 | + const key = label.toLowerCase(); |
| 56 | + if (!seen.has(key)) { |
| 57 | + seen.add(key); |
| 58 | + uniqueLabelArgs.push(label); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const repoLabels = await fetchRepoLabels(github, owner, repo); |
| 63 | + const validLabels = []; |
| 64 | + const invalidLabels = []; |
| 65 | + |
| 66 | + for (const labelArg of uniqueLabelArgs) { |
| 67 | + const canonical = repoLabels.get(labelArg.toLowerCase()); |
| 68 | + if (canonical) { |
| 69 | + validLabels.push(canonical); |
| 70 | + } else { |
| 71 | + invalidLabels.push(labelArg); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (invalidLabels.length > 0) { |
| 76 | + const invalidList = invalidLabels.map((l) => `\`${l}\``).join(', '); |
| 77 | + const availableLabels = [...repoLabels.values()] |
| 78 | + .slice(0, 20) |
| 79 | + .map((l) => `\`${l}\``) |
| 80 | + .join(', '); |
| 81 | + |
| 82 | + const moreLabelsNote = |
| 83 | + repoLabels.size > 20 |
| 84 | + ? `\n> _...and ${repoLabels.size - 20} more. Check the [Labels page](../labels) for the full list._` |
| 85 | + : ''; |
| 86 | + |
| 87 | + await github.rest.issues.createComment({ |
| 88 | + owner, |
| 89 | + repo, |
| 90 | + issue_number: issueNumber, |
| 91 | + body: `❌ The following label(s) do not exist in this repository: ${invalidList}\n\n**Available labels:**\n${availableLabels}${moreLabelsNote}\n\n> 💡 Labels are case-insensitive. If you need a new label created, please ask a maintainer.`, |
| 92 | + }); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const existingLabels = context.payload.issue.labels.map((l) => l.name.toLowerCase()); |
| 97 | + const newLabels = validLabels.filter((l) => !existingLabels.includes(l.toLowerCase())); |
| 98 | + const alreadyApplied = validLabels.filter((l) => existingLabels.includes(l.toLowerCase())); |
| 99 | + |
| 100 | + if (newLabels.length === 0) { |
| 101 | + const alreadyList = alreadyApplied.map((l) => `\`${l}\``).join(', '); |
| 102 | + await github.rest.issues.createComment({ |
| 103 | + owner, |
| 104 | + repo, |
| 105 | + issue_number: issueNumber, |
| 106 | + body: `ℹ️ All specified labels are already applied to this issue: ${alreadyList}`, |
| 107 | + }); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + await github.rest.issues.addLabels({ |
| 112 | + owner, |
| 113 | + repo, |
| 114 | + issue_number: issueNumber, |
| 115 | + labels: newLabels, |
| 116 | + }); |
| 117 | + |
| 118 | + const addedList = newLabels.map((l) => `\`${l}\``).join(', '); |
| 119 | + let body = `✅ Added labels: ${addedList}`; |
| 120 | + |
| 121 | + if (alreadyApplied.length > 0) { |
| 122 | + const skippedList = alreadyApplied.map((l) => `\`${l}\``).join(', '); |
| 123 | + body += `\n\n> ℹ️ Already applied (skipped): ${skippedList}`; |
| 124 | + } |
| 125 | + |
| 126 | + await github.rest.issues.createComment({ |
| 127 | + owner, |
| 128 | + repo, |
| 129 | + issue_number: issueNumber, |
| 130 | + body, |
| 131 | + }); |
| 132 | +} |
| 133 | + |
| 134 | +module.exports = { handleAddLabel }; |
0 commit comments