|
| 1 | +name: Apply Labels from Issue Form |
| 2 | + |
| 3 | +# trigger the workflow when a new issue is opened |
| 4 | +on: |
| 5 | + issues: |
| 6 | + types: [opened] |
| 7 | + |
| 8 | +# provide the write permission to add labels to issue |
| 9 | +permissions: |
| 10 | + issues: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + add_labels: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Extract and apply label from issue body |
| 17 | + uses: actions/github-script@v7 |
| 18 | + with: |
| 19 | + script: | |
| 20 | + // get the full text body of the issue |
| 21 | + const issueBody = context.payload.issue.body; |
| 22 | +
|
| 23 | + // seperate the issue content by each new line (\n) |
| 24 | + const lines = issueBody.split('\n'); |
| 25 | +
|
| 26 | + // initialize a reference for storing the selected label |
| 27 | + let selectedLabel = null; |
| 28 | +
|
| 29 | + // iterate the each line of the issue content |
| 30 | + for (let i = 0; i < lines.length; i++) { |
| 31 | +
|
| 32 | + // check if the main heading of the drop down section is found or not |
| 33 | + if (lines[i].includes('Select applicable labels')) { |
| 34 | + // main heading of the drop down section is found |
| 35 | +
|
| 36 | + // find the first non-empty line as the label |
| 37 | + for (let j = i + 1; j < lines.length; j++) { |
| 38 | + // get the line |
| 39 | + const candidate = lines[j].trim(); |
| 40 | + |
| 41 | + // check if the line is empty or not |
| 42 | + if (candidate) { |
| 43 | + // line is not empty |
| 44 | + selectedLabel = candidate; |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | +
|
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + // check if the selected label available or not |
| 54 | + if (selectedLabel) { |
| 55 | + // selected label available and construct the necessary credentials for creating the new label object |
| 56 | + const repoOwner = context.repo.owner; |
| 57 | + const repoName = context.repo.repo; |
| 58 | + const issueNumber = context.issue.number; |
| 59 | + const labels = [selectedLabel]; |
| 60 | +
|
| 61 | + // apply the all the credentials to the issue |
| 62 | + await github.rest.issues.addLabels({ |
| 63 | + owner: repoOwner, |
| 64 | + repo: repoName, |
| 65 | + issue_number: issueNumber, |
| 66 | + labels: labels |
| 67 | + }); |
| 68 | + } |
0 commit comments