|
| 1 | +name: Auto Assign Project and Fields |
| 2 | +on: |
| 3 | + issues: |
| 4 | + types: [opened] |
| 5 | + |
| 6 | +jobs: |
| 7 | + assign: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + steps: |
| 10 | + - name: Add new issues to Petlibro project |
| 11 | + uses: actions/github-script@v7 |
| 12 | + with: |
| 13 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 14 | + script: | |
| 15 | + const issue = context.payload.issue; |
| 16 | + const labels = issue.labels.map(l => l.name); |
| 17 | +
|
| 18 | + const projectId = "PVT_kwHOAclbns4AqfIH"; // Petlibro Project ID |
| 19 | +
|
| 20 | + // Only process your templates |
| 21 | + const allowedLabels = ["Bug", "Feature Request", "New Device"]; |
| 22 | + if (!labels.some(l => allowedLabels.includes(l))) { |
| 23 | + console.log("Skipping — issue does not match your templates."); |
| 24 | + return; |
| 25 | + } |
| 26 | +
|
| 27 | + // 1️⃣ Add issue to the project |
| 28 | + const addIssue = await github.graphql(` |
| 29 | + mutation($projectId: ID!, $issueId: ID!) { |
| 30 | + addProjectV2ItemById(input: {projectId: $projectId, contentId: $issueId}) { |
| 31 | + item { id } |
| 32 | + } |
| 33 | + } |
| 34 | + `, { |
| 35 | + projectId, |
| 36 | + issueId: issue.node_id |
| 37 | + }); |
| 38 | +
|
| 39 | + const itemId = addIssue.addProjectV2ItemById.item.id; |
| 40 | + console.log(`✅ Added issue #${issue.number} to Petlibro project.`); |
| 41 | +
|
| 42 | + // 2️⃣ Set Priority (Single Select) |
| 43 | + const priorityFieldId = "PVTSSF_lAHOAclbns4AqfIHzghudTQ"; // Priority field ID |
| 44 | + const priorityOptions = { |
| 45 | + "Critical": "79628723", |
| 46 | + "High": "0a877460", |
| 47 | + "Medium": "da944a9c", |
| 48 | + "Low": "0b6337eb", |
| 49 | + "Deferred": "9d531ad0", |
| 50 | + "Completed": "20ffdcc8" |
| 51 | + }; |
| 52 | +
|
| 53 | + let priorityValue = "Medium"; |
| 54 | + if (labels.includes("Bug")) priorityValue = "High"; |
| 55 | + else if (labels.includes("Feature Request")) priorityValue = "Medium"; |
| 56 | + else if (labels.includes("New Device")) priorityValue = "Low"; |
| 57 | +
|
| 58 | + await github.graphql(` |
| 59 | + mutation($itemId: ID!, $fieldId: ID!, $optionId: String!) { |
| 60 | + updateProjectV2ItemFieldValue(input: { |
| 61 | + itemId: $itemId, |
| 62 | + fieldId: $fieldId, |
| 63 | + value: { singleSelectOptionId: $optionId } |
| 64 | + }) { |
| 65 | + item { id } |
| 66 | + } |
| 67 | + } |
| 68 | + `, { |
| 69 | + itemId, |
| 70 | + fieldId: priorityFieldId, |
| 71 | + optionId: priorityOptions[priorityValue] |
| 72 | + }); |
| 73 | + console.log(`✅ Set Priority to ${priorityValue}`); |
| 74 | +
|
| 75 | + // 3️⃣ Set Status dynamically (Single Select) |
| 76 | + const statusFieldId = "PVTSSF_lAHOAclbns4AqfIHzghucgA"; // Status field ID |
| 77 | + const statusOptions = { |
| 78 | + "To triage": "f971fb55", |
| 79 | + "Pending": "f75ad846", |
| 80 | + "Ready": "856cdede", |
| 81 | + "In progress": "47fc9ee4", |
| 82 | + "In review": "5ef0dc97", |
| 83 | + "Deferred": "e0243c67", |
| 84 | + "Done": "98236657" |
| 85 | + }; |
| 86 | +
|
| 87 | + let statusValue = "To triage"; // default |
| 88 | + if (labels.includes("Bug")) statusValue = "Pending"; |
| 89 | + else if (labels.includes("Feature Request")) statusValue = "To triage"; |
| 90 | + else if (labels.includes("New Device")) statusValue = "Ready"; |
| 91 | +
|
| 92 | + await github.graphql(` |
| 93 | + mutation($itemId: ID!, $fieldId: ID!, $optionId: String!) { |
| 94 | + updateProjectV2ItemFieldValue(input: { |
| 95 | + itemId: $itemId, |
| 96 | + fieldId: $fieldId, |
| 97 | + value: { singleSelectOptionId: $optionId } |
| 98 | + }) { |
| 99 | + item { id } |
| 100 | + } |
| 101 | + } |
| 102 | + `, { |
| 103 | + itemId, |
| 104 | + fieldId: statusFieldId, |
| 105 | + optionId: statusOptions[statusValue] |
| 106 | + }); |
| 107 | + console.log(`✅ Set Status to ${statusValue}`); |
| 108 | +
|
| 109 | + // 4️⃣ Optional: Set Start Date (if you have a field) |
| 110 | + // const startDateFieldId = "YOUR_START_DATE_FIELD_ID"; |
| 111 | + // const today = new Date().toISOString().split("T")[0]; // YYYY-MM-DD |
| 112 | + // await github.graphql(` |
| 113 | + // mutation($itemId: ID!, $fieldId: ID!, $value: String!) { |
| 114 | + // updateProjectV2ItemFieldValue(input: { |
| 115 | + // itemId: $itemId, |
| 116 | + // fieldId: $fieldId, |
| 117 | + // value: { date: $value } |
| 118 | + // }) { |
| 119 | + // item { id } |
| 120 | + // } |
| 121 | + // } |
| 122 | + // `, { itemId, fieldId: startDateFieldId, value: today }); |
| 123 | + // console.log(`✅ Set Start Date to ${today}`); |
0 commit comments