-
Notifications
You must be signed in to change notification settings - Fork 0
213 lines (170 loc) · 8.08 KB
/
process-node-documentation.yml
File metadata and controls
213 lines (170 loc) · 8.08 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
name: Process Node Documentation Issue
on:
issues:
types: [opened]
jobs:
process-node-issue:
if: contains(github.event.issue.labels.*.name, 'documentation')
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Parse issue and update node data
id: parse_issue
env:
ISSUE_BODY: ${{ github.event.issue.body }}
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
function parseIssueBody(body) {
console.log('Parsing issue body:', body);
// Extract the JavaScript config object from the code block
const configMatch = body.match(/```javascript\s*([\s\S]*?)\n```/);
if (!configMatch) {
throw new Error('No JavaScript config object found in issue body');
}
let configString = configMatch[1].trim();
// Remove any comment lines at the beginning
configString = configString.replace(/^\/\/.*\n/gm, '');
// Clean up the config string - remove comments and ensure it's valid JSON
configString = configString
.replace(/\/\/.*$/gm, '') // Remove line comments
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove block comments
.replace(/,(\s*[}\]])/g, '$1'); // Remove trailing commas
// Try to parse as JavaScript object by wrapping in parentheses
let nodeConfig;
try {
nodeConfig = eval('(' + configString + ')');
} catch (error) {
console.error('Failed to parse config:', error);
throw new Error('Invalid JavaScript config object format');
}
// Parse long description
const longDescMatch = body.match(/\*\*Long Description:\*\*\s*\n([\s\S]*?)(?=\n\*\*[^*]|\n##|\n---|\n```|$)/);
if (longDescMatch) {
nodeConfig.longDescription = longDescMatch[1].trim();
}
// Parse use cases
const useCasesSection = body.match(/\*\*Use Cases:\*\*\s*\n((?:- [^\n]*\n?)*)/);
if (useCasesSection) {
nodeConfig.useCases = useCasesSection[1]
.split('\n')
.filter(line => line.trim().startsWith('- '))
.map(line => line.replace(/^- /, '').trim())
.filter(useCase => useCase);
} else {
nodeConfig.useCases = [];
}
// Transform difficulty to diff
if (nodeConfig.difficulty) {
nodeConfig.diff = nodeConfig.difficulty;
delete nodeConfig.difficulty;
}
// Ensure icon object has required fields
if (!nodeConfig.icon) {
nodeConfig.icon = { type: '', content: '' };
} else {
if (!nodeConfig.icon.type) nodeConfig.icon.type = '';
if (!nodeConfig.icon.content) nodeConfig.icon.content = '';
}
console.log('Parsed node:', JSON.stringify(nodeConfig, null, 2));
return nodeConfig;
}
const issueBody = process.env.ISSUE_BODY;
const nodeData = parseIssueBody(issueBody);
// Debug: Log parsed data
console.log('Final parsed data:', JSON.stringify(nodeData, null, 2));
// Validate required fields from config object
const requiredFields = ['title', 'category', 'type', 'desc', 'diff'];
const missingFields = requiredFields.filter(field => !nodeData[field]);
if (missingFields.length > 0) {
core.setFailed(`Missing required fields in config object: ${missingFields.join(', ')}`);
return;
}
// Validate difficulty
const validDifficulties = ['easy', 'medium', 'hard'];
if (!validDifficulties.includes(nodeData.diff)) {
core.setFailed(`Invalid difficulty: ${nodeData.diff}. Must be one of: ${validDifficulties.join(', ')}`);
return;
}
// Warn if longDescription or useCases are missing but don't fail
if (!nodeData.longDescription) {
console.log('Warning: longDescription is missing');
}
if (!nodeData.useCases || nodeData.useCases.length === 0) {
console.log('Warning: useCases are missing');
}
// Read current node-data.js file
const nodeDataPath = 'lib/node-data.js';
let fileContent = fs.readFileSync(nodeDataPath, 'utf8');
// Find the nodeData array and add the new node
const nodeDataString = JSON.stringify(nodeData, null, 2);
const insertPosition = fileContent.lastIndexOf('];');
if (insertPosition === -1) {
core.setFailed('Could not find nodeData array in node-data.js');
return;
}
// Insert the new node before the closing bracket
const beforeArray = fileContent.substring(0, insertPosition - 1);
const afterArray = fileContent.substring(insertPosition);
const newContent = beforeArray + ',\n ' + nodeDataString.split('\n').join('\n ') + '\n' + afterArray;
fs.writeFileSync(nodeDataPath, newContent);
core.setOutput('node_title', nodeData.title);
core.setOutput('node_type', nodeData.type);
- name: Create branch and commit changes
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
# Create new branch
branch_name="add-node-${{ steps.parse_issue.outputs.node_type }}-$(date +%s)"
git checkout -b "$branch_name"
# Commit changes
git add lib/node-data.js
git commit -m "Add ${{ steps.parse_issue.outputs.node_title }} node documentation"
# Push branch
git push origin "$branch_name"
echo "BRANCH_NAME=$branch_name" >> $GITHUB_ENV
- name: Create Pull Request
uses: actions/github-script@v7
with:
script: |
const { data: pullRequest } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `Add ${{ steps.parse_issue.outputs.node_title }} node documentation`,
head: process.env.BRANCH_NAME,
base: 'main',
body: `
## 📚 Node Documentation Added
This PR adds documentation for the **${{ steps.parse_issue.outputs.node_title }}** node.
### Changes
- Added new node to \`lib/node-data.js\`
- Node type: \`${{ steps.parse_issue.outputs.node_type }}\`
### Related Issue
Closes #${{ github.event.issue.number }}
### Review Checklist
- [ ] Node information is complete and accurate
- [ ] All required fields are present
- [ ] Documentation follows the established format
- [ ] No syntax errors in the updated file
---
*This PR was automatically created from issue #${{ github.event.issue.number }}*
`
});
// Comment on the original issue
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `✅ **Documentation processed successfully!**
A pull request has been created: #${pullRequest.number}
The node documentation will be reviewed and merged if everything looks correct.`
});