Skip to content

chore: update to actions/checkout v6 #29

chore: update to actions/checkout v6

chore: update to actions/checkout v6 #29

# Managed by https://github.com/linkorb/repo-ansible. Manual changes will be overwritten.
name: Auto-label Pull Request
permissions:
contents: read
pull-requests: write # Allow writing labels on PRs
on:
pull_request_target:
types: [opened, edited, reopened, synchronize] # trigger when PR is opened, description edited, or commits change
jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Label pull requests based on conventional commits
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const prNumber = context.payload.pull_request.number;
const prBody = context.payload.pull_request.body || "";
const breakingChangeLabel = "BREAKING CHANGE";
// Fetch all commits from the PR
const { data: commits } = await github.rest.pulls.listCommits({
owner,
repo,
pull_number: prNumber
});
// Define Conventional Commit prefixes and corresponding labels
const labelMap = {
"feat": "feat",
"fix": "fix",
"chore": "chore",
"test": "test",
"docs": "docs",
"style": "style",
"perf": "perf",
"revert": "revert",
"refactor": "refactor",
"ci": "ci",
"chore": "chore",
};
// Get current labels on the PR
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: prNumber
});
const existingLabels = new Set(currentLabels.map(label => label.name));
const labelsToAdd = new Set();
const labelsToRemove = new Set(existingLabels);
// Inspect commit messages
commits.forEach(commit => {
const commitMessage = commit.commit.message;
for (const [prefix, label] of Object.entries(labelMap)) {
if (commitMessage.startsWith(prefix)) {
labelsToAdd.add(label);
labelsToRemove.delete(label);
}
}
});
// Apply label updates
if (labelsToAdd.size > 0) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: Array.from(labelsToAdd)
});
}
for (const label of labelsToRemove) {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: prNumber,
name: label
}).catch(error => {
if (error.status !== 404) throw error;
});
}
const { data: updatedCurrentLabels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: prNumber
});
// Handle "BREAKING CHANGE" label
const hasBreakingChangeLabel = updatedCurrentLabels.some(label => label.name === breakingChangeLabel);
if (prBody.includes("[x] Breaking change") && !hasBreakingChangeLabel) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: [breakingChangeLabel]
});
} else if (!prBody.includes("[x] Breaking change") && hasBreakingChangeLabel) {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: prNumber,
name: breakingChangeLabel
});
}