Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaofficial/main'
Browse files Browse the repository at this point in the history
* giteaofficial/main:
  Fix go-gitea#31185 try fix lfs download from bitbucket failed (go-gitea#31201)
  Enable `unparam` linter (go-gitea#31277)
  Make template `Iif` exactly match `if` (go-gitea#31322)
  update nix flake and add gofumpt (go-gitea#31320)
  code optimization (go-gitea#31315)
  • Loading branch information
zjjhot committed Jun 12, 2024
2 parents 17f77f3 + e25d696 commit 66bf728
Show file tree
Hide file tree
Showing 51 changed files with 222 additions and 199 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ linters:
- typecheck
- unconvert
- unused
- unparam
- wastedassign

run:
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

# backend
go_1_22
gofumpt
];
};
}
Expand Down
17 changes: 5 additions & 12 deletions models/dbfs/dbfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
51 changes: 20 additions & 31 deletions models/issues/issue_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,19 @@ 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
if opts.Paginator.Page > 1 {
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)")
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -166,21 +160,19 @@ 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 {
sess.In("issue.id", builder.Select("issue_id").From("project_issue").Where(builder.Eq{"project_board_id": opts.ProjectColumnID}))
} 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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand All @@ -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}
Expand All @@ -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").
Expand Down
16 changes: 4 additions & 12 deletions models/issues/pull_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion modules/auth/password/hash/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions modules/lfs/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions modules/lfs/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions modules/lfs/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions modules/lfs/transferadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/lfs/transferadapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 8 additions & 10 deletions modules/packages/cran/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ func ParseDescription(r io.Reader) (*Package, error) {
}

func setField(p *Package, data string) error {
const listDelimiter = ", "

if data == "" {
return nil
}
Expand Down Expand Up @@ -215,28 +213,28 @@ 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"
}

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])
}
Expand Down
2 changes: 1 addition & 1 deletion modules/setting/config_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 66bf728

Please sign in to comment.