-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
347 lines (302 loc) · 9.42 KB
/
Copy pathindex.js
File metadata and controls
347 lines (302 loc) · 9.42 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const core = require("@actions/core")
const github = require("@actions/github")
const { Configuration, OpenAIApi } = require("openai")
const languageNames = {
en: "English",
sv: "Swedish",
fr: "French",
de: "German",
es: "Spanish",
it: "Italian",
ja: "Japanese",
ko: "Korean",
pt: "Portuguese",
ru: "Russian",
zh: "Chinese",
nl: "Dutch",
pl: "Polish",
tr: "Turkish",
ar: "Arabic",
da: "Danish",
fi: "Finnish",
no: "Norwegian",
cs: "Czech",
hu: "Hungarian",
ro: "Romanian",
sk: "Slovak",
th: "Thai",
vi: "Vietnamese",
id: "Indonesian",
hi: "Hindi",
he: "Hebrew",
bg: "Bulgarian",
el: "Greek",
hr: "Croatian",
lt: "Lithuanian",
sl: "Slovenian",
et: "Estonian",
lv: "Latvian",
ms: "Malay",
sw: "Swahili",
tl: "Tagalog",
uk: "Ukrainian",
bg: "Bulgarian",
is: "Icelandic",
sk: "Slovak",
sr: "Serbian",
cy: "Welsh",
ca: "Catalan",
bn: "Bengali",
sw: "Swahili",
}
/**
* Fetches release data from GitHub repository
*/
async function fetchReleaseData(octokit, owner, repo, releaseTag) {
if (releaseTag === "latest") {
// Get latest release
const { data: latestRelease } = await octokit.rest.repos.getLatestRelease({
owner,
repo,
})
return latestRelease
} else {
// Get specific release
const { data: specificRelease } = await octokit.rest.repos.getReleaseByTag({
owner,
repo,
tag: releaseTag,
})
return specificRelease
}
}
/**
* Generates user-friendly release notes in multiple languages using OpenAI
*/
async function generateReleaseNotes(
originalReleaseNotes,
maxLength,
openaiApiKey,
languages = ["en", "sv", "fr"]
) {
// Initialize OpenAI
const configuration = new Configuration({
apiKey: openaiApiKey,
})
const openai = new OpenAIApi(configuration)
// Generate language tags for prompt
const languageTags = languages.map((lang) => {
return {
code: lang,
name: languageNames[lang] || lang.toUpperCase(),
outputTag: `<${lang}_release_notes>`,
}
})
// Language description for prompt
const languageDescription = languageTags
.map((lang) => `${lang.name} (${lang.code})`)
.join(", ")
// Output format instructions for prompt
const outputFormatInstructions = languageTags
.map(
(lang) =>
`<${lang.code}_release_notes>\n[${lang.name} version here]\n</${lang.code}_release_notes>`
)
.join("\n\n")
// New prompt template
const promptTemplate = `
You are an AI assistant specialized in creating user-friendly release notes for mobile apps. Your task is to convert technical GitHub release notes into consumer-friendly descriptions suitable for app stores, and then translate them into multiple languages.
First, here are the original GitHub release notes:
<original_release_notes>
{{ORIGINAL_RELEASE_NOTES}}
</original_release_notes>
And here is the maximum character length for the final release notes:
<max_length>
{{MAX_LENGTH}}
</max_length>
Please follow these steps to create the release notes in the following languages: {{LANGUAGES}}
1. Read through the original release notes carefully.
2. Inside <release_notes_planning> tags, analyze the notes and plan your approach:
a. List key features, improvements, and bug fixes that will be most relevant to end-users.
b. Brainstorm user-friendly phrasing for each item.
c. Plan the structure (bullet points vs. paragraph).
d. Consider idiomatic expressions or culture-specific phrases for each language.
e. Draft a sample English version and count its characters to ensure it's within the limit.
3. Create versions of the release notes for each requested language, following these guidelines:
- Remove any technical details or implementation specifics.
- Omit very minor changes that users won't notice.
- Focus on new features, improvements, and bug fixes that impact the user experience.
- Keep it concise but informative.
- Use bullet points if there are multiple changes.
- Ensure a friendly and enthusiastic tone.
- Stay within the specified character limit.
- Ensure the translations feel native and idiomatic to each language.
- Be cautious with technical terms - use the accepted term in each language rather than a literal translation.
- Maintain the friendly and enthusiastic tone in each language.
4. Present your final output in the following format:
{{OUTPUT_FORMAT}}
Remember to check that each language version adheres to the character limit and captures the essence of the updates in a user-friendly manner.
`
// Replace placeholders in the template
const prompt = promptTemplate
.replace("{{ORIGINAL_RELEASE_NOTES}}", originalReleaseNotes)
.replace("{{MAX_LENGTH}}", maxLength.toString())
.replace("{{LANGUAGES}}", languageDescription)
.replace("{{OUTPUT_FORMAT}}", outputFormatInstructions)
// Call OpenAI API
const response = await openai.createChatCompletion({
model: "gpt-4o",
messages: [
{
role: "system",
content:
"You are a helpful assistant that transforms technical release notes into user-friendly app store release notes in multiple languages.",
},
{ role: "user", content: prompt },
],
max_tokens: 3000,
temperature: 0.7,
})
return response.data.choices[0].message.content
}
/**
* Parses OpenAI response to extract language-specific release notes
*/
function parseReleaseNotes(assistantResponse, languages = ["en", "sv", "fr"]) {
const result = {}
// Parse each language
for (const lang of languages) {
const regex = new RegExp(
`<${lang}_release_notes>([\\s\\S]*?)</${lang}_release_notes>`,
"i"
)
const match = assistantResponse.match(regex)
result[lang] = match ? match[1].trim() : ""
}
return result
}
/**
* Creates a formatted markdown string with all release notes combined
*/
function createCombinedReleaseNotes(releaseNotes, languages) {
let combinedText = "# Combined Release Notes\n\n"
for (const lang of languages) {
const langName = languageNames[lang] || lang.toUpperCase()
combinedText += `## ${langName} (${lang})\n\n`
combinedText +=
releaseNotes[lang] || "*No release notes available for this language.*"
combinedText += "\n\n"
}
return combinedText.trim()
}
/**
* Main function that can be used both in GitHub Actions and standalone
*/
async function generateAppStoreReleaseNotes({
githubToken,
owner,
repo,
releaseTag = "latest",
openaiApiKey,
maxLength = 500,
languages = ["en", "sv", "fr"],
}) {
try {
// Initialize GitHub client
const octokit = github.getOctokit(githubToken)
// Get release info
const releaseData = await fetchReleaseData(octokit, owner, repo, releaseTag)
// Extract original release notes
const originalReleaseNotes = releaseData.body || ""
// Generate release notes
const assistantResponse = await generateReleaseNotes(
originalReleaseNotes,
maxLength,
openaiApiKey,
languages
)
// Parse the response
const releaseNotes = parseReleaseNotes(assistantResponse, languages)
// Create combined release notes
const combinedReleaseNotes = createCombinedReleaseNotes(
releaseNotes,
languages
)
return {
success: true,
releaseNotes,
originalReleaseNotes,
releaseData,
languages,
combinedReleaseNotes,
}
} catch (error) {
return {
success: false,
error: error.message || String(error),
}
}
}
/**
* Function to run as GitHub Action
*/
async function runAsGitHubAction() {
try {
// Get inputs from GitHub Actions
const githubToken = core.getInput("github_token", { required: true })
const releaseTag = core.getInput("release_tag")
const openaiApiKey = core.getInput("openai_api_key", { required: true })
const maxLength = parseInt(core.getInput("max_length"))
const languagesInput = core.getInput("languages")
// Parse languages input
const languages = languagesInput
? languagesInput.split(",").map((lang) => lang.trim().toLowerCase())
: ["en", "sv", "fr"]
const context = github.context
const owner = context.repo.owner
const repo = context.repo.repo
// Run the main function
const result = await generateAppStoreReleaseNotes({
githubToken,
owner,
repo,
releaseTag,
openaiApiKey,
maxLength,
languages,
})
if (!result.success) {
core.setFailed(`Action failed with error: ${result.error}`)
return
}
// Set combined output as JSON for more robust handling
core.setOutput("release_notes", JSON.stringify(result.releaseNotes))
core.setOutput("language_codes", languages.join(","))
core.setOutput("combined_release_notes", result.combinedReleaseNotes)
// Set individual outputs for backward compatibility
for (const [lang, notes] of Object.entries(result.releaseNotes)) {
core.setOutput(`${lang}_release_notes`, notes)
}
// Log success
console.log(
`Successfully generated app store release notes in ${
languages.length
} languages: ${languages.join(", ")}`
)
} catch (error) {
core.setFailed(`Action failed with error: ${error}`)
}
}
// Run as GitHub Action if this is the entry point
if (require.main === module) {
runAsGitHubAction()
}
// Export functions for testing and reuse
module.exports = {
fetchReleaseData,
generateReleaseNotes,
parseReleaseNotes,
generateAppStoreReleaseNotes,
runAsGitHubAction,
createCombinedReleaseNotes,
}