Skip to content

Commit b25bb34

Browse files
committed
refactor: simplify README fetching logic
- Replace complex logic for fetching and prioritizing localized README variants with a single request for the root `README.md` file. - Remove the use of `coroutineScope`, `async`, and `awaitAll` for concurrent README path attempts. - Simplify the fetch process to target `https://raw.githubusercontent.com/{owner}/{repo}/{branch}/README.md` directly. - Maintain markdown preprocessing and language detection for the fetched `README.md`. - Remove unused imports related to coroutines and the legacy readme helper attempts.
1 parent 64dc2d5 commit b25bb34

1 file changed

Lines changed: 18 additions & 129 deletions

File tree

feature/details/data/src/commonMain/kotlin/zed/rainxch/details/data/repository/DetailsRepositoryImpl.kt

Lines changed: 18 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import io.ktor.client.request.get
55
import io.ktor.client.request.header
66
import io.ktor.client.request.parameter
77
import io.ktor.http.HttpHeaders
8-
import kotlinx.coroutines.CoroutineStart
9-
import kotlinx.coroutines.async
10-
import kotlinx.coroutines.awaitAll
11-
import kotlinx.coroutines.coroutineScope
128
import kotlinx.serialization.Serializable
139
import zed.rainxch.core.data.cache.CacheManager
1410
import zed.rainxch.core.data.cache.CacheManager.CacheTtl.README
@@ -276,136 +272,29 @@ class DetailsRepositoryImpl(
276272
repo: String,
277273
defaultBranch: String,
278274
): Triple<String, String?, String>? {
279-
val attempts = readmeHelper.generateReadmeAttempts()
280275
val baseUrl = "https://raw.githubusercontent.com/$owner/$repo/$defaultBranch/"
281-
val primaryLang = localizationManager.getPrimaryLanguageCode()
276+
val path = "README.md"
282277

283-
logger.debug(
284-
"Attempting to fetch README for language preference: ${localizationManager.getCurrentLanguageCode()}",
285-
)
286-
287-
val foundReadmes =
288-
coroutineScope {
289-
attempts
290-
.map { attempt ->
291-
async(start = CoroutineStart.LAZY) {
292-
try {
293-
logger.debug("Trying ${attempt.path} (priority: ${attempt.priority})...")
294-
295-
val rawMarkdown =
296-
httpClient
297-
.executeRequest<String> {
298-
get("$baseUrl${attempt.path}")
299-
}.getOrNull()
300-
301-
if (rawMarkdown != null) {
302-
logger.debug("Successfully fetched ${attempt.path}")
303-
304-
val processed =
305-
preprocessMarkdown(
306-
markdown = rawMarkdown,
307-
baseUrl = baseUrl,
308-
)
309-
310-
val detectedLang = readmeHelper.detectReadmeLanguage(processed)
311-
logger.debug("Detected language: ${detectedLang ?: "unknown"} for ${attempt.path}")
312-
313-
attempt to Pair(processed, detectedLang)
314-
} else {
315-
null
316-
}
317-
} catch (e: Throwable) {
318-
logger.debug("Failed to fetch ${attempt.path}: ${e.message}")
319-
null
320-
}
321-
}
322-
}.also { asyncTasks ->
323-
asyncTasks.take(6).forEach { it.start() }
324-
}.awaitAll()
325-
.filterNotNull()
326-
.associateBy({ it.first }, { it.second })
327-
}
328-
329-
if (foundReadmes.isEmpty()) {
330-
logger.error("Failed to fetch any README variant.")
331-
return null
332-
}
333-
334-
foundReadmes.entries
335-
.firstOrNull { (attempt, content) ->
336-
attempt.filename != "README.md" && content.second == primaryLang
337-
}?.let { (attempt, content) ->
338-
logger.debug("Found localized README matching user language: ${attempt.path}")
339-
return Triple(content.first, content.second, attempt.path)
340-
}
341-
342-
foundReadmes.entries
343-
.firstOrNull { (attempt, _) ->
344-
attempt.filename.contains(".$primaryLang.", ignoreCase = true) ||
345-
attempt.filename.contains("-${primaryLang.uppercase()}.", ignoreCase = true)
346-
}?.let { (attempt, content) ->
347-
logger.debug("Found explicit language file for user: ${attempt.path}")
348-
return Triple(content.first, content.second ?: primaryLang, attempt.path)
349-
}
350-
351-
foundReadmes.entries
352-
.firstOrNull { (attempt, content) ->
353-
attempt.filename == "README.md" && content.second == primaryLang
354-
}?.let { (attempt, content) ->
355-
logger.debug("Default README matches user language: ${attempt.path}")
356-
return Triple(content.first, content.second, attempt.path)
357-
}
358-
359-
if (primaryLang == "en") {
360-
foundReadmes.entries
361-
.firstOrNull { (_, content) ->
362-
content.second == "en"
363-
}?.let { (attempt, content) ->
364-
logger.debug("Found English README for English user: ${attempt.path}")
365-
return Triple(content.first, content.second, attempt.path)
366-
}
367-
}
368-
369-
foundReadmes.entries
370-
.firstOrNull { (_, content) ->
371-
content.second == primaryLang
372-
}?.let { (attempt, content) ->
373-
logger.debug("Fallback: Using README matching user language: ${attempt.path}")
374-
return Triple(content.first, content.second, attempt.path)
375-
}
376-
377-
if (primaryLang == "en") {
378-
foundReadmes.entries
379-
.firstOrNull { (_, content) ->
380-
content.second == "en"
381-
}?.let { (attempt, content) ->
382-
logger.debug("Fallback: Using English README: ${attempt.path}")
383-
return Triple(content.first, content.second, attempt.path)
384-
}
385-
}
386-
387-
foundReadmes.entries
388-
.firstOrNull { (attempt, _) ->
389-
attempt.path == "README.md"
390-
}?.let { (attempt, content) ->
391-
logger.debug("Fallback: Using root README.md (language: ${content.second}): ${attempt.path}")
392-
return Triple(content.first, content.second, attempt.path)
393-
}
278+
return try {
279+
val rawMarkdown =
280+
httpClient
281+
.executeRequest<String> {
282+
get("$baseUrl$path")
283+
}.getOrNull()
394284

395-
foundReadmes.entries
396-
.firstOrNull { (attempt, _) ->
397-
attempt.path.startsWith(".github/")
398-
}?.let { (attempt, content) ->
399-
logger.debug("Fallback: Using .github README: ${attempt.path}")
400-
return Triple(content.first, content.second, attempt.path)
285+
if (rawMarkdown != null) {
286+
val processed = preprocessMarkdown(markdown = rawMarkdown, baseUrl = baseUrl)
287+
val detectedLang = readmeHelper.detectReadmeLanguage(processed)
288+
logger.debug("Fetched README.md (detected language: ${detectedLang ?: "unknown"})")
289+
Triple(processed, detectedLang, path)
290+
} else {
291+
logger.error("Failed to fetch README.md for $owner/$repo")
292+
null
401293
}
402-
403-
foundReadmes.entries.minByOrNull { it.key.priority }?.let { (attempt, content) ->
404-
logger.debug("Fallback: Using highest priority README: ${attempt.path}")
405-
return Triple(content.first, content.second, attempt.path)
294+
} catch (e: Throwable) {
295+
logger.error("Failed to fetch README.md: ${e.message}")
296+
null
406297
}
407-
408-
return null
409298
}
410299

411300
override suspend fun getRepoStats(

0 commit comments

Comments
 (0)