-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean-issue-optional-fields.yml
More file actions
122 lines (97 loc) · 4.23 KB
/
clean-issue-optional-fields.yml
File metadata and controls
122 lines (97 loc) · 4.23 KB
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
name: Clean Issue Optional Fields
# This workflow automatically cleans up issue bodies when they are created by
# removing sections that contain "_No response_" (which appears in optional fields
# when users don't fill them out). This helps keep issues looking clean and minimal.
on:
issues:
types: [opened]
permissions:
issues: write
contents: read
jobs:
clean-optional-fields:
runs-on: ubuntu-latest
steps:
- name: Clean issue body
uses: actions/github-script@v7
with:
script: |
const issueNumber = context.issue.number;
const issueBody = context.payload.issue.body;
if (!issueBody) {
console.log('No issue body to clean');
return;
}
console.log('Original issue body:');
console.log(issueBody);
// Clean the issue body by removing sections with "_No response_"
function cleanIssueBody(body) {
const lines = body.split('\n');
const cleanedLines = [];
let i = 0;
while (i < lines.length) {
const line = lines[i];
// Check if current line is a heading (starts with #)
if (line.match(/^#+\s+/)) {
// Look ahead to see if the next non-empty line contains "_No response_"
let nextContentIndex = i + 1;
// Skip empty lines and find the next content line
while (nextContentIndex < lines.length &&
lines[nextContentIndex].trim() === '') {
nextContentIndex++;
}
// If next content line contains "_No response_", skip this section
if (nextContentIndex < lines.length &&
lines[nextContentIndex].includes('_No response_')) {
console.log(`Removing section: ${line.trim()}`);
// Skip the heading line
i++;
// Skip all content until we reach the next heading or end of body
while (i < lines.length && !lines[i].match(/^#+\s+/)) {
i++;
}
continue;
}
}
// Keep this line
cleanedLines.push(line);
i++;
}
// Clean up excessive empty lines
const finalLines = [];
let consecutiveEmptyLines = 0;
for (let i = 0; i < cleanedLines.length; i++) {
const line = cleanedLines[i];
if (line.trim() === '') {
consecutiveEmptyLines++;
// Allow at most 2 consecutive empty lines
if (consecutiveEmptyLines <= 2) {
finalLines.push(line);
}
} else {
consecutiveEmptyLines = 0;
finalLines.push(line);
}
}
// Remove trailing empty lines
while (finalLines.length > 0 &&
finalLines[finalLines.length - 1].trim() === '') {
finalLines.pop();
}
return finalLines.join('\n');
}
const cleanedBody = cleanIssueBody(issueBody);
// Only update if the body actually changed
if (cleanedBody !== issueBody) {
console.log('Cleaned issue body:');
console.log(cleanedBody);
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: cleanedBody
});
console.log('Issue body updated successfully');
} else {
console.log('No changes needed - issue body already clean');
}