diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 0eda8a1877..3b25995c09 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -324,7 +324,7 @@ rules: jquery/no-sizzle: [2] jquery/no-slide: [2] jquery/no-submit: [2] - jquery/no-text: [0] + jquery/no-text: [2] jquery/no-toggle: [2] jquery/no-trigger: [0] jquery/no-trim: [2] @@ -477,7 +477,7 @@ rules: no-jquery/no-slide: [2] no-jquery/no-sub: [2] no-jquery/no-support: [2] - no-jquery/no-text: [0] + no-jquery/no-text: [2] no-jquery/no-trigger: [0] no-jquery/no-trim: [2] no-jquery/no-type: [2] @@ -798,7 +798,7 @@ rules: unicorn/prefer-object-has-own: [0] unicorn/prefer-optional-catch-binding: [2] unicorn/prefer-prototype-methods: [0] - unicorn/prefer-query-selector: [0] + unicorn/prefer-query-selector: [2] unicorn/prefer-reflect-apply: [0] unicorn/prefer-regexp-test: [2] unicorn/prefer-set-has: [0] diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index b752abb794..b7594a1ba7 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,9 +1,10 @@ - + Please check the following: 1. Make sure you are targeting the `main` branch, pull requests on release branches are only allowed for backports. 2. Make sure you have read contributing guidelines: https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md . -3. Describe what your pull request does and which issue you're targeting (if any). -4. It is recommended to enable "Allow edits by maintainers", so maintainers can help more easily. -5. Your input here will be included in the commit message when this PR has been merged. If you don't want some content to be included, please separate them with a line like `---`. -6. Delete all these tips before posting. +3. For documentations contribution, please go to https://gitea.com/gitea/docs +4. Describe what your pull request does and which issue you're targeting (if any). +5. It is recommended to enable "Allow edits by maintainers", so maintainers can help more easily. +6. Your input here will be included in the commit message when this PR has been merged. If you don't want some content to be included, please separate them with a line like `---`. +7. Delete all these tips before posting. diff --git a/.golangci.yml b/.golangci.yml index 1750872765..37617ad365 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters: - typecheck - unconvert - unused + - unparam - wastedassign run: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 04c06ffd14..60146276db 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -358,7 +358,8 @@ $REWRITTEN_PR_SUMMARY ## Documentation -If you add a new feature or change an existing aspect of Gitea, the documentation for that feature must be created or updated in the same PR. +If you add a new feature or change an existing aspect of Gitea, the documentation for that feature must be created or updated in another PR at [https://gitea.com/gitea/docs](https://gitea.com/gitea/docs). +**The docs directory on main repository will be removed at some time. We will have a yaml file to store configuration file's meta data. After that completed, configuration documentation should be in the main repository.** ## API v1 diff --git a/Makefile b/Makefile index d97360c9f4..b5a79091eb 100644 --- a/Makefile +++ b/Makefile @@ -878,7 +878,7 @@ node_modules: package-lock.json @touch node_modules .venv: poetry.lock - poetry install --no-root + poetry install @touch .venv .PHONY: update @@ -895,7 +895,7 @@ update-js: node-check | node_modules update-py: node-check | node_modules npx updates -u -f pyproject.toml rm -rf .venv poetry.lock - poetry install --no-root + poetry install @touch .venv .PHONY: fomantic diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index e619aae729..9196180d81 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -81,6 +81,10 @@ RUN_USER = ; git ;; Overwrite the automatically generated public URL. Necessary for proxies and docker. ;ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/ ;; +;; For development purpose only. It makes Gitea handle sub-path ("/sub-path/owner/repo/...") directly when debugging without a reverse proxy. +;; DO NOT USE IT IN PRODUCTION!!! +;USE_SUB_URL_PATH = false +;; ;; when STATIC_URL_PREFIX is empty it will follow ROOT_URL ;STATIC_URL_PREFIX = ;; diff --git a/flake.lock b/flake.lock index 0b2278f080..606f8836c1 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1715534503, - "narHash": "sha256-5ZSVkFadZbFP1THataCaSf0JH2cAH3S29hU9rrxTEqk=", + "lastModified": 1717974879, + "narHash": "sha256-GTO3C88+5DX171F/gVS3Qga/hOs/eRMxPFpiHq2t+D8=", "owner": "nixos", "repo": "nixpkgs", - "rev": "2057814051972fa1453ddfb0d98badbea9b83c06", + "rev": "c7b821ba2e1e635ba5a76d299af62821cbcb09f3", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index c6e915e9db..22354663dd 100644 --- a/flake.nix +++ b/flake.nix @@ -30,6 +30,7 @@ # backend go_1_22 + gofumpt ]; }; } diff --git a/models/db/search.go b/models/db/search.go index aa577f08e0..37565f45e1 100644 --- a/models/db/search.go +++ b/models/db/search.go @@ -18,12 +18,6 @@ const ( SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC" SearchOrderByOldest SearchOrderBy = "created_unix ASC" SearchOrderByNewest SearchOrderBy = "created_unix DESC" - SearchOrderBySize SearchOrderBy = "size ASC" - SearchOrderBySizeReverse SearchOrderBy = "size DESC" - SearchOrderByGitSize SearchOrderBy = "git_size ASC" - SearchOrderByGitSizeReverse SearchOrderBy = "git_size DESC" - SearchOrderByLFSSize SearchOrderBy = "lfs_size ASC" - SearchOrderByLFSSizeReverse SearchOrderBy = "lfs_size DESC" SearchOrderByID SearchOrderBy = "id ASC" SearchOrderByIDReverse SearchOrderBy = "id DESC" SearchOrderByStars SearchOrderBy = "num_stars ASC" diff --git a/models/dbfs/dbfile.go b/models/dbfs/dbfile.go index 3650ce057e..dd27b5c36b 100644 --- a/models/dbfs/dbfile.go +++ b/models/dbfs/dbfile.go @@ -215,16 +215,15 @@ func fileTimestampToTime(timestamp int64) time.Time { return time.UnixMicro(timestamp) } -func (f *file) loadMetaByPath() (*dbfsMeta, error) { +func (f *file) loadMetaByPath() error { var fileMeta dbfsMeta if ok, err := db.GetEngine(f.ctx).Where("full_path = ?", f.fullPath).Get(&fileMeta); err != nil { - return nil, err + return err } else if ok { f.metaID = fileMeta.ID f.blockSize = fileMeta.BlockSize - return &fileMeta, nil } - return nil, nil + return nil } func (f *file) open(flag int) (err error) { @@ -288,10 +287,7 @@ func (f *file) createEmpty() error { if err != nil { return err } - if _, err = f.loadMetaByPath(); err != nil { - return err - } - return nil + return f.loadMetaByPath() } func (f *file) truncate() error { @@ -368,8 +364,5 @@ func buildPath(path string) string { func newDbFile(ctx context.Context, path string) (*file, error) { path = buildPath(path) f := &file{ctx: ctx, fullPath: path, blockSize: defaultFileBlockSize} - if _, err := f.loadMetaByPath(); err != nil { - return nil, err - } - return f, nil + return f, f.loadMetaByPath() } diff --git a/models/git/protected_tag.go b/models/git/protected_tag.go index 8a05045651..9a6646c742 100644 --- a/models/git/protected_tag.go +++ b/models/git/protected_tag.go @@ -110,6 +110,19 @@ func GetProtectedTagByID(ctx context.Context, id int64) (*ProtectedTag, error) { return tag, nil } +// GetProtectedTagByNamePattern gets protected tag by name_pattern +func GetProtectedTagByNamePattern(ctx context.Context, repoID int64, pattern string) (*ProtectedTag, error) { + tag := &ProtectedTag{NamePattern: pattern, RepoID: repoID} + has, err := db.GetEngine(ctx).Get(tag) + if err != nil { + return nil, err + } + if !has { + return nil, nil + } + return tag, nil +} + // IsUserAllowedToControlTag checks if a user can control the specific tag. // It returns true if the tag name is not protected or the user is allowed to control it. func IsUserAllowedToControlTag(ctx context.Context, tags []*ProtectedTag, tagName string, userID int64) (bool, error) { diff --git a/models/issues/issue_search.go b/models/issues/issue_search.go index 491def1229..c1d7d921a9 100644 --- a/models/issues/issue_search.go +++ b/models/issues/issue_search.go @@ -99,9 +99,9 @@ func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) { } } -func applyLimit(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyLimit(sess *xorm.Session, opts *IssuesOptions) { if opts.Paginator == nil || opts.Paginator.IsListAll() { - return sess + return } start := 0 @@ -109,11 +109,9 @@ func applyLimit(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { start = (opts.Paginator.Page - 1) * opts.Paginator.PageSize } sess.Limit(opts.Paginator.PageSize, start) - - return sess } -func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) { if len(opts.LabelIDs) > 0 { if opts.LabelIDs[0] == 0 { sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_label)") @@ -136,11 +134,9 @@ func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session if len(opts.ExcludedLabelNames) > 0 { sess.And(builder.NotIn("issue.id", BuildLabelNamesIssueIDsCondition(opts.ExcludedLabelNames))) } - - return sess } -func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) { if len(opts.MilestoneIDs) == 1 && opts.MilestoneIDs[0] == db.NoConditionID { sess.And("issue.milestone_id = 0") } else if len(opts.MilestoneIDs) > 0 { @@ -153,11 +149,9 @@ func applyMilestoneCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Sess From("milestone"). Where(builder.In("name", opts.IncludeMilestones))) } - - return sess } -func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) { if opts.ProjectID > 0 { // specific project sess.Join("INNER", "project_issue", "issue.id = project_issue.issue_id"). And("project_issue.project_id=?", opts.ProjectID) @@ -166,10 +160,9 @@ func applyProjectCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Sessio } // opts.ProjectID == 0 means all projects, // do not need to apply any condition - return sess } -func applyProjectColumnCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyProjectColumnCondition(sess *xorm.Session, opts *IssuesOptions) { // opts.ProjectColumnID == 0 means all project columns, // do not need to apply any condition if opts.ProjectColumnID > 0 { @@ -177,10 +170,9 @@ func applyProjectColumnCondition(sess *xorm.Session, opts *IssuesOptions) *xorm. } else if opts.ProjectColumnID == db.NoConditionID { sess.In("issue.id", builder.Select("issue_id").From("project_issue").Where(builder.Eq{"project_board_id": 0})) } - return sess } -func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) { if len(opts.RepoIDs) == 1 { opts.RepoCond = builder.Eq{"issue.repo_id": opts.RepoIDs[0]} } else if len(opts.RepoIDs) > 1 { @@ -195,10 +187,9 @@ func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session if opts.RepoCond != nil { sess.And(opts.RepoCond) } - return sess } -func applyConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { +func applyConditions(sess *xorm.Session, opts *IssuesOptions) { if len(opts.IssueIDs) > 0 { sess.In("issue.id", opts.IssueIDs) } @@ -261,8 +252,6 @@ func applyConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session { if opts.User != nil { sess.And(issuePullAccessibleRepoCond("issue.repo_id", opts.User.ID, opts.Org, opts.Team, opts.IsPull.Value())) } - - return sess } // teamUnitsRepoCond returns query condition for those repo id in the special org team with special units access @@ -339,22 +328,22 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organizati return cond } -func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) *xorm.Session { - return sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id"). +func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) { + sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id"). And("issue_assignees.assignee_id = ?", assigneeID) } -func applyPosterCondition(sess *xorm.Session, posterID int64) *xorm.Session { - return sess.And("issue.poster_id=?", posterID) +func applyPosterCondition(sess *xorm.Session, posterID int64) { + sess.And("issue.poster_id=?", posterID) } -func applyMentionedCondition(sess *xorm.Session, mentionedID int64) *xorm.Session { - return sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id"). +func applyMentionedCondition(sess *xorm.Session, mentionedID int64) { + sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id"). And("issue_user.is_mentioned = ?", true). And("issue_user.uid = ?", mentionedID) } -func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64) *xorm.Session { +func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64) { existInTeamQuery := builder.Select("team_user.team_id"). From("team_user"). Where(builder.Eq{"team_user.uid": reviewRequestedID}) @@ -375,11 +364,11 @@ func applyReviewRequestedCondition(sess *xorm.Session, reviewRequestedID int64) ), builder.In("review.id", maxReview), )) - return sess.Where("issue.poster_id <> ?", reviewRequestedID). + sess.Where("issue.poster_id <> ?", reviewRequestedID). And(builder.In("issue.id", subQuery)) } -func applyReviewedCondition(sess *xorm.Session, reviewedID int64) *xorm.Session { +func applyReviewedCondition(sess *xorm.Session, reviewedID int64) { // Query for pull requests where you are a reviewer or commenter, excluding // any pull requests already returned by the review requested filter. notPoster := builder.Neq{"issue.poster_id": reviewedID} @@ -406,11 +395,11 @@ func applyReviewedCondition(sess *xorm.Session, reviewedID int64) *xorm.Session builder.In("type", CommentTypeComment, CommentTypeCode, CommentTypeReview), )), ) - return sess.And(notPoster, builder.Or(reviewed, commented)) + sess.And(notPoster, builder.Or(reviewed, commented)) } -func applySubscribedCondition(sess *xorm.Session, subscriberID int64) *xorm.Session { - return sess.And( +func applySubscribedCondition(sess *xorm.Session, subscriberID int64) { + sess.And( builder. NotIn("issue.id", builder.Select("issue_id"). diff --git a/models/issues/pull_list.go b/models/issues/pull_list.go index e8011a916f..f80a2284f0 100644 --- a/models/issues/pull_list.go +++ b/models/issues/pull_list.go @@ -28,7 +28,7 @@ type PullRequestsOptions struct { MilestoneID int64 } -func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) (*xorm.Session, error) { +func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullRequestsOptions) *xorm.Session { sess := db.GetEngine(ctx).Where("pull_request.base_repo_id=?", baseRepoID) sess.Join("INNER", "issue", "pull_request.issue_id = issue.id") @@ -46,7 +46,7 @@ func listPullRequestStatement(ctx context.Context, baseRepoID int64, opts *PullR sess.And("issue.milestone_id=?", opts.MilestoneID) } - return sess, nil + return sess } // GetUnmergedPullRequestsByHeadInfo returns all pull requests that are open and has not been merged @@ -130,23 +130,15 @@ func PullRequests(ctx context.Context, baseRepoID int64, opts *PullRequestsOptio opts.Page = 1 } - countSession, err := listPullRequestStatement(ctx, baseRepoID, opts) - if err != nil { - log.Error("listPullRequestStatement: %v", err) - return nil, 0, err - } + countSession := listPullRequestStatement(ctx, baseRepoID, opts) maxResults, err := countSession.Count(new(PullRequest)) if err != nil { log.Error("Count PRs: %v", err) return nil, maxResults, err } - findSession, err := listPullRequestStatement(ctx, baseRepoID, opts) + findSession := listPullRequestStatement(ctx, baseRepoID, opts) applySorts(findSession, opts.SortType, 0) - if err != nil { - log.Error("listPullRequestStatement: %v", err) - return nil, maxResults, err - } findSession = db.SetSessionPagination(findSession, opts) prs := make([]*PullRequest, 0, opts.PageSize) return prs, maxResults, findSession.Find(&prs) @@ -200,8 +192,10 @@ func (prs PullRequestList) LoadIssues(ctx context.Context) (IssueList, error) { return nil, nil } - // Load issues. - issueIDs := prs.GetIssueIDs() + // Load issues which are not loaded + issueIDs := container.FilterSlice(prs, func(pr *PullRequest) (int64, bool) { + return pr.IssueID, pr.Issue == nil && pr.IssueID > 0 + }) issues := make(map[int64]*Issue, len(issueIDs)) if err := db.GetEngine(ctx). In("id", issueIDs). @@ -237,10 +231,7 @@ func (prs PullRequestList) LoadIssues(ctx context.Context) (IssueList, error) { // GetIssueIDs returns all issue ids func (prs PullRequestList) GetIssueIDs() []int64 { return container.FilterSlice(prs, func(pr *PullRequest) (int64, bool) { - if pr.Issue == nil { - return pr.IssueID, pr.IssueID > 0 - } - return 0, false + return pr.IssueID, pr.IssueID > 0 }) } diff --git a/models/repo/avatar_test.go b/models/repo/avatar_test.go new file mode 100644 index 0000000000..fc1f8baeca --- /dev/null +++ b/models/repo/avatar_test.go @@ -0,0 +1,28 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" + + "github.com/stretchr/testify/assert" +) + +func TestRepoAvatarLink(t *testing.T) { + defer test.MockVariableValue(&setting.AppURL, "https://localhost/")() + defer test.MockVariableValue(&setting.AppSubURL, "")() + + repo := &Repository{ID: 1, Avatar: "avatar.png"} + link := repo.AvatarLink(db.DefaultContext) + assert.Equal(t, "https://localhost/repo-avatars/avatar.png", link) + + setting.AppURL = "https://localhost/sub-path/" + setting.AppSubURL = "/sub-path" + link = repo.AvatarLink(db.DefaultContext) + assert.Equal(t, "https://localhost/sub-path/repo-avatars/avatar.png", link) +} diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go index eacc98e222..e22d872ae4 100644 --- a/models/repo/repo_list.go +++ b/models/repo/repo_list.go @@ -207,31 +207,6 @@ type SearchRepoOptions struct { OnlyShowRelevant bool } -// SearchOrderBy is used to sort the result -type SearchOrderBy string - -func (s SearchOrderBy) String() string { - return string(s) -} - -// Strings for sorting result -const ( - SearchOrderByAlphabetically SearchOrderBy = "name ASC" - SearchOrderByAlphabeticallyReverse SearchOrderBy = "name DESC" - SearchOrderByLeastUpdated SearchOrderBy = "updated_unix ASC" - SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC" - SearchOrderByOldest SearchOrderBy = "created_unix ASC" - SearchOrderByNewest SearchOrderBy = "created_unix DESC" - SearchOrderBySize SearchOrderBy = "size ASC" - SearchOrderBySizeReverse SearchOrderBy = "size DESC" - SearchOrderByID SearchOrderBy = "id ASC" - SearchOrderByIDReverse SearchOrderBy = "id DESC" - SearchOrderByStars SearchOrderBy = "num_stars ASC" - SearchOrderByStarsReverse SearchOrderBy = "num_stars DESC" - SearchOrderByForks SearchOrderBy = "num_forks ASC" - SearchOrderByForksReverse SearchOrderBy = "num_forks DESC" -) - // UserOwnedRepoCond returns user ownered repositories func UserOwnedRepoCond(userID int64) builder.Cond { return builder.Eq{ diff --git a/models/repo/search.go b/models/repo/search.go index 54d6dcfb44..a73d9fc215 100644 --- a/models/repo/search.go +++ b/models/repo/search.go @@ -5,20 +5,48 @@ package repo import "code.gitea.io/gitea/models/db" -// SearchOrderByMap represents all possible search order -var SearchOrderByMap = map[string]map[string]db.SearchOrderBy{ +// OrderByMap represents all possible search order +var OrderByMap = map[string]map[string]db.SearchOrderBy{ "asc": { - "alpha": "owner_name ASC, name ASC", - "created": db.SearchOrderByOldest, - "updated": db.SearchOrderByLeastUpdated, - "size": db.SearchOrderBySize, - "id": db.SearchOrderByID, + "alpha": "owner_name ASC, name ASC", + "created": db.SearchOrderByOldest, + "updated": db.SearchOrderByLeastUpdated, + "size": "size ASC", + "git_size": "git_size ASC", + "lfs_size": "lfs_size ASC", + "id": db.SearchOrderByID, + "stars": db.SearchOrderByStars, + "forks": db.SearchOrderByForks, }, "desc": { - "alpha": "owner_name DESC, name DESC", - "created": db.SearchOrderByNewest, - "updated": db.SearchOrderByRecentUpdated, - "size": db.SearchOrderBySizeReverse, - "id": db.SearchOrderByIDReverse, + "alpha": "owner_name DESC, name DESC", + "created": db.SearchOrderByNewest, + "updated": db.SearchOrderByRecentUpdated, + "size": "size DESC", + "git_size": "git_size DESC", + "lfs_size": "lfs_size DESC", + "id": db.SearchOrderByIDReverse, + "stars": db.SearchOrderByStarsReverse, + "forks": db.SearchOrderByForksReverse, }, } + +// OrderByFlatMap is similar to OrderByMap but use human language keywords +// to decide between asc and desc +var OrderByFlatMap = map[string]db.SearchOrderBy{ + "newest": OrderByMap["desc"]["created"], + "oldest": OrderByMap["asc"]["created"], + "leastupdate": OrderByMap["asc"]["updated"], + "reversealphabetically": OrderByMap["desc"]["alpha"], + "alphabetically": OrderByMap["asc"]["alpha"], + "reversesize": OrderByMap["desc"]["size"], + "size": OrderByMap["asc"]["size"], + "reversegitsize": OrderByMap["desc"]["git_size"], + "gitsize": OrderByMap["asc"]["git_size"], + "reverselfssize": OrderByMap["desc"]["lfs_size"], + "lfssize": OrderByMap["asc"]["lfs_size"], + "moststars": OrderByMap["desc"]["stars"], + "feweststars": OrderByMap["asc"]["stars"], + "mostforks": OrderByMap["desc"]["forks"], + "fewestforks": OrderByMap["asc"]["forks"], +} diff --git a/models/user/avatar.go b/models/user/avatar.go index 921bc1b1a1..5453c78fc6 100644 --- a/models/user/avatar.go +++ b/models/user/avatar.go @@ -89,9 +89,11 @@ func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string { return avatars.GenerateEmailAvatarFastLink(ctx, u.AvatarEmail, size) } -// AvatarLink returns the full avatar url with http host. TODO: refactor it to a relative URL, but it is still used in API response at the moment +// AvatarLink returns the full avatar url with http host. +// TODO: refactor it to a relative URL, but it is still used in API response at the moment func (u *User) AvatarLink(ctx context.Context) string { - return httplib.MakeAbsoluteURL(ctx, u.AvatarLinkWithSize(ctx, 0)) + relLink := u.AvatarLinkWithSize(ctx, 0) // it can't be empty + return httplib.MakeAbsoluteURL(ctx, relLink) } // IsUploadAvatarChanged returns true if the current user's avatar would be changed with the provided data diff --git a/models/user/avatar_test.go b/models/user/avatar_test.go new file mode 100644 index 0000000000..1078875ee1 --- /dev/null +++ b/models/user/avatar_test.go @@ -0,0 +1,28 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package user + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" + + "github.com/stretchr/testify/assert" +) + +func TestUserAvatarLink(t *testing.T) { + defer test.MockVariableValue(&setting.AppURL, "https://localhost/")() + defer test.MockVariableValue(&setting.AppSubURL, "")() + + u := &User{ID: 1, Avatar: "avatar.png"} + link := u.AvatarLink(db.DefaultContext) + assert.Equal(t, "https://localhost/avatars/avatar.png", link) + + setting.AppURL = "https://localhost/sub-path/" + setting.AppSubURL = "/sub-path" + link = u.AvatarLink(db.DefaultContext) + assert.Equal(t, "https://localhost/sub-path/avatars/avatar.png", link) +} diff --git a/modules/auth/password/hash/common.go b/modules/auth/password/hash/common.go index ac6faf35cf..487c0738f4 100644 --- a/modules/auth/password/hash/common.go +++ b/modules/auth/password/hash/common.go @@ -18,7 +18,7 @@ func parseIntParam(value, param, algorithmName, config string, previousErr error return parsed, previousErr // <- Keep the previous error as this function should still return an error once everything has been checked if any call failed } -func parseUIntParam(value, param, algorithmName, config string, previousErr error) (uint64, error) { +func parseUIntParam(value, param, algorithmName, config string, previousErr error) (uint64, error) { //nolint:unparam parsed, err := strconv.ParseUint(value, 10, 64) if err != nil { log.Error("invalid integer for %s representation in %s hash spec %s", param, algorithmName, config) diff --git a/modules/base/natural_sort.go b/modules/base/natural_sort.go index 0f90ec70ce..acb9002276 100644 --- a/modules/base/natural_sort.go +++ b/modules/base/natural_sort.go @@ -4,12 +4,67 @@ package base import ( + "unicode/utf8" + "golang.org/x/text/collate" "golang.org/x/text/language" ) +func naturalSortGetRune(str string, pos int) (r rune, size int, has bool) { + if pos >= len(str) { + return 0, 0, false + } + r, size = utf8.DecodeRuneInString(str[pos:]) + if r == utf8.RuneError { + r, size = rune(str[pos]), 1 // if invalid input, treat it as a single byte ascii + } + return r, size, true +} + +func naturalSortAdvance(str string, pos int) (end int, isNumber bool) { + end = pos + for { + r, size, has := naturalSortGetRune(str, end) + if !has { + break + } + isCurRuneNum := '0' <= r && r <= '9' + if end == pos { + isNumber = isCurRuneNum + end += size + } else if isCurRuneNum == isNumber { + end += size + } else { + break + } + } + return end, isNumber +} + // NaturalSortLess compares two strings so that they could be sorted in natural order func NaturalSortLess(s1, s2 string) bool { + // There is a bug in Golang's collate package: https://github.com/golang/go/issues/67997 + // text/collate: CompareString(collate.Numeric) returns wrong result for "0.0" vs "1.0" #67997 + // So we need to handle the number parts by ourselves c := collate.New(language.English, collate.Numeric) - return c.CompareString(s1, s2) < 0 + pos1, pos2 := 0, 0 + for pos1 < len(s1) && pos2 < len(s2) { + end1, isNum1 := naturalSortAdvance(s1, pos1) + end2, isNum2 := naturalSortAdvance(s2, pos2) + part1, part2 := s1[pos1:end1], s2[pos2:end2] + if isNum1 && isNum2 { + if part1 != part2 { + if len(part1) != len(part2) { + return len(part1) < len(part2) + } + return part1 < part2 + } + } else { + if cmp := c.CompareString(part1, part2); cmp != 0 { + return cmp < 0 + } + } + pos1, pos2 = end1, end2 + } + return len(s1) < len(s2) } diff --git a/modules/base/natural_sort_test.go b/modules/base/natural_sort_test.go index f27a4eb53a..b001bc4ac9 100644 --- a/modules/base/natural_sort_test.go +++ b/modules/base/natural_sort_test.go @@ -10,21 +10,36 @@ import ( ) func TestNaturalSortLess(t *testing.T) { - test := func(s1, s2 string, less bool) { - assert.Equal(t, less, NaturalSortLess(s1, s2), "s1=%q, s2=%q", s1, s2) + testLess := func(s1, s2 string) { + assert.True(t, NaturalSortLess(s1, s2), "s1 0 { + searchOpt.ProjectID = optional.Some(opts.ProjectID) + } else if opts.ProjectID == -1 { // FIXME: this is inconsistent from other places + searchOpt.ProjectID = optional.Some[int64](0) // Those issues with no project(projectid==0) + } + // See the comment of issues_model.SearchOptions for the reason why we need to convert convertID := func(id int64) optional.Option[int64] { if id > 0 { @@ -49,7 +55,6 @@ func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOp return nil } - searchOpt.ProjectID = convertID(opts.ProjectID) searchOpt.ProjectColumnID = convertID(opts.ProjectColumnID) searchOpt.PosterID = convertID(opts.PosterID) searchOpt.AssigneeID = convertID(opts.AssigneeID) diff --git a/modules/lfs/http_client.go b/modules/lfs/http_client.go index e06879baea..f5ddd38b09 100644 --- a/modules/lfs/http_client.go +++ b/modules/lfs/http_client.go @@ -211,7 +211,7 @@ func createRequest(ctx context.Context, method, url string, headers map[string]s for key, value := range headers { req.Header.Set(key, value) } - req.Header.Set("Accept", MediaType) + req.Header.Set("Accept", AcceptHeader) return req, nil } @@ -251,6 +251,6 @@ func handleErrorResponse(resp *http.Response) error { return err } - log.Trace("ErrorResponse: %v", er) + log.Trace("ErrorResponse(%v): %v", resp.Status, er) return errors.New(er.Message) } diff --git a/modules/lfs/http_client_test.go b/modules/lfs/http_client_test.go index 7459d9c0c9..7431132f76 100644 --- a/modules/lfs/http_client_test.go +++ b/modules/lfs/http_client_test.go @@ -155,7 +155,7 @@ func TestHTTPClientDownload(t *testing.T) { hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { assert.Equal(t, "POST", req.Method) assert.Equal(t, MediaType, req.Header.Get("Content-type")) - assert.Equal(t, MediaType, req.Header.Get("Accept")) + assert.Equal(t, AcceptHeader, req.Header.Get("Accept")) var batchRequest BatchRequest err := json.NewDecoder(req.Body).Decode(&batchRequest) @@ -263,7 +263,7 @@ func TestHTTPClientUpload(t *testing.T) { hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { assert.Equal(t, "POST", req.Method) assert.Equal(t, MediaType, req.Header.Get("Content-type")) - assert.Equal(t, MediaType, req.Header.Get("Accept")) + assert.Equal(t, AcceptHeader, req.Header.Get("Accept")) var batchRequest BatchRequest err := json.NewDecoder(req.Body).Decode(&batchRequest) diff --git a/modules/lfs/shared.go b/modules/lfs/shared.go index 6b2e55f2fb..80f4fed00d 100644 --- a/modules/lfs/shared.go +++ b/modules/lfs/shared.go @@ -10,6 +10,8 @@ import ( const ( // MediaType contains the media type for LFS server requests MediaType = "application/vnd.git-lfs+json" + // Some LFS servers offer content with other types, so fallback to '*/*' if application/vnd.git-lfs+json cannot be served + AcceptHeader = "application/vnd.git-lfs+json;q=0.9, */*;q=0.8" ) // BatchRequest contains multiple requests processed in one batch operation. diff --git a/modules/lfs/transferadapter.go b/modules/lfs/transferadapter.go index d425b91946..fbc3a3ad8c 100644 --- a/modules/lfs/transferadapter.go +++ b/modules/lfs/transferadapter.go @@ -37,6 +37,7 @@ func (a *BasicTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCl if err != nil { return nil, err } + log.Debug("Download Request: %+v", req) resp, err := performRequest(ctx, a.client, req) if err != nil { return nil, err diff --git a/modules/lfs/transferadapter_test.go b/modules/lfs/transferadapter_test.go index 6023cd07d3..7fec137efe 100644 --- a/modules/lfs/transferadapter_test.go +++ b/modules/lfs/transferadapter_test.go @@ -26,7 +26,7 @@ func TestBasicTransferAdapter(t *testing.T) { p := Pointer{Oid: "b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259", Size: 5} roundTripHandler := func(req *http.Request) *http.Response { - assert.Equal(t, MediaType, req.Header.Get("Accept")) + assert.Equal(t, AcceptHeader, req.Header.Get("Accept")) assert.Equal(t, "test-value", req.Header.Get("test-header")) url := req.URL.String() diff --git a/modules/markup/html.go b/modules/markup/html.go index 8dbc958299..565bc175b7 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -49,7 +49,7 @@ var ( // hashCurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae // Although SHA1 hashes are 40 chars long, SHA256 are 64, the regex matches the hash from 7 to 64 chars in length // so that abbreviated hash links can be used as well. This matches git and GitHub usability. - hashCurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,64})(?:\s|$|\)|\]|[.,](\s|$))`) + hashCurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,64})(?:\s|$|\)|\]|[.,:](\s|$))`) // shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`) diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 9aa9c22d70..74089cffdd 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -380,6 +380,7 @@ func TestRegExp_sha1CurrentPattern(t *testing.T) { "(abcdefabcdefabcdefabcdefabcdefabcdefabcd)", "[abcdefabcdefabcdefabcdefabcdefabcdefabcd]", "abcdefabcdefabcdefabcdefabcdefabcdefabcd.", + "abcdefabcdefabcdefabcdefabcdefabcdefabcd:", } falseTestCases := []string{ "test", diff --git a/modules/markup/markdown/prefixed_id.go b/modules/markup/markdown/prefixed_id.go index 9c60949202..63d7fadc0a 100644 --- a/modules/markup/markdown/prefixed_id.go +++ b/modules/markup/markdown/prefixed_id.go @@ -9,9 +9,9 @@ import ( "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/markup/common" + "code.gitea.io/gitea/modules/util" "github.com/yuin/goldmark/ast" - "github.com/yuin/goldmark/util" ) type prefixedIDs struct { @@ -36,7 +36,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte { if !bytes.HasPrefix(result, []byte("user-content-")) { result = append([]byte("user-content-"), result...) } - if p.values.Add(util.BytesToReadOnlyString(result)) { + if p.values.Add(util.UnsafeBytesToString(result)) { return result } for i := 1; ; i++ { @@ -49,7 +49,7 @@ func (p *prefixedIDs) GenerateWithDefault(value, dft []byte) []byte { // Put puts a given element id to the used ids table. func (p *prefixedIDs) Put(value []byte) { - p.values.Add(util.BytesToReadOnlyString(value)) + p.values.Add(util.UnsafeBytesToString(value)) } func newPrefixedIDs() *prefixedIDs { diff --git a/modules/markup/markdown/transform_heading.go b/modules/markup/markdown/transform_heading.go index 6f38abfad9..6d48f34d93 100644 --- a/modules/markup/markdown/transform_heading.go +++ b/modules/markup/markdown/transform_heading.go @@ -7,10 +7,10 @@ import ( "fmt" "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/modules/util" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/text" - "github.com/yuin/goldmark/util" ) func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Heading, reader text.Reader, tocList *[]markup.Header) { @@ -21,11 +21,11 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin } txt := v.Text(reader.Source()) header := markup.Header{ - Text: util.BytesToReadOnlyString(txt), + Text: util.UnsafeBytesToString(txt), Level: v.Level, } if id, found := v.AttributeString("id"); found { - header.ID = util.BytesToReadOnlyString(id.([]byte)) + header.ID = util.UnsafeBytesToString(id.([]byte)) } *tocList = append(*tocList, header) g.applyElementDir(v) diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index 66e8cf611d..5eb568cb1f 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -86,10 +86,10 @@ type RenderContext struct { } type Links struct { - AbsolutePrefix bool - Base string - BranchPath string - TreePath string + AbsolutePrefix bool // add absolute URL prefix to auto-resolved links like "#issue", but not for pre-provided links and medias + Base string // base prefix for pre-provided links and medias (images, videos) + BranchPath string // actually it is the ref path, eg: "branch/features/feat-12", "tag/v1.0" + TreePath string // the dir of the file, eg: "doc" if the file "doc/CHANGE.md" is being rendered } func (l *Links) Prefix() string { diff --git a/modules/packages/composer/metadata.go b/modules/packages/composer/metadata.go index 1d0f025648..2c2e9ebf27 100644 --- a/modules/packages/composer/metadata.go +++ b/modules/packages/composer/metadata.go @@ -6,6 +6,7 @@ package composer import ( "archive/zip" "io" + "path" "regexp" "strings" @@ -36,10 +37,14 @@ type Package struct { Metadata *Metadata } +// https://getcomposer.org/doc/04-schema.md + // Metadata represents the metadata of a Composer package type Metadata struct { Description string `json:"description,omitempty"` + Readme string `json:"readme,omitempty"` Keywords []string `json:"keywords,omitempty"` + Comments Comments `json:"_comments,omitempty"` Homepage string `json:"homepage,omitempty"` License Licenses `json:"license,omitempty"` Authors []Author `json:"authors,omitempty"` @@ -74,6 +79,28 @@ func (l *Licenses) UnmarshalJSON(data []byte) error { return nil } +// Comments represents the comments of a Composer package +type Comments []string + +// UnmarshalJSON reads from a string or array +func (c *Comments) UnmarshalJSON(data []byte) error { + switch data[0] { + case '"': + var value string + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *c = Comments{value} + case '[': + values := make([]string, 0, 5) + if err := json.Unmarshal(data, &values); err != nil { + return err + } + *c = Comments(values) + } + return nil +} + // Author represents an author type Author struct { Name string `json:"name,omitempty"` @@ -101,14 +128,14 @@ func ParsePackage(r io.ReaderAt, size int64) (*Package, error) { } defer f.Close() - return ParseComposerFile(f) + return ParseComposerFile(archive, path.Dir(file.Name), f) } } return nil, ErrMissingComposerFile } // ParseComposerFile parses a composer.json file to retrieve the metadata of a Composer package -func ParseComposerFile(r io.Reader) (*Package, error) { +func ParseComposerFile(archive *zip.Reader, pathPrefix string, r io.Reader) (*Package, error) { var cj struct { Name string `json:"name"` Version string `json:"version"` @@ -137,6 +164,19 @@ func ParseComposerFile(r io.Reader) (*Package, error) { cj.Type = "library" } + if cj.Readme == "" { + cj.Readme = "README.md" + } + f, err := archive.Open(path.Join(pathPrefix, cj.Readme)) + if err == nil { + // 10kb limit for readme content + buf, _ := io.ReadAll(io.LimitReader(f, 10*1024)) + cj.Readme = string(buf) + _ = f.Close() + } else { + cj.Readme = "" + } + return &Package{ Name: cj.Name, Version: cj.Version, diff --git a/modules/packages/composer/metadata_test.go b/modules/packages/composer/metadata_test.go index a0e1a77a6e..a5e317daf1 100644 --- a/modules/packages/composer/metadata_test.go +++ b/modules/packages/composer/metadata_test.go @@ -17,6 +17,8 @@ import ( const ( name = "gitea/composer-package" description = "Package Description" + readme = "Package Readme" + comments = "Package Comment" packageType = "composer-plugin" author = "Gitea Authors" email = "no.reply@gitea.io" @@ -41,7 +43,8 @@ const composerContent = `{ }, "require": { "php": ">=7.2 || ^8.0" - } + }, + "_comments": "` + comments + `" }` func TestLicenseUnmarshal(t *testing.T) { @@ -54,18 +57,30 @@ func TestLicenseUnmarshal(t *testing.T) { assert.Equal(t, "MIT", l[0]) } +func TestCommentsUnmarshal(t *testing.T) { + var c Comments + assert.NoError(t, json.NewDecoder(strings.NewReader(`["comment"]`)).Decode(&c)) + assert.Len(t, c, 1) + assert.Equal(t, "comment", c[0]) + assert.NoError(t, json.NewDecoder(strings.NewReader(`"comment"`)).Decode(&c)) + assert.Len(t, c, 1) + assert.Equal(t, "comment", c[0]) +} + func TestParsePackage(t *testing.T) { - createArchive := func(name, content string) []byte { + createArchive := func(files map[string]string) []byte { var buf bytes.Buffer archive := zip.NewWriter(&buf) - w, _ := archive.Create(name) - w.Write([]byte(content)) + for name, content := range files { + w, _ := archive.Create(name) + w.Write([]byte(content)) + } archive.Close() return buf.Bytes() } t.Run("MissingComposerFile", func(t *testing.T) { - data := createArchive("dummy.txt", "") + data := createArchive(map[string]string{"dummy.txt": ""}) cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) assert.Nil(t, cp) @@ -73,7 +88,7 @@ func TestParsePackage(t *testing.T) { }) t.Run("MissingComposerFileInRoot", func(t *testing.T) { - data := createArchive("sub/sub/composer.json", "") + data := createArchive(map[string]string{"sub/sub/composer.json": ""}) cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) assert.Nil(t, cp) @@ -81,43 +96,52 @@ func TestParsePackage(t *testing.T) { }) t.Run("InvalidComposerFile", func(t *testing.T) { - data := createArchive("composer.json", "") + data := createArchive(map[string]string{"composer.json": ""}) cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) assert.Nil(t, cp) assert.Error(t, err) }) - t.Run("Valid", func(t *testing.T) { - data := createArchive("composer.json", composerContent) + t.Run("InvalidPackageName", func(t *testing.T) { + data := createArchive(map[string]string{"composer.json": "{}"}) cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) - assert.NoError(t, err) - assert.NotNil(t, cp) - }) -} - -func TestParseComposerFile(t *testing.T) { - t.Run("InvalidPackageName", func(t *testing.T) { - cp, err := ParseComposerFile(strings.NewReader(`{}`)) assert.Nil(t, cp) assert.ErrorIs(t, err, ErrInvalidName) }) t.Run("InvalidPackageVersion", func(t *testing.T) { - cp, err := ParseComposerFile(strings.NewReader(`{"name": "gitea/composer-package", "version": "1.a.3"}`)) + data := createArchive(map[string]string{"composer.json": `{"name": "gitea/composer-package", "version": "1.a.3"}`}) + + cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) assert.Nil(t, cp) assert.ErrorIs(t, err, ErrInvalidVersion) }) + t.Run("InvalidReadmePath", func(t *testing.T) { + data := createArchive(map[string]string{"composer.json": `{"name": "gitea/composer-package", "readme": "sub/README.md"}`}) + + cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) + assert.NoError(t, err) + assert.NotNil(t, cp) + + assert.Empty(t, cp.Metadata.Readme) + }) + t.Run("Valid", func(t *testing.T) { - cp, err := ParseComposerFile(strings.NewReader(composerContent)) + data := createArchive(map[string]string{"composer.json": composerContent, "README.md": readme}) + + cp, err := ParsePackage(bytes.NewReader(data), int64(len(data))) assert.NoError(t, err) assert.NotNil(t, cp) assert.Equal(t, name, cp.Name) assert.Empty(t, cp.Version) assert.Equal(t, description, cp.Metadata.Description) + assert.Equal(t, readme, cp.Metadata.Readme) + assert.Len(t, cp.Metadata.Comments, 1) + assert.Equal(t, comments, cp.Metadata.Comments[0]) assert.Len(t, cp.Metadata.Authors, 1) assert.Equal(t, author, cp.Metadata.Authors[0].Name) assert.Equal(t, email, cp.Metadata.Authors[0].Email) diff --git a/modules/packages/cran/metadata.go b/modules/packages/cran/metadata.go index 24e6f323af..0b0bfb07c6 100644 --- a/modules/packages/cran/metadata.go +++ b/modules/packages/cran/metadata.go @@ -185,8 +185,6 @@ func ParseDescription(r io.Reader) (*Package, error) { } func setField(p *Package, data string) error { - const listDelimiter = ", " - if data == "" { return nil } @@ -215,19 +213,19 @@ func setField(p *Package, data string) error { case "Description": p.Metadata.Description = value case "URL": - p.Metadata.ProjectURL = splitAndTrim(value, listDelimiter) + p.Metadata.ProjectURL = splitAndTrim(value) case "License": p.Metadata.License = value case "Author": - p.Metadata.Authors = splitAndTrim(authorReplacePattern.ReplaceAllString(value, ""), listDelimiter) + p.Metadata.Authors = splitAndTrim(authorReplacePattern.ReplaceAllString(value, "")) case "Depends": - p.Metadata.Depends = splitAndTrim(value, listDelimiter) + p.Metadata.Depends = splitAndTrim(value) case "Imports": - p.Metadata.Imports = splitAndTrim(value, listDelimiter) + p.Metadata.Imports = splitAndTrim(value) case "Suggests": - p.Metadata.Suggests = splitAndTrim(value, listDelimiter) + p.Metadata.Suggests = splitAndTrim(value) case "LinkingTo": - p.Metadata.LinkingTo = splitAndTrim(value, listDelimiter) + p.Metadata.LinkingTo = splitAndTrim(value) case "NeedsCompilation": p.Metadata.NeedsCompilation = value == "yes" } @@ -235,8 +233,8 @@ func setField(p *Package, data string) error { return nil } -func splitAndTrim(s, sep string) []string { - items := strings.Split(s, sep) +func splitAndTrim(s string) []string { + items := strings.Split(s, ", ") for i := range items { items[i] = strings.TrimSpace(items[i]) } diff --git a/modules/references/references.go b/modules/references/references.go index 1b656ed4cb..2889430bcf 100644 --- a/modules/references/references.go +++ b/modules/references/references.go @@ -14,8 +14,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup/mdstripper" "code.gitea.io/gitea/modules/setting" - - "github.com/yuin/goldmark/util" + "code.gitea.io/gitea/modules/util" ) var ( @@ -341,7 +340,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool return false, nil } } - r := getCrossReference(util.StringToReadOnlyBytes(content), match[2], match[3], false, prOnly) + r := getCrossReference(util.UnsafeStringToBytes(content), match[2], match[3], false, prOnly) if r == nil { return false, nil } diff --git a/modules/repository/branch.go b/modules/repository/branch.go index a3fca7c7ce..2bf9930f19 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -45,6 +45,7 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, if err != nil { return 0, fmt.Errorf("UpdateRepository: %w", err) } + repo.ObjectFormatName = objFmt.Name() // keep consistent with db allBranches := container.Set[string]{} { diff --git a/modules/setting/config_env.go b/modules/setting/config_env.go index 242f40914a..dfcb7db3c8 100644 --- a/modules/setting/config_env.go +++ b/modules/setting/config_env.go @@ -97,7 +97,7 @@ func decodeEnvSectionKey(encoded string) (ok bool, section, key string) { // decodeEnvironmentKey decode the environment key to section and key // The environment key is in the form of GITEA__SECTION__KEY or GITEA__SECTION__KEY__FILE -func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) { +func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, section, key string, useFileValue bool) { //nolint:unparam if !strings.HasPrefix(envKey, prefixGitea) { return false, "", "", false } diff --git a/modules/setting/global.go b/modules/setting/global.go new file mode 100644 index 0000000000..55dfe485b2 --- /dev/null +++ b/modules/setting/global.go @@ -0,0 +1,18 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package setting + +// Global settings +var ( + // RunUser is the OS user that Gitea is running as. ini:"RUN_USER" + RunUser string + // RunMode is the running mode of Gitea, it only accepts two values: "dev" and "prod". + // Non-dev values will be replaced by "prod". ini: "RUN_MODE" + RunMode string + // IsProd is true if RunMode is not "dev" + IsProd bool + + // AppName is the Application name, used in the page title. ini: "APP_NAME" + AppName string +) diff --git a/modules/setting/packages.go b/modules/setting/packages.go index b225615a24..00fba67b39 100644 --- a/modules/setting/packages.go +++ b/modules/setting/packages.go @@ -6,7 +6,6 @@ package setting import ( "fmt" "math" - "net/url" "os" "path/filepath" @@ -19,7 +18,6 @@ var ( Storage *Storage Enabled bool ChunkedUploadPath string - RegistryHost string LimitTotalOwnerCount int64 LimitTotalOwnerSize int64 @@ -66,9 +64,6 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) { return err } - appURL, _ := url.Parse(AppURL) - Packages.RegistryHost = appURL.Host - Packages.ChunkedUploadPath = filepath.ToSlash(sec.Key("CHUNKED_UPLOAD_PATH").MustString("tmp/package-upload")) if !filepath.IsAbs(Packages.ChunkedUploadPath) { Packages.ChunkedUploadPath = filepath.ToSlash(filepath.Join(AppDataPath, Packages.ChunkedUploadPath)) diff --git a/modules/setting/server.go b/modules/setting/server.go index 7d6ece2727..d7a71578d4 100644 --- a/modules/setting/server.go +++ b/modules/setting/server.go @@ -40,16 +40,16 @@ const ( LandingPageLogin LandingPage = "/user/login" ) +// Server settings var ( - // AppName is the Application name, used in the page title. - // It maps to ini:"APP_NAME" - AppName string // AppURL is the Application ROOT_URL. It always has a '/' suffix // It maps to ini:"ROOT_URL" AppURL string // AppSubURL represents the sub-url mounting point for gitea. It is either "" or starts with '/' and ends without '/', such as '/{subpath}'. // This value is empty if site does not have sub-url. AppSubURL string + // UseSubURLPath makes Gitea handle requests with sub-path like "/sub-path/owner/repo/...", to make it easier to debug sub-path related problems without a reverse proxy. + UseSubURLPath bool // AppDataPath is the default path for storing data. // It maps to ini:"APP_DATA_PATH" in [server] and defaults to AppWorkPath + "/data" AppDataPath string @@ -59,8 +59,6 @@ var ( // AssetVersion holds a opaque value that is used for cache-busting assets AssetVersion string - // Server settings - Protocol Scheme UseProxyProtocol bool // `ini:"USE_PROXY_PROTOCOL"` ProxyProtocolTLSBridging bool //`ini:"PROXY_PROTOCOL_TLS_BRIDGING"` @@ -275,9 +273,10 @@ func loadServerFrom(rootCfg ConfigProvider) { // This should be TrimRight to ensure that there is only a single '/' at the end of AppURL. AppURL = strings.TrimRight(appURL.String(), "/") + "/" - // Suburl should start with '/' and end without '/', such as '/{subpath}'. + // AppSubURL should start with '/' and end without '/', such as '/{subpath}'. // This value is empty if site does not have sub-url. AppSubURL = strings.TrimSuffix(appURL.Path, "/") + UseSubURLPath = sec.Key("USE_SUB_URL_PATH").MustBool(false) StaticURLPrefix = strings.TrimSuffix(sec.Key("STATIC_URL_PREFIX").MustString(AppSubURL), "/") // Check if Domain differs from AppURL domain than update it to AppURL's domain diff --git a/modules/setting/setting.go b/modules/setting/setting.go index f056fbfc6c..b4f913cdae 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -25,12 +25,7 @@ var ( // AppStartTime store time gitea has started AppStartTime time.Time - // Other global setting objects - CfgProvider ConfigProvider - RunMode string - RunUser string - IsProd bool IsWindows bool // IsInTesting indicates whether the testing is running. A lot of unreliable code causes a lot of nonsense error logs during testing diff --git a/modules/setting/storage.go b/modules/setting/storage.go index d44c968423..d6f7672b61 100644 --- a/modules/setting/storage.go +++ b/modules/setting/storage.go @@ -161,7 +161,7 @@ const ( targetSecIsSec // target section is from the name seciont [name] ) -func getStorageSectionByType(rootCfg ConfigProvider, typ string) (ConfigSection, targetSecType, error) { +func getStorageSectionByType(rootCfg ConfigProvider, typ string) (ConfigSection, targetSecType, error) { //nolint:unparam targetSec, err := rootCfg.GetSection(storageSectionName + "." + typ) if err != nil { if !IsValidStorageType(StorageType(typ)) { diff --git a/modules/storage/azureblob.go b/modules/storage/azureblob.go index 52a7d1637e..211522c5bb 100644 --- a/modules/storage/azureblob.go +++ b/modules/storage/azureblob.go @@ -163,10 +163,7 @@ func (a *AzureBlobStorage) getObjectNameFromPath(path string) string { // Open opens a file func (a *AzureBlobStorage) Open(path string) (Object, error) { - blobClient, err := a.getBlobClient(path) - if err != nil { - return nil, convertAzureBlobErr(err) - } + blobClient := a.getBlobClient(path) res, err := blobClient.GetProperties(a.ctx, &blob.GetPropertiesOptions{}) if err != nil { return nil, convertAzureBlobErr(err) @@ -229,10 +226,7 @@ func (a azureBlobFileInfo) Sys() any { // Stat returns the stat information of the object func (a *AzureBlobStorage) Stat(path string) (os.FileInfo, error) { - blobClient, err := a.getBlobClient(path) - if err != nil { - return nil, convertAzureBlobErr(err) - } + blobClient := a.getBlobClient(path) res, err := blobClient.GetProperties(a.ctx, &blob.GetPropertiesOptions{}) if err != nil { return nil, convertAzureBlobErr(err) @@ -247,20 +241,14 @@ func (a *AzureBlobStorage) Stat(path string) (os.FileInfo, error) { // Delete delete a file func (a *AzureBlobStorage) Delete(path string) error { - blobClient, err := a.getBlobClient(path) - if err != nil { - return convertAzureBlobErr(err) - } - _, err = blobClient.Delete(a.ctx, nil) + blobClient := a.getBlobClient(path) + _, err := blobClient.Delete(a.ctx, nil) return convertAzureBlobErr(err) } // URL gets the redirect URL to a file. The presigned link is valid for 5 minutes. func (a *AzureBlobStorage) URL(path, name string) (*url.URL, error) { - blobClient, err := a.getBlobClient(path) - if err != nil { - return nil, convertAzureBlobErr(err) - } + blobClient := a.getBlobClient(path) startTime := time.Now() u, err := blobClient.GetSASURL(sas.BlobPermissions{ @@ -290,10 +278,7 @@ func (a *AzureBlobStorage) IterateObjects(dirName string, fn func(path string, o return convertAzureBlobErr(err) } for _, object := range resp.Segment.BlobItems { - blobClient, err := a.getBlobClient(*object.Name) - if err != nil { - return convertAzureBlobErr(err) - } + blobClient := a.getBlobClient(*object.Name) object := &azureBlobObject{ Context: a.ctx, blobClient: blobClient, @@ -313,8 +298,8 @@ func (a *AzureBlobStorage) IterateObjects(dirName string, fn func(path string, o } // Delete delete a file -func (a *AzureBlobStorage) getBlobClient(path string) (*blob.Client, error) { - return a.client.ServiceClient().NewContainerClient(a.cfg.Container).NewBlobClient(a.buildAzureBlobPath(path)), nil +func (a *AzureBlobStorage) getBlobClient(path string) *blob.Client { + return a.client.ServiceClient().NewContainerClient(a.cfg.Container).NewBlobClient(a.buildAzureBlobPath(path)) } func init() { diff --git a/modules/structs/miscellaneous.go b/modules/structs/miscellaneous.go index bff10f95b7..3b206c1dd7 100644 --- a/modules/structs/miscellaneous.go +++ b/modules/structs/miscellaneous.go @@ -25,7 +25,8 @@ type MarkupOption struct { // // in: body Mode string - // Context to render + // URL path for rendering issue, media and file links + // Expected format: /subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}/{file/dir} // // in: body Context string @@ -53,7 +54,8 @@ type MarkdownOption struct { // // in: body Mode string - // Context to render + // URL path for rendering issue, media and file links + // Expected format: /subpath/{user}/{repo}/src/{branch, commit, tag}/{identifier/path}/{file/dir} // // in: body Context string diff --git a/modules/structs/repo_tag.go b/modules/structs/repo_tag.go index 4a7d895288..5722513f4f 100644 --- a/modules/structs/repo_tag.go +++ b/modules/structs/repo_tag.go @@ -3,6 +3,8 @@ package structs +import "time" + // Tag represents a repository tag type Tag struct { Name string `json:"name"` @@ -38,3 +40,29 @@ type CreateTagOption struct { Message string `json:"message"` Target string `json:"target"` } + +// TagProtection represents a tag protection +type TagProtection struct { + ID int64 `json:"id"` + NamePattern string `json:"name_pattern"` + WhitelistUsernames []string `json:"whitelist_usernames"` + WhitelistTeams []string `json:"whitelist_teams"` + // swagger:strfmt date-time + Created time.Time `json:"created_at"` + // swagger:strfmt date-time + Updated time.Time `json:"updated_at"` +} + +// CreateTagProtectionOption options for creating a tag protection +type CreateTagProtectionOption struct { + NamePattern string `json:"name_pattern"` + WhitelistUsernames []string `json:"whitelist_usernames"` + WhitelistTeams []string `json:"whitelist_teams"` +} + +// EditTagProtectionOption options for editing a tag protection +type EditTagProtectionOption struct { + NamePattern *string `json:"name_pattern"` + WhitelistUsernames []string `json:"whitelist_usernames"` + WhitelistTeams []string `json:"whitelist_teams"` +} diff --git a/modules/system/db.go b/modules/system/db.go index 05e9de0ae8..17178283d9 100644 --- a/modules/system/db.go +++ b/modules/system/db.go @@ -8,8 +8,7 @@ import ( "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/json" - - "github.com/yuin/goldmark/util" + "code.gitea.io/gitea/modules/util" ) // DBStore can be used to store app state items in local filesystem @@ -24,7 +23,7 @@ func (f *DBStore) Get(ctx context.Context, item StateItem) error { if content == "" { return nil } - return json.Unmarshal(util.StringToReadOnlyBytes(content), item) + return json.Unmarshal(util.UnsafeStringToBytes(content), item) } // Set saves the state item @@ -33,5 +32,5 @@ func (f *DBStore) Set(ctx context.Context, item StateItem) error { if err != nil { return err } - return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b)) + return system.SaveAppStateContent(ctx, item.Name(), util.UnsafeBytesToString(b)) } diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 94464fe628..330cbf8908 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -9,6 +9,7 @@ import ( "html" "html/template" "net/url" + "reflect" "slices" "strings" "time" @@ -237,8 +238,8 @@ func DotEscape(raw string) string { // Iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version, // and it could be simply used as "{{Iif expr trueVal}}" (omit the falseVal). -func Iif(condition bool, vals ...any) any { - if condition { +func Iif(condition any, vals ...any) any { + if isTemplateTruthy(condition) { return vals[0] } else if len(vals) > 1 { return vals[1] @@ -246,6 +247,32 @@ func Iif(condition bool, vals ...any) any { return nil } +func isTemplateTruthy(v any) bool { + if v == nil { + return false + } + + rv := reflect.ValueOf(v) + switch rv.Kind() { + case reflect.Bool: + return rv.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return rv.Int() != 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return rv.Uint() != 0 + case reflect.Float32, reflect.Float64: + return rv.Float() != 0 + case reflect.Complex64, reflect.Complex128: + return rv.Complex() != 0 + case reflect.String, reflect.Slice, reflect.Array, reflect.Map: + return rv.Len() > 0 + case reflect.Struct: + return true + default: + return !rv.IsNil() + } +} + // Eval the expression and return the result, see the comment of eval.Expr for details. // To use this helper function in templates, pass each token as a separate parameter. // diff --git a/modules/templates/helper_test.go b/modules/templates/helper_test.go index 0cefb7a6b2..ea5da7be80 100644 --- a/modules/templates/helper_test.go +++ b/modules/templates/helper_test.go @@ -5,8 +5,11 @@ package templates import ( "html/template" + "strings" "testing" + "code.gitea.io/gitea/modules/util" + "github.com/stretchr/testify/assert" ) @@ -65,3 +68,41 @@ func TestHTMLFormat(t *testing.T) { func TestSanitizeHTML(t *testing.T) { assert.Equal(t, template.HTML(`link xss
inline
`), SanitizeHTML(`link xss
inline
`)) } + +func TestTemplateTruthy(t *testing.T) { + tmpl := template.New("test") + tmpl.Funcs(template.FuncMap{"Iif": Iif}) + template.Must(tmpl.Parse(`{{if .Value}}true{{else}}false{{end}}:{{Iif .Value "true" "false"}}`)) + + cases := []any{ + nil, false, true, "", "string", 0, 1, + byte(0), byte(1), int64(0), int64(1), float64(0), float64(1), + complex(0, 0), complex(1, 0), + (chan int)(nil), make(chan int), + (func())(nil), func() {}, + util.ToPointer(0), util.ToPointer(util.ToPointer(0)), + util.ToPointer(1), util.ToPointer(util.ToPointer(1)), + [0]int{}, + [1]int{0}, + []int(nil), + []int{}, + []int{0}, + map[any]any(nil), + map[any]any{}, + map[any]any{"k": "v"}, + (*struct{})(nil), + struct{}{}, + util.ToPointer(struct{}{}), + } + w := &strings.Builder{} + truthyCount := 0 + for i, v := range cases { + w.Reset() + assert.NoError(t, tmpl.Execute(w, struct{ Value any }{v}), "case %d (%T) %#v fails", i, v, v) + out := w.String() + truthyCount += util.Iif(out == "true:true", 1, 0) + truthyMatches := out == "true:true" || out == "false:false" + assert.True(t, truthyMatches, "case %d (%T) %#v fail: %s", i, v, v, out) + } + assert.True(t, truthyCount != 0 && truthyCount != len(cases)) +} diff --git a/modules/test/utils.go b/modules/test/utils.go index 4a0c2f1b3b..8dee92fbce 100644 --- a/modules/test/utils.go +++ b/modules/test/utils.go @@ -34,8 +34,10 @@ func IsNormalPageCompleted(s string) bool { return strings.Contains(s, `