Added Solution to Gfg q104 #4035
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: Auto Assign | |
on: | |
issues: | |
types: [opened, edited] | |
# Trigger on issue opened or edited events | |
pull_request: | |
types: [opened, edited] | |
# Trigger on pull request opened or edited events | |
issue_comment: | |
types: [created] | |
# Trigger on new comments on issues or pull requests | |
jobs: | |
run: | |
runs-on: ubuntu-latest | |
permissions: | |
issues: write | |
pull-requests: write | |
# Ensure write permissions are granted | |
steps: | |
- name: Check for /assign command | |
id: check_assign_command | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const isCommentEvent = context.eventName === 'issue_comment'; | |
let assignee = ''; | |
let issueOrPrNumber = ''; | |
if (isCommentEvent) { | |
const body = context.payload.comment.body; | |
const assignees = body.match(/\/assign(?:\s+@([^\s]+))?/); | |
if (assignees) { | |
assignee = assignees[1] || `${issueCreator}`; // Default assignee if not specified in command | |
issueOrPrNumber = context.payload.issue ? context.payload.issue.number : context.payload.pull_request.number; | |
} | |
} | |
core.setOutput('assignee', assignee); | |
core.setOutput('issueOrPrNumber', issueOrPrNumber); | |
- name: Auto-assign issue or PR | |
if: steps.check_assign_command.outputs.issueOrPrNumber != '' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { owner, repo } = context.repo; | |
const issue_number = parseInt('${{ steps.check_assign_command.outputs.issueOrPrNumber }}', 10); | |
const assignee = '${{ steps.check_assign_command.outputs.assignee }}'; | |
const response = await github.rest.issues.addAssignees({ | |
owner, | |
repo, | |
issue_number, | |
assignees: [assignee] | |
}); | |
console.log(response); |