|
1 | | -name: Take Issue |
2 | | -permissions: read-all |
| 1 | +name: Handle Issue Assignments |
3 | 2 |
|
4 | 3 | on: |
5 | 4 | issue_comment: |
6 | | - types: |
7 | | - - created |
8 | | - - edited |
| 5 | + types: [created] |
| 6 | + issues: |
| 7 | + types: [assigned, unassigned] |
| 8 | + |
| 9 | +permissions: read-all |
9 | 10 |
|
10 | 11 | jobs: |
11 | | - take-issue: |
12 | | - name: Take issue |
| 12 | + handle-issue: |
13 | 13 | runs-on: ubuntu-latest |
| 14 | + timeout-minutes: 10 |
14 | 15 | permissions: |
15 | 16 | issues: write |
16 | | - timeout-minutes: 10 |
| 17 | + repository-projects: write |
17 | 18 | steps: |
18 | | - - name: take an issue |
19 | | - uses: bdougie/take-action@1439165ac45a7461c2d89a59952cd7d941964b87 # v1.6.1 |
| 19 | + - name: Process issue |
| 20 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v6 |
20 | 21 | with: |
21 | | - message: Thank you for looking into this issue! Please let us know if you have any questions or require any help. |
22 | | - issueCurrentlyAssignedMessage: Thanks for being interested in this issue. It looks like this ticket is already assigned to a contributor. Please communicate with the assigned contributor to confirm the status of the issue. |
23 | | - trigger: .take |
24 | | - token: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + script: | |
| 24 | + const LABEL = "good first issue"; |
| 25 | + const COLUMNS = { |
| 26 | + todo: "Contributors Needed", |
| 27 | + inProgress: "Assigned", |
| 28 | + done: "Closed" |
| 29 | + }; |
| 30 | +
|
| 31 | + const issue = context.payload.issue; |
| 32 | +
|
| 33 | + async function getProjectId() { |
| 34 | + try { |
| 35 | + const projects = await github.rest.projects.listForRepo({ |
| 36 | + owner: context.repo.owner, |
| 37 | + repo: context.repo.repo |
| 38 | + }); |
| 39 | + const project = projects.data.find(p => p.name === "Good first issues"); |
| 40 | + if (!project) throw new Error("Project not found"); |
| 41 | + return project.id; |
| 42 | + } catch (err) { |
| 43 | + console.log("Error fetching project ID:", err); |
| 44 | + throw err; |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + async function moveIssue(columnName) { |
| 49 | + if (!issue.labels?.some(l => l.name.toLowerCase() === LABEL)) { |
| 50 | + console.log(`Issue #${issue.number} - not a good first issue, skipping`); |
| 51 | + return; |
| 52 | + } |
| 53 | +
|
| 54 | + try { |
| 55 | + const PROJECT_ID = await getProjectId(); |
| 56 | + const columns = await github.rest.projects.listColumns({ project_id: PROJECT_ID }); |
| 57 | + const column = columns.data.find(c => c.name === columnName); |
| 58 | + if (!column) throw new Error(`Can't find column ${columnName}`); |
| 59 | +
|
| 60 | + const cards = await github.rest.projects.listCards({ column_id: column.id }); |
| 61 | + const existingCard = cards.data.find(c => c.content_url.endsWith(`/issues/${issue.number}`)); |
| 62 | +
|
| 63 | + if (existingCard) { |
| 64 | + await github.rest.projects.moveCard({ |
| 65 | + card_id: existingCard.id, |
| 66 | + position: "top", |
| 67 | + column_id: column.id |
| 68 | + }); |
| 69 | + } else { |
| 70 | + await github.rest.projects.createCard({ |
| 71 | + column_id: column.id, |
| 72 | + content_id: issue.id, |
| 73 | + content_type: "Issue" |
| 74 | + }); |
| 75 | + } |
| 76 | +
|
| 77 | + console.log(`Updated issue #${issue.number} to ${columnName}`); |
| 78 | + } catch (err) { |
| 79 | + console.log(`Error updating issue #${issue.number}:`, err); |
| 80 | + throw err; |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + // Handle comment-based triggers (.take and .release) |
| 85 | + if (context.payload.comment) { |
| 86 | + const comment = context.payload.comment.body.trim(); |
| 87 | + const user = context.payload.comment.user.login; |
| 88 | +
|
| 89 | + if (comment === ".take") { |
| 90 | + if (!issue.labels?.some(l => l.name.toLowerCase() === LABEL)) { |
| 91 | + console.log(`Issue #${issue.number} - not a good first issue, ignoring .take command`); |
| 92 | + return; |
| 93 | + } |
| 94 | + if (issue.assignees?.length) { |
| 95 | + const currentAssignee = issue.assignees[0].login; |
| 96 | + await github.rest.issues.createComment({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + issue_number: issue.number, |
| 100 | + body: `@${user} this issue is already assigned to a contributor.` |
| 101 | + }); |
| 102 | + console.log(`Issue #${issue.number} already assigned to ${currentAssignee}, ignoring .take from ${user}`); |
| 103 | + return; |
| 104 | + } |
| 105 | + await github.rest.issues.addAssignees({ |
| 106 | + owner: context.repo.owner, |
| 107 | + repo: context.repo.repo, |
| 108 | + issue_number: issue.number, |
| 109 | + assignees: [user] |
| 110 | + }); |
| 111 | + // No need to move issue here as the assignment event will trigger that |
| 112 | + } |
| 113 | +
|
| 114 | + if (comment === ".release") { |
| 115 | + await github.rest.issues.removeAssignees({ |
| 116 | + owner: context.repo.owner, |
| 117 | + repo: context.repo.repo, |
| 118 | + issue_number: issue.number, |
| 119 | + assignees: [user] |
| 120 | + }); |
| 121 | + // No need to move issue here as the unassignment event will trigger that |
| 122 | + } |
| 123 | + } |
| 124 | +
|
| 125 | + // Handle assignment/unassignment events |
| 126 | + if (context.payload.action === "assigned" || context.payload.action === "unassigned") { |
| 127 | + const hasAssignee = issue.assignees?.length > 0; |
| 128 | + await moveIssue(hasAssignee ? COLUMNS.inProgress : COLUMNS.todo); |
| 129 | + } |
0 commit comments