Repository: go-gitea/gitea
File: models/issues/issue.go (Go)

Code before:
	}
	return issue, nil
}

func isPullToCond(isPull optional.Option[bool]) builder.Cond {
	if isPull.Has() {
		return builder.Eq{"is_pull": isPull.Value()}
	}
	return builder.NewCond()
}

func FindLatestIssues(ctx context.Context, repoID int64, isPull optional.Option[bool], pageSize int) (IssueList, error) {
	issues := make([]*Issue, 0, pageSize)
	err := db.GetEngine(ctx).Where("repo_id = ?", repoID).
		And(isPullToCond(isPull)).
		OrderBy("created_unix DESC").
		Limit(pageSize).
		Find(&issues)
	return issues, err
}

func FindIssuesSuggestionByKeyword(ctx context.Context, repoID int64, keyword string, isPull optional.Option[bool], pageSize int) (IssueList, error) {
	indexKeyword, _ := strconv.ParseInt(keyword, 10, 64)
	cond := builder.NewCond()
	if indexKeyword > 0 {
		cond = cond.Or(builder.Eq{"`index`": indexKeyword})
	}
	cond = cond.Or(builder.Expr("name LIKE ?", "%"+keyword+"%"))

	issues := make([]*Issue, 0, pageSize)
	err := db.GetEngine(ctx).Where("repo_id = ?", repoID).
		And(isPullToCond(isPull)).
		And(cond).
		OrderBy("`index` DESC").
		Limit(pageSize).
		Find(&issues)
	return issues, err
}

// GetIssueWithAttrsByIndex returns issue by index in a repository.
func GetIssueWithAttrsByIndex(ctx context.Context, repoID, index int64) (*Issue, error) {
	issue, err := GetIssueByIndex(ctx, repoID, index)
	if err != nil {
		return nil, err
	}
	return issue, issue.LoadAttributes(ctx)
}

// GetIssueByID returns an issue by given ID.
func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
	issue := new(Issue)

Review comment (suggestion):
I think we need a separate search to make the "number" be the first one.

Otherwise it might not appear due to `		Limit(pageSize).`

Code after:
	return issue, nil
}

func isPullToCond(isPull optional.Option[bool]) builder.Cond {
	if isPull.Has() {
		return builder.Eq{"is_pull": isPull.Value()}
	}
	return builder.NewCond()
}

func FindLatestUpdatedIssues(ctx context.Context, repoID int64, isPull optional.Option[bool], pageSize int) (IssueList, error) {
	issues := make([]*Issue, 0, pageSize)
	err := db.GetEngine(ctx).Where("repo_id = ?", repoID).
		And(isPullToCond(isPull)).
		OrderBy("updated_unix DESC").
		Limit(pageSize).
		Find(&issues)
	return issues, err
}

func FindIssuesSuggestionByKeyword(ctx context.Context, repoID int64, keyword string, isPull optional.Option[bool], excludedID int64, pageSize int) (IssueList, error) {
	cond := builder.NewCond()
	if excludedID > 0 {
		cond = cond.And(builder.Neq{"`id`": excludedID})
	}

	// It seems that GitHub searches both title and content (maybe sorting by the search engine's ranking system?)
	// The first PR (https://github.com/go-gitea/gitea/pull/32327) uses "search indexer" to search "name(title) +  content"
	// But it seems that searching "content" (especially LIKE by DB engine) generates worse (unusable) results.
	// So now (https://github.com/go-gitea/gitea/pull/33538) it only searches "name(title)", leave the improvements to the future.
	cond = cond.And(db.BuildCaseInsensitiveLike("`name`", keyword))

	issues := make([]*Issue, 0, pageSize)
	err := db.GetEngine(ctx).Where("repo_id = ?", repoID).
		And(isPullToCond(isPull)).
		And(cond).
		OrderBy("updated_unix DESC, `index` DESC").
		Limit(pageSize).
		Find(&issues)
	return issues, err
}

// GetIssueWithAttrsByIndex returns issue by index in a repository.
func GetIssueWithAttrsByIndex(ctx context.Context, repoID, index int64) (*Issue, error) {
	issue, err := GetIssueByIndex(ctx, repoID, index)
	if err != nil {
		return nil, err
	}
	return issue, issue.LoadAttributes(ctx)
}