|
| 1 | +--- |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +# This workflow utilizes https://github.com/actions/github-script in GitHub Actions to apply labels to pull requests. |
| 17 | +# |
| 18 | +name: Label PR |
| 19 | +on: [pull_request_target] |
| 20 | +jobs: |
| 21 | + label: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + pull-requests: write |
| 26 | + steps: |
| 27 | + - name: Label PR |
| 28 | + uses: actions/github-script@v6 |
| 29 | + with: |
| 30 | + script: |- |
| 31 | + const keywords = { |
| 32 | + "breaking": "kind/breaking", |
| 33 | + "bug": "kind/bug", |
| 34 | + "enhancement": "kind/enhancement", |
| 35 | + "cleanup": "kind/cleanup", |
| 36 | + "documentation": "kind/documentation" |
| 37 | + }; |
| 38 | + const prBody = context.payload.pull_request.body; |
| 39 | + const prLabels = []; |
| 40 | + if (prBody === null || prBody.trim() === "") { |
| 41 | + console.log("Pull Request body is empty"); |
| 42 | + prLabels.push("kind/other"); |
| 43 | + } else { |
| 44 | + const regex = /^\s*\/kind\s+(.+)$/m; |
| 45 | + const match = prBody.match(regex); |
| 46 | + console.log(`PR body: '${prBody}'`); |
| 47 | + console.log(`Regex match: '${match}'`); |
| 48 | + if (match && match[1] in keywords) { |
| 49 | + const keyword = match[1]; |
| 50 | + const label = keywords[keyword]; |
| 51 | + console.log(`Adding label: '${label}' based on keyword '${keyword}'`); |
| 52 | + prLabels.push(label); |
| 53 | + } else { |
| 54 | + console.log(`Adding label: 'kind/other' as no matching keyword found.`); |
| 55 | + prLabels.push("kind/other"); |
| 56 | + } |
| 57 | + } |
| 58 | + try { |
| 59 | + github.rest.issues.addLabels({ |
| 60 | + owner: context.repo.owner, |
| 61 | + repo: context.repo.repo, |
| 62 | + issue_number: context.payload.pull_request.number, |
| 63 | + labels: prLabels |
| 64 | + }); |
| 65 | + } catch (error) { |
| 66 | + console.error(`Error retrieving files: ${error}`); |
| 67 | + } |
0 commit comments