mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 19:06:18 +01:00 
			
		
		
		
	Use Project-URL metadata field to get a PyPI package's homepage URL (#33089)
				
					
				
			Resolves #33085.
This commit is contained in:
		| @@ -10,6 +10,7 @@ import ( | ||||
| 	"regexp" | ||||
| 	"sort" | ||||
| 	"strings" | ||||
| 	"unicode" | ||||
|  | ||||
| 	packages_model "code.gitea.io/gitea/models/packages" | ||||
| 	packages_module "code.gitea.io/gitea/modules/packages" | ||||
| @@ -139,9 +140,30 @@ func UploadPackageFile(ctx *context.Context) { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	projectURL := ctx.Req.FormValue("home_page") | ||||
| 	if !validation.IsValidURL(projectURL) { | ||||
| 		projectURL = "" | ||||
| 	// Ensure ctx.Req.Form exists. | ||||
| 	_ = ctx.Req.ParseForm() | ||||
|  | ||||
| 	var homepageURL string | ||||
| 	projectURLs := ctx.Req.Form["project_urls"] | ||||
| 	for _, purl := range projectURLs { | ||||
| 		label, url, found := strings.Cut(purl, ",") | ||||
| 		if !found { | ||||
| 			continue | ||||
| 		} | ||||
| 		if normalizeLabel(label) != "homepage" { | ||||
| 			continue | ||||
| 		} | ||||
| 		homepageURL = strings.TrimSpace(url) | ||||
| 		break | ||||
| 	} | ||||
|  | ||||
| 	if len(homepageURL) == 0 { | ||||
| 		// TODO: Home-page is a deprecated metadata field. Remove this branch once it's no longer apart of the spec. | ||||
| 		homepageURL = ctx.Req.FormValue("home_page") | ||||
| 	} | ||||
|  | ||||
| 	if !validation.IsValidURL(homepageURL) { | ||||
| 		homepageURL = "" | ||||
| 	} | ||||
|  | ||||
| 	_, _, err = packages_service.CreatePackageOrAddFileToExisting( | ||||
| @@ -160,7 +182,7 @@ func UploadPackageFile(ctx *context.Context) { | ||||
| 				Description:     ctx.Req.FormValue("description"), | ||||
| 				LongDescription: ctx.Req.FormValue("long_description"), | ||||
| 				Summary:         ctx.Req.FormValue("summary"), | ||||
| 				ProjectURL:      projectURL, | ||||
| 				ProjectURL:      homepageURL, | ||||
| 				License:         ctx.Req.FormValue("license"), | ||||
| 				RequiresPython:  ctx.Req.FormValue("requires_python"), | ||||
| 			}, | ||||
| @@ -189,6 +211,23 @@ func UploadPackageFile(ctx *context.Context) { | ||||
| 	ctx.Status(http.StatusCreated) | ||||
| } | ||||
|  | ||||
| // Normalizes a Project-URL label. | ||||
| // See https://packaging.python.org/en/latest/specifications/well-known-project-urls/#label-normalization. | ||||
| func normalizeLabel(label string) string { | ||||
| 	var builder strings.Builder | ||||
|  | ||||
| 	// "A label is normalized by deleting all ASCII punctuation and whitespace, and then converting the result | ||||
| 	// to lowercase." | ||||
| 	for _, r := range label { | ||||
| 		if unicode.IsPunct(r) || unicode.IsSpace(r) { | ||||
| 			continue | ||||
| 		} | ||||
| 		builder.WriteRune(unicode.ToLower(r)) | ||||
| 	} | ||||
|  | ||||
| 	return builder.String() | ||||
| } | ||||
|  | ||||
| func isValidNameAndVersion(packageName, packageVersion string) bool { | ||||
| 	return nameMatcher.MatchString(packageName) && versionMatcher.MatchString(packageVersion) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user