|
| 1 | +// This script is used to add labels to pull requests based on the title of the pull request. |
| 2 | +// The title of the pull request is checked for specific keywords and based on the keyword, a label is added |
| 3 | +// to the pull request. If the label does not exist, it is created and then added to the pull request. |
| 4 | +module.exports = async ({ github, context }) => { |
| 5 | + |
| 6 | + const commits = await github.rest.pulls.listCommits({ |
| 7 | + owner: context.repo.owner, |
| 8 | + repo: context.repo.repo, |
| 9 | + pull_number: context.payload.pull_request.number, |
| 10 | + }); |
| 11 | + |
| 12 | + for (const commit of commits.data) { |
| 13 | + |
| 14 | + const title = commit.commit.message.split('\n')[0].toLowerCase(); |
| 15 | + |
| 16 | + const regex = /^(fix|feat|build|docs|test|chore)(\((\w+)\))?(!)?:\s.+$/; |
| 17 | + const match = title.match(regex); |
| 18 | + if (!match) continue; |
| 19 | + |
| 20 | + const type = match[1]; |
| 21 | + const scope = match[3]; |
| 22 | + const breaking = match[4] === "!"; |
| 23 | + |
| 24 | + const config = { |
| 25 | + "fix": { name: "fix", description: "Fixes specific bug or issue", color: "FF1938" }, |
| 26 | + "feat": { name: "feature", description: "Adds new features", color: "00EEBC" }, |
| 27 | + "build": { name: "build", description: "Updates in build components", color: "04103A" }, |
| 28 | + "docs": { name: "documentation", description: "Documentation updates", color: "19ABFF" }, |
| 29 | + "test": { name: "test", description: "Test suite changes", color: "EBF0F6" }, |
| 30 | + "chore": { name: "chore", description: "Other changes", color: "19ABFF" }, |
| 31 | + "deps": { name: "dependencies", description: "Dependency updates", color: "FFE019" } |
| 32 | + }; |
| 33 | + |
| 34 | + let labels = []; |
| 35 | + if (config[type]) labels.push(config[type]); |
| 36 | + if (config[scope]) labels.push(config[scope]); |
| 37 | + if (breaking) { |
| 38 | + labels.push({ name: "breaking change", description: "Introduces significant changes", color: "FFE019" }); |
| 39 | + } |
| 40 | + |
| 41 | + if (labels.length === 0) continue; |
| 42 | + |
| 43 | + const existing = await github.rest.issues.listLabelsForRepo({ |
| 44 | + owner: context.repo.owner, |
| 45 | + repo: context.repo.repo, |
| 46 | + }); |
| 47 | + |
| 48 | + for (const label of labels) { |
| 49 | + const exists = existing.data.some(data => data.name === label.name); |
| 50 | + if (!exists) { |
| 51 | + await github.rest.issues.createLabel({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + name: label.name, |
| 55 | + color: label.color, |
| 56 | + description: label.description, |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + await github.rest.issues.addLabels({ |
| 62 | + issue_number: context.payload.pull_request.number, |
| 63 | + owner: context.repo.owner, |
| 64 | + repo: context.repo.repo, |
| 65 | + labels: labels.map(label => label.name), |
| 66 | + }); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | +}; |
0 commit comments