mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
fix(repo): limit the commit status fields returned by repo search (#40377)
The web repo search response embedded the whole commit status model although the dashboard only uses its state and target URL. It now returns just those two fields. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
1 parent
2b30d077b8
commit
0a66642da0
5 files changed
+74
-11
No files matched your search
@@ -39,7 +39,7 @@ type CommitStatus struct {
|
||||
RepoID int64 `xorm:"UNIQUE(repo_sha_index)"`
|
||||
SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
|
||||
Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
|
||||
Repo *repo_model.Repository `xorm:"-"`
|
||||
Repo *repo_model.Repository `xorm:"-" json:"-"`
|
||||
State commitstatus.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
|
||||
|
||||
// TargetURL points to the commit status page reported by a CI system
|
||||
@@ -49,7 +49,7 @@ type CommitStatus struct {
|
||||
Description string `xorm:"TEXT"`
|
||||
ContextHash string `xorm:"VARCHAR(64) index"`
|
||||
Context string `xorm:"TEXT"`
|
||||
Creator *user_model.User `xorm:"-"`
|
||||
Creator *user_model.User `xorm:"-" json:"-"`
|
||||
CreatorID int64
|
||||
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
|
||||
@@ -537,7 +537,10 @@ func SearchRepo(ctx *context.Context) {
|
||||
}
|
||||
|
||||
if latestCommitStatuses[i] != nil {
|
||||
results[i].LatestCommitStatus = latestCommitStatuses[i]
|
||||
results[i].LatestCommitStatus = &repo_service.WebSearchCommitStatus{
|
||||
State: latestCommitStatuses[i].State,
|
||||
TargetURL: latestCommitStatuses[i].TargetURL,
|
||||
}
|
||||
results[i].LocaleLatestCommitStatus = latestCommitStatuses[i].LocaleString(ctx.Locale)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ import (
|
||||
activities_model "gitea.dev/models/activities"
|
||||
audit_model "gitea.dev/models/audit"
|
||||
"gitea.dev/models/db"
|
||||
git_model "gitea.dev/models/git"
|
||||
issues_model "gitea.dev/models/issues"
|
||||
"gitea.dev/models/organization"
|
||||
access_model "gitea.dev/models/perm/access"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unit"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/commitstatus"
|
||||
"gitea.dev/modules/git"
|
||||
"gitea.dev/modules/git/gitrepo"
|
||||
"gitea.dev/modules/graceful"
|
||||
@@ -33,11 +33,17 @@ import (
|
||||
pull_service "gitea.dev/services/pull"
|
||||
)
|
||||
|
||||
// WebSearchCommitStatus is the latest commit status of a repository returned by web search
|
||||
type WebSearchCommitStatus struct {
|
||||
State commitstatus.CommitStatusState `json:"state"`
|
||||
TargetURL string `json:"target_url"`
|
||||
}
|
||||
|
||||
// WebSearchRepository represents a repository returned by web search
|
||||
type WebSearchRepository struct {
|
||||
Repository *structs.Repository `json:"repository"`
|
||||
LatestCommitStatus *git_model.CommitStatus `json:"latest_commit_status"`
|
||||
LocaleLatestCommitStatus string `json:"locale_latest_commit_status"`
|
||||
Repository *structs.Repository `json:"repository"`
|
||||
LatestCommitStatus *WebSearchCommitStatus `json:"latest_commit_status"`
|
||||
LocaleLatestCommitStatus string `json:"locale_latest_commit_status"`
|
||||
}
|
||||
|
||||
// WebSearchResults results of a successful web search
|
||||
|
||||
@@ -9,12 +9,17 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
git_model "gitea.dev/models/git"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unittest"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/commitstatus"
|
||||
"gitea.dev/modules/git"
|
||||
code_indexer "gitea.dev/modules/indexer/code"
|
||||
"gitea.dev/modules/indexer/code/gitgrep"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
commitstatus_service "gitea.dev/services/repository/commitstatus"
|
||||
"gitea.dev/tests"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
@@ -122,3 +127,52 @@ func testSearch(t *testing.T, url string, expected []string) *HTMLDoc {
|
||||
assert.Equal(t, expected, filenames, "url=%s", url)
|
||||
return doc
|
||||
}
|
||||
|
||||
func TestWebSearchRepoCommitStatus(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||||
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, commitstatus_service.CreateCommitStatus(t.Context(), repo, owner, branch.CommitID, &git_model.CommitStatus{
|
||||
State: commitstatus.CommitStatusSuccess,
|
||||
TargetURL: "https://example.com/ci",
|
||||
Context: "ci",
|
||||
}))
|
||||
|
||||
req := NewRequestf(t, "GET", "/repo/search?uid=%d&q=%s", owner.ID, repo.Name)
|
||||
resp := loginUser(t, "user4").MakeRequest(t, req, http.StatusOK)
|
||||
assert.JSONEq(t, `{
|
||||
"ok": true,
|
||||
"data": [ {
|
||||
"repository": {
|
||||
"id": 1, "owner": null, "name": "", "full_name": "user2/repo1", "description": "",
|
||||
"empty": false, "private": false, "fork": false, "template": false, "mirror": false, "archived": false, "internal": false,
|
||||
"size": 0, "language": "", "languages_url": "", "topics": null, "licenses": null,
|
||||
"html_url": "`+setting.AppURL+`user2/repo1", "url": "", "link": "/user2/repo1", "avatar_url": "",
|
||||
"ssh_url": "", "clone_url": "", "original_url": "", "website": "",
|
||||
"stars_count": 0, "forks_count": 0, "watchers_count": 0, "branch_count": 0,
|
||||
"open_issues_count": 0, "open_pr_counter": 0, "release_counter": 0,
|
||||
"default_branch": "", "object_format_name": "",
|
||||
"created_at": "0001-01-01T00:00:00Z",
|
||||
"updated_at": "0001-01-01T00:00:00Z",
|
||||
"archived_at": "0001-01-01T00:00:00Z",
|
||||
"has_code": false, "has_issues": false, "has_wiki": false, "has_pull_requests": false, "has_projects": false,
|
||||
"has_releases": false, "has_packages": false, "has_actions": false,
|
||||
"projects_mode": "",
|
||||
"ignore_whitespace_conflicts": false,
|
||||
"allow_merge_commits": false, "allow_rebase": false, "allow_rebase_explicit": false, "allow_squash_merge": false,
|
||||
"allow_fast_forward_only_merge": false, "allow_merge_update": false, "allow_rebase_update": false, "allow_manual_merge": false,
|
||||
"autodetect_manual_merge": false, "default_delete_branch_after_merge": false, "default_merge_style": "", "default_update_style": "",
|
||||
"squash_merge_commit_message": "",
|
||||
"default_allow_maintainer_edit": false,
|
||||
"pull_requests_creation_restriction": "",
|
||||
"issues_creation_restriction": "",
|
||||
"mirror_interval": "", "mirror_updated": "0001-01-01T00:00:00Z", "mirror_last_sync_at": "0001-01-01T00:00:00Z"
|
||||
},
|
||||
"latest_commit_status":{"state": "success", "target_url": "https://example.com/ci"},
|
||||
"locale_latest_commit_status": "Success"
|
||||
} ]
|
||||
}`, resp.Body.String())
|
||||
}
|
||||
@@ -27,8 +27,8 @@ type CommitStatus = 'pending' | 'success' | 'error' | 'failure' | 'warning' | 's
|
||||
type WebSearchRepo = {
|
||||
repository: DashboardRepo,
|
||||
latest_commit_status: {
|
||||
State: CommitStatus,
|
||||
TargetURL: string,
|
||||
state: CommitStatus,
|
||||
target_url: string,
|
||||
} | null,
|
||||
locale_latest_commit_status: string,
|
||||
};
|
||||
@@ -306,8 +306,8 @@ async function searchRepos() {
|
||||
repos.value = json.data.map((webSearchRepo) => {
|
||||
return {
|
||||
...webSearchRepo.repository,
|
||||
latest_commit_status_state: webSearchRepo.latest_commit_status?.State, // if latest_commit_status is null, it means there is no commit status
|
||||
latest_commit_status_state_link: webSearchRepo.latest_commit_status?.TargetURL,
|
||||
latest_commit_status_state: webSearchRepo.latest_commit_status?.state, // if latest_commit_status is null, it means there is no commit status
|
||||
latest_commit_status_state_link: webSearchRepo.latest_commit_status?.target_url,
|
||||
locale_latest_commit_status_state: webSearchRepo.locale_latest_commit_status,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user