summaryrefslogtreecommitdiffstats
path: root/.github/workflows/add-issue-header.yml
blob: 1ef9178b95e5f62d38c9f159c4f96b42b22ac477 (plain)
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
name: Add issue header
# Automatically edits an issue's descriptions with a header,
# one of:
#
# - Bug report
# - Crash report
# - Feature or enhancement

on:
  issues:
    types:
      # Only ever run once
      - opened


jobs:
  add-header:
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - uses: actions/github-script@v6
        with:
          # language=JavaScript
          script: |
            // https://devguide.python.org/triage/labels/#type-labels
            const HEADERS = new Map([
              ['type-bug', 'Bug report'],
              ['type-crash', 'Crash report'],
              ['type-feature', 'Feature or enhancement'],
            ]);
            let issue_data = await github.rest.issues.get({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo
            }).then(issue => issue.data);
            let header = '';
            for (const label_data of issue_data.labels) {
                const label_name = (typeof label_data === 'string') ? label_data : label_data.name;
                if (HEADERS.has(label_name)) {
                    header = HEADERS.get(label_name);
                    break;
                }
            }
            if (header !== '') {
              console.log(`Setting new header: ${header}`);
              await github.rest.issues.update({
                  issue_number: context.issue.number,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  body: `# ${header}\n\n${issue_data.body.replaceAll('\r', '')}`
              });
            }