|
| 1 | +name: Copilot Templated Discussions |
| 2 | + |
| 3 | +on: |
| 4 | + discussion: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +jobs: |
| 8 | + label-copilot-discussion: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + if: ${{ contains(github.event.discussion.category.name, 'Copilot') }} |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Get discussion body html |
| 14 | + id: get_discussion_body_html |
| 15 | + env: |
| 16 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 17 | + OWNER: ${{ github.repository_owner }} |
| 18 | + REPO: ${{ github.event.repository.name }} |
| 19 | + DISCUSSION_NUMBER: ${{ github.event.discussion.number }} |
| 20 | + run: | |
| 21 | + gh api graphql -F owner=$OWNER -F name=$REPO -F number=$DISCUSSION_NUMBER -f query=' |
| 22 | + query($owner: String!, $name: String!, $number: Int!) { |
| 23 | + repository(owner: $owner, name: $name){ |
| 24 | + discussion(number: $number) { |
| 25 | + bodyHTML |
| 26 | + id |
| 27 | + } |
| 28 | + } |
| 29 | + }' > discussion_data.json |
| 30 | +
|
| 31 | + echo 'DISCUSSION_BODY_HTML='$(jq -r '.data.repository.discussion.bodyHTML' discussion_data.json) >> $GITHUB_ENV |
| 32 | + echo 'DISCUSSION_ID='$(jq -r '.data.repository.discussion.id' discussion_data.json) >> $GITHUB_ENV |
| 33 | + - run: npm install jsdom |
| 34 | + |
| 35 | + - name: Get selected Copilot feature area |
| 36 | + id: get_selected_feature_area |
| 37 | + uses: actions/github-script@v6 |
| 38 | + with: |
| 39 | + result-encoding: string |
| 40 | + script: | |
| 41 | + try { |
| 42 | + const jsdom = require('jsdom'); |
| 43 | + const { JSDOM } = jsdom; |
| 44 | + const { DISCUSSION_BODY_HTML } = process.env |
| 45 | +
|
| 46 | + const fragment = JSDOM.fragment(DISCUSSION_BODY_HTML); |
| 47 | + const featureAreaHeaders = fragment.querySelectorAll("h3"); |
| 48 | + const featureAreaHeader = Array.from(featureAreaHeaders).find(header => |
| 49 | + header.textContent.trim().toLowerCase().includes('copilot feature area')); |
| 50 | + if (!featureAreaHeader) { |
| 51 | + return ""; |
| 52 | + } |
| 53 | +
|
| 54 | + const selectedAreaElement = featureAreaHeader.nextElementSibling; |
| 55 | + if (!selectedAreaElement) { |
| 56 | + return ""; |
| 57 | + } |
| 58 | +
|
| 59 | + const selectedArea = selectedAreaElement.textContent.trim(); |
| 60 | + // Simplify area matching by converting to lowercase |
| 61 | + const selectedAreaLower = selectedArea.toLowerCase(); |
| 62 | + |
| 63 | + // Valid Copilot feature areas (case insensitive matching) |
| 64 | + const validAreas = { |
| 65 | + "vs code": "VS Code", |
| 66 | + "visual studio": "Visual Studio", |
| 67 | + "jetbrains & xcode": "JetBrains & Xcode", |
| 68 | + "copilot in github": "Copilot in GitHub", |
| 69 | + "copilot workspace": "Copilot Workspace", |
| 70 | + "copilot edits and code review": "Copilot Edits and Code Review", |
| 71 | + "copilot agent mode": "Copilot Agent Mode", |
| 72 | + "copilot enterprise": "Copilot Enterprise", |
| 73 | + "copilot billing or account‑related": "Copilot Billing or Account‑Related", |
| 74 | + "other copilot areas": "Other Copilot Areas" |
| 75 | + }; |
| 76 | + |
| 77 | + // Try to find a matching area (case insensitive) |
| 78 | + for (const [key, value] of Object.entries(validAreas)) { |
| 79 | + if (selectedAreaLower.includes(key)) { |
| 80 | + return value; // Return the properly cased label |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // If no match found, check if it's exactly one of our valid areas |
| 85 | + // This helps with unexpected formatting or spacing |
| 86 | + if (selectedArea && Object.values(validAreas).includes(selectedArea)) { |
| 87 | + return selectedArea; |
| 88 | + } |
| 89 | + |
| 90 | + // Default to "Other Copilot Areas" if we can't find a specific match |
| 91 | + if (selectedArea) { |
| 92 | + return "Other Copilot Areas"; |
| 93 | + } |
| 94 | + |
| 95 | + return ""; |
| 96 | + } catch (error) { |
| 97 | + console.error(error); |
| 98 | + return ""; |
| 99 | + } |
| 100 | + |
| 101 | + - name: Fetch label id for selected area |
| 102 | + id: fetch_label_id |
| 103 | + if: ${{ steps.get_selected_feature_area.outputs.result != '' }} |
| 104 | + env: |
| 105 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 106 | + OWNER: ${{ github.repository_owner }} |
| 107 | + REPO: ${{ github.event.repository.name }} |
| 108 | + AREA: ${{ steps.get_selected_feature_area.outputs.result }} |
| 109 | + run: | |
| 110 | + gh api graphql -F owner=$OWNER -F name=$REPO -F topic="$AREA" -f query=' |
| 111 | + query($owner: String!, $name: String!, $topic: String) { |
| 112 | + repository(owner: $owner, name: $name){ |
| 113 | + labels(first: 1, query: $topic) { |
| 114 | + edges { |
| 115 | + node { |
| 116 | + id |
| 117 | + name |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + }' > repository_label_data.json |
| 123 | +
|
| 124 | + LABEL_ID=$(jq -r '.data.repository.labels.edges[0]?.node?.id // empty' repository_label_data.json) |
| 125 | + if [ -z "$LABEL_ID" ]; then |
| 126 | + echo "No matching label found for the selected area. Skipping labeling step." |
| 127 | + fi |
| 128 | + echo "LABEL_ID=$LABEL_ID" >> $GITHUB_ENV |
| 129 | + |
| 130 | + - name: Label the discussion |
| 131 | + if: ${{ steps.get_selected_feature_area.outputs.result != '' && env.LABEL_ID != '' }} |
| 132 | + env: |
| 133 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 134 | + run: | |
| 135 | + gh api graphql -f query=' |
| 136 | + mutation($labelableId: ID!, $labelIds: [ID!]!) { |
| 137 | + addLabelsToLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) { |
| 138 | + labelable { |
| 139 | + labels(first: 10) { |
| 140 | + edges { |
| 141 | + node { |
| 142 | + id |
| 143 | + name |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + }' -f labelableId=$DISCUSSION_ID -f labelIds[]=$LABEL_ID |
0 commit comments