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: Handle Low Quality Label | |
on: | |
issues: | |
types: [labeled] | |
pull_request: | |
types: [labeled] | |
jobs: | |
handle_low_quality: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check and Handle Low Quality Label | |
if: ${{ github.event.label.name == 'low quality' }} | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const github = require('@actions/github'); | |
// Define the labels and the issue/PR number | |
const labelToAdd = 'invalid'; | |
const issue_number = github.context.payload.issue ? github.context.payload.issue.number : github.context.payload.pull_request.number; | |
// Get the existing labels | |
const existingLabels = github.context.payload.issue ? github.context.payload.issue.labels : github.context.payload.pull_request.labels; | |
const labelExists = existingLabels.some(label => label.name === labelToAdd); | |
// Function to add a label | |
async function addLabel() { | |
await github.rest.issues.addLabels({ | |
owner: github.context.repo.owner, | |
repo: github.context.repo.repo, | |
issue_number: issue_number, | |
labels: [labelToAdd] | |
}); | |
console.log(`Label "${labelToAdd}" added to issue/pr #${issue_number}.`); | |
} | |
// Function to close the issue or PR | |
async function closeIssueOrPR() { | |
await github.rest.issues.update({ | |
owner: github.context.repo.owner, | |
repo: github.context.repo.repo, | |
issue_number: issue_number, | |
state: 'closed' | |
}); | |
console.log(`Issue/PR #${issue_number} closed.`); | |
} | |
// Add the 'invalid' label if not already present | |
if (!labelExists) { | |
await addLabel(); | |
} else { | |
console.log(`Label "${labelToAdd}" already exists on issue/pr #${issue_number}. No action taken.`); | |
} | |
// Close the issue or PR | |
await closeIssueOrPR(); |