Skip to content

Commit 646d2c3

Browse files
Merge branch 'main' into feat-32257-add-comments-unchanged-lines-and-show
2 parents fe0efb8 + ead716d commit 646d2c3

File tree

11 files changed

+58
-28
lines changed

11 files changed

+58
-28
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ insert_final_newline = false
1717

1818
[templates/swagger/v1_json.tmpl]
1919
indent_style = space
20+
insert_final_newline = false
2021

2122
[templates/user/auth/oidc_wellknown.tmpl]
2223
indent_style = space

.github/workflows/files-changed.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ jobs:
8585
8686
swagger:
8787
- "templates/swagger/v1_json.tmpl"
88+
- "templates/swagger/v1_input.json"
8889
- "Makefile"
8990
- "package.json"
9091
- "package-lock.json"

Makefile

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ ifdef DEPS_PLAYWRIGHT
165165
endif
166166

167167
SWAGGER_SPEC := templates/swagger/v1_json.tmpl
168-
SWAGGER_SPEC_S_TMPL := s|"basePath": *"/api/v1"|"basePath": "{{AppSubUrl \| JSEscape}}/api/v1"|g
169-
SWAGGER_SPEC_S_JSON := s|"basePath": *"{{AppSubUrl \| JSEscape}}/api/v1"|"basePath": "/api/v1"|g
168+
SWAGGER_SPEC_INPUT := templates/swagger/v1_input.json
170169
SWAGGER_EXCLUDE := code.gitea.io/sdk
171-
SWAGGER_NEWLINE_COMMAND := -e '$$a\'
172170

173171
TEST_MYSQL_HOST ?= mysql:3306
174172
TEST_MYSQL_DBNAME ?= testgitea
@@ -271,10 +269,8 @@ endif
271269
.PHONY: generate-swagger
272270
generate-swagger: $(SWAGGER_SPEC) ## generate the swagger spec from code comments
273271

274-
$(SWAGGER_SPEC): $(GO_SOURCES_NO_BINDATA)
275-
$(GO) run $(SWAGGER_PACKAGE) generate spec -x "$(SWAGGER_EXCLUDE)" -o './$(SWAGGER_SPEC)'
276-
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
277-
$(SED_INPLACE) $(SWAGGER_NEWLINE_COMMAND) './$(SWAGGER_SPEC)'
272+
$(SWAGGER_SPEC): $(GO_SOURCES_NO_BINDATA) $(SWAGGER_SPEC_INPUT)
273+
$(GO) run $(SWAGGER_PACKAGE) generate spec --exclude "$(SWAGGER_EXCLUDE)" --input "$(SWAGGER_SPEC_INPUT)" --output './$(SWAGGER_SPEC)'
278274

279275
.PHONY: swagger-check
280276
swagger-check: generate-swagger
@@ -287,9 +283,11 @@ swagger-check: generate-swagger
287283

288284
.PHONY: swagger-validate
289285
swagger-validate: ## check if the swagger spec is valid
290-
$(SED_INPLACE) '$(SWAGGER_SPEC_S_JSON)' './$(SWAGGER_SPEC)'
286+
@# swagger "validate" requires that the "basePath" must start with a slash, but we are using Golang template "{{...}}"
287+
@$(SED_INPLACE) -E -e 's|"basePath":( *)"(.*)"|"basePath":\1"/\2"|g' './$(SWAGGER_SPEC)' # add a prefix slash to basePath
288+
@# FIXME: there are some warnings
291289
$(GO) run $(SWAGGER_PACKAGE) validate './$(SWAGGER_SPEC)'
292-
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
290+
@$(SED_INPLACE) -E -e 's|"basePath":( *)"/(.*)"|"basePath":\1"\2"|g' './$(SWAGGER_SPEC)' # remove the prefix slash from basePath
293291

294292
.PHONY: checks
295293
checks: checks-frontend checks-backend ## run various consistency checks
@@ -380,6 +378,7 @@ lint-go-gopls: ## lint go files with gopls
380378

381379
.PHONY: lint-editorconfig
382380
lint-editorconfig:
381+
@echo "Running editorconfig check..."
383382
@$(GO) run $(EDITORCONFIG_CHECKER_PACKAGE) $(EDITORCONFIG_FILES)
384383

385384
.PHONY: lint-actions

routers/api/packages/maven/api.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
packages_model "code.gitea.io/gitea/models/packages"
11-
maven_module "code.gitea.io/gitea/modules/packages/maven"
1211
)
1312

1413
// MetadataResponse https://maven.apache.org/ref/3.2.5/maven-repository-metadata/repository-metadata.html
@@ -22,7 +21,7 @@ type MetadataResponse struct {
2221
}
2322

2423
// pds is expected to be sorted ascending by CreatedUnix
25-
func createMetadataResponse(pds []*packages_model.PackageDescriptor) *MetadataResponse {
24+
func createMetadataResponse(pds []*packages_model.PackageDescriptor, groupID, artifactID string) *MetadataResponse {
2625
var release *packages_model.PackageDescriptor
2726

2827
versions := make([]string, 0, len(pds))
@@ -35,11 +34,9 @@ func createMetadataResponse(pds []*packages_model.PackageDescriptor) *MetadataRe
3534

3635
latest := pds[len(pds)-1]
3736

38-
metadata := latest.Metadata.(*maven_module.Metadata)
39-
4037
resp := &MetadataResponse{
41-
GroupID: metadata.GroupID,
42-
ArtifactID: metadata.ArtifactID,
38+
GroupID: groupID,
39+
ArtifactID: artifactID,
4340
Latest: latest.Version.Version,
4441
Version: versions,
4542
}

routers/api/packages/maven/maven.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,19 @@ func handlePackageFile(ctx *context.Context, serveContent bool) {
8484
}
8585

8686
func serveMavenMetadata(ctx *context.Context, params parameters) {
87-
// /com/foo/project/maven-metadata.xml[.md5/.sha1/.sha256/.sha512]
88-
89-
pvs, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeMaven, params.toInternalPackageName())
90-
if errors.Is(err, util.ErrNotExist) {
91-
pvs, err = packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeMaven, params.toInternalPackageNameLegacy())
92-
}
87+
// path pattern: /com/foo/project/maven-metadata.xml[.md5/.sha1/.sha256/.sha512]
88+
// in case there are legacy package names ("GroupID-ArtifactID") we need to check both, new packages always use ":" as separator("GroupID:ArtifactID")
89+
pvsLegacy, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeMaven, params.toInternalPackageNameLegacy())
9390
if err != nil {
9491
apiError(ctx, http.StatusInternalServerError, err)
9592
return
9693
}
97-
if len(pvs) == 0 {
98-
apiError(ctx, http.StatusNotFound, packages_model.ErrPackageNotExist)
94+
pvs, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeMaven, params.toInternalPackageName())
95+
if err != nil {
96+
apiError(ctx, http.StatusInternalServerError, err)
9997
return
10098
}
99+
pvs = append(pvsLegacy, pvs...)
101100

102101
pds, err := packages_model.GetPackageDescriptors(ctx, pvs)
103102
if err != nil {
@@ -110,7 +109,7 @@ func serveMavenMetadata(ctx *context.Context, params parameters) {
110109
return pds[i].Version.CreatedUnix < pds[j].Version.CreatedUnix
111110
})
112111

113-
xmlMetadata, err := xml.Marshal(createMetadataResponse(pds))
112+
xmlMetadata, err := xml.Marshal(createMetadataResponse(pds, params.GroupID, params.ArtifactID))
114113
if err != nil {
115114
apiError(ctx, http.StatusInternalServerError, err)
116115
return

routers/api/v1/api.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// This documentation describes the Gitea API.
88
//
99
// Schemes: https, http
10-
// BasePath: /api/v1
11-
// Version: {{AppVer | JSEscape}}
1210
// License: MIT http://opensource.org/licenses/MIT
1311
//
1412
// Consumes:

templates/swagger/v1_input.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info": {
3+
"version": "{{AppVer | JSEscape}}"
4+
},
5+
"basePath": "{{AppSubUrl | JSEscape}}/api/v1"
6+
}

templates/swagger/v1_json.tmpl

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/api_packages_maven_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func TestPackageMaven(t *testing.T) {
8080
t.Run("UploadLegacy", func(t *testing.T) {
8181
defer tests.PrintCurrentTest(t)()
8282

83+
// try to upload a package with legacy package name (will be saved as "GroupID-ArtifactID")
8384
legacyRootLink := "/api/packages/user2/maven/com/gitea/legacy-project"
8485
req := NewRequestWithBody(t, "PUT", legacyRootLink+"/1.0.2/any-file-name?use_legacy_package_name=1", strings.NewReader("test-content")).AddBasicAuth(user.Name)
8586
MakeRequest(t, req, http.StatusCreated)
@@ -97,6 +98,13 @@ func TestPackageMaven(t *testing.T) {
9798
req = NewRequest(t, "GET", "/user2/-/packages/maven/com.gitea%3Alegacy-project/1.0.2")
9899
MakeRequest(t, req, http.StatusNotFound)
99100

101+
// legacy package names should also be able to be listed
102+
req = NewRequest(t, "GET", legacyRootLink+"/maven-metadata.xml").AddBasicAuth(user.Name)
103+
resp := MakeRequest(t, req, http.StatusOK)
104+
respBody := resp.Body.String()
105+
assert.Contains(t, respBody, "<version>1.0.2</version>")
106+
107+
// then upload a package with correct package name (will be saved as "GroupID:ArtifactID")
100108
req = NewRequestWithBody(t, "PUT", legacyRootLink+"/1.0.3/any-file-name", strings.NewReader("test-content")).AddBasicAuth(user.Name)
101109
MakeRequest(t, req, http.StatusCreated)
102110
_, err = packages.GetPackageByName(db.DefaultContext, user.ID, packages.TypeMaven, "com.gitea-legacy-project")
@@ -114,6 +122,12 @@ func TestPackageMaven(t *testing.T) {
114122
req = NewRequest(t, "GET", "/user2/-/packages/maven/com.gitea%3Alegacy-project/1.0.2")
115123
MakeRequest(t, req, http.StatusOK)
116124

125+
// now 2 packages should be listed
126+
req = NewRequest(t, "GET", legacyRootLink+"/maven-metadata.xml").AddBasicAuth(user.Name)
127+
resp = MakeRequest(t, req, http.StatusOK)
128+
respBody = resp.Body.String()
129+
assert.Contains(t, respBody, "<version>1.0.2</version>")
130+
assert.Contains(t, respBody, "<version>1.0.3</version>")
117131
require.NoError(t, packages.DeletePackageByID(db.DefaultContext, p.ID))
118132
})
119133

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {substituteRepoOpenWithUrl} from './repo-common.ts';
2+
3+
test('substituteRepoOpenWithUrl', () => {
4+
// For example: "x-github-client://openRepo/https://github.com/go-gitea/gitea"
5+
expect(substituteRepoOpenWithUrl('proto://a/{url}', 'https://gitea')).toEqual('proto://a/https://gitea');
6+
expect(substituteRepoOpenWithUrl('proto://a?link={url}', 'https://gitea')).toEqual('proto://a?link=https%3A%2F%2Fgitea');
7+
});

0 commit comments

Comments
 (0)