-
-
Notifications
You must be signed in to change notification settings - Fork 219
135 lines (120 loc) · 5.79 KB
/
update-docs.yml
File metadata and controls
135 lines (120 loc) · 5.79 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
name: Auto-update Documentation
on:
pull_request:
types: [closed]
jobs:
update-docs:
# Only run if PR was actually merged (not just closed)
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Get PR details
id: pr_details
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_DIFF=$(gh api \
-H "Accept: application/vnd.github.v3.diff" \
/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }})
FILES_CHANGED=$(gh api \
/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files \
--jq '[.[] | .filename] | join(", ")')
echo "pr_diff<<EOF" >> $GITHUB_OUTPUT
echo "$PR_DIFF" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "files_changed=$FILES_CHANGED" >> $GITHUB_OUTPUT
- name: Call Mintlify Agent API
uses: actions/github-script@v8
env:
MINTLIFY_API_KEY: ${{ secrets.MINTLIFY_API_KEY }}
PROJECT_ID: ${{ secrets.MINTLIFY_PROJECT_ID }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_URL: ${{ github.event.pull_request.html_url }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_DIFF: ${{ steps.pr_details.outputs.pr_diff }}
FILES_CHANGED: ${{ steps.pr_details.outputs.files_changed }}
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;
}
const prBody = process.env.PR_BODY || 'No description provided';
const userMessage = [
`You are a documentation maintainer for ${owner}/${repo}. This is a monorepo: application code and documentation live in the same repository. All documentation files are under the \`docs/\` directory (Mintlify config at \`docs/docs.json\`). Scope every edit to \`docs/\` only — do not modify application code. Open the documentation PR against this same repository (${owner}/${repo}).`,
``,
`## Merged PR Details`,
`- **PR**: #${process.env.PR_NUMBER} — ${process.env.PR_TITLE}`,
`- **URL**: ${process.env.PR_URL}`,
`- **Files Changed**: ${process.env.FILES_CHANGED}`,
``,
`## PR Description`,
prBody,
``,
`## Code Changes (Diff)`,
'```diff',
process.env.PR_DIFF,
'```',
``,
`## Instructions`,
`First, review the merged diff and the repository's \`docs/\` directory to assess whether documentation changes are actually needed. If no relevant updates are needed, make no file changes and do not open a PR.`,
``,
`If updates are needed, categorize the change and update only the appropriate docs under \`docs/\`. We have two main types of docs:`,
`1. **Feature docs** (how users use features)`,
`2. **Contributing docs** (engineering concepts for technical contributors) live under \`docs/contributing/\` and \`docs/evals/\``,
``,
`Keep edits minimal, precise, and user-focused. When adding/moving pages, update \`docs/docs.json\` to keep navigation accurate. Use concise commit messages and open a focused PR only when changes are required. Use .mmd files for diagrams and other visual content if needed.`,
].join('\n');
const url = `https://api.mintlify.com/v1/agent/${projectId}/job`;
const payload = {
branch: `mintlify/docs-update-pr-${process.env.PR_NUMBER}-${Date.now()}`,
messages: [
{
role: 'system',
content: 'You are an action runner that updates documentation based on code changes. You should never ask questions. If you are not able to access the repository, report the error and exit.'
},
{
role: 'user',
content: userMessage
}
],
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) {
throw new Error(`API request failed with status ${response.status}: ${await response.text()}`);
}
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} PR #${process.env.PR_NUMBER}`);
} catch (error) {
core.setFailed(`Failed to create documentation update job: ${error.message}`);
}