-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
npm packages: set repository link based on the url in package.json #20379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jolheiser
merged 13 commits into
go-gitea:main
from
Mai-Lapyst:npm-automatic-repositoryLink
Mar 28, 2023
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eb798b4
npm packages: automatically set repository link for package based on …
Mai-Lapyst 66eecab
Moved repository lookup for npm packages into own function and adding…
Mai-Lapyst bf19f4e
Moving url to repo lookup to repo model; adding permission check when…
Mai-Lapyst 0a14698
Make gofmt happy
Mai-Lapyst 6a49131
Adding better errors; moving permission check before upload handling …
Mai-Lapyst 4386794
Removing unnecessary test code
Mai-Lapyst 976d8d5
Adding better comments & variable names; refactored anonymous functio…
Mai-Lapyst 382ffe3
Replacing some strings.Replace with strings.TrimSuffix; make cleaner …
Mai-Lapyst 70bc174
Fixing length check for pathSegments in GetRepositoryByURL
Mai-Lapyst 275ac8a
Refactor GetRepositoryByURL to support ctx as first parameter
Mai-Lapyst af00cce
NPM Packages: change Metadata.RepositoryUrl to Metadata.Repository.URL
Mai-Lapyst bf25dc2
NPM Packages: revert change that empties the repositories url in the …
Mai-Lapyst 4a7afda
Merge branch 'main' into npm-automatic-repositoryLink
jolheiser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -658,6 +658,49 @@ func GetRepositoryByName(ownerID int64, name string) (*Repository, error) { | |
return repo, err | ||
} | ||
|
||
// getRepositoryURLPathSegments returns segments (owner, reponame) extracted from a url | ||
func getRepositoryURLPathSegments(repoURL string) []string { | ||
if strings.HasPrefix(repoURL, setting.AppURL) { | ||
return strings.Split(strings.TrimPrefix(repoURL, setting.AppURL), "/") | ||
} | ||
|
||
sshURLVariants := [4]string{ | ||
setting.SSH.Domain + ":", | ||
setting.SSH.User + "@" + setting.SSH.Domain + ":", | ||
"git+ssh://" + setting.SSH.Domain + "/", | ||
"git+ssh://" + setting.SSH.User + "@" + setting.SSH.Domain + "/", | ||
} | ||
|
||
for _, sshURL := range sshURLVariants { | ||
if strings.HasPrefix(repoURL, sshURL) { | ||
return strings.Split(strings.TrimPrefix(repoURL, sshURL), "/") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// GetRepositoryByURL returns the repository by given url | ||
func GetRepositoryByURL(ctx context.Context, repoURL string) (*Repository, error) { | ||
// possible urls for git: | ||
// https://my.domain/sub-path/<owner>/<repo>.git | ||
// https://my.domain/sub-path/<owner>/<repo> | ||
// git+ssh://[email protected]/<owner>/<repo>.git | ||
// git+ssh://[email protected]/<owner>/<repo> | ||
// [email protected]:<owner>/<repo>.git | ||
// [email protected]:<owner>/<repo> | ||
|
||
pathSegments := getRepositoryURLPathSegments(repoURL) | ||
|
||
if len(pathSegments) != 2 { | ||
return nil, fmt.Errorf("unknown or malformed repository URL") | ||
} | ||
|
||
ownerName := pathSegments[0] | ||
repoName := strings.TrimSuffix(pathSegments[1], ".git") | ||
return GetRepositoryByOwnerAndName(ctx, ownerName, repoName) | ||
} | ||
|
||
// GetRepositoryByID returns the repository by given id if exists. | ||
func GetRepositoryByID(ctx context.Context, id int64) (*Repository, error) { | ||
repo := new(Repository) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,65 @@ func TestMetas(t *testing.T) { | |
assert.Equal(t, "user3", metas["org"]) | ||
assert.Equal(t, ",owners,team1,", metas["teams"]) | ||
} | ||
|
||
func TestGetRepositoryByURL(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
t.Run("InvalidPath", func(t *testing.T) { | ||
repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, "something") | ||
|
||
assert.Nil(t, repo) | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("ValidHttpURL", func(t *testing.T) { | ||
test := func(t *testing.T, url string) { | ||
repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, url) | ||
|
||
assert.NotNil(t, repo) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, repo.ID, int64(2)) | ||
assert.Equal(t, repo.OwnerID, int64(2)) | ||
} | ||
|
||
test(t, "https://try.gitea.io/user2/repo2") | ||
test(t, "https://try.gitea.io/user2/repo2.git") | ||
}) | ||
|
||
t.Run("ValidGitSshURL", func(t *testing.T) { | ||
test := func(t *testing.T, url string) { | ||
repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, url) | ||
|
||
assert.NotNil(t, repo) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, repo.ID, int64(2)) | ||
assert.Equal(t, repo.OwnerID, int64(2)) | ||
} | ||
|
||
test(t, "git+ssh://[email protected]/user2/repo2") | ||
test(t, "git+ssh://[email protected]/user2/repo2.git") | ||
|
||
test(t, "git+ssh://try.gitea.io/user2/repo2") | ||
test(t, "git+ssh://try.gitea.io/user2/repo2.git") | ||
}) | ||
|
||
t.Run("ValidImplicitSshURL", func(t *testing.T) { | ||
test := func(t *testing.T, url string) { | ||
repo, err := repo_model.GetRepositoryByURL(db.DefaultContext, url) | ||
|
||
assert.NotNil(t, repo) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, repo.ID, int64(2)) | ||
assert.Equal(t, repo.OwnerID, int64(2)) | ||
} | ||
|
||
test(t, "[email protected]:user2/repo2") | ||
test(t, "[email protected]:user2/repo2.git") | ||
|
||
test(t, "try.gitea.io:user2/repo2") | ||
test(t, "try.gitea.io:user2/repo2.git") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.