Assign user on comment #2
This file contains hidden or 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: Assign user on comment | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
assign-user: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check if comment contains assignment phrase | |
id: check_comment | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const commentBody = context.payload.comment.body.toLowerCase(); | |
const assignPatterns = [ | |
/[Aa]signar a @([a-zA-Z0-9_-]+)/, | |
/[Aa]signar @([a-zA-Z0-9_-]+)/, | |
/[Aa]ssign to @([a-zA-Z0-9_-]+)/, | |
/[Aa]ssign @([a-zA-Z0-9_-]+)/ | |
]; | |
let assignee = null; | |
for (const pattern of assignPatterns) { | |
const match = commentBody.match(pattern); | |
if (match && match[1]) { | |
assignee = match[1]; | |
break; | |
} | |
} | |
if (assignee) { | |
return { assignee, should_assign: true }; | |
} | |
return { should_assign: false }; | |
- name: Assign user | |
if: ${{ fromJson(steps.check_comment.outputs.result).should_assign == 'true' }} | |
uses: actions/github-script@v7 | |
env: | |
ASSIGNEE: ${{ fromJson(steps.check_comment.outputs.result).assignee }} | |
with: | |
script: | | |
try { | |
await github.rest.issues.addAssignees({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.issue.number, | |
assignees: ['${{ env.ASSIGNEE }}'] | |
}); | |
console.log(`Successfully assigned @${{ env.ASSIGNEE }}`); | |
} catch (error) { | |
console.error('Error assigning user:', error); | |
if (error.status === 403) { | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.issue.number, | |
body: `⚠️ No tengo permisos para asignar usuarios. Por favor, un mantenedor debe asignar a @${{ env.ASSIGNEE }} manualmente.` | |
}); | |
} else if (error.status === 404) { | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.payload.issue.number, | |
body: `⚠️ El usuario @${{ env.ASSIGNEE }} no existe o no es miembro de este repositorio.` | |
}); | |
} | |
} |