Skip to content

Commit d74b4f2

Browse files
fleizeanclaude
andcommitted
fix(pypi): find the repository in the README when project_urls has none
Selenium lists only www.selenium.dev in its metadata; its GitHub repository appears only in the package description. A repository named after the package is taken from there when no project link points at GitHub. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent f3f90be commit d74b4f2

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

‎src/backend/PkgDiff.Infrastructure/Services/PyPIService.cs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,29 @@ private static bool IsGitHubUrl(string? url)
163163
.Concat(links.Where(l => !NotRepositoryKeys.Any(k => l.Key.Contains(k, StringComparison.OrdinalIgnoreCase)))
164164
.Select(l => l.Value));
165165

166-
return candidates.Select(RepositoryRoot).FirstOrDefault(url => url != null);
166+
return candidates.Select(RepositoryRoot).FirstOrDefault(url => url != null)
167+
?? RepositoryFromDescription(info);
167168
}
168169

170+
// Some packages link only their website (selenium: www.selenium.dev) and name the repository in the
171+
// README alone. Only a repository named after the package counts there: READMEs link other projects too.
172+
private static string? RepositoryFromDescription(PyPIPackageInfo info)
173+
{
174+
if (string.IsNullOrEmpty(info.Description) || string.IsNullOrEmpty(info.Name))
175+
return null;
176+
177+
var name = NormalizeName(info.Name);
178+
return System.Text.RegularExpressions.Regex.Matches(info.Description,
179+
@"https?://(?:www\.)?github\.com/[\w.-]+/[\w.-]+",
180+
System.Text.RegularExpressions.RegexOptions.IgnoreCase)
181+
.Select(m => RepositoryRoot(m.Value.TrimEnd('.')))
182+
.FirstOrDefault(url => url != null && NormalizeName(url[(url.LastIndexOf('/') + 1)..]) == name);
183+
}
184+
185+
// PEP 503: names compare case-insensitively with runs of "-", "_" and "." as one "-"
186+
private static string NormalizeName(string name) =>
187+
System.Text.RegularExpressions.Regex.Replace(name, @"[-_.]+", "-").ToLowerInvariant();
188+
169189
// https://github.com/{owner}/{repo} of a GitHub link (issues, a changelog file, releases), keeping a
170190
// /tree/{ref}/{folder} part that names the package folder of a monorepo; null for any other link
171191
private static string? RepositoryRoot(string? url)
@@ -245,6 +265,9 @@ private class PyPIPackageInfo
245265

246266
[JsonPropertyName("project_urls")]
247267
public Dictionary<string, string>? ProjectUrls { get; set; }
268+
269+
[JsonPropertyName("description")]
270+
public string? Description { get; set; }
248271
}
249272

250273
private class PyPIReleaseFile

0 commit comments

Comments
 (0)