aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/issue-gate.yml
blob: fdeaea46e825b059cfed1b58e91845a81e46da65 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: Issue Gate

on:
  issues:
    types: [opened]

permissions:
  issues: write

jobs:
  check-template:
    if: ${{ !github.event.issue.pull_request }}
    runs-on: ubuntu-latest

    steps:
      - name: Close non-template issues
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
        with:
          github-token: ${{ secrets.KANGAL_GITHUB_TOKEN }}
          script: |
            const issue = context.payload.issue;
            const author = issue.user.login;
            const sender = context.payload.sender?.login ?? author;
            const bugConfirmationPattern = /^\s*-\s*\[[xX]\]\s*I confirm this is a reproducible bug, not a feature request, idea, question, contribution proposal, or direction check\.\s*$/m;
            const requiredSections = [
              '### Is this a reproducible bug?',
              '### Current behavior',
              '### Expected behavior',
              '### Reproduction',
              '### Impact',
              '### Environment',
            ];
            const requiredEnvironmentFields = [
              /^\s*(?:-\s*)?Herdr version:[^\S\r\n]*\S.*$/m,
              /^\s*(?:-\s*)?Operating system:[^\S\r\n]*\S.*$/m,
              /^\s*(?:-\s*)?Terminal:[^\S\r\n]*\S.*$/m,
            ];

            function escapeRegExp(value) {
              return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
            }

            function extractSection(body, heading) {
              const pattern = new RegExp(`^${escapeRegExp(heading)}\\s*$`, 'm');
              const match = pattern.exec(body);
              if (!match) {
                return null;
              }

              const sectionStart = match.index + match[0].length;
              const rest = body.slice(sectionStart);
              const headingIndex = requiredSections.indexOf(heading);
              const nextTemplateHeading = requiredSections
                .slice(headingIndex + 1)
                .map((section) => {
                  const nextPattern = new RegExp(`^${escapeRegExp(section)}\\s*$`, 'm');
                  const nextMatch = nextPattern.exec(rest);
                  return nextMatch ? nextMatch.index : -1;
                })
                .filter((index) => index !== -1)
                .sort((left, right) => left - right)[0];
              return (nextTemplateHeading === undefined ? rest : rest.slice(0, nextTemplateHeading)).trim();
            }

            function hasContent(section) {
              return typeof section === 'string' && section.trim().length > 0;
            }

            async function getPermission(username) {
              try {
                const { data: permissionLevel } = await github.rest.repos.getCollaboratorPermissionLevel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  username,
                });
                return permissionLevel.permission;
              } catch {
                return null;
              }
            }

            const maintainerPermissions = ['admin', 'maintain', 'write'];
            const authorPermission = await getPermission(author);
            if (maintainerPermissions.includes(authorPermission)) {
              console.log(`${author} is a collaborator with ${authorPermission} access; leaving issue open`);
              return;
            }

            const senderPermission = sender === author ? authorPermission : await getPermission(sender);
            if (maintainerPermissions.includes(senderPermission)) {
              console.log(`${sender} opened this issue with ${senderPermission} access; leaving issue open`);
              return;
            }

            const body = issue.body || '';
            const hasBugConfirmation = bugConfirmationPattern.test(body);
            const hasBugTemplate = requiredSections.every((section) => body.includes(section));
            const currentBehavior = extractSection(body, '### Current behavior');
            const expectedBehavior = extractSection(body, '### Expected behavior');
            const reproduction = extractSection(body, '### Reproduction');
            const impact = extractSection(body, '### Impact');
            const environment = extractSection(body, '### Environment');
            const hasRequiredContent = [
              currentBehavior,
              expectedBehavior,
              reproduction,
              impact,
            ].every(hasContent);
            const hasEnvironmentFields = hasContent(environment) && requiredEnvironmentFields.every((pattern) => pattern.test(environment));
            if (hasBugConfirmation && hasBugTemplate && hasRequiredContent && hasEnvironmentFields) {
              console.log(`#${issue.number} matches the bug report template`);
              return;
            }

            if (hasBugConfirmation && hasBugTemplate && hasRequiredContent && !hasEnvironmentFields) {
              const message = [
                `hi @${author}, thanks for opening this.`,
                '',
                'this bug report is missing required environment details. please edit the issue and fill in herdr version, operating system, and terminal.',
                '',
                'shell and relevant config are optional, but they help when they affect the bug.',
              ].join('\n');

              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: issue.number,
                body: message,
              });

              console.log(`#${issue.number} is missing required environment details; leaving issue open for correction`);
              return;
            }

            const message = [
              `hi @${author}, thanks for opening this.`,
              '',
              'this issue does not appear to use the full bug report template. herdr issues are only for reproducible bugs and maintainer-created or maintainer-converted work items.',
              '',
              'bug reports must include the reproducible-bug confirmation, current behavior, expected behavior, reproduction, impact, and environment fields with herdr version, operating system, and terminal.',
              '',
              'feature requests, ideas, questions, contribution proposals, and direction checks belong in discussions:',
              `https://github.com/${context.repo.owner}/${context.repo.repo}/discussions`,
              '',
              'please read the contributing guidelines before opening issues or prs:',
              `https://github.com/${context.repo.owner}/${context.repo.repo}/blob/${context.payload.repository.default_branch}/CONTRIBUTING.md`,
              '',
              'closing this so the issue tracker stays limited to actionable bug reports.',
            ].join('\n');

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issue.number,
              body: message,
            });

            await github.rest.issues.update({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issue.number,
              state: 'closed',
              state_reason: 'not_planned',
            });