|
| 1 | +name: Monthly Issue Labeler |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + label-issue: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + permissions: |
| 11 | + issues: write |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Add month-year label |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const date = new Date(); |
| 19 | + const monthNames = [ |
| 20 | + "January", "February", "March", "April", "May", "June", |
| 21 | + "July", "August", "September", "October", "November", "December" |
| 22 | + ]; |
| 23 | + |
| 24 | + const month = monthNames[date.getMonth()]; |
| 25 | + const year = date.getFullYear(); |
| 26 | + const labelName = `${month} ${year}`; |
| 27 | + |
| 28 | + console.log(`Creating/applying label: ${labelName}`); |
| 29 | + |
| 30 | + // Define a set of 12 standard colors (no black or white) |
| 31 | + const standardColors = [ |
| 32 | + "e11d21", // Red |
| 33 | + "fbca04", // Yellow |
| 34 | + "009800", // Green |
| 35 | + "006b75", // Teal |
| 36 | + "207de5", // Blue |
| 37 | + "0052cc", // Dark Blue |
| 38 | + "5319e7", // Purple |
| 39 | + "d93f0b", // Orange |
| 40 | + "fbad50", // Light Orange |
| 41 | + "fc6399", // Pink |
| 42 | + "c5def5", // Light Blue |
| 43 | + "bfdadc" // Pale Teal |
| 44 | + ]; |
| 45 | + |
| 46 | + // Function to get a color from the standard colors |
| 47 | + function getStandardColor() { |
| 48 | + const randomIndex = Math.floor(Math.random() * standardColors.length); |
| 49 | + return standardColors[randomIndex]; |
| 50 | + } |
| 51 | + |
| 52 | + // Check if the label exists, create it if it doesn't |
| 53 | + try { |
| 54 | + await github.rest.issues.getLabel({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + name: labelName |
| 58 | + }); |
| 59 | + console.log(`Label ${labelName} already exists`); |
| 60 | + } catch (error) { |
| 61 | + if (error.status === 404) { |
| 62 | + const selectedColor = getStandardColor(); |
| 63 | + console.log(`Creating new label: ${labelName} with color #${selectedColor}`); |
| 64 | + await github.rest.issues.createLabel({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + name: labelName, |
| 68 | + color: selectedColor |
| 69 | + }); |
| 70 | + } else { |
| 71 | + throw error; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Apply the label to the issue |
| 76 | + await github.rest.issues.addLabels({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + issue_number: context.issue.number, |
| 80 | + labels: [labelName] |
| 81 | + }); |
0 commit comments