aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/pr_labels.yaml
blob: efcf96c9dc9bc59d10230f5880a76d45979a0d13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name: PR-labels
on: 
  pull_request_target:
    branches:
      - 'main'
    types:
      - 'opened'
      - 'edited'
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  cancel-in-progress: true
jobs:
  update-labels:
    runs-on: ubuntu-latest
    steps:
      - name: Update PR labels
        id: update-pr-labels
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const { owner, repo } = context.repo;
            const prNumber = context.payload.pull_request.number;
            const description = context.payload.pull_request.body;
            const mapping = [
              ['* New feature', 'new-feature'],
              ['* Experimental feature', 'experimental-feature'],
              ['* Improvement', 'improvement'],
              ['* Performance improvement', 'performance'],
              ['* Bugfix', 'bugfix'],
              ['* Backward incompatible change', 'backward-incompatible'],
              ['* Documentation', 'documentation'],
              ['* Not for changelog', 'not-for-changelog']
            ];
            // remove all labels owned by current script 
            for (let pair of mapping) {
              try {
                const result = await github.rest.issues.removeLabel({
                  owner,
                  repo,
                  issue_number: prNumber,
                  name: pair[1]
                });
                console.log('Removed label', pair[1]);
              } catch(e) {
                console.log(e);
              }
            }
            // add first encountered label
            if (!description) return;
            for (let pair of mapping) {
              if (!description.includes(pair[0])) continue;
              // add label
              try {
                const result = await github.rest.issues.addLabels({
                  owner,
                  repo,
                  issue_number: prNumber,
                  labels: [pair[1]]
                });
                console.log('Added label', pair[1]);
              } catch(e) {
                console.log(e);
              }
              break;
            }