diff --git a/models/git/commit_status.go b/models/git/commit_status.go index ebabb7d4627..9c57f54384f 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -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"` diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index f29a56a69ab..2a4c88226dd 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -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) } } diff --git a/services/repository/repository.go b/services/repository/repository.go index 47a2c30b68f..995777812a5 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -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 diff --git a/tests/integration/repo_search_test.go b/tests/integration/repo_search_test.go index c77a7b0eca6..a38715bed71 100644 --- a/tests/integration/repo_search_test.go +++ b/tests/integration/repo_search_test.go @@ -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()) +} diff --git a/web_src/js/components/DashboardRepoList.vue b/web_src/js/components/DashboardRepoList.vue index ce80141e75a..04891827cdc 100644 --- a/web_src/js/components/DashboardRepoList.vue +++ b/web_src/js/components/DashboardRepoList.vue @@ -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, }; });