|
| 1 | +--- |
| 2 | +title: Search contexts |
| 3 | +sidebarTitle: Search contexts (EE) |
| 4 | +--- |
| 5 | + |
| 6 | +<Note> |
| 7 | +This is only available in the Enterprise Edition. Please add your [license key](/self-hosting/license-key) to activate it. |
| 8 | +</Note> |
| 9 | + |
| 10 | +A **search context** is a user-defined grouping of repositories that helps focus searches on specific areas of your codebase, like frontend, backend, or infrastructure code. Some example queries using search contexts: |
| 11 | + |
| 12 | +- `context:data_engineering userId` - search for `userId` across all repos related to Data Engineering. |
| 13 | +- `context:k8s ingress` - search for anything related to ingresses in your k8's configs. |
| 14 | +- `( context:project1 or context:project2 ) logger\.debug` - search for debug log calls in project1 and project2 |
| 15 | + |
| 16 | + |
| 17 | +Search contexts are defined in the `context` object inside of a [declarative config](/self-hosting/more/declarative-config). Repositories can be included / excluded from a search context by specifying the repo's URL in either the `include` array or `exclude` array. Glob patterns are supported. |
| 18 | + |
| 19 | +## Example |
| 20 | + |
| 21 | +Let's assume we have a GitLab instance hosted at `https://gitlab.example.com` with three top-level groups, `web`, `backend`, and `shared`: |
| 22 | + |
| 23 | +```sh |
| 24 | +web/ |
| 25 | +├─ admin_panel/ |
| 26 | +├─ customer_portal/ |
| 27 | +├─ pipelines/ |
| 28 | +├─ ... |
| 29 | +backend/ |
| 30 | +├─ billing_server/ |
| 31 | +├─ auth_server/ |
| 32 | +├─ db_migrations/ |
| 33 | +├─ pipelines/ |
| 34 | +├─ ... |
| 35 | +shared/ |
| 36 | +├─ protobufs/ |
| 37 | +├─ react/ |
| 38 | +├─ pipelines/ |
| 39 | +├─ ... |
| 40 | +``` |
| 41 | + |
| 42 | +To make searching easier, we can create three search contexts in our [config.json](/self-hosting/more/declarative-config): |
| 43 | +- `web`: For all frontend-related code |
| 44 | +- `backend`: For backend services and shared APIs |
| 45 | +- `pipelines`: For all CI/CD configurations |
| 46 | + |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "$schema": "https://raw.githubusercontent.com/sourcebot-dev/sourcebot/main/schemas/v3/index.json", |
| 51 | + "contexts": { |
| 52 | + "web": { |
| 53 | + // To include repositories in a search context, |
| 54 | + // you can reference them... |
| 55 | + "include": [ |
| 56 | + // ... individually by specifying the repo URL. |
| 57 | + "gitlab.example.com/web/admin_panel/core", |
| 58 | + |
| 59 | + |
| 60 | + // ... or as groups using glob patterns. This is |
| 61 | + // particularly useful for including entire "sub-folders" |
| 62 | + // of repositories in one go. |
| 63 | + "gitlab.example.com/web/customer_portal/**", |
| 64 | + "gitlab.example.com/shared/react/**", |
| 65 | + "gitlab.example.com/shared/protobufs/**" |
| 66 | + ], |
| 67 | + |
| 68 | + // Same with excluding repositories. |
| 69 | + "exclude": [ |
| 70 | + "gitlab.example.com/web/customer_portal/pipelines", |
| 71 | + "gitlab.example.com/shared/react/hooks/**", |
| 72 | + ], |
| 73 | + |
| 74 | + // Optional description of the search context |
| 75 | + // that surfaces in the UI. |
| 76 | + "description": "Web related repos." |
| 77 | + }, |
| 78 | + "backend": { /* ... specifies backend replated repos ... */}, |
| 79 | + "pipelines": { /* ... specifies pipeline related repos ... */ } |
| 80 | + }, |
| 81 | + "connections": { |
| 82 | + /* ...connection definitions... */ |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +<Accordion title="Repository URL details"> |
| 88 | + Repo URLs are expected to be formatted without the leading http(s):// prefix. For example: |
| 89 | + - `github.com/sourcebot-dev/sourcebot` ([link](https://github.com/sourcebot-dev/sourcebot)) |
| 90 | + - `gitlab.com/gitlab-org/gitlab` ([link](https://gitlab.com/gitlab-org/gitlab)) |
| 91 | + - `chromium.googlesource.com/chromium` ([link](https://chromium-review.googlesource.com/admin/repos/chromium,general)) |
| 92 | +</Accordion> |
| 93 | + |
| 94 | + |
| 95 | +Once configured, you can use these contexts in the search bar by prefixing your query with the context name. For example: |
| 96 | +- `context:web login form` searches for login form code in frontend repositories |
| 97 | +- `context:backend auth` searches for authentication code in backend services |
| 98 | +- `context:pipelines deploy` searches for deployment configurations |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +Like other prefixes, contexts can be negated using `-` or combined using `or`: |
| 103 | +- `-context:web` excludes frontend repositories from results |
| 104 | +- `( context:web or context:backend )` searches across both frontend and backend code |
| 105 | + |
| 106 | +See [this doc](/docs/more/syntax-reference) for more details on the search query syntax. |
| 107 | + |
| 108 | +## Schema reference |
| 109 | + |
| 110 | +<Accordion title="Reference"> |
| 111 | +```json |
| 112 | +{ |
| 113 | + "type": "object", |
| 114 | + "description": "Search context", |
| 115 | + "properties": { |
| 116 | + "include": { |
| 117 | + "type": "array", |
| 118 | + "description": "List of repositories to include in the search context. Expected to be formatted as a URL without any leading http(s):// prefix (e.g., 'github.com/sourcebot-dev/sourcebot'). Glob patterns are supported.", |
| 119 | + "items": { |
| 120 | + "type": "string" |
| 121 | + }, |
| 122 | + "examples": [ |
| 123 | + [ |
| 124 | + "github.com/sourcebot-dev/**", |
| 125 | + "gerrit.example.org/sub/path/**" |
| 126 | + ] |
| 127 | + ] |
| 128 | + }, |
| 129 | + "exclude": { |
| 130 | + "type": "array", |
| 131 | + "description": "List of repositories to exclude from the search context. Expected to be formatted as a URL without any leading http(s):// prefix (e.g., 'github.com/sourcebot-dev/sourcebot'). Glob patterns are supported.", |
| 132 | + "items": { |
| 133 | + "type": "string" |
| 134 | + }, |
| 135 | + "examples": [ |
| 136 | + [ |
| 137 | + "github.com/sourcebot-dev/sourcebot", |
| 138 | + "gerrit.example.org/sub/path/**" |
| 139 | + ] |
| 140 | + ] |
| 141 | + }, |
| 142 | + "description": { |
| 143 | + "type": "string", |
| 144 | + "description": "Optional description of the search context that surfaces in the UI." |
| 145 | + } |
| 146 | + }, |
| 147 | + "required": [ |
| 148 | + "include" |
| 149 | + ], |
| 150 | + "additionalProperties": false |
| 151 | +} |
| 152 | +``` |
| 153 | +</Accordion> |
0 commit comments