Skip to content

Commit bf6502a

Browse files
authored
Fix incorrect relative/absolute URL usages (#29531)
Add two "HTMLURL" methods for PackageDescriptor. And rename "FullWebLink" to "VersionWebLink"
1 parent 70c126e commit bf6502a

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

models/packages/descriptor.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,26 @@ type PackageFileDescriptor struct {
7070
Properties PackagePropertyList
7171
}
7272

73-
// PackageWebLink returns the package web link
73+
// PackageWebLink returns the relative package web link
7474
func (pd *PackageDescriptor) PackageWebLink() string {
7575
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HomeLink(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName))
7676
}
7777

78-
// FullWebLink returns the package version web link
79-
func (pd *PackageDescriptor) FullWebLink() string {
78+
// VersionWebLink returns the relative package version web link
79+
func (pd *PackageDescriptor) VersionWebLink() string {
8080
return fmt.Sprintf("%s/%s", pd.PackageWebLink(), url.PathEscape(pd.Version.LowerVersion))
8181
}
8282

83+
// PackageHTMLURL returns the absolute package HTML URL
84+
func (pd *PackageDescriptor) PackageHTMLURL() string {
85+
return fmt.Sprintf("%s/-/packages/%s/%s", pd.Owner.HTMLURL(), string(pd.Package.Type), url.PathEscape(pd.Package.LowerName))
86+
}
87+
88+
// VersionHTMLURL returns the absolute package version HTML URL
89+
func (pd *PackageDescriptor) VersionHTMLURL() string {
90+
return fmt.Sprintf("%s/%s", pd.PackageHTMLURL(), url.PathEscape(pd.Version.LowerVersion))
91+
}
92+
8393
// CalculateBlobSize returns the total blobs size in bytes
8494
func (pd *PackageDescriptor) CalculateBlobSize() int64 {
8595
size := int64(0)

routers/api/packages/npm/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
packages_model "code.gitea.io/gitea/models/packages"
1414
npm_module "code.gitea.io/gitea/modules/packages/npm"
15+
"code.gitea.io/gitea/modules/setting"
1516
)
1617

1718
func createPackageMetadataResponse(registryURL string, pds []*packages_model.PackageDescriptor) *npm_module.PackageMetadata {
@@ -98,7 +99,7 @@ func createPackageSearchResponse(pds []*packages_model.PackageDescriptor, total
9899
Maintainers: []npm_module.User{}, // npm cli needs this field
99100
Keywords: metadata.Keywords,
100101
Links: &npm_module.PackageSearchPackageLinks{
101-
Registry: pd.FullWebLink(),
102+
Registry: setting.AppURL + "api/packages/" + pd.Owner.Name + "/npm",
102103
Homepage: metadata.ProjectURL,
103104
},
104105
},

routers/web/user/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func RedirectToLastVersion(ctx *context.Context) {
163163
return
164164
}
165165

166-
ctx.Redirect(pd.FullWebLink())
166+
ctx.Redirect(pd.VersionWebLink())
167167
}
168168

169169
// ViewPackageVersion displays a single package version

services/convert/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func ToPackage(ctx context.Context, pd *packages.PackageDescriptor, doer *user_m
3535
Name: pd.Package.Name,
3636
Version: pd.Version.Version,
3737
CreatedAt: pd.Version.CreatedUnix.AsTime(),
38-
HTMLURL: pd.FullWebLink(),
38+
HTMLURL: pd.VersionHTMLURL(),
3939
}, nil
4040
}
4141

0 commit comments

Comments
 (0)