forked from coinbase/x402
-
Notifications
You must be signed in to change notification settings - Fork 0
173 lines (152 loc) · 6.55 KB
/
update-docs.yml
File metadata and controls
173 lines (152 loc) · 6.55 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
name: Update Docs
on:
push:
branches: [main]
paths-ignore:
- 'docs/**'
- '**/*.test.ts'
- '**/*.test.js'
- '**/__tests__/**'
- '**/test/**'
- '**/tests/**'
- '**/typescript/site/**'
- 'typescript/packages/legacy/**'
- 'go/legacy/**'
- 'python/legacy/**'
jobs:
update-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
MINTLIFY_API_KEY: ${{ secrets.MINTLIFY_API_KEY }}
PROJECT_ID: ${{ secrets.MINTLIFY_PROJECT_ID }}
with:
script: |
const { owner, repo } = context.repo;
const projectId = process.env.PROJECT_ID;
const apiKey = process.env.MINTLIFY_API_KEY;
if (!projectId || !apiKey) {
core.setFailed('Missing MINTLIFY_PROJECT_ID or MINTLIFY_API_KEY secrets');
return;
}
// Get SHAs from push payload
const beforeSha = context.payload.before;
const afterSha = context.payload.after;
// Skip initial commits (no previous SHA to compare against)
if (/^0+$/.test(beforeSha)) {
core.notice('Initial commit detected. Skipping documentation update.');
return;
}
// Get changed files using GitHub Compare API (reliable for all merge types)
let uniqueFiles = [];
try {
const compareResponse = await github.rest.repos.compareCommits({
owner,
repo,
base: beforeSha,
head: afterSha
});
// Extract filenames, excluding test files and legacy packages
const excludePattern = /\.(test|spec)\.(ts|js|tsx|jsx)$|__tests__|\/test\/|\/tests\/|typescript\/packages\/legacy\/|go\/legacy\/|python\/legacy\//;
uniqueFiles = (compareResponse.data.files || [])
.map(f => f.filename)
.filter(f => !excludePattern.test(f));
} catch (error) {
core.setFailed('Failed to compare commits: ' + error.message);
return;
}
// Log changed files for debugging
console.log('Changed files (' + uniqueFiles.length + '):', uniqueFiles.slice(0, 10));
if (uniqueFiles.length > 10) {
console.log('... and ' + (uniqueFiles.length - 10) + ' more');
}
// Skip if no non-test files changed
if (uniqueFiles.length === 0) {
core.notice('No documentation-relevant files changed. Skipping agent trigger.');
return;
}
// Get commit message from head commit
const headCommit = context.payload.head_commit;
const commitMessage = headCommit
? '- ' + headCommit.message.split('\n')[0]
: '- No commit message available';
// Compare URL for reference
const compareUrl = context.payload.compare || '';
// Build the system prompt
const systemPrompt = [
'You are a documentation updater that ONLY updates docs directly related to specific code changes.',
'',
'CRITICAL RULES:',
'- ONLY update documentation that is directly affected by the code changes provided',
'- DO NOT perform general documentation audits or sync operations',
'- DO NOT add documentation for unrelated features, ecosystem partners, or missing content',
'- If the code changes do not require documentation updates, report "No documentation updates needed" and exit without creating a PR',
'- Focus on the specific files and commits mentioned, not the entire repository state',
'- Read the AGENTS.md file in the docs directory for additional guidance'
].join('\n');
// Build the user prompt
const userPrompt = [
'Review these SPECIFIC code changes and update documentation ONLY if directly related:',
'',
'**Repository:** ' + owner + '/' + repo,
'**Compare URL:** ' + compareUrl,
'',
'**Changed Files (' + uniqueFiles.length + '):**',
uniqueFiles.join('\n'),
'',
'**Commit Message:**',
commitMessage,
'',
'IMPORTANT: Only update documentation that is directly impacted by these specific file changes. Do not add documentation for unrelated features or perform general audits. If these changes do not affect documentation, do NOT create a PR.'
].join('\n');
const url = 'https://api.mintlify.com/v1/agent/' + projectId + '/job';
const payload = {
branch: 'mintlify/docs-update-' + Date.now(),
messages: [
{
role: 'system',
content: systemPrompt
},
{
role: 'user',
content: userPrompt
}
],
asDraft: false
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error('API request failed with status ' + response.status + ': ' + errorText);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.trim()) {
console.log(line);
}
}
}
if (buffer.trim()) {
console.log(buffer);
}
core.notice('Documentation update job triggered for ' + owner + '/' + repo);
} catch (error) {
core.setFailed('Failed to create documentation update job: ' + error.message);
}