-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_pr_description.js
More file actions
103 lines (85 loc) · 3.09 KB
/
generate_pr_description.js
File metadata and controls
103 lines (85 loc) · 3.09 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
(async () => {
const [, , diffFile] = process.argv;
if (!diffFile) {
console.error('Usage: generate_pr_description.js <diff_file>');
process.exit(1);
}
if (!fs.existsSync(diffFile)) {
console.error(`Error: Diff file not found at ${diffFile}`);
process.exit(1);
}
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
console.error('Error: GEMINI_API_KEY environment variable is required');
process.exit(1);
}
// Create prompt for PR description generation
const promptTemplate = `Write a concise pull request description based on the git diff. Use this exact format:
## Description
Brief summary of changes (1-2 sentences max).
## Changes
- [ ] Key change 1
- [ ] Key change 2
- [ ] Key change 3 (max 5 items)
## Verification
- [ ] Test step 1
- [ ] Test step 2
- [ ] Test step 3 (max 3 items)
Keep it concise and focused on the most important changes.`;
const diffContent = fs.readFileSync(diffFile, 'utf8');
const combinedPrompt = `${promptTemplate}\n\nHere is the git diff:\n\n${diffContent}`;
try {
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{
parts: [{
text: combinedPrompt
}]
}],
generationConfig: {
temperature: 0.7,
topK: 40,
topP: 0.95,
maxOutputTokens: 8192,
}
})
});
if (!response.ok) {
const errorText = await response.text();
console.error(`Error: Gemini API request failed with status ${response.status}`);
console.error(`Response: ${errorText}`);
process.exit(1);
}
const json = await response.json();
if (!json.candidates || !json.candidates[0]) {
console.error('Error: Invalid response from Gemini API');
console.error(JSON.stringify(json, null, 2));
process.exit(1);
}
// Check if response was truncated due to max tokens
if (json.candidates[0].finishReason === 'MAX_TOKENS') {
console.error('Warning: Response was truncated due to token limit. Consider reducing diff size or using more specific ignore-files.');
// Continue processing the partial response
}
if (!json.candidates[0].content) {
console.error('Error: No content in API response');
console.error(JSON.stringify(json, null, 2));
process.exit(1);
}
if (!json.candidates[0].content.parts || !json.candidates[0].content.parts[0] || !json.candidates[0].content.parts[0].text) {
console.error('Error: Invalid response structure from Gemini API - missing parts or text');
console.error(JSON.stringify(json, null, 2));
process.exit(1);
}
const result = json.candidates[0].content.parts[0].text;
process.stdout.write(result);
} catch (error) {
console.error(`Error: Failed to generate pull request description: ${error.message}`);
process.exit(1);
}
})();