PR Issue Labeler #9
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: PR Label Management | |
on: | |
pull_request: | |
types: [opened, edited, reopened, synchronize] | |
workflow_dispatch: | |
jobs: | |
manage-labels: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Check PR descriptions and manage labels | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const { owner, repo } = context.repo; | |
function extractIssueNumber(description) { | |
const match = description?.match(/#(\d+)/); | |
return match ? match[1] : null; | |
} | |
async function addLabelsFromIssue(issueNumber, prNumber) { | |
try { | |
console.log(`Fetching labels for issue #${issueNumber}`); | |
const issue = await github.rest.issues.get({ | |
owner, | |
repo, | |
issue_number: issueNumber | |
}); | |
if (issue.data.labels.length > 0) { | |
const labels = issue.data.labels.map(label => label.name); | |
console.log(`Adding labels to PR #${prNumber}: ${labels.join(', ')}`); | |
await github.rest.issues.addLabels({ | |
owner, | |
repo, | |
issue_number: prNumber, | |
labels: labels | |
}); | |
console.log(`Labels added successfully to PR #${prNumber}`); | |
} else { | |
console.log(`No labels found on the linked issue #${issueNumber}`); | |
} | |
} catch (error) { | |
console.error(`Error in addLabelsFromIssue for PR #${prNumber}: ${error.message}`); | |
} | |
} | |
async function processOpenPRs() { | |
let page = 1; | |
let openPRs = []; | |
while (true) { | |
const response = await github.rest.pulls.list({ | |
owner, | |
repo, | |
state: 'open', | |
per_page: 100, | |
page: page | |
}); | |
if (response.data.length === 0) break; | |
openPRs = openPRs.concat(response.data); | |
if (response.data.length < 100) break; | |
page++; | |
} | |
console.log(`Found ${openPRs.length} open PRs`); | |
for (const pr of openPRs) { | |
console.log(`Processing PR #${pr.number}`); | |
console.log(`PR Body: ${pr.body}`); | |
const issueNumber = extractIssueNumber(pr.body); | |
if (issueNumber) { | |
console.log(`Issue number found: #${issueNumber}`); | |
await addLabelsFromIssue(issueNumber, pr.number); | |
} else { | |
console.log('No issue number found in PR description'); | |
await github.rest.issues.createComment({ | |
owner, | |
repo, | |
issue_number: pr.number, | |
body: 'Please update the PR description to include the related issue number (e.g., #123).' | |
}); | |
} | |
} | |
} | |
try { | |
console.log('Starting PR label management'); | |
if (context.eventName === 'pull_request') { | |
const pr = context.payload.pull_request; | |
console.log(`Processing single PR #${pr.number} from event payload`); | |
const issueNumber = extractIssueNumber(pr.body); | |
if (issueNumber) { | |
await addLabelsFromIssue(issueNumber, pr.number); | |
} else { | |
console.log('No issue number found in PR description'); | |
await github.rest.issues.createComment({ | |
owner, | |
repo, | |
issue_number: pr.number, | |
body: 'Please update the PR description to include the related issue number (e.g., #123).' | |
}); | |
} | |
} else if (context.eventName === 'workflow_dispatch') { | |
console.log('Manual trigger: processing all open PRs'); | |
await processOpenPRs(); | |
} | |
console.log('PR label management completed successfully'); | |
} catch (error) { | |
console.error(`Error in main script execution: ${error.message}`); | |
core.setFailed(`Action failed with error: ${error.message}`); | |
} |