Add pull requests component #254
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Pull Request Labeler" | |
on: | |
pull_request_target: | |
jobs: | |
labeler: | |
name: "Apply labels" | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
pull-requests: write | |
steps: | |
- name: "Checkout repository" | |
uses: actions/checkout@v4 | |
- uses: actions/labeler@v5 | |
with: | |
configuration-path: .github/labeler.yml | |
- name: "Apply size label" | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
console.log("Fetching pull request diff..."); | |
const diff = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.issue.number, | |
mediaType: { | |
format: "diff", | |
}, | |
}); | |
console.log("Pull request diff fetched successfully."); | |
const changedLines = diff.data | |
.split("\n") | |
.filter(line => line.startsWith('+') || line.startsWith('-')) | |
.length; | |
console.log(`Number of changed lines: ${changedLines}`); | |
let label = ''; | |
if (changedLines > 1000) label = 'size:XXL'; | |
else if (changedLines > 499) label = 'size:XL'; | |
else if (changedLines > 99) label = 'size:L'; | |
else if (changedLines > 29) label = 'size:M'; | |
else if (changedLines > 9) label = 'size:S'; | |
else label = 'size:XS'; | |
console.log("Fetching existing labels..."); | |
const labels = await github.rest.issues.listLabelsOnIssue({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
}); | |
const sizeLabels = labels.data | |
.filter(label => label.name.startsWith('size:')) | |
.map(label => label.name); | |
const removeLabels = sizeLabels.filter(sizeLabel => sizeLabel !== label); | |
if (removeLabels.length > 0) { | |
console.log(`Removing existing size labels: ${removeLabels.join(', ')}`); | |
for (const label of removeLabels) { | |
await github.rest.issues.removeLabel({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
name: label, | |
}); | |
} | |
console.log("Existing size labels removed."); | |
} else { | |
console.log("No size labels to remove."); | |
} | |
if (label) { | |
if (sizeLabels.includes(label)) { | |
console.log(`Label "${label}" already applied.`); | |
} else { | |
console.log(`Applying label "${label}" to the pull request...`); | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
labels: [label] | |
}); | |
console.log(`Label "${label}" applied successfully.`); | |
} | |
} else { | |
console.log("No label to apply."); | |
} | |